ownclouddolphinoverlayplugin.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <QDir>
  24. #include <QTimer>
  25. #include "ownclouddolphinpluginhelper.h"
  26. class OwncloudDolphinPlugin : public KOverlayIconPlugin
  27. {
  28. Q_PLUGIN_METADATA(IID "com.owncloud.ovarlayiconplugin" FILE "ownclouddolphinoverlayplugin.json")
  29. Q_OBJECT
  30. using StatusMap = QHash<QByteArray, QByteArray>;
  31. StatusMap m_status;
  32. public:
  33. OwncloudDolphinPlugin() {
  34. auto helper = OwncloudDolphinPluginHelper::instance();
  35. QObject::connect(helper, &OwncloudDolphinPluginHelper::commandRecieved,
  36. this, &OwncloudDolphinPlugin::slotCommandRecieved);
  37. }
  38. QStringList getOverlays(const QUrl& url) override {
  39. auto helper = OwncloudDolphinPluginHelper::instance();
  40. if (!helper->isConnected())
  41. return QStringList();
  42. if (!url.isLocalFile())
  43. return QStringList();
  44. QDir localPath(url.toLocalFile());
  45. const QByteArray localFile = localPath.canonicalPath().toUtf8();
  46. helper->sendCommand(QByteArray("RETRIEVE_FILE_STATUS:" + localFile + "\n"));
  47. StatusMap::iterator it = m_status.find(localFile);
  48. if (it != m_status.constEnd()) {
  49. return overlaysForString(*it);
  50. }
  51. return QStringList();
  52. }
  53. private:
  54. QStringList overlaysForString(const QByteArray &status) {
  55. QStringList r;
  56. if (status.startsWith("NOP"))
  57. return r;
  58. if (status.startsWith("OK"))
  59. r << "vcs-normal";
  60. if (status.startsWith("SYNC") || status.startsWith("NEW"))
  61. r << "vcs-update-required";
  62. if (status.startsWith("IGNORE") || status.startsWith("WARN"))
  63. r << "vcs-locally-modified-unstaged";
  64. if (status.startsWith("ERROR"))
  65. r << "vcs-conflicting";
  66. if (status.contains("+SWM"))
  67. r << "document-share";
  68. return r;
  69. }
  70. void slotCommandRecieved(const QByteArray &line) {
  71. QList<QByteArray> tokens = line.split(':');
  72. if (tokens.count() < 3)
  73. return;
  74. if (tokens[0] != "STATUS" && tokens[0] != "BROADCAST")
  75. return;
  76. if (tokens[2].isEmpty())
  77. return;
  78. // We can't use tokens[2] because the filename might contain ':'
  79. int secondColon = line.indexOf(":", line.indexOf(":") + 1);
  80. const QByteArray name = line.mid(secondColon + 1);
  81. QByteArray &status = m_status[name]; // reference to the item in the hash
  82. if (status == tokens[1])
  83. return;
  84. status = tokens[1];
  85. emit overlaysChanged(QUrl::fromLocalFile(QString::fromUtf8(name)), overlaysForString(status));
  86. }
  87. };
  88. #include "ownclouddolphinoverlayplugin.moc"