ownclouddolphinpluginhelper.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /******************************************************************************
  2. * Copyright (C) 2014 by Olivier Goffart <ogoffart@woboq.com *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
  18. ******************************************************************************/
  19. #include <QtNetwork/QLocalSocket>
  20. #include <qcoreevent.h>
  21. #include <QStandardPaths>
  22. #include <QFile>
  23. #include "ownclouddolphinpluginhelper.h"
  24. OwncloudDolphinPluginHelper* OwncloudDolphinPluginHelper::instance()
  25. {
  26. static OwncloudDolphinPluginHelper self;
  27. return &self;
  28. }
  29. OwncloudDolphinPluginHelper::OwncloudDolphinPluginHelper()
  30. {
  31. connect(&_socket, &QLocalSocket::connected, this, &OwncloudDolphinPluginHelper::slotConnected);
  32. connect(&_socket, &QLocalSocket::readyRead, this, &OwncloudDolphinPluginHelper::slotReadyRead);
  33. _connectTimer.start(45 * 1000, Qt::VeryCoarseTimer, this);
  34. tryConnect();
  35. }
  36. void OwncloudDolphinPluginHelper::timerEvent(QTimerEvent *e)
  37. {
  38. if (e->timerId() == _connectTimer.timerId()) {
  39. tryConnect();
  40. return;
  41. }
  42. QObject::timerEvent(e);
  43. }
  44. bool OwncloudDolphinPluginHelper::isConnected() const
  45. {
  46. return _socket.state() == QLocalSocket::ConnectedState;
  47. }
  48. void OwncloudDolphinPluginHelper::sendCommand(const char* data)
  49. {
  50. _socket.write(data);
  51. _socket.flush();
  52. }
  53. void OwncloudDolphinPluginHelper::slotConnected()
  54. {
  55. sendCommand("VERSION:\n");
  56. sendCommand("GET_STRINGS:\n");
  57. }
  58. void OwncloudDolphinPluginHelper::tryConnect()
  59. {
  60. if (_socket.state() != QLocalSocket::UnconnectedState) {
  61. return;
  62. }
  63. QString socketPath = QStandardPaths::locate(QStandardPaths::RuntimeLocation,
  64. APPLICATION_SHORTNAME,
  65. QStandardPaths::LocateDirectory);
  66. if(socketPath.isEmpty())
  67. return;
  68. _socket.connectToServer(socketPath + QLatin1String("/socket"));
  69. }
  70. void OwncloudDolphinPluginHelper::slotReadyRead()
  71. {
  72. while (_socket.bytesAvailable()) {
  73. _line += _socket.readLine();
  74. if (!_line.endsWith("\n"))
  75. continue;
  76. QByteArray line;
  77. qSwap(line, _line);
  78. line.chop(1);
  79. if (line.isEmpty())
  80. continue;
  81. if (line.startsWith("REGISTER_PATH:")) {
  82. auto col = line.indexOf(':');
  83. QString file = QString::fromUtf8(line.constData() + col + 1, line.size() - col - 1);
  84. _paths.append(file);
  85. continue;
  86. } else if (line.startsWith("STRING:")) {
  87. auto args = QString::fromUtf8(line).split(QLatin1Char(':'));
  88. if (args.size() >= 3) {
  89. _strings[args[1]] = args.mid(2).join(QLatin1Char(':'));
  90. }
  91. continue;
  92. } else if (line.startsWith("VERSION:")) {
  93. auto args = line.split(':');
  94. auto version = args.value(2);
  95. _version = version;
  96. if (!version.startsWith("1.")) {
  97. // Incompatible version, disconnect forever
  98. _connectTimer.stop();
  99. _socket.disconnectFromServer();
  100. return;
  101. }
  102. }
  103. emit commandRecieved(line);
  104. }
  105. }