[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdebase/workspace/libs/plasma
From:       Aaron J. Seigo <aseigo () kde ! org>
Date:       2008-11-03 20:56:44
Message-ID: 1225745804.427286.31103.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 879704 by aseigo:

actions for runners


 M  +46 -2     abstractrunner.cpp  
 M  +64 -1     abstractrunner.h  
 M  +15 -2     querymatch.cpp  
 M  +11 -0     querymatch.h  
 M  +14 -2     runnermanager.cpp  
 M  +6 -0      runnermanager.h  


--- trunk/KDE/kdebase/workspace/libs/plasma/abstractrunner.cpp #879703:879704
@@ -19,6 +19,8 @@
 
 #include "abstractrunner.h"
 
+#include <QAction>
+#include <QHash>
 #include <QMutex>
 #include <QMutexLocker>
 
@@ -93,6 +95,7 @@
     QTime runtime;
     int fastRuns;
     Package *package;
+    QHash<QString, QAction*> actions;
 };
 
 K_GLOBAL_STATIC(QMutex, s_bigLock)
@@ -163,6 +166,47 @@
     }
 }
 
+QList<QAction*> AbstractRunner::actionsForMatch(const Plasma::QueryMatch &match)
+{
+    Q_UNUSED(match)
+    QList<QAction*> ret;
+    return ret;
+}
+
+QAction* AbstractRunner::addAction(const QString &id, const QIcon &icon, const QString &text)
+{
+    QAction *a = new QAction(icon, text, this);
+    d->actions.insert(id, a);
+    return a;
+}
+
+void AbstractRunner::addAction(const QString &id, QAction *action)
+{
+    d->actions.insert(id, action);
+}
+
+void AbstractRunner::removeAction(const QString &id)
+{
+    QAction *a = d->actions.take(id);
+    delete a;
+}
+
+QAction* AbstractRunner::action(const QString &id) const
+{
+    return d->actions.value(id);
+}
+
+QHash<QString, QAction*> AbstractRunner::actions() const
+{
+    return d->actions;
+}
+
+void AbstractRunner::clearActions()
+{
+    qDeleteAll(d->actions);
+    d->actions.clear();
+}
+
 bool AbstractRunner::hasRunOptions()
 {
     return d->hasRunOptions;
@@ -214,7 +258,7 @@
     return KServiceTypeTrader::self()->query(serviceType, constraint);
 }
 
-QMutex *AbstractRunner::bigLock() const
+QMutex* AbstractRunner::bigLock() const
 {
     return s_bigLock;
 }
@@ -257,7 +301,7 @@
     return d->runnerDescription.property("Comment").toString();
 }
 
-const Package *AbstractRunner::package() const
+const Package* AbstractRunner::package() const
 {
     return d->package;
 }
--- trunk/KDE/kdebase/workspace/libs/plasma/abstractrunner.h #879703:879704
@@ -32,6 +32,8 @@
 #include <plasma/querymatch.h>
 #include <plasma/version.h>
 
+class QAction;
+
 class KCompletion;
 
 namespace Plasma
@@ -114,6 +116,12 @@
          * quit the loop and make the match() method return. The local status is kept
          * entirely in MyAsyncWorker which makes match() trivially thread-safe.
          *
+         * If a particular match supports multiple actions, set up the corresponding
+         * actions in the actionsForMatch method. Do not call any of the action methods
+         * within this method!
+         * @see actionsForMatch
+         *
+         * Execution of the correct action should be handled in the run method.
          * @caution This method needs to be thread-safe since KRunner will simply
          * start a new thread for each new term.
          *
@@ -121,7 +129,6 @@
          *
          * @sa run(), RunnerContext::addMatch, RunnerContext::addMatches, QueryMatch
          */
-        // trueg: why is this method not protected?
         virtual void match(Plasma::RunnerContext &context);
 
         /**
@@ -264,6 +271,62 @@
 
         QMutex *bigLock() const;
 
+        /**
+         * A given match can have more than action that can be performed on it.
+         * For example, a song match returned by a music player runner can be queued,
+         * added to the playlist, or played.
+         *
+         * Call this method to add actions that can be performed by the runner.
+         * Actions must first be added to the runner's action registry.
+         * Note: execution of correct action is left up to the runner.
+         */
+        virtual QList<QAction*> actionsForMatch(const Plasma::QueryMatch &match);
+
+        /**
+         * Creates and then adds an action to the action registry.
+         * AbstractRunner assumes ownership of the created action.
+         *
+         * @param id A unique identifier string
+         * @param icon The icon to display
+         * @param text The text to display
+         * @return the created QAction
+         */
+        QAction* addAction(const QString &id, const QIcon &icon, const QString &text);
+
+        /**
+         * Adds an action to the runner's action registry.
+         *
+         * The QAction must be created within the GUI thread;
+         * do not create it within the match method of AbstractRunner.
+         *
+         * @param id A unique identifier string
+         * @param action The QAction to be stored
+         */
+        void addAction(const QString &id, QAction *action);
+
+        /**
+         * Removes the action from the action registry.
+         * AbstractRunner deletes the action once removed.
+         *
+         * @param id The id of the action to be removed
+         */
+        void removeAction(const QString &id);
+
+        /**
+         * Returns the action associated with the id
+         */
+        QAction* action(const QString &id) const;
+
+        /**
+         * Returns all registered actions
+         */
+        QHash<QString, QAction*> actions() const;
+        /**
+         * Clears the action registry.
+         * The action pool deletes the actions.
+         */
+        void clearActions();
+
     protected Q_SLOTS:
         void init();
 
