File indexing completed on 2024-04-28 11:44:19
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 PLASMA_RUNNERCONTEXT_H 0008 #define PLASMA_RUNNERCONTEXT_H 0009 0010 #include <QList> 0011 #include <QObject> 0012 #include <QSharedDataPointer> 0013 0014 #include "krunner_export.h" 0015 0016 class KConfigGroup; 0017 0018 namespace Plasma 0019 { 0020 class QueryMatch; 0021 class AbstractRunner; 0022 class RunnerContextPrivate; 0023 0024 /** 0025 * @class RunnerContext runnercontext.h <KRunner/RunnerContext> 0026 * 0027 * @short The RunnerContext class provides information related to a search, 0028 * including the search term, metadata on the search term and collected 0029 * matches. 0030 */ 0031 class KRUNNER_EXPORT RunnerContext : public QObject 0032 { 0033 Q_OBJECT 0034 0035 public: 0036 enum Type { 0037 None = 0, 0038 UnknownType = 1, 0039 Directory = 2, 0040 File = 4, 0041 NetworkLocation = 8, 0042 Executable = 16, 0043 ShellCommand = 32, 0044 Help = 64, 0045 FileSystem = Directory | File | Executable | ShellCommand, 0046 }; 0047 0048 Q_DECLARE_FLAGS(Types, Type) 0049 0050 explicit RunnerContext(QObject *parent = nullptr); 0051 0052 /** 0053 * Copy constructor 0054 */ 0055 RunnerContext(RunnerContext &other, QObject *parent = nullptr); 0056 0057 /** 0058 * Assignment operator 0059 * @since 4.4 0060 */ 0061 RunnerContext &operator=(const RunnerContext &other); 0062 0063 ~RunnerContext() override; 0064 0065 /** 0066 * Resets the search term for this object. 0067 * This removes all current matches in the process and 0068 * turns off single runner query mode. 0069 */ 0070 void reset(); 0071 0072 /** 0073 * Sets the query term for this object and attempts to determine 0074 * the type of the search. 0075 */ 0076 void setQuery(const QString &term); 0077 0078 /** 0079 * @return the current search query term. 0080 */ 0081 QString query() const; 0082 0083 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76) 0084 /** 0085 * The type of item the search term might refer to. 0086 * @see Type 0087 * @deprecated feature is deprecated. Do the checks manually inside of the match logic 0088 */ 0089 KRUNNER_DEPRECATED_VERSION(5, 76, "feature is deprecated. Do the checks manually inside of the match logic") 0090 Type type() const; 0091 #endif 0092 0093 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76) 0094 /** 0095 * A list of categories of which results should be returned. 0096 * This list is typically populated from the AbstractRunner::categories 0097 * function. 0098 * @deprecated Since 5.76, feature is unused and not supported by most runners 0099 */ 0100 KRUNNER_DEPRECATED_VERSION(5, 76, "feature is unused and not supported by most runners") 0101 QStringList enabledCategories() const; 0102 #endif 0103 0104 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76) 0105 /** 0106 * Sets the list of enabled categories. Runners can use this list 0107 * to optimize themselves by only returning results from the enabled 0108 * categories 0109 * @deprecated Since 5.76, feature is unused and not supported by most runners 0110 */ 0111 KRUNNER_DEPRECATED_VERSION(5, 76, "feature is unused and not supported by most runners") 0112 void setEnabledCategories(const QStringList &categories); 0113 #endif 0114 0115 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 76) 0116 /** 0117 * The mimetype that the search term refers to, if discoverable. 0118 * 0119 * @return QString() if the mimetype can not be determined, otherwise 0120 * the mimetype of the object being referred to by the search 0121 * string. 0122 * @deprecated feature is unused, determine the mime type manually if needed 0123 */ 0124 KRUNNER_DEPRECATED_VERSION(5, 76, "feature is unused, determine the mime type manually if needed") 0125 QString mimeType() const; 0126 #endif 0127 0128 /** 0129 * @returns true if this context is no longer valid and therefore 0130 * matching using it should abort. Most useful as an optimization technique 0131 * inside of AbstractRunner subclasses in the match method, e.g.: 0132 * 0133 * while (.. a possibly large iteration) { 0134 * if (!context.isValid()) { 0135 * return; 0136 * } 0137 * 0138 * ... some processing ... 0139 * } 0140 * 0141 * While not required to be used within runners, it provides a nice way 0142 * to avoid unnecessary processing in runners that may run for an extended 0143 * period (as measured in 10s of ms) and therefore improve the user experience. 0144 * @since 4.2.3 0145 */ 0146 bool isValid() const; 0147 0148 /** 0149 * Appends lists of matches to the list of matches. 0150 * 0151 * @param matches the matches to add 0152 * @return true if matches were added, false if matches were e.g. outdated 0153 */ 0154 bool addMatches(const QList<QueryMatch> &matches); 0155 0156 /** 0157 * Appends a match to the existing list of matches. 0158 * 0159 * If you are going to be adding multiple matches, use @see addMatches instead. 0160 * 0161 * @param match the match to add 0162 * @return true if the match was added, false otherwise. 0163 */ 0164 bool addMatch(const QueryMatch &match); 0165 0166 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 81) 0167 /** 0168 * Removes a match from the existing list of matches. 0169 * 0170 * If you are going to be removing multiple matches, use removeMatches instead. 0171 * 0172 * @param matchId the id of match to remove 0173 * 0174 * @return true if the match was removed, false otherwise. 0175 * @since 4.4 0176 * @deprecated Since 5.81, feature is unused, aggregate and filter the matches before adding them to the runnercontext instead 0177 */ 0178 KRUNNER_DEPRECATED_VERSION(5, 81, "feature is unused, aggregate and filter the matches before adding them to the runnercontext instead") 0179 bool removeMatch(const QString matchId); 0180 #endif 0181 0182 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 81) 0183 /** 0184 * Removes lists of matches from the existing list of matches. 0185 * 0186 * This method is thread safe and causes the matchesChanged() signal to be emitted. 0187 * 0188 * @param matchIdList the list of matches id to remove 0189 * 0190 * @return true if at least one match was removed, false otherwise. 0191 * @since 4.4 0192 * @deprecated Since 5.81, feature is unused, aggregate and filter the matches before adding them to the runnercontext instead 0193 */ 0194 KRUNNER_DEPRECATED_VERSION(5, 81, "feature is unused, aggregate and filter the matches before adding them to the runnercontext instead") 0195 bool removeMatches(const QStringList matchIdList); 0196 #endif 0197 0198 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 81) 0199 /** 0200 * Removes lists of matches from a given AbstractRunner 0201 * 0202 * This method is thread safe and causes the matchesChanged() signal to be emitted. 0203 * 0204 * @param runner the AbstractRunner from which to remove matches 0205 * 0206 * @return true if at least one match was removed, false otherwise. 0207 * @since 4.10 0208 * @deprecated Since 5.81, feature is unused, aggregate and filter the matches before adding them to the runnercontext instead 0209 */ 0210 KRUNNER_DEPRECATED_VERSION(5, 81, "feature is unused, aggregate and filter the matches before adding them to the runnercontext instead") 0211 bool removeMatches(AbstractRunner *runner); 0212 #endif 0213 0214 /** 0215 * Retrieves all available matches for the current search term. 0216 * 0217 * @return a list of matches 0218 */ 0219 QList<QueryMatch> matches() const; 0220 0221 #if KRUNNER_ENABLE_DEPRECATED_SINCE(5, 79) 0222 /** 0223 * Retrieves a match by id. 0224 * 0225 * @param id the id of the match to return 0226 * @return the match associated with this id, or an invalid QueryMatch object 0227 * if the id does not exist 0228 * @deprecated Since 5.79, deprecated due to lack of usage. Instead filter the matches manually based on the id 0229 */ 0230 KRUNNER_DEPRECATED_VERSION(5, 79, "Deprecated due to lack of usage, instead filter the matches manually based on the id") 0231 QueryMatch match(const QString &id) const; 0232 #endif 0233 0234 /** 0235 * Request that KRunner updates the query string and stasy open, even after running a match. 0236 * This method is const so it can be called in a const context. 0237 * 0238 * @param text Text that will be displayed in the search field 0239 * @param cursorPosition Position of the cursor, if this is different than the length of the text, 0240 * the characters between the position and text will be selected 0241 * 0242 * @since 5.90 0243 */ 0244 void requestQueryStringUpdate(const QString &text, int cursorPosition) const; 0245 0246 /** 0247 * Sets single runner query mode. Note that a call to reset() will 0248 * turn off single runner query mode. 0249 * 0250 * @note This method is considered internal. To set the single runner mode you should pass in a runnerId to @ref RunnerManager::launchQuery 0251 * @see reset() 0252 * @internal 0253 * @since 4.4 0254 */ 0255 // TODO KF6 Make private 0256 void setSingleRunnerQueryMode(bool enabled); 0257 0258 /** 0259 * @return true if the current query is a single runner query 0260 * @since 4.4 0261 */ 0262 bool singleRunnerQueryMode() const; 0263 0264 /** 0265 * Set this to true in the AbstractRunner::run method to prevent the entry 0266 * from being saved to the history. 0267 * @since 5.90 0268 */ 0269 void ignoreCurrentMatchForHistory() const; 0270 0271 /** 0272 * Sets the launch counts for the associated match ids 0273 * 0274 * If a runner adds a match to this context, the context will check if the 0275 * match id has been launched before and increase the matches relevance 0276 * correspondingly. In this manner, any front end can implement adaptive search 0277 * by sorting items according to relevance. 0278 * 0279 * @param config the config group where launch data was stored 0280 */ 0281 void restore(const KConfigGroup &config); 0282 0283 /** 0284 * @param config the config group where launch data should be stored 0285 */ 0286 void save(KConfigGroup &config); 0287 0288 /** 0289 * Run a match using the information from this context 0290 * 0291 * The context will also keep track of the number of times the match was 0292 * launched to sort future matches according to user habits 0293 * 0294 * @param match the match to run 0295 */ 0296 void run(const QueryMatch &match); 0297 0298 Q_SIGNALS: 0299 void matchesChanged(); 0300 0301 private: 0302 KRUNNER_NO_EXPORT QString requestedQueryString() const; 0303 KRUNNER_NO_EXPORT int requestedCursorPosition() const; 0304 KRUNNER_NO_EXPORT bool shouldIgnoreCurrentMatchForHistory() const; 0305 friend class RunnerManager; 0306 QExplicitlySharedDataPointer<RunnerContextPrivate> d; 0307 }; 0308 0309 Q_DECLARE_OPERATORS_FOR_FLAGS(RunnerContext::Types) 0310 0311 } 0312 0313 #endif