ownclouddolphinactionplugin.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <KCoreAddons/KPluginFactory>
  20. #include <KCoreAddons/KPluginLoader>
  21. #include <KIOWidgets/kabstractfileitemactionplugin.h>
  22. #include <QtNetwork/QLocalSocket>
  23. #include <KIOCore/kfileitem.h>
  24. #include <KIOCore/KFileItemListProperties>
  25. #include <QtWidgets/QAction>
  26. #include <QtWidgets/QMenu>
  27. #include <QtCore/QDir>
  28. #include <QtCore/QTimer>
  29. #include <QtCore/QEventLoop>
  30. #include "ownclouddolphinpluginhelper.h"
  31. class OwncloudDolphinPluginAction : public KAbstractFileItemActionPlugin
  32. {
  33. Q_OBJECT
  34. public:
  35. explicit OwncloudDolphinPluginAction(QObject* parent, const QList<QVariant>&)
  36. : KAbstractFileItemActionPlugin(parent) { }
  37. QList<QAction*> actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) override
  38. {
  39. auto helper = OwncloudDolphinPluginHelper::instance();
  40. if (!helper->isConnected() || !fileItemInfos.isLocal())
  41. return {};
  42. // If any of the url is outside of a sync folder, return an empty menu.
  43. const QList<QUrl> urls = fileItemInfos.urlList();
  44. const auto paths = helper->paths();
  45. QByteArray files;
  46. for (const auto &url : urls) {
  47. QDir localPath(url.toLocalFile());
  48. auto localFile = localPath.canonicalPath();
  49. if (!std::any_of(paths.begin(), paths.end(), [&](const QString &s) {
  50. return localFile.startsWith(s);
  51. }))
  52. return {};
  53. if (!files.isEmpty())
  54. files += '\x1e'; // Record separator
  55. files += localFile.toUtf8();
  56. }
  57. if (helper->version() < "1.1") { // in this case, lexicographic order works
  58. return legacyActions(fileItemInfos, parentWidget);
  59. }
  60. auto menu = new QMenu(parentWidget);
  61. QEventLoop loop;
  62. auto con = connect(helper, &OwncloudDolphinPluginHelper::commandRecieved, this, [&](const QByteArray &cmd) {
  63. if (cmd.startsWith("GET_MENU_ITEMS:END")) {
  64. loop.quit();
  65. } else if (cmd.startsWith("MENU_ITEM:")) {
  66. auto args = QString::fromUtf8(cmd).split(QLatin1Char(':'));
  67. if (args.size() < 4)
  68. return;
  69. auto action = menu->addAction(args.mid(3).join(QLatin1Char(':')));
  70. if (args.value(2).contains(QLatin1Char('d')))
  71. action->setDisabled(true);
  72. auto call = args.value(1).toLatin1();
  73. connect(action, &QAction::triggered, [helper, call, files] {
  74. helper->sendCommand(QByteArray(call + ":" + files + "\n"));
  75. });
  76. }
  77. });
  78. QTimer::singleShot(100, &loop, SLOT(quit())); // add a timeout to be sure we don't freeze dolphin
  79. helper->sendCommand(QByteArray("GET_MENU_ITEMS:" + files + "\n"));
  80. loop.exec(QEventLoop::ExcludeUserInputEvents);
  81. disconnect(con);
  82. if (menu->actions().isEmpty()) {
  83. delete menu;
  84. return {};
  85. }
  86. menu->setTitle(helper->contextMenuTitle());
  87. menu->setIcon(QIcon::fromTheme(helper->contextMenuIconName()));
  88. return { menu->menuAction() };
  89. }
  90. QList<QAction *> legacyActions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
  91. {
  92. QList<QUrl> urls = fileItemInfos.urlList();
  93. if (urls.count() != 1)
  94. return {};
  95. QDir localPath(urls.first().toLocalFile());
  96. auto localFile = localPath.canonicalPath();
  97. auto helper = OwncloudDolphinPluginHelper::instance();
  98. auto menuaction = new QAction(parentWidget);
  99. menuaction->setText(helper->contextMenuTitle());
  100. auto menu = new QMenu(parentWidget);
  101. menuaction->setMenu(menu);
  102. auto shareAction = menu->addAction(helper->shareActionTitle());
  103. connect(shareAction, &QAction::triggered, this, [localFile, helper] {
  104. helper->sendCommand(QByteArray("SHARE:" + localFile.toUtf8() + "\n"));
  105. });
  106. if (!helper->copyPrivateLinkTitle().isEmpty()) {
  107. auto copyPrivateLinkAction = menu->addAction(helper->copyPrivateLinkTitle());
  108. connect(copyPrivateLinkAction, &QAction::triggered, this, [localFile, helper] {
  109. helper->sendCommand(QByteArray("COPY_PRIVATE_LINK:" + localFile.toUtf8() + "\n"));
  110. });
  111. }
  112. if (!helper->emailPrivateLinkTitle().isEmpty()) {
  113. auto emailPrivateLinkAction = menu->addAction(helper->emailPrivateLinkTitle());
  114. connect(emailPrivateLinkAction, &QAction::triggered, this, [localFile, helper] {
  115. helper->sendCommand(QByteArray("EMAIL_PRIVATE_LINK:" + localFile.toUtf8() + "\n"));
  116. });
  117. }
  118. return { menuaction };
  119. }
  120. };
  121. K_PLUGIN_CLASS_WITH_JSON(OwncloudDolphinPluginAction, APPLICATION_EXECUTABLE "dolphinactionplugin.json")
  122. #include "ownclouddolphinactionplugin.moc"