ownclouddolphinoverlayplugin.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <KOverlayIconPlugin>
  20. #include <KPluginFactory>
  21. #include <QtNetwork/QLocalSocket>
  22. #include <KIOCore/kfileitem.h>
  23. #include <QTimer>
  24. #include "ownclouddolphinpluginhelper.h"
  25. class OwncloudDolphinPlugin : public KOverlayIconPlugin
  26. {
  27. Q_PLUGIN_METADATA(IID "com.owncloud.ovarlayiconplugin" FILE "ownclouddolphinoverlayplugin.json");
  28. Q_OBJECT
  29. typedef QHash<QByteArray, QByteArray> StatusMap;
  30. StatusMap m_status;
  31. public:
  32. OwncloudDolphinPlugin() {
  33. auto helper = OwncloudDolphinPluginHelper::instance();
  34. QObject::connect(helper, &OwncloudDolphinPluginHelper::commandRecieved,
  35. this, &OwncloudDolphinPlugin::slotCommandRecieved);
  36. }
  37. QStringList getOverlays(const QUrl& url) override {
  38. auto helper = OwncloudDolphinPluginHelper::instance();
  39. if (!helper->isConnected())
  40. return QStringList();
  41. if (!url.isLocalFile())
  42. return QStringList();
  43. const QByteArray localFile = url.toLocalFile().toUtf8();
  44. helper->sendCommand("RETRIEVE_FILE_STATUS:" + localFile + "\n");
  45. StatusMap::iterator it = m_status.find(localFile);
  46. if (it != m_status.constEnd()) {
  47. return overlaysForString(*it);
  48. }
  49. return QStringList();
  50. }
  51. private:
  52. QStringList overlaysForString(const QByteArray &status) {
  53. QStringList r;
  54. if (status.startsWith("NOP"))
  55. return r;
  56. if (status.startsWith("OK"))
  57. r << "dialog-ok";
  58. if (status.startsWith("SYNC") || status.startsWith("NEW"))
  59. r << "view-refresh";
  60. if (status.contains("+SWM"))
  61. r << "document-share";
  62. return r;
  63. }
  64. void slotCommandRecieved(const QByteArray &line) {
  65. QList<QByteArray> tokens = line.split(':');
  66. if (tokens.count() != 3)
  67. return;
  68. if (tokens[0] != "STATUS" && tokens[0] != "BROADCAST")
  69. return;
  70. if (tokens[2].isEmpty())
  71. return;
  72. const QByteArray name = tokens[2];
  73. QByteArray &status = m_status[name]; // reference to the item in the hash
  74. if (status == tokens[1])
  75. return;
  76. status = tokens[1];
  77. emit overlaysChanged(QUrl::fromLocalFile(QString::fromUtf8(name)), overlaysForString(status));
  78. }
  79. };
  80. #include "ownclouddolphinoverlayplugin.moc"