From 7eb365fac26969b7f123a99a8c8982fc5892659c Mon Sep 17 00:00:00 2001 From: Jacopo De Simoi Date: Wed, 25 Nov 2009 22:31:56 +0000 Subject: [PATCH] Load single runners on the fly svn path=/trunk/KDE/kdelibs/; revision=1054351 --- runnermanager.cpp | 96 +++++++++++++++++++++++++++++++++++++++++------ runnermanager.h | 21 ++++++++++- 2 files changed, 104 insertions(+), 13 deletions(-) diff --git a/runnermanager.cpp b/runnermanager.cpp index 17ca6a837..2f39c9116 100644 --- a/runnermanager.cpp +++ b/runnermanager.cpp @@ -58,6 +58,7 @@ public: RunnerManagerPrivate(RunnerManager *parent) : q(parent), deferredRun(0), + currentSingleRunner(0), prepped(false), teardownRequested(false) { @@ -112,6 +113,52 @@ public: return conf.isValid() ? conf : KConfigGroup(KGlobal::config(), "PlasmaRunnerManager"); } + AbstractRunner *loadSingleRunner(QString id) + { + if (q->runner(id)) { + return q->runner(id); + } + + if (currentSingleRunner && currentSingleRunner->id() == id) { + return currentSingleRunner; + } + delete currentSingleRunner; + currentSingleRunner = 0; + + KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner", QString("[X-KDE-PluginInfo-Name] == '%1'").arg(id)); + + if (offers.isEmpty()) { + return 0; + } + + const KService::Ptr &service = offers[0]; + QString api = service->property("X-Plasma-API").toString(); + QString error; + AbstractRunner *runner = 0; + + if (api.isEmpty()) { + QVariantList args; + args << service->storageId(); + if (Plasma::isPluginVersionCompatible(KPluginLoader(*service).pluginVersion())) { + currentSingleRunner = service->createInstance(q, args, &error); + } + } else { + currentSingleRunner = new AbstractRunner(q, service->storageId()); + } + + if (currentSingleRunner) { + QMetaObject::invokeMethod(currentSingleRunner, "init"); + emit currentSingleRunner->prepare(); + } + return currentSingleRunner; + } + + void clearSingleRunner() + { + delete currentSingleRunner; + currentSingleRunner = 0; + } + void loadRunners() { KConfigGroup config = configGroup(); @@ -127,6 +174,8 @@ public: pluginConf = KConfigGroup(KGlobal::config(), "Plugins"); } + singleRunnerEnabledNames.clear(); + foreach (const KService::Ptr &service, offers) { //kDebug() << "Loading runner: " << service->name() << service->storageId(); QString tryExec = service->property("TryExec", QVariant::String).toString(); @@ -146,6 +195,10 @@ public: const bool singleQueryModeEnabled = service->property("SingleRunnerQueryMode", QVariant::Bool).toBool(); + if (singleQueryModeEnabled) { + singleRunnerEnabledNames.insert(runnerName, description.name()); + } + //kDebug() << loadAll << description.isPluginEnabled() << noWhiteList << whiteList.contains(runnerName); if (selected) { if (!loaded) { @@ -181,12 +234,6 @@ public: continue; } } - - if (singleQueryModeEnabled) { - singleQueryModeEnabledRunners.insert(runnerName, runners.value(runnerName)); - } else { - singleQueryModeEnabledRunners.remove(runnerName); - } } else if (loaded) { //Remove runner AbstractRunner *runner = runners.take(runnerName); @@ -246,6 +293,9 @@ public: foreach (AbstractRunner *runner, runners) { emit runner->teardown(); } + if (currentSingleRunner) { + emit currentSingleRunner->teardown(); + } prepped = false; teardownRequested = false; @@ -276,7 +326,8 @@ public: QTimer matchChangeTimer; QTimer delayTimer; // Timer to control when to run slow runners QHash runners; - QHash singleQueryModeEnabledRunners; + QHash singleRunnerEnabledNames; + AbstractRunner* currentSingleRunner; QSet searchJobs; QSet oldSearchJobs; KConfigGroup conf; @@ -347,14 +398,32 @@ AbstractRunner* RunnerManager::runner(const QString &name) const return d->runners.value(name, 0); } +AbstractRunner * RunnerManager::retrieveSingleRunner(const QString &name) +{ + if (!singleRunnerEnabledIds().contains(name)) { + return 0; + } + + return d->loadSingleRunner(name); +} + QList RunnerManager::runners() const { return d->runners.values(); } -QList RunnerManager::singleQueryModeEnabledRunners() const +QStringList RunnerManager::singleRunnerEnabledIds() const { - return d->singleQueryModeEnabledRunners.values(); + return d->singleRunnerEnabledNames.keys(); +} + +QString RunnerManager::runnerName(const QString &id) const +{ + if (runner(id)) { + return runner(id)->name(); + } else { + return d->singleRunnerEnabledNames.value(id, QString()); + } } RunnerContext* RunnerManager::searchContext() const @@ -419,6 +488,9 @@ void RunnerManager::setupMatchSession() foreach (AbstractRunner *runner, d->runners) { emit runner->prepare(); } + if (d->currentSingleRunner) { + emit d->currentSingleRunner->prepare(); + } } void RunnerManager::matchSessionComplete() @@ -442,8 +514,10 @@ void RunnerManager::launchQuery(const QString &untrimmedTerm, const QString &run QString term = untrimmedTerm.trimmed(); AbstractRunner *singleRunner = 0; - if (!runnerName.isEmpty()) { - singleRunner = runner(runnerName); + if (!runnerName.isEmpty() && singleRunnerEnabledIds().contains(runnerName)) { + singleRunner = d->loadSingleRunner(runnerName); + } else { + d->clearSingleRunner(); } if (term.isEmpty() && singleRunner && singleRunner->defaultSyntax()) { diff --git a/runnermanager.h b/runnermanager.h index 4647e6ca8..1d67896b8 100644 --- a/runnermanager.h +++ b/runnermanager.h @@ -60,16 +60,33 @@ class PLASMA_EXPORT RunnerManager : public QObject */ AbstractRunner *runner(const QString &name) const; + /** + * Returns the single runner enabled runner given by + * the argument or NULL if not valid; the runner will be loaded + * if necessary + * @arg name the name of the runner + * @return Pointer to the runner + */ + AbstractRunner *retrieveSingleRunner(const QString &name); + + /** + * Returns the translated name of a runner + * @arg id the id of the runner + * + * @since 4.4 + */ + QString runnerName(const QString &id) const; + /** * @return the list of all currently loaded runners */ QList runners() const; /** - * @return the list of all single query mode enabled runners + * @return the names of all single query mode enabled runners * @since 4.4 */ - QList singleQueryModeEnabledRunners() const; + QStringList singleRunnerEnabledIds() const; /** * Retrieves the current context