diff options
author | Olivier Goffart <ogoffart@woboq.com> | 2018-01-18 17:17:29 +0300 |
---|---|---|
committer | Olivier Goffart <olivier@woboq.com> | 2018-01-23 16:02:56 +0300 |
commit | 1782ae3c0861635e105e79a413e395969227b00f (patch) | |
tree | 400966c1de4b412557ec8af6816b66f7892b348f /shell_integration | |
parent | 59f2e0634e349fa911d05b28a3b8e5d066fdc8f9 (diff) |
SocketAPI: Make it easier to add or remove item in the action menu
By making it dynamic.
So far only the dolphin shell extension have been ported
Diffstat (limited to 'shell_integration')
3 files changed, 79 insertions, 16 deletions
diff --git a/shell_integration/dolphin/ownclouddolphinactionplugin.cpp b/shell_integration/dolphin/ownclouddolphinactionplugin.cpp index 71c1f1171..a2010be90 100644 --- a/shell_integration/dolphin/ownclouddolphinactionplugin.cpp +++ b/shell_integration/dolphin/ownclouddolphinactionplugin.cpp @@ -17,8 +17,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ******************************************************************************/ -#include <KPluginFactory> -#include <KPluginLoader> +#include <KCoreAddons/KPluginFactory> +#include <KCoreAddons/KPluginLoader> #include <KIOWidgets/kabstractfileitemactionplugin.h> #include <QtNetwork/QLocalSocket> #include <KIOCore/kfileitem.h> @@ -27,6 +27,7 @@ #include <QtWidgets/QMenu> #include <QtCore/QDir> #include <QtCore/QTimer> +#include <QtCore/QEventLoop> #include "ownclouddolphinpluginhelper.h" class OwncloudDolphinPluginAction : public KAbstractFileItemActionPlugin @@ -39,22 +40,70 @@ public: QList<QAction*> actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) Q_DECL_OVERRIDE { auto helper = OwncloudDolphinPluginHelper::instance(); - QList<QUrl> urls = fileItemInfos.urlList(); - if (urls.count() != 1 || !helper->isConnected()) + if (!helper->isConnected() || !fileItemInfos.isLocal()) return {}; - auto url = urls.first(); - if (!url.isLocalFile()) + // If any of the url is outside of a sync folder, return an empty menu. + const QList<QUrl> urls = fileItemInfos.urlList(); + const auto paths = helper->paths(); + QByteArray files; + for (const auto &url : urls) { + QDir localPath(url.toLocalFile()); + auto localFile = localPath.canonicalPath(); + if (!std::any_of(paths.begin(), paths.end(), [&](const QString &s) { + return localFile.startsWith(s); + })) + return {}; + + if (!files.isEmpty()) + files += '\x1e'; // Record separator + files += localFile.toUtf8(); + } + + if (helper->version() < "1.1") { // in this case, lexicographic order works + return legacyActions(fileItemInfos, parentWidget); + } + + auto menu = new QMenu(parentWidget); + QEventLoop loop; + auto con = connect(helper, &OwncloudDolphinPluginHelper::commandRecieved, this, [&](const QByteArray &cmd) { + if (cmd.startsWith("GET_MENU_ITEMS:END")) { + loop.quit(); + } else if (cmd.startsWith("MENU_ITEM:")) { + auto args = QString::fromUtf8(cmd).split(QLatin1Char(':')); + if (args.size() < 3) + return; + auto action = menu->addAction(args.mid(2).join(QLatin1Char(':'))); + auto call = args.value(1).toLatin1(); + connect(action, &QAction::triggered, [helper, call, files] { + helper->sendCommand(QByteArray(call + ":" + files + "\n")); + }); + } + }); + QTimer::singleShot(100, &loop, SLOT(quit())); // add a timeout to be sure we don't freeze dolphin + helper->sendCommand(QByteArray("GET_MENU_ITEMS:" + files + "\n")); + loop.exec(QEventLoop::ExcludeUserInputEvents); + disconnect(con); + if (menu->actions().isEmpty()) { + delete menu; return {}; - QDir localPath(url.toLocalFile()); - auto localFile = localPath.canonicalPath(); + } + + auto menuaction = new QAction(parentWidget); + menuaction->setText(helper->contextMenuTitle()); + menuaction->setMenu(menu); + return { menuaction }; + } - const auto paths = helper->paths(); - if (!std::any_of(paths.begin(), paths.end(), [&](const QString &s) { - return localFile.startsWith(s); - } )) - return {}; + QList<QAction *> legacyActions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget) + { + QList<QUrl> urls = fileItemInfos.urlList(); + if (urls.count() != 1) + return {}; + QDir localPath(urls.first().toLocalFile()); + auto localFile = localPath.canonicalPath(); + auto helper = OwncloudDolphinPluginHelper::instance(); auto menuaction = new QAction(parentWidget); menuaction->setText(helper->contextMenuTitle()); auto menu = new QMenu(parentWidget); @@ -62,8 +111,8 @@ public: auto shareAction = menu->addAction(helper->shareActionTitle()); connect(shareAction, &QAction::triggered, this, [localFile, helper] { - helper->sendCommand(QByteArray("SHARE:"+localFile.toUtf8()+"\n")); - } ); + helper->sendCommand(QByteArray("SHARE:" + localFile.toUtf8() + "\n")); + }); if (!helper->copyPrivateLinkTitle().isEmpty()) { auto copyPrivateLinkAction = menu->addAction(helper->copyPrivateLinkTitle()); @@ -78,7 +127,6 @@ public: helper->sendCommand(QByteArray("EMAIL_PRIVATE_LINK:" + localFile.toUtf8() + "\n")); }); } - return { menuaction }; } diff --git a/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp b/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp index ae1705220..248aa06cf 100644 --- a/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp +++ b/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp @@ -59,6 +59,7 @@ void OwncloudDolphinPluginHelper::sendCommand(const char* data) void OwncloudDolphinPluginHelper::slotConnected() { + sendCommand("VERSION:\n"); sendCommand("GET_STRINGS:\n"); } @@ -98,6 +99,16 @@ void OwncloudDolphinPluginHelper::slotReadyRead() _strings[args[1]] = args.mid(2).join(QLatin1Char(':')); } continue; + } else if (line.startsWith("VERSION:")) { + auto args = line.split(':'); + auto version = args.value(2); + _version = version; + if (!version.startsWith("1.")) { + // Incompatible version, disconnect forever + _connectTimer.stop(); + _socket.disconnectFromServer(); + return; + } } emit commandRecieved(line); } diff --git a/shell_integration/dolphin/ownclouddolphinpluginhelper.h b/shell_integration/dolphin/ownclouddolphinpluginhelper.h index 294d1969d..3e7a97477 100644 --- a/shell_integration/dolphin/ownclouddolphinpluginhelper.h +++ b/shell_integration/dolphin/ownclouddolphinpluginhelper.h @@ -21,6 +21,7 @@ #include <QObject> #include <QBasicTimer> #include <QLocalSocket> +#include <QRegularExpression> #include "ownclouddolphinpluginhelper_export.h" class OWNCLOUDDOLPHINPLUGINHELPER_EXPORT OwncloudDolphinPluginHelper : public QObject { @@ -44,6 +45,8 @@ public: QString copyPrivateLinkTitle() const { return _strings["COPY_PRIVATE_LINK_MENU_TITLE"]; } QString emailPrivateLinkTitle() const { return _strings["EMAIL_PRIVATE_LINK_MENU_TITLE"]; } + QByteArray version() { return _version; } + signals: void commandRecieved(const QByteArray &cmd); @@ -61,4 +64,5 @@ private: QBasicTimer _connectTimer; QMap<QString, QString> _strings; + QByteArray _version; }; |