--- trunk/KDE/kdebase/workspace/libs/plasma/querymatch.cpp #879703:879704
@@ -19,6 +19,7 @@
 
 #include "querymatch.h"
 
+#include <QAction>
 #include <QPointer>
 #include <QVariant>
 #include <QSharedData>
@@ -40,7 +41,8 @@
               runner(r),
               type(QueryMatch::ExactMatch),
               enabled(true),
-              relevance(.7)
+              relevance(.7),
+              selAction(0)
         {
         }
 
@@ -53,6 +55,7 @@
         QVariant data;
         bool enabled;
         qreal relevance;
+        QAction *selAction;
 };
 
 QueryMatch::QueryMatch(AbstractRunner *runner)
@@ -103,7 +106,7 @@
     return d->relevance;
 }
 
-AbstractRunner *QueryMatch::runner() const
+AbstractRunner* QueryMatch::runner() const
 {
     return d->runner;
 }
@@ -170,6 +173,16 @@
     return d->enabled && d->runner;
 }
 
+QAction* QueryMatch::selectedAction() const
+{
+    return d->selAction;
+}
+
+void QueryMatch::setSelectedAction(QAction *action)
+{
+    d->selAction = action;
+}
+
 bool QueryMatch::operator<(const QueryMatch &other) const
 {
     if (d->type == other.d->type) {
--- trunk/KDE/kdebase/workspace/libs/plasma/querymatch.h #879703:879704
@@ -20,10 +20,12 @@
 #ifndef PLASMA_QUERYMATCH_H
 #define PLASMA_QUERYMATCH_H
 
+#include <QList>
 #include <QtCore/QSharedDataPointer>
 
 #include <plasma/plasma_export.h>
 
+class QAction;
 class QIcon;
 class QVariant;
 class QString;
@@ -168,6 +170,15 @@
         void setIcon(const QIcon &icon);
         void setEnabled(bool enable);
 
+        /**
+         * The current action.
+         */
+        QAction* selectedAction() const;
+        /**
+        * Sets the selected action
+        */
+        void setSelectedAction(QAction *action);
+
     private:
         QSharedDataPointer<QueryMatchPrivate> d;
 };
--- trunk/KDE/kdebase/workspace/libs/plasma/runnermanager.cpp #879703:879704
@@ -226,7 +226,9 @@
         KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner");
 
         bool loadAll = config.readEntry("loadAll", false);
-        KConfigGroup conf(&config, "Plugins");
+        //The plugin configuration is stored under the section Plugins
+        //and not PlasmaRunnerManager->Plugins
+        KConfigGroup conf(KGlobal::config(), "Plugins");
 
         foreach (const KService::Ptr &service, offers) {
             //kDebug() << "Loading runner: " << service->name() << service->storageId();
@@ -277,7 +279,7 @@
             }
         }
 
-        //kDebug() << "All runners loaded, total:" << runners.count();
+        kDebug() << "All runners loaded, total:" << runners.count();
     }
 
     void jobDone(ThreadWeaver::Job *job)
@@ -387,6 +389,16 @@
     }
 }
 
+QList<QAction*> RunnerManager::actionsForMatch(const QueryMatch &match)
+{
+    AbstractRunner *runner = match.runner();
+    if (runner) {
+        return runner->actionsForMatch(match);
+    }
+
+    return QList<QAction*>();
+}
+
 void RunnerManager::launchQuery(const QString &term)
 {
     launchQuery(term, QString());
--- trunk/KDE/kdebase/workspace/libs/plasma/runnermanager.h #879703:879704
@@ -28,6 +28,7 @@
 #include <plasma/plasma_export.h>
 #include "abstractrunner.h"
 
+class QAction;
 class KConfigGroup;
 
 namespace Plasma
@@ -84,6 +85,11 @@
         void run(const QString &id);
 
         /**
+         * Retrieves the list of actions, if any, for a match
+         */
+        QList<QAction*> actionsForMatch(const QueryMatch &match);
+
+        /**
          * @return the current query term
          */
         QString query() const;
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic