OSDN Git Service

plasma: make share dataengine use QScriptEngine instead of Kross
authorIvailo Monev <xakepa10@gmail.com>
Sat, 2 Apr 2016 00:25:23 +0000 (00:25 +0000)
committerIvailo Monev <xakepa10@gmail.com>
Sat, 2 Apr 2016 00:25:23 +0000 (00:25 +0000)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
plasma/dataengines/share/CMakeLists.txt
plasma/dataengines/share/shareservice.cpp
plasma/dataengines/share/shareservice.h

index e2e58a6..c3849ef 100644 (file)
@@ -10,8 +10,7 @@ kde4_add_plugin(plasma_engine_share ${share_engine_SRCS})
 target_link_libraries(plasma_engine_share
                       ${KDE4_KDECORE_LIBS}
                       ${KDE4_PLASMA_LIBS}
-                      ${KDE4_KIO_LIBS}
-                      ${KDE4_KROSSCORE_LIBS})
+                      ${KDE4_KIO_LIBS})
 
 install(TARGETS plasma_engine_share
         DESTINATION ${PLUGIN_INSTALL_DIR})
index 1b71a86..7f3dc59 100644 (file)
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
  ***************************************************************************/
 
-#include <QtCore/QFile>
-
-#include <kross/core/action.h>
-#include <kross/core/interpreter.h>
-#include <kross/core/manager.h>
 #include <kstandarddirs.h>
 
 #include <Plasma/Package>
@@ -45,13 +40,13 @@ Plasma::ServiceJob *ShareService::createJob(const QString &operation,
 ShareJob::ShareJob(const QString &destination, const QString &operation,
                    QMap<QString, QVariant> &parameters, QObject *parent)
     : Plasma::ServiceJob(destination, operation, parameters, parent),
-      m_action(0), m_provider(0), m_package(0)
+      m_engine(0), m_provider(0), m_package(0)
 {
 }
 
 ShareJob::~ShareJob()
 {
-    delete m_action;
+    delete m_engine;
     delete m_provider;
     delete m_package;
 }
@@ -82,77 +77,65 @@ void ShareJob::start()
             m_package->path() + m_package->structure()->contentsPrefixPaths().at(0) +
             m_package->structure()->path("mainscript");
 
-        if (!QFile::exists(mainscript)) {
+        QFile mainfile(mainscript);
+        if (!mainfile.exists()) {
             showError(i18n("Selected provider does not have a valid script file"));
             return;
         }
+        mainfile.open(QIODevice::ReadOnly);
+
+        QScriptEngine * m_engine = new QScriptEngine(parent());
+        if (!m_engine) {
+            showError(i18n("Could not initialize engine"));
+            return;
+        }
 
-        const QString interpreter =
-            Kross::Manager::self().interpreternameForFile(mainscript);
+        m_provider = new ShareProvider(this);
+        connect(m_provider, SIGNAL(readyToPublish()), this, SLOT(publish()));
+        connect(m_provider, SIGNAL(finished(QString)),
+                this, SLOT(showResult(QString)));
+        connect(m_provider, SIGNAL(finishedError(QString)),
+                this, SLOT(showError(QString)));
 
-        if (interpreter.isEmpty()) {
-            showError(i18n("Selected provider does not provide a supported script file"));
+        QScriptValue provideproto = m_engine->newQObject(m_provider);
+        m_engine->globalObject().setProperty("provider", provideproto);
+
+        QScriptValue maincode = m_engine->evaluate(mainfile.readAll(), mainscript);
+
+        if(m_engine->hasUncaughtException()) {
+            showError(i18n("Error trying to execute script"));
             return;
         }
 
-        m_action = new Kross::Action(parent(), pluginName);
-        if (m_action) {
-            m_provider = new ShareProvider(this);
-            connect(m_provider, SIGNAL(readyToPublish()), this, SLOT(publish()));
-            connect(m_provider, SIGNAL(finished(QString)),
-                    this, SLOT(showResult(QString)));
-            connect(m_provider, SIGNAL(finishedError(QString)),
-                    this, SLOT(showError(QString)));
-
-            // automatically connects signals and slots with the script
-            m_action->addObject(m_provider, "provider",
-                                Kross::ChildrenInterface::AutoConnectSignals);
-
-            // set the main script file and load it
-            m_action->setFile(mainscript);
-            m_action->trigger();
-
-            // check for any errors
-            if(m_action->hadError()) {
-                showError(i18n("Error trying to execute script"));
-                return;
-            }
-
-            // do the work together with the loaded plugin
-            const QStringList functions = m_action->functionNames();
-            if (!functions.contains("url") || !functions.contains("contentKey") ||
-                !functions.contains("setup")) {
-                showError(i18n("Could not find all required functions"));
-                return;
-            }
-
-            // call the methods from the plugin
-            const QString url =
-                m_action->callFunction("url", QVariantList()).toString();
-            m_provider->setUrl(url);
-
-            // setup the method (get/post)
-            QVariant vmethod;
-            if (functions.contains("method")) {
-                vmethod =
-                    m_action->callFunction("method", QVariantList()).toString();
-            }
-
-            // default is POST (if the plugin does not specify one method)
-            const QString method = vmethod.isValid() ? vmethod.toString() : "POST";
-            m_provider->setMethod(method);
-
-            // setup the provider
-            QVariant setup = m_action->callFunction("setup", QVariantList());
-
-            // get the content from the parameters, set the url and add the file
-            // then we can wait the signal to publish the information
-            const QString contentKey =
-                m_action->callFunction("contentKey", QVariantList()).toString();
-
-            const QString content(parameters()["content"].toString());
-            m_provider->addPostFile(contentKey, content);
+        QScriptValue mainobject = m_engine->globalObject();
+        // do the work together with the loaded plugin
+        if (!mainobject.property("url").isFunction()
+            || !mainobject.property("contentKey").isFunction()
+            || !mainobject.property("setup").isFunction()) {
+            showError(i18n("Could not find all required functions"));
+            return;
         }
+
+        // call the methods from the plugin
+        const QString url = mainobject.property("url").call().toString();
+        m_provider->setUrl(url);
+
+        // default is POST (if the plugin does not specify one method)
+        QVariant method = "POST";
+        if (mainobject.property("method").isFunction()) {
+            method = mainobject.property("method").call().toString();
+        }
+        m_provider->setMethod(method.toString());
+
+        // setup the provider
+        QScriptValue setup = mainobject.property("setup").call();
+
+        // get the content from the parameters, set the url and add the file
+        // then we can wait the signal to publish the information
+        const QString contentKey = mainobject.property("contentKey").call().toString();
+
+        const QString content(parameters()["content"].toString());
+        m_provider->addPostFile(contentKey, content);
     }
 }
 
index aa07591..fbdc04f 100644 (file)
@@ -22,6 +22,8 @@
 
 #include "shareengine.h"
 
+#include <QtScript/QScriptEngine>
+
 #include <Plasma/Service>
 #include <Plasma/ServiceJob>
 
@@ -32,10 +34,6 @@ namespace Plasma {
     class Package;
 }
 
-namespace Kross {
-    class Action;
-}
-
 class ShareService : public Plasma::Service
 {
     Q_OBJECT
@@ -62,7 +60,7 @@ public slots:
     void showError(const QString &msg);
 
 private:
-    Kross::Action *m_action;
+    QScriptEngine *m_engine;
     ShareProvider *m_provider;
     Plasma::Package *m_package;
 };