From 8c73ce4a7ee3f27bc6cac5cd6e1ac37cfb9f4280 Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Mon, 26 Jun 2017 10:09:42 +0900
Subject: [PATCH] Add function to evalute script with plasmashell

---
 app/src/handlers/systemhandler.cpp     | 104 ++++++++-----------------
 app/src/handlers/systemhandler.h       |   1 +
 app/src/websockets/websocketserver.cpp |   4 +
 3 files changed, 36 insertions(+), 73 deletions(-)

diff --git a/app/src/handlers/systemhandler.cpp b/app/src/handlers/systemhandler.cpp
index a719dac..7d39f68 100644
--- a/app/src/handlers/systemhandler.cpp
+++ b/app/src/handlers/systemhandler.cpp
@@ -64,8 +64,8 @@ bool SystemHandler::openUrl(const QString &url) const
 
 QString SystemHandler::desktopEnvironment() const
 {
-    QString desktop = "";
-    QString currentDesktop = "";
+    QString desktop;
+    QString currentDesktop;
 
     if (!qgetenv("XDG_CURRENT_DESKTOP").isEmpty()) {
         currentDesktop = QString::fromLocal8Bit(qgetenv("XDG_CURRENT_DESKTOP").constData()).toLower();
@@ -168,10 +168,21 @@ bool SystemHandler::applyFile(const QString &path, const QString &installType) c
 #endif
 
 #ifdef QTLIB_UNIX
-bool SystemHandler::applyKdeWallpaper(const QString &path) const
+bool SystemHandler::evaluateScriptWithPlasmaShell(const QString &script) const
 {
     auto message = QDBusMessage::createMethodCall("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", "evaluateScript");
+    message.setArguments(QVariantList() << QVariant(script));
+    auto reply = QDBusConnection::sessionBus().call(message);
+
+    if (reply.type() == QDBusMessage::ErrorMessage) {
+        qWarning() << reply.errorMessage();
+        return false;
+    }
+    return true;
+}
 
+bool SystemHandler::applyKdeWallpaper(const QString &path) const
+{
     QString script;
     QTextStream out(&script);
     out << "for (var key in desktops()) {"
@@ -181,24 +192,12 @@ bool SystemHandler::applyKdeWallpaper(const QString &path) const
         << "d.writeConfig('Image', 'file://" + path + "');"
         << "}";
 
-    QVariantList arguments;
-    arguments << QVariant(script);
-    message.setArguments(arguments);
-
-    auto reply = QDBusConnection::sessionBus().call(message);
-
-    if (reply.type() == QDBusMessage::ErrorMessage) {
-        qWarning() << reply.errorMessage();
-        return false;
-    }
-
-    return true;
+    return evaluateScriptWithPlasmaShell(script);
 }
 
 bool SystemHandler::applyKdeIcon(const QString &path) const
 {
     auto themeName = QFileInfo(path).fileName();
-    auto message = QDBusMessage::createMethodCall("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", "evaluateScript");
 
     QString script;
     QTextStream out(&script);
@@ -206,27 +205,17 @@ bool SystemHandler::applyKdeIcon(const QString &path) const
         << "c.group = 'Icons';"
         << "c.writeEntry('Theme', '" + themeName + "');";
 
-    QVariantList arguments;
-    arguments << QVariant(script);
-    message.setArguments(arguments);
-
-    auto reply = QDBusConnection::sessionBus().call(message);
-
-    if (reply.type() == QDBusMessage::ErrorMessage) {
-        qWarning() << reply.errorMessage();
-        return false;
+    if (evaluateScriptWithPlasmaShell(script)) {
+        QProcess::startDetached("kquitapp5 plasmashell");
+        QProcess::startDetached("kstart5 plasmashell");
+        return true;
     }
-
-    QProcess::startDetached("kquitapp5 plasmashell");
-    QProcess::startDetached("kstart5 plasmashell");
-
-    return true;
+    return false;
 }
 
 bool SystemHandler::applyKdeCursor(const QString &path) const
 {
     auto themeName = QFileInfo(path).fileName();
-    auto message = QDBusMessage::createMethodCall("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", "evaluateScript");
 
     QString script;
     QTextStream out(&script);
@@ -234,24 +223,12 @@ bool SystemHandler::applyKdeCursor(const QString &path) const
         << "c.group = 'Mouse';"
         << "c.writeEntry('cursorTheme', '" + themeName + "');";
 
-    QVariantList arguments;
-    arguments << QVariant(script);
-    message.setArguments(arguments);
-
-    auto reply = QDBusConnection::sessionBus().call(message);
-
-    if (reply.type() == QDBusMessage::ErrorMessage) {
-        qWarning() << reply.errorMessage();
-        return false;
-    }
-
-    return true;
+    return evaluateScriptWithPlasmaShell(script);
 }
 
 bool SystemHandler::applyKdePlasmaDesktoptheme(const QString &path) const
 {
     auto themeName = QFileInfo(path).fileName();
-    auto message = QDBusMessage::createMethodCall("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", "evaluateScript");
 
     QString script;
     QTextStream out(&script);
@@ -259,27 +236,17 @@ bool SystemHandler::applyKdePlasmaDesktoptheme(const QString &path) const
         << "c.group = 'Theme';"
         << "c.writeEntry('name', '" + themeName + "');";
 
-    QVariantList arguments;
-    arguments << QVariant(script);
-    message.setArguments(arguments);
-
-    auto reply = QDBusConnection::sessionBus().call(message);
-
-    if (reply.type() == QDBusMessage::ErrorMessage) {
-        qWarning() << reply.errorMessage();
-        return false;
+    if (evaluateScriptWithPlasmaShell(script)) {
+        QProcess::startDetached("kquitapp5 plasmashell");
+        QProcess::startDetached("kstart5 plasmashell");
+        return true;
     }
-
-    QProcess::startDetached("kquitapp5 plasmashell");
-    QProcess::startDetached("kstart5 plasmashell");
-
-    return true;
+    return false;
 }
 
 bool SystemHandler::applyKdeAuroraeTheme(const QString &path) const
 {
     auto themeName = QFileInfo(path).fileName();
-    auto message = QDBusMessage::createMethodCall("org.kde.plasmashell", "/PlasmaShell", "org.kde.PlasmaShell", "evaluateScript");
 
     QString script;
     QTextStream out(&script);
@@ -288,21 +255,12 @@ bool SystemHandler::applyKdeAuroraeTheme(const QString &path) const
         << "c.writeEntry('library', 'org.kde.kwin.aurorae');"
         << "c.writeEntry('theme', '__aurorae__svg__" + themeName + "');";
 
-    QVariantList arguments;
-    arguments << QVariant(script);
-    message.setArguments(arguments);
-
-    auto reply = QDBusConnection::sessionBus().call(message);
-
-    if (reply.type() == QDBusMessage::ErrorMessage) {
-        qWarning() << reply.errorMessage();
-        return false;
+    if (evaluateScriptWithPlasmaShell(script)) {
+        auto refreshMessage = QDBusMessage::createMethodCall("org.kde.KWin", "/KWin", "org.kde.KWin", "reconfigure");
+        QDBusConnection::sessionBus().call(refreshMessage);
+        return true;
     }
-
-    auto refreshMessage = QDBusMessage::createMethodCall("org.kde.KWin", "/KWin", "org.kde.KWin", "reconfigure");
-    QDBusConnection::sessionBus().call(refreshMessage);
-
-    return true;
+    return false;
 }
 
 bool SystemHandler::applyGnomeWallpaper(const QString &path) const
diff --git a/app/src/handlers/systemhandler.h b/app/src/handlers/systemhandler.h
index f9bd1ee..06cd247 100644
--- a/app/src/handlers/systemhandler.h
+++ b/app/src/handlers/systemhandler.h
@@ -23,6 +23,7 @@ public slots:
 
 private:
 #ifdef QTLIB_UNIX
+    bool evaluateScriptWithPlasmaShell(const QString &script) const;
     bool applyKdeWallpaper(const QString &path) const;
     bool applyKdeIcon(const QString &path) const;
     bool applyKdeCursor(const QString &path) const;
diff --git a/app/src/websockets/websocketserver.cpp b/app/src/websockets/websocketserver.cpp
index 8eb69e9..59f8f78 100644
--- a/app/src/websockets/websocketserver.cpp
+++ b/app/src/websockets/websocketserver.cpp
@@ -15,6 +15,7 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se
     : QObject(parent), configHandler_(configHandler), serverName_(serverName), serverPort_(serverPort)
 {
     wsServer_ = new QWebSocketServer(serverName_, QWebSocketServer::NonSecureMode, this);
+
     connect(wsServer_, &QWebSocketServer::newConnection, this, &WebSocketServer::wsNewConnection);
     connect(wsServer_, &QWebSocketServer::closed, this, &WebSocketServer::stopped);
 
@@ -22,6 +23,7 @@ WebSocketServer::WebSocketServer(ConfigHandler *configHandler, const QString &se
     systemHandler_ = new SystemHandler(this);
     ocsApiHandler_ = new OcsApiHandler(configHandler_, this);
     itemHandler_ = new ItemHandler(configHandler_, this);
+
     connect(itemHandler_, &ItemHandler::metadataSetChanged, this, &WebSocketServer::itemMetadataSetChanged);
     connect(itemHandler_, &ItemHandler::downloadStarted, this, &WebSocketServer::itemDownloadStarted);
     connect(itemHandler_, &ItemHandler::downloadFinished, this, &WebSocketServer::itemDownloadFinished);
@@ -75,9 +77,11 @@ QUrl WebSocketServer::serverUrl() const
 void WebSocketServer::wsNewConnection()
 {
     auto *wsClient = wsServer_->nextPendingConnection();
+
     connect(wsClient, &QWebSocket::disconnected, this, &WebSocketServer::wsDisconnected);
     connect(wsClient, &QWebSocket::textMessageReceived, this, &WebSocketServer::wsTextMessageReceived);
     connect(wsClient, &QWebSocket::binaryMessageReceived, this, &WebSocketServer::wsBinaryMessageReceived);
+
     wsClients_ << wsClient;
 }
 
-- 
GitLab