File indexing completed on 2024-04-28 11:44:20

0001 /*
0002     SPDX-FileCopyrightText: 2006 Aaron Seigo <aseigo@kde.org>
0003     SPDX-FileCopyrightText: 2007 Ryan P. Bitanga <ryan.bitanga@gmail.com>
0004     SPDX-FileCopyrightText: 2008 Jordi Polo <mumismo@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef PLASMA_RUNNERMANAGER_H
0010 #define PLASMA_RUNNERMANAGER_H
0011 
0012 #include <QList>
0013 #include <QObject>
0014 
0015 #include "krunner_export.h"
0016 
0017 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 91)
0018 #include <KPluginInfo>
0019 #endif
0020 #include <KPluginMetaData>
0021 
0022 #include <memory>
0023 
0024 #include "abstractrunner.h"
0025 
0026 class QAction;
0027 class KConfigGroup;
0028 namespace
0029 {
0030 class AbstractRunnerTest;
0031 }
0032 
0033 namespace Plasma
0034 {
0035 class QueryMatch;
0036 class AbstractRunner;
0037 class RunnerContext;
0038 class RunnerManagerPrivate;
0039 
0040 /**
0041  * @class RunnerManager runnermanager.h <KRunner/RunnerManager>
0042  *
0043  * @short The RunnerManager class decides what installed runners are runnable,
0044  *        and their ratings. It is the main proxy to the runners.
0045  */
0046 class KRUNNER_EXPORT RunnerManager : public QObject
0047 {
0048     Q_OBJECT
0049     Q_PROPERTY(bool retainPriorSearch READ retainPriorSearch)
0050     Q_PROPERTY(QString priorSearch READ priorSearch WRITE setPriorSearch)
0051     Q_PROPERTY(QStringList history READ history)
0052     Q_PROPERTY(bool historyEnabled READ historyEnabled NOTIFY historyEnabledChanged)
0053 
0054 public:
0055     explicit RunnerManager(QObject *parent = nullptr);
0056     explicit RunnerManager(const QString &configFile, QObject *parent = nullptr);
0057 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76)
0058     /**
0059      * @deprecated Since 5.76, use "RunnerManager(const QString &configFile, QObject *parent)" instead.
0060      */
0061     KRUNNER_DEPRECATED_VERSION(5, 76, "use RunnerManager(const QString &configFile, QObject *parent) instead")
0062     explicit RunnerManager(KConfigGroup &config, QObject *parent = nullptr);
0063 #endif
0064     ~RunnerManager() override;
0065 
0066     /**
0067      * Finds and returns a loaded runner or NULL
0068      * @param pluginName the name of the runner plugin
0069      * @return Pointer to the runner
0070      */
0071     AbstractRunner *runner(const QString &pluginName) const;
0072 
0073 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 82)
0074     /**
0075      * @return the currently active "single mode" runner, or null if none
0076      * @since 4.4
0077      * @deprecated Since 5.81, the dedicated singleRunnerMode methods are deprecated, use runner(const QString &pluginName) with the singleRunnerId instead"
0078      */
0079     KRUNNER_DEPRECATED_VERSION(5,
0080                                82,
0081                                "The dedicated singleRunnerMode methods are deprecated, use runner(const QString &pluginName) with the singleRunnerId instead")
0082     AbstractRunner *singleModeRunner() const;
0083 
0084     /**
0085      * Puts the manager into "single runner" mode using the given
0086      * runner; if the runner does not exist or can not be loaded then
0087      * the single runner mode will not be started and singleModeRunner()
0088      * will return NULL
0089      * @param id the id of the runner to use
0090      * @since 4.4
0091      * @deprecated Since 5.82, the dedicated singleRunnerMode methods are deprecated, pass in the singleModeRunnerId into the launchQuery overload instead
0092      */
0093     KRUNNER_DEPRECATED_VERSION(5,
0094                                82,
0095                                "The dedicated singleRunnerMode methods are deprecated, pass in the singleModeRunnerId into the launchQuery overload instead")
0096     void setSingleModeRunnerId(const QString &id);
0097 
0098     /**
0099      * @return the id of the runner to use in single mode
0100      * @since 4.4
0101      * @deprecated Since 5.82, the dedicated singleRunnerMode methods are deprecated, use runner(const QString &pluginName) with the singleRunnerId instead
0102      */
0103     KRUNNER_DEPRECATED_VERSION(5, 82, "The dedicated singleRunnerMode methods are deprecated, save the variable before using it in launchQuery() instead")
0104     QString singleModeRunnerId() const;
0105 
0106     /**
0107      * @return true if the manager is set to run in single runner mode
0108      * @since 4.4
0109      * @deprecated Since 5.82, the dedicated singleRunnerMode methods are deprecated, call the RunnerContext::singleRunnerQueryMode on the searchContext instead
0110      */
0111     KRUNNER_DEPRECATED_VERSION(
0112         5,
0113         82,
0114         "The dedicated singleRunnerMode methods are deprecated, call the RunnerContext::singleRunnerQueryMode on the searchContext instead")
0115     bool singleMode() const;
0116 
0117     /**
0118      * Sets whether or not the manager is in single mode.
0119      *
0120      * @param singleMode true if the manager should be in single mode, false otherwise
0121      * @since 4.4
0122      * @deprecated Since 5.82, the dedicated singleRunnerMode methods are deprecated, the single mode is set to true when launchQuery is called with a non
0123      * empty and existing runnerId
0124      */
0125     KRUNNER_DEPRECATED_VERSION(5,
0126                                82,
0127                                "The dedicated singleRunnerMode methods are deprecated, the single mode is set to true when launchQuery is called with a non "
0128                                "empty and existing runnerId")
0129     void setSingleMode(bool singleMode);
0130 
0131     /**
0132      * @return the names of all runners that advertise single query mode
0133      * @since 4.4
0134      * @deprecated Since 5.81, filter the runners manually using the X-Plasma-AdvertiseSingleRunnerQueryMode of the metadata
0135      */
0136     KRUNNER_DEPRECATED_VERSION(5, 81, "filter the runners manually using the X-Plasma-AdvertiseSingleRunnerQueryMode of the metadata")
0137     QStringList singleModeAdvertisedRunnerIds() const;
0138 
0139     /**
0140      * Returns the translated name of a runner
0141      * @param id the id of the runner
0142      *
0143      * @since 4.4
0144      * @deprecated Since 5.81, call runner(const QString &id) and fetch the name from the returned object instead
0145      */
0146     KRUNNER_DEPRECATED_VERSION(5, 81, "call runner(const QString &id) and fetch the name from the returned object instead")
0147     QString runnerName(const QString &id) const;
0148 #endif
0149 
0150     /**
0151      * @return the list of all currently loaded runners
0152      */
0153     QList<AbstractRunner *> runners() const;
0154 
0155     /**
0156      * Retrieves the current context
0157      * @return pointer to the current context
0158      */
0159     RunnerContext *searchContext() const;
0160 
0161     /**
0162      * Retrieves all available matches found so far for the previously launched query
0163      * @return List of matches
0164      */
0165     QList<QueryMatch> matches() const;
0166 
0167     /**
0168      * Runs a given match
0169      * @param match the match to be executed
0170      */
0171     void run(const QueryMatch &match);
0172 
0173 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 79)
0174     /**
0175      * Runs a given match
0176      * @param id the id of the match to run
0177      * @deprecated Since 5.79, use run(const QueryMatch &match) instead
0178      */
0179     KRUNNER_DEPRECATED_VERSION(5, 79, "Use run(const QueryMatch &match) instead")
0180     void run(const QString &id);
0181 #endif
0182 
0183     /**
0184      * Runs a given match. This also respects the extra handling for the InformationalMatch.
0185      * This also handles the history automatically
0186      * @param match the match to be executed
0187      * @return if the RunnerWindow should close
0188      * @since 5.78
0189      */
0190     bool runMatch(const QueryMatch &match);
0191 
0192     /**
0193      * Retrieves the list of actions, if any, for a match
0194      */
0195     QList<QAction *> actionsForMatch(const QueryMatch &match);
0196 
0197     /**
0198      * @return the current query term
0199      */
0200     QString query() const;
0201 
0202     /**
0203      * @return History of this runner for the current activity. If the RunnerManager is not history
0204      * aware the global entries will be returned.
0205      * @since 5.78
0206      */
0207     QStringList history() const;
0208 
0209     /**
0210      * Delete the given index from the history.
0211      * @param historyEntry
0212      * @since 5.78
0213      */
0214     Q_INVOKABLE void removeFromHistory(int index);
0215 
0216     /**
0217      * Get the suggested history entry for the typed query. If no entry is found an empty string is returned.
0218      * @param typedQuery
0219      * @return completion for typedQuery
0220      * @since 5.78
0221      */
0222     Q_INVOKABLE QString getHistorySuggestion(const QString &typedQuery) const;
0223 
0224     /**
0225      * Get the suggested prior search for this runner.
0226      * Just like the history this value can be activity specific, depending on the build/config.
0227      * @return priorSearch
0228      * @since 5.78
0229      */
0230     QString priorSearch() const;
0231 
0232     /**
0233      * Set the prior search for this runner. Setting an empty string will clear this value.
0234      * @since 5.78
0235      */
0236     void setPriorSearch(const QString &search);
0237 
0238     /**
0239      * If the prior search should be restored when KRunner is reopened
0240      * @since 5.78
0241      */
0242     bool retainPriorSearch();
0243 
0244     /**
0245      * If history completion is enabled, the default value is true.
0246      * @since 5.78
0247      */
0248     bool historyEnabled();
0249 
0250     /**
0251      * Causes a reload of the current configuration
0252      */
0253     void reloadConfiguration();
0254 
0255     /**
0256      * Sets a whitelist for the plugins that can be loaded by this manager.
0257      *
0258      * @param plugins the plugin names of allowed runners
0259      * @since 4.4
0260      */
0261     void setAllowedRunners(const QStringList &runners);
0262 
0263 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76)
0264     /**
0265      * Sets the list of categories which matches should be
0266      * returned for. It also internally tries not to execute the
0267      * runners which do not fall in this category.
0268      * @deprecated Since 5.76, feature is unused and not supported by most runners
0269      */
0270     KRUNNER_DEPRECATED_VERSION(5, 76, "feature is unused and not supported by most runners")
0271     void setEnabledCategories(const QStringList &categories);
0272 #endif
0273 
0274 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 72)
0275 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 0)
0276     /**
0277      * Attempts to add the AbstractRunner plugin represented
0278      * by the KService passed in. Usually one can simply let
0279      * the configuration of plugins handle loading Runner plugins,
0280      * but in cases where specific runners should be loaded this
0281      * allows for that to take place
0282      *
0283      * @param service the service to use to load the plugin
0284      * @since 4.5
0285      * @deprecated Since 5.72, use loadRunner(const KPluginMetaData &)
0286      */
0287     KRUNNER_DEPRECATED_VERSION(5, 72, "use loadRunner(const KPluginMetaData &)")
0288     void loadRunner(const KService::Ptr service);
0289 #endif
0290 #endif
0291 
0292     /**
0293      * Attempts to add the AbstractRunner plugin represented
0294      * by the plugin info passed in. Usually one can simply let
0295      * the configuration of plugins handle loading Runner plugins,
0296      * but in cases where specific runners should be loaded this
0297      * allows for that to take place
0298      *
0299      * @param pluginMetaData the metaData to use to load the plugin
0300      * @since 5.72
0301      */
0302     void loadRunner(const KPluginMetaData &pluginMetaData);
0303 
0304 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 77)
0305     /**
0306      * Attempts to add the AbstractRunner from a Plasma::Package on disk.
0307      * Usually one can simply let the configuration of plugins
0308      * handle loading Runner plugins, but in cases where specific
0309      * runners should be loaded this allows for that to take place
0310      *
0311      * @param path the path to a Runner package to load
0312      * @since 4.5
0313      * @deprecated Since 5.0, the KPackage support was removed in Plasma 5.0
0314      */
0315     KRUNNER_DEPRECATED_VERSION_BELATED(5, 77, 5, 0, "the KPackage support was removed in Plasma 5.0")
0316     void loadRunner(const QString &path);
0317 #endif
0318 
0319 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 88)
0320     /**
0321      * @return the list of allowed plugins
0322      * @since 4.4
0323      * @deprecated Since 5.88, reading allowed runners from the config is deprecated, use @p runners() and get their @p AbstractRunner::id instead
0324      */
0325     KRUNNER_DEPRECATED_VERSION(5, 88, "reading allowed runners from the config is deprecated, use runners() and get their ids instead")
0326     QStringList allowedRunners() const;
0327 #endif
0328 
0329 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76)
0330     /**
0331      * @return the list of enabled categories
0332      * @deprecated Since 5.76, feature is unused and not supported by most runners
0333      */
0334     KRUNNER_DEPRECATED_VERSION(5, 76, "feature is unused and not supported by most runners")
0335     QStringList enabledCategories() const;
0336 #endif
0337 
0338     /**
0339      * @return mime data of the specified match
0340      * @since 4.5
0341      */
0342     QMimeData *mimeDataForMatch(const QueryMatch &match) const;
0343 
0344 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 79)
0345     /**
0346      * @return mime data of the specified match
0347      * @since 4.5
0348      * @deprecated Since 5.79, use mimeDataForMatch(const QueryMatch &match) instead
0349      */
0350     KRUNNER_DEPRECATED_VERSION(5, 79, "Use mimeDataForMatch(const QueryMatch &match) instead")
0351     QMimeData *mimeDataForMatch(const QString &matchId) const;
0352 #endif
0353 
0354 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 85)
0355     /**
0356      * Returns a list of all known Runner implementations
0357      *
0358      * @param parentApp the application to filter runners on. Uses the
0359      *                  X-KDE-ParentApp entry (if any) in the plugin metadata.
0360      *                  The default value of QString() will result in a
0361      *                  list containing only runners not specifically
0362      *                  registered to an application.
0363      * @return list of metadata of known runners
0364      * @since 5.72
0365      * @deprecated Since 5.85, the concept of parent apps for runners is deprecated, use no-arg overload instead
0366      **/
0367     KRUNNER_DEPRECATED_VERSION(5, 85, "The concept of parent apps for runners is deprecated, use no-arg overload instead")
0368     static QVector<KPluginMetaData> runnerMetaDataList(const QString &parentApp);
0369 #endif
0370 
0371     /**
0372      * @return metadata list of all known Runner implementations
0373      * @since 5.72
0374      */
0375     static QVector<KPluginMetaData> runnerMetaDataList();
0376 
0377 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 72)
0378     /**
0379      * Returns a list of all known Runner implementations
0380      *
0381      * @param parentApp the application to filter applets on. Uses the
0382      *                  X-KDE-ParentApp entry (if any) in the plugin info.
0383      *                  The default value of QString() will result in a
0384      *                  list containing only applets not specifically
0385      *                  registered to an application.
0386      * @return list of AbstractRunners
0387      * @since 4.6
0388      * @deprecated since 5.72, use runnerMetaDataList() instead
0389      **/
0390     KRUNNER_DEPRECATED_VERSION(5, 72, "Use runnerMetaDataList() instead")
0391     static KPluginInfo::List listRunnerInfo(const QString &parentApp = QString());
0392 #endif
0393 
0394     /**
0395      * If you call this method the manager will create a KConfigWatcher
0396      * which reload its runners or the runner configuration when the settings in the KCM are edited.
0397      * @since 5.73
0398      * @see reloadConfiguration
0399      */
0400     void enableKNotifyPluginWatcher(); // TODO KF6 make enabling the watcher default behavior and remove the method
0401 
0402 public Q_SLOTS:
0403     /**
0404      * Call this method when the runners should be prepared for a query session.
0405      * Call matchSessionComplete when the query session is finished for the time
0406      * being.
0407      * @since 4.4
0408      * @see matchSessionComplete
0409      */
0410     void setupMatchSession();
0411 
0412     /**
0413      * Call this method when the query session is finished for the time
0414      * being.
0415      * @since 4.4
0416      * @see prepareForMatchSession
0417      */
0418     void matchSessionComplete();
0419 
0420     /**
0421      * Launch a query, this will create threads and return immediately.
0422      * When the information will be available can be known using the
0423      * matchesChanged signal.
0424      *
0425      * @param term the term we want to find matches for
0426      * @param runnerId optional, if only one specific runner is to be used;
0427      *               providing an id will put the manager into single runner mode
0428      */
0429     void launchQuery(const QString &term, const QString &runnerId);
0430 
0431     /**
0432      * Convenience version of above
0433      */
0434     void launchQuery(const QString &term); // TODO KF6 Merge with other overload and use default argument
0435 
0436     /**
0437      * Reset the current data and stops the query
0438      */
0439     void reset();
0440 
0441 Q_SIGNALS:
0442     /**
0443      * Emitted each time a new match is added to the list
0444      */
0445     void matchesChanged(const QList<Plasma::QueryMatch> &matches);
0446 
0447     /**
0448      * Emitted when the launchQuery finish
0449      * @since 4.5
0450      */
0451     void queryFinished();
0452 
0453     /**
0454      * Put the given search term in the KRunner search field
0455      * @param term The term that should be displayed
0456      * @param cursorPosition Where the cursor should be positioned
0457      * @since 5.78
0458      */
0459     void setSearchTerm(const QString &term, int cursorPosition);
0460 
0461     /**
0462      * @see @p historyEnabled
0463      * @since 5.78
0464      */
0465     void historyEnabledChanged();
0466 
0467 private:
0468     Q_PRIVATE_SLOT(d, void jobDone(ThreadWeaver::JobPointer))
0469     KPluginMetaData convertDBusRunnerToJson(const QString &filename) const;
0470     // exported for dbusrunnertest
0471 
0472     std::unique_ptr<RunnerManagerPrivate> const d;
0473 
0474     friend class RunnerManagerPrivate;
0475     friend AbstractRunnerTest;
0476 };
0477 
0478 }
0479 #if !KRUNNER_ENABLE_DEPRECATED_SINCE(5, 91)
0480 namespace KRunner
0481 {
0482 using RunnerManager = Plasma::RunnerManager;
0483 }
0484 #endif
0485 
0486 #endif