diff options
author | Gabriele Mohr <75983861+gabi18@users.noreply.github.com> | 2021-05-26 16:03:32 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-26 16:03:32 +0300 |
commit | 066ed56370ebcad5caa4949b882af1d14a922d25 (patch) | |
tree | d209e0db05f188a4c6131e0890a81ad1f513d744 /shell_integration | |
parent | eac5bcbd306d09995f6b9fe4956c38d2142ad2d2 (diff) |
support V2/GET_CLIENT_ICON in socketapi, add icon to dolphin right click menu (#8638)
* support V2/GET_CLIENT_ICON in socketapi, add icon to dolphin right click menu
* apply review improvements
* further improvements
* logging added, improvements
* improve OwncloudDolphinPluginHelper::slotReadyRead
* use line.mid(), add const
* changlog for #8464
Diffstat (limited to 'shell_integration')
4 files changed, 78 insertions, 15 deletions
diff --git a/shell_integration/dolphin/CMakeLists.txt b/shell_integration/dolphin/CMakeLists.txt index ff8888a7a..6a06517c2 100644 --- a/shell_integration/dolphin/CMakeLists.txt +++ b/shell_integration/dolphin/CMakeLists.txt @@ -40,7 +40,7 @@ endif() #---HELPER--- set(OWNCLOUDDOLPHINHELPER ${APPLICATION_EXECUTABLE}dolphinpluginhelper) add_library(${OWNCLOUDDOLPHINHELPER} SHARED ownclouddolphinpluginhelper.cpp) -target_link_libraries(${OWNCLOUDDOLPHINHELPER} Qt5::Network) +target_link_libraries(${OWNCLOUDDOLPHINHELPER} Qt5::Network Qt5::Gui) generate_export_header(${OWNCLOUDDOLPHINHELPER} BASE_NAME ownclouddolphinpluginhelper) install(TARGETS ${OWNCLOUDDOLPHINHELPER} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/shell_integration/dolphin/ownclouddolphinactionplugin.cpp b/shell_integration/dolphin/ownclouddolphinactionplugin.cpp index 713d075a5..016968115 100644 --- a/shell_integration/dolphin/ownclouddolphinactionplugin.cpp +++ b/shell_integration/dolphin/ownclouddolphinactionplugin.cpp @@ -86,6 +86,9 @@ public: }); QTimer::singleShot(500, &loop, SLOT(quit())); // add a timeout to be sure we don't freeze dolphin helper->sendCommand(QByteArray("GET_MENU_ITEMS:" + files + "\n")); + + helper->sendGetClientIconCommand(16); // get client icon with size 16x16 + loop.exec(QEventLoop::ExcludeUserInputEvents); disconnect(con); if (menu->actions().isEmpty()) { @@ -95,7 +98,9 @@ public: auto menuaction = new QAction(parentWidget); menuaction->setText(helper->contextMenuTitle()); + menuaction->setIcon(QIcon(helper->clientIcon())); menuaction->setMenu(menu); + return { menuaction }; } diff --git a/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp b/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp index 1bb10e6c5..27c2daf3f 100644 --- a/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp +++ b/shell_integration/dolphin/ownclouddolphinpluginhelper.cpp @@ -21,8 +21,13 @@ #include <qcoreevent.h> #include <QStandardPaths> #include <QFile> +#include <QLoggingCategory> #include "ownclouddolphinpluginhelper.h" #include "config.h" +#include <QJsonObject> +#include <QJsonDocument> + +Q_LOGGING_CATEGORY(lcPluginHelper, "owncloud.dolphin", QtInfoMsg) OwncloudDolphinPluginHelper* OwncloudDolphinPluginHelper::instance() { @@ -58,6 +63,16 @@ void OwncloudDolphinPluginHelper::sendCommand(const char* data) _socket.flush(); } +void OwncloudDolphinPluginHelper::sendGetClientIconCommand(int size) +{ + const QByteArray cmd = QByteArrayLiteral("V2/GET_CLIENT_ICON:"); + const QByteArray newLine = QByteArrayLiteral("\n"); + const QJsonObject args { { QStringLiteral("size"), size } }; + const QJsonObject obj { { QStringLiteral("id"), QString::number(_msgId++) }, { QStringLiteral("arguments"), args } }; + const auto json = QJsonDocument(obj).toJson(QJsonDocument::Compact); + sendCommand(QByteArray(cmd + json + newLine)); +} + void OwncloudDolphinPluginHelper::slotConnected() { sendCommand("VERSION:\n"); @@ -69,7 +84,7 @@ void OwncloudDolphinPluginHelper::tryConnect() if (_socket.state() != QLocalSocket::UnconnectedState) { return; } - + QString socketPath = QStandardPaths::locate(QStandardPaths::RuntimeLocation, APPLICATION_SHORTNAME, QStandardPaths::LocateDirectory); @@ -83,36 +98,70 @@ void OwncloudDolphinPluginHelper::slotReadyRead() { while (_socket.bytesAvailable()) { _line += _socket.readLine(); - if (!_line.endsWith("\n")) + if (!_line.endsWith("\n")) { continue; + } QByteArray line; qSwap(line, _line); line.chop(1); if (line.isEmpty()) continue; + const int firstColon = line.indexOf(':'); + if (firstColon == -1) { + continue; + } + // get the command (at begin of line, before first ':') + const QByteArray command = line.left(firstColon); + // rest of line contains the information + const QByteArray info = line.mid(firstColon + 1); - if (line.startsWith("REGISTER_PATH:")) { - auto col = line.indexOf(':'); - QString file = QString::fromUtf8(line.constData() + col + 1, line.size() - col - 1); + if (command == QByteArrayLiteral("REGISTER_PATH")) { + const QString file = QString::fromUtf8(info); _paths.append(file); continue; - } else if (line.startsWith("STRING:")) { - auto args = QString::fromUtf8(line).split(QLatin1Char(':')); - if (args.size() >= 3) { - _strings[args[1]] = args.mid(2).join(QLatin1Char(':')); + } else if (command == QByteArrayLiteral("STRING")) { + auto args = QString::fromUtf8(info).split(':'); + if (args.size() >= 2) { + _strings[args[0]] = args.mid(1).join(':'); } continue; - } else if (line.startsWith("VERSION:")) { - auto args = line.split(':'); - auto version = args.value(2); - _version = version; - if (!version.startsWith("1.")) { + } else if (command == QByteArrayLiteral("VERSION")) { + auto args = info.split(':'); + if (args.size() >= 2) { + auto version = args.value(1); + _version = version; + } + if (!_version.startsWith("1.")) { // Incompatible version, disconnect forever _connectTimer.stop(); _socket.disconnectFromServer(); return; } + } else if (command == QByteArrayLiteral("V2/GET_CLIENT_ICON_RESULT")) { + QJsonParseError error; + auto json = QJsonDocument::fromJson(info, &error).object(); + if (error.error != QJsonParseError::NoError) { + qCWarning(lcPluginHelper) << "Error while parsing result: " << error.error; + continue; + } + + auto jsonArgs = json.value("arguments").toObject(); + if (jsonArgs.isEmpty()) { + auto jsonErr = json.value("error").toObject(); + qCWarning(lcPluginHelper) << "Error getting client icon: " << jsonErr; + continue; + } + + const QByteArray pngBase64 = jsonArgs.value("png").toString().toUtf8(); + QByteArray png = QByteArray::fromBase64(pngBase64); + + QPixmap pixmap; + bool isLoaded = pixmap.loadFromData(png, "PNG"); + if (isLoaded) { + _clientIcon = pixmap; + } } + emit commandRecieved(line); } } diff --git a/shell_integration/dolphin/ownclouddolphinpluginhelper.h b/shell_integration/dolphin/ownclouddolphinpluginhelper.h index 3e7a97477..71388c651 100644 --- a/shell_integration/dolphin/ownclouddolphinpluginhelper.h +++ b/shell_integration/dolphin/ownclouddolphinpluginhelper.h @@ -22,6 +22,7 @@ #include <QBasicTimer> #include <QLocalSocket> #include <QRegularExpression> +#include <QPixmap> #include "ownclouddolphinpluginhelper_export.h" class OWNCLOUDDOLPHINPLUGINHELPER_EXPORT OwncloudDolphinPluginHelper : public QObject { @@ -31,6 +32,8 @@ public: bool isConnected() const; void sendCommand(const char *data); + void sendGetClientIconCommand(int size); + QVector<QString> paths() const { return _paths; } QString contextMenuTitle() const @@ -41,6 +44,10 @@ public: { return _strings.value("SHARE_MENU_TITLE", "Share..."); } + QPixmap clientIcon() const + { + return _clientIcon; + } QString copyPrivateLinkTitle() const { return _strings["COPY_PRIVATE_LINK_MENU_TITLE"]; } QString emailPrivateLinkTitle() const { return _strings["EMAIL_PRIVATE_LINK_MENU_TITLE"]; } @@ -65,4 +72,6 @@ private: QMap<QString, QString> _strings; QByteArray _version; + QPixmap _clientIcon; + int _msgId = 1; }; |