ownclouddolphinpluginhelper.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <QFile>
  22. #include "ownclouddolphinpluginhelper.h"
  23. OwncloudDolphinPluginHelper* OwncloudDolphinPluginHelper::instance()
  24. {
  25. static OwncloudDolphinPluginHelper self;
  26. return &self;
  27. }
  28. OwncloudDolphinPluginHelper::OwncloudDolphinPluginHelper()
  29. {
  30. connect(&_socket, &QLocalSocket::connected, this, &OwncloudDolphinPluginHelper::slotConnected);
  31. connect(&_socket, &QLocalSocket::readyRead, this, &OwncloudDolphinPluginHelper::slotReadyRead);
  32. _connectTimer.start(45 * 1000, Qt::VeryCoarseTimer, this);
  33. tryConnect();
  34. }
  35. void OwncloudDolphinPluginHelper::timerEvent(QTimerEvent *e)
  36. {
  37. if (e->timerId() == _connectTimer.timerId()) {
  38. tryConnect();
  39. return;
  40. }
  41. QObject::timerEvent(e);
  42. }
  43. bool OwncloudDolphinPluginHelper::isConnected() const
  44. {
  45. return _socket.state() == QLocalSocket::ConnectedState;
  46. }
  47. void OwncloudDolphinPluginHelper::sendCommand(const char* data)
  48. {
  49. _socket.write(data);
  50. _socket.flush();
  51. }
  52. void OwncloudDolphinPluginHelper::slotConnected()
  53. {
  54. sendCommand("SHARE_MENU_TITLE:\n");
  55. }
  56. void OwncloudDolphinPluginHelper::tryConnect()
  57. {
  58. if (_socket.state() != QLocalSocket::UnconnectedState) {
  59. return;
  60. }
  61. QString runtimeDir = QFile::decodeName(qgetenv("XDG_RUNTIME_DIR"));
  62. QString socketPath = runtimeDir + QLatin1String("/ownCloud/socket");
  63. _socket.connectToServer(socketPath);
  64. }
  65. void OwncloudDolphinPluginHelper::slotReadyRead()
  66. {
  67. while (_socket.bytesAvailable()) {
  68. _line += _socket.readLine();
  69. if (!_line.endsWith("\n"))
  70. continue;
  71. QByteArray line;
  72. qSwap(line, _line);
  73. line.chop(1);
  74. if (line.isEmpty())
  75. continue;
  76. if (line.startsWith("REGISTER_PATH:")) {
  77. auto col = line.indexOf(':');
  78. QString file = QString::fromUtf8(line.constData() + col + 1, line.size() - col - 1);
  79. _paths.append(file);
  80. continue;
  81. } else if (line.startsWith("SHARE_MENU_TITLE:")) {
  82. auto col = line.indexOf(':');
  83. _shareActionString = QString::fromUtf8(line.constData() + col + 1, line.size() - col - 1);
  84. continue;
  85. }
  86. emit commandRecieved(line);
  87. }
  88. }