actions for runners

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=879704
This commit is contained in:
Aaron J. Seigo 2008-11-03 20:56:43 +00:00
parent 4a38cf2542
commit 7c7f8b098a
6 changed files with 156 additions and 7 deletions

View File

@ -19,6 +19,8 @@
#include "abstractrunner.h" #include "abstractrunner.h"
#include <QAction>
#include <QHash>
#include <QMutex> #include <QMutex>
#include <QMutexLocker> #include <QMutexLocker>
@ -93,6 +95,7 @@ public:
QTime runtime; QTime runtime;
int fastRuns; int fastRuns;
Package *package; Package *package;
QHash<QString, QAction*> actions;
}; };
K_GLOBAL_STATIC(QMutex, s_bigLock) K_GLOBAL_STATIC(QMutex, s_bigLock)
@ -163,6 +166,47 @@ void AbstractRunner::performMatch(Plasma::RunnerContext &globalContext)
} }
} }
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() bool AbstractRunner::hasRunOptions()
{ {
return d->hasRunOptions; return d->hasRunOptions;
@ -214,7 +258,7 @@ KService::List AbstractRunner::serviceQuery(const QString &serviceType, const QS
return KServiceTypeTrader::self()->query(serviceType, constraint); return KServiceTypeTrader::self()->query(serviceType, constraint);
} }
QMutex *AbstractRunner::bigLock() const QMutex* AbstractRunner::bigLock() const
{ {
return s_bigLock; return s_bigLock;
} }
@ -257,7 +301,7 @@ QString AbstractRunner::description() const
return d->runnerDescription.property("Comment").toString(); return d->runnerDescription.property("Comment").toString();
} }
const Package *AbstractRunner::package() const const Package* AbstractRunner::package() const
{ {
return d->package; return d->package;
} }

View File

@ -32,6 +32,8 @@
#include <plasma/querymatch.h> #include <plasma/querymatch.h>
#include <plasma/version.h> #include <plasma/version.h>
class QAction;
class KCompletion; class KCompletion;
namespace Plasma namespace Plasma
@ -114,6 +116,12 @@ class PLASMA_EXPORT AbstractRunner : public QObject
* quit the loop and make the match() method return. The local status is kept * quit the loop and make the match() method return. The local status is kept
* entirely in MyAsyncWorker which makes match() trivially thread-safe. * 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 * @caution This method needs to be thread-safe since KRunner will simply
* start a new thread for each new term. * start a new thread for each new term.
* *
@ -121,7 +129,6 @@ class PLASMA_EXPORT AbstractRunner : public QObject
* *
* @sa run(), RunnerContext::addMatch, RunnerContext::addMatches, QueryMatch * @sa run(), RunnerContext::addMatch, RunnerContext::addMatches, QueryMatch
*/ */
// trueg: why is this method not protected?
virtual void match(Plasma::RunnerContext &context); virtual void match(Plasma::RunnerContext &context);
/** /**
@ -264,6 +271,62 @@ class PLASMA_EXPORT AbstractRunner : public QObject
QMutex *bigLock() const; 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: protected Q_SLOTS:
void init(); void init();

View File

@ -19,6 +19,7 @@
#include "querymatch.h" #include "querymatch.h"
#include <QAction>
#include <QPointer> #include <QPointer>
#include <QVariant> #include <QVariant>
#include <QSharedData> #include <QSharedData>
@ -40,7 +41,8 @@ class QueryMatchPrivate : public QSharedData
runner(r), runner(r),
type(QueryMatch::ExactMatch), type(QueryMatch::ExactMatch),
enabled(true), enabled(true),
relevance(.7) relevance(.7),
selAction(0)
{ {
} }
@ -53,6 +55,7 @@ class QueryMatchPrivate : public QSharedData
QVariant data; QVariant data;
bool enabled; bool enabled;
qreal relevance; qreal relevance;
QAction *selAction;
}; };
QueryMatch::QueryMatch(AbstractRunner *runner) QueryMatch::QueryMatch(AbstractRunner *runner)
@ -103,7 +106,7 @@ qreal QueryMatch::relevance() const
return d->relevance; return d->relevance;
} }
AbstractRunner *QueryMatch::runner() const AbstractRunner* QueryMatch::runner() const
{ {
return d->runner; return d->runner;
} }
@ -170,6 +173,16 @@ bool QueryMatch::isEnabled() const
return d->enabled && d->runner; 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 bool QueryMatch::operator<(const QueryMatch &other) const
{ {
if (d->type == other.d->type) { if (d->type == other.d->type) {

View File

@ -20,10 +20,12 @@
#ifndef PLASMA_QUERYMATCH_H #ifndef PLASMA_QUERYMATCH_H
#define PLASMA_QUERYMATCH_H #define PLASMA_QUERYMATCH_H
#include <QList>
#include <QtCore/QSharedDataPointer> #include <QtCore/QSharedDataPointer>
#include <plasma/plasma_export.h> #include <plasma/plasma_export.h>
class QAction;
class QIcon; class QIcon;
class QVariant; class QVariant;
class QString; class QString;
@ -168,6 +170,15 @@ class PLASMA_EXPORT QueryMatch
void setIcon(const QIcon &icon); void setIcon(const QIcon &icon);
void setEnabled(bool enable); void setEnabled(bool enable);
/**
* The current action.
*/
QAction* selectedAction() const;
/**
* Sets the selected action
*/
void setSelectedAction(QAction *action);
private: private:
QSharedDataPointer<QueryMatchPrivate> d; QSharedDataPointer<QueryMatchPrivate> d;
}; };

View File

@ -226,7 +226,9 @@ public:
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner"); KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner");
bool loadAll = config.readEntry("loadAll", false); 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) { foreach (const KService::Ptr &service, offers) {
//kDebug() << "Loading runner: " << service->name() << service->storageId(); //kDebug() << "Loading runner: " << service->name() << service->storageId();
@ -277,7 +279,7 @@ public:
} }
} }
//kDebug() << "All runners loaded, total:" << runners.count(); kDebug() << "All runners loaded, total:" << runners.count();
} }
void jobDone(ThreadWeaver::Job *job) void jobDone(ThreadWeaver::Job *job)
@ -387,6 +389,16 @@ void RunnerManager::run(const QueryMatch &match)
} }
} }
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) void RunnerManager::launchQuery(const QString &term)
{ {
launchQuery(term, QString()); launchQuery(term, QString());

View File

@ -28,6 +28,7 @@
#include <plasma/plasma_export.h> #include <plasma/plasma_export.h>
#include "abstractrunner.h" #include "abstractrunner.h"
class QAction;
class KConfigGroup; class KConfigGroup;
namespace Plasma namespace Plasma
@ -83,6 +84,11 @@ class PLASMA_EXPORT RunnerManager : public QObject
*/ */
void run(const QString &id); 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 * @return the current query term
*/ */