File indexing completed on 2024-04-14 03:54:32

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KRUNNER_RUNNERCONTEXT_H
0008 #define KRUNNER_RUNNERCONTEXT_H
0009 
0010 #include <QList>
0011 #include <QMetaType>
0012 #include <QSharedDataPointer>
0013 
0014 #include "krunner_export.h"
0015 
0016 class KConfigGroup;
0017 
0018 namespace KRunner
0019 {
0020 class RunnerManager;
0021 class QueryMatch;
0022 class AbstractRunner;
0023 class RunnerContextPrivate;
0024 
0025 /**
0026  * @class RunnerContext runnercontext.h <KRunner/RunnerContext>
0027  *
0028  * @short The RunnerContext class provides information related to a search,
0029  *        including the search term and collected matches.
0030  */
0031 class KRUNNER_EXPORT RunnerContext final
0032 {
0033 public:
0034     explicit RunnerContext(RunnerManager *manager = nullptr);
0035 
0036     /**
0037      * Copy constructor
0038      */
0039     RunnerContext(const RunnerContext &other);
0040 
0041     /**
0042      * Assignment operator
0043      */
0044     RunnerContext &operator=(const RunnerContext &other);
0045 
0046     ~RunnerContext();
0047 
0048     /**
0049      * Sets the query term for this object and attempts to determine
0050      * the type of the search.
0051      */
0052     void setQuery(const QString &term);
0053 
0054     /**
0055      * @return the current search query term.
0056      */
0057     QString query() const;
0058 
0059     /**
0060      * @returns true if this context is no longer valid and therefore
0061      * matching using it should abort.
0062      * While not required to be used within runners, it provides a nice way
0063      * to avoid unnecessary processing in runners that may run for an extended
0064      * period (as measured in 10s of ms) and therefore improve the user experience.
0065      */
0066     bool isValid() const;
0067 
0068     /**
0069      * Appends lists of matches to the list of matches.
0070      *
0071      * @param matches the matches to add
0072      * @return true if matches were added, false if matches were e.g. outdated
0073      */
0074     bool addMatches(const QList<QueryMatch> &matches);
0075 
0076     /**
0077      * Appends a match to the existing list of matches.
0078      *
0079      * If you are going to be adding multiple matches, it is
0080      * more performant to use @see addMatches instead.
0081      *
0082      * @param match the match to add
0083      * @return true if the match was added, false otherwise.
0084      */
0085     bool addMatch(const QueryMatch &match);
0086 
0087     /**
0088      * Retrieves all available matches for the current search term.
0089      *
0090      * @return a list of matches
0091      */
0092     QList<QueryMatch> matches() const;
0093 
0094     /**
0095      * Request that KRunner updates the query string and stasy open, even after running a match.
0096      * This method is const so it can be called in a const context.
0097      *
0098      * @param text Text that will be displayed in the search field
0099      * @param cursorPosition Position of the cursor, if this is different than the length of the text,
0100      * the characters between the position and text will be selected
0101      *
0102      * @since 5.90
0103      */
0104     void requestQueryStringUpdate(const QString &text, int cursorPosition) const;
0105 
0106     /**
0107      * @return true if the current query is a single runner query
0108      */
0109     bool singleRunnerQueryMode() const;
0110 
0111     /**
0112      * Set this to true in the AbstractRunner::run method to prevent the entry
0113      * from being saved to the history.
0114      * @since 5.90
0115      */
0116     void ignoreCurrentMatchForHistory() const;
0117 
0118 private:
0119     KRUNNER_NO_EXPORT void increaseLaunchCount(const QueryMatch &match);
0120     KRUNNER_NO_EXPORT QString requestedQueryString() const;
0121     KRUNNER_NO_EXPORT int requestedCursorPosition() const;
0122     KRUNNER_NO_EXPORT bool shouldIgnoreCurrentMatchForHistory() const;
0123     // Sets single runner query mode. Note that a call to reset() will turn off single runner query mode.
0124     KRUNNER_NO_EXPORT void setSingleRunnerQueryMode(bool enabled);
0125 
0126     friend class RunnerManager;
0127     friend class AbstractRunner;
0128     friend class DBusRunner;
0129     friend class RunnerManagerPrivate;
0130 
0131     KRUNNER_NO_EXPORT void restore(const KConfigGroup &config);
0132     KRUNNER_NO_EXPORT void save(KConfigGroup &config);
0133     KRUNNER_NO_EXPORT void reset();
0134     KRUNNER_NO_EXPORT void setJobStartTs(qint64 queryStartTs);
0135     KRUNNER_NO_EXPORT QString runnerJobId(AbstractRunner *runner) const;
0136 
0137     QExplicitlySharedDataPointer<RunnerContextPrivate> d;
0138 };
0139 }
0140 
0141 Q_DECLARE_METATYPE(KRunner::RunnerContext)
0142 #endif