KService

kservice.h
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 1998, 1999 Torben Weis <[email protected]>
4  SPDX-FileCopyrightText: 1999-2006 David Faure <[email protected]>
5  SPDX-FileCopyrightText: 2022 Harald Sitter <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #ifndef KSERVICE_H
11 #define KSERVICE_H
12 
13 #include "kserviceaction.h"
14 #include <QCoreApplication>
15 #include <QStringList>
16 #include <QVariant>
17 #include <ksycocaentry.h>
18 
19 class QDataStream;
20 class KDesktopFile;
21 class QWidget;
22 
23 class KServicePrivate;
24 
25 /**
26  * @class KService kservice.h <KService>
27  *
28  * Represents an installed application.
29  *
30  * To obtain a KService instance for a specific application you typically use serviceByDesktopName(), e.g.:
31  *
32  * @code
33  * KService::Ptr service = KService::serviceByDesktopName("org.kde.kate");
34  * @endcode
35  *
36  * Other typical usage would be in combination with KApplicationTrader to obtain e.g. the default application for a given file type.
37  *
38  * @see <a href="https://specifications.freedesktop.org/desktop-entry-spec/latest/">Desktop Entry Specification</a>
39  */
40 class KSERVICE_EXPORT KService : public KSycocaEntry
41 {
42 public:
43  /**
44  * A shared data pointer for KService.
45  */
47  /**
48  * A list of shared data pointers for KService.
49  */
50  typedef QList<Ptr> List;
51 
52  /**
53  * Construct a temporary service with a given name, exec-line and icon.
54  * @param name the name of the service
55  * @param exec the executable
56  * @param icon the name of the icon
57  */
58  KService(const QString &name, const QString &exec, const QString &icon);
59 
60  /**
61  * Construct a service and take all information from a .desktop file.
62  *
63  * @param fullpath Full path to the .desktop file.
64  */
65  explicit KService(const QString &fullpath);
66 
67  /**
68  * Construct a service and take all information from a desktop file.
69  * @param config the desktop file to read
70  * @param optional relative path to store for findByName
71  */
72  explicit KService(const KDesktopFile *config, const QString &entryPath = QString());
73 
74  KService(const KService &other);
75 
76  ~KService() override;
77 
78  /**
79  * Whether this service is an application
80  * @return true if this service is an application, i.e. it has Type=Application in its
81  * .desktop file and exec() will not be empty.
82  */
83  bool isApplication() const;
84 
85  /**
86  * Returns the executable.
87  * @return the command that the service executes,
88  * or QString() if not set
89  */
90  QString exec() const;
91  /**
92  * Returns the name of the service's library.
93  * @return the name of the library that contains the service's
94  * implementation, or QString() if not set
95  */
96  QString library() const;
97 
98  /**
99  * Returns the name of the icon.
100  * @return the icon associated with the service,
101  * or QString() if not set
102  */
103  QString icon() const;
104  /**
105  * Checks whether the application should be run in a terminal.
106  *
107  * This corresponds to `Terminal=true` in the .desktop file.
108  *
109  * @return @c true if the application should be run in a terminal.
110  */
111  bool terminal() const;
112 
113  /**
114  * Returns any options associated with the terminal the application
115  * runs in, if it requires a terminal.
116  *
117  * The application must be a TTY-oriented program.
118  * @return the terminal options,
119  * or QString() if not set
120  */
121  QString terminalOptions() const;
122 
123  /**
124  * Returns @c true if the application indicates that it's preferred to run
125  * on a discrete graphics card, otherwise return @c false.
126  *
127  * In releases older than 5.86, this method checked for the @c X-KDE-RunOnDiscreteGpu
128  * key in the .desktop file represented by this service; starting from 5.86 this method
129  * now also checks for @c PrefersNonDefaultGPU key (added to the Freedesktop.org desktop
130  * entry spec in version 1.4 of the spec).
131  *
132  * @since 5.30
133  */
134  bool runOnDiscreteGpu() const;
135 
136  /**
137  * @brief Checks whether the application needs to run under a different UID.
138  * @return @c true if the application needs to run under a different UID.
139  * @see username()
140  */
141  bool substituteUid() const;
142  /**
143  * Returns the user name if the application runs with a
144  * different user id.
145  * @return the username under which the service has to be run,
146  * or QString() if not set
147  * @see substituteUid()
148  */
149  QString username() const;
150 
151  /**
152  * Returns the filename of the desktop entry without any
153  * extension, e.g. "org.kde.kate"
154  * @return the name of the desktop entry without path or extension,
155  * or QString() if not set
156  */
157  QString desktopEntryName() const;
158 
159  /**
160  * Returns the menu ID of the application desktop entry.
161  * The menu ID is used to add or remove the entry to a menu.
162  * @return the menu ID
163  */
164  QString menuId() const;
165 
166  /**
167  * Returns a normalized ID suitable for storing in configuration files.
168  * It will be based on the menu-id when available and otherwise falls
169  * back to entryPath()
170  * @return the storage ID
171  */
172  QString storageId() const;
173 
174  /**
175  * @return the working directory to run the program in,
176  * or QString() if not set
177  * @since 5.63
178  */
179  QString workingDirectory() const;
180 
181  /**
182  * Returns the descriptive comment for the application, if there is one.
183  * @return the descriptive comment for the application, or QString()
184  * if not set
185  */
186  QString comment() const;
187 
188  /**
189  * Returns the generic name for the application, if there is one
190  * (e.g. "Mail Client").
191  * @return the generic name,
192  * or QString() if not set
193  */
194  QString genericName() const;
195 
196  /**
197  * Returns the untranslated (US English) generic name
198  * for the application, if there is one
199  * (e.g. "Mail Client").
200  * @return the generic name,
201  * or QString() if not set
202  */
203  QString untranslatedGenericName() const;
204 
205  /**
206  * Returns a list of descriptive keywords for the application, if there are any.
207  * @return the list of keywords
208  */
209  QStringList keywords() const;
210 
211  /**
212  * Returns a list of VFolder categories.
213  * @return the list of VFolder categories
214  */
215  QStringList categories() const;
216 
217  /**
218  * Returns the list of MIME types that this application supports.
219  * Note that this doesn't include inherited MIME types,
220  * only the MIME types listed in the .desktop file.
221  * @since 4.8.3
222  */
223  QStringList mimeTypes() const;
224 
225  /**
226  * Returns the list of protocols this application supports.
227  *
228  * This is taken from the x-scheme-handler MIME types
229  * listed in the .destkop file as well as the 'X-KDE-Protocols'
230  * entry
231  *
232  * For example a web browser could return {"http", "https"}.
233  * @since 6.0
234  */
235  QStringList supportedProtocols() const;
236 
237  /**
238  * Checks whether the application supports this MIME type
239  * @param mimeType The name of the MIME type you are
240  * interested in determining whether this service supports.
241  * @since 4.6
242  */
243  bool hasMimeType(const QString &mimeType) const;
244 
245  /**
246  * Returns the actions defined in this desktop file
247  */
248  QList<KServiceAction> actions() const;
249 
250  /**
251  * Checks whether this application can handle several files as
252  * startup arguments.
253  * @return true if multiple files may be passed to this service at
254  * startup. False if only one file at a time may be passed.
255  */
256  bool allowMultipleFiles() const;
257 
258  /**
259  * What preference to associate with this application initially (before
260  * the user has had any chance to define a profile for it).
261  * The bigger the value, the stronger the preference for the application.
262  * @return the service preference level of the application
263  */
264  int initialPreference() const;
265 
266  /**
267  * Whether the entry should be hidden from the menu.
268  * @return @c true to hide this application from the menu
269  *
270  * Such services still appear in trader queries, i.e. in
271  * "Open With" popup menus for instance.
272  */
273  bool noDisplay() const;
274 
275  /**
276  * Whether the application should be shown in the current desktop
277  * (including in context menus).
278  * @return true if the application should be shown in the current desktop.
279  *
280  * KApplicationTrader honors this and removes such services
281  * from its results.
282  *
283  * @since 5.0
284  */
285  bool showInCurrentDesktop() const;
286 
287  /**
288  * Whether the application should be shown on the current
289  * platform (e.g. on xcb or on wayland).
290  * @return @c true if the application should be shown on the current platform.
291  *
292  * @since 5.0
293  */
294  bool showOnCurrentPlatform() const;
295 
296  /**
297  * The path to the documentation for this application.
298  * @since 4.2
299  * @return the documentation path, or QString() if not set
300  */
301  QString docPath() const;
302 
303  /**
304  * Returns the requested property.
305  *
306  * @param _name the name of the property
307  * @param t the assumed type of the property
308  * @return the property, or invalid if not found
309  *
310  * @since 5.102
311  */
312  QVariant property(const QString &_name, QMetaType::Type t) const;
313 
315 
316  /**
317  * Returns a path that can be used for saving changes to this
318  * application
319  * @return path that can be used for saving changes to this application
320  */
321  QString locateLocal() const;
322 
323  /**
324  * @internal
325  * Set the menu id
326  */
327  void setMenuId(const QString &menuId);
328  /**
329  * @internal
330  * Sets whether to use a terminal or not
331  */
332  void setTerminal(bool b);
333  /**
334  * @internal
335  * Sets the terminal options to use
336  */
337  void setTerminalOptions(const QString &options);
338 
339  /**
340  * Overrides the "Exec=" line of the service.
341  *
342  * If @ref exec is not empty, its value will override the one
343  * the one set when this application was created.
344  *
345  * Please note that @ref entryPath is also cleared so the application
346  * will no longer be associated with a specific config file.
347  *
348  * @internal
349  * @since 4.11
350  */
351  void setExec(const QString &exec);
352 
353  /**
354  * Overrides the "Path=" line of the application.
355  *
356  * If @ref workingDir is not empty, its value will override
357  * the one set when this application was created.
358  *
359  * Please note that @ref entryPath is also cleared so the application
360  * will no longer be associated with a specific config file.
361  *
362  * @internal
363  * @param workingDir
364  * @since 5.79
365  */
366  void setWorkingDirectory(const QString &workingDir);
367 
368  /**
369  * Find a application based on its path as returned by entryPath().
370  * It's usually better to use serviceByStorageId() instead.
371  *
372  * @param _path the path of the configuration file
373  * @return a pointer to the requested application or @c nullptr if the application is
374  * unknown.
375  * @em Very @em important: Don't store the result in a KService* !
376  */
377  static Ptr serviceByDesktopPath(const QString &_path);
378 
379  /**
380  * Find an application by the name of its desktop file, not depending on
381  * its actual location (as long as it's under the applications or application
382  * directories). For instance "konqbrowser" or "kcookiejar". Note that
383  * the ".desktop" extension is implicit.
384  *
385  * This is the recommended method (safe even if the user moves stuff)
386  * but note that it assumes that no two entries have the same filename.
387  *
388  * @param _name the name of the configuration file
389  * @return a pointer to the requested application or @c nullptr if the application is
390  * unknown.
391  * @em Very @em important: Don't store the result in a KService* !
392  */
393  static Ptr serviceByDesktopName(const QString &_name);
394 
395  /**
396  * Find a application by its menu-id
397  *
398  * @param _menuId the menu id of the application
399  * @return a pointer to the requested application or @c nullptr if the application is
400  * unknown.
401  * @em Very @em important: Don't store the result in a KService* !
402  */
403  static Ptr serviceByMenuId(const QString &_menuId);
404 
405  /**
406  * Find a application by its storage-id or desktop-file path. This
407  * function will try very hard to find a matching application.
408  *
409  * @param _storageId the storage id or desktop-file path of the application
410  * @return a pointer to the requested application or @c nullptr if the application is
411  * unknown.
412  * @em Very @em important: Don't store the result in a KService* !
413  */
414  static Ptr serviceByStorageId(const QString &_storageId);
415 
416  /**
417  * Returns the whole list of applications.
418  *
419  * Useful for being able to
420  * to display them in a list box, for example.
421  * More memory consuming than the ones above, don't use unless
422  * really necessary.
423  * @return the list of all applications
424  */
425  static List allServices();
426 
427  /**
428  * Returns a path that can be used to create a new KService based
429  * on @p suggestedName.
430  * @param showInMenu @c true, if the application should be shown in the KDE menu
431  * @c false, if the application should be hidden from the menu
432  * This argument isn't used anymore, use `NoDisplay=true` to hide the application.
433  * @param suggestedName name to base the file on, if an application with such a
434  * name already exists, a suffix will be added to make it unique
435  * (e.g. foo.desktop, foo-1.desktop, foo-2.desktop).
436  * @param menuId If provided, menuId will be set to the menu id to use for
437  * the KService
438  * @param reservedMenuIds If provided, the path and menu id will be chosen
439  * in such a way that the new menu id does not conflict with any
440  * of the reservedMenuIds
441  * @return The path to use for the new KService.
442  */
443  static QString newServicePath(bool showInMenu, const QString &suggestedName, QString *menuId = nullptr, const QStringList *reservedMenuIds = nullptr);
444 
445  /**
446  * @brief A desktop file name that this application is an alias for.
447  *
448  * This is used when a `NoDisplay` application is used to enforce specific handling
449  * for an application. In that case the `NoDisplay` application is an `AliasFor` another
450  * application and be considered roughly equal to the `AliasFor` application (which should
451  * not be `NoDisplay=true`)
452  * For example Okular supplies a desktop file for each supported format (e.g. PDF), all
453  * of which `NoDisplay` and it is merely there to selectively support specific file formats.
454  * A UI may choose to display the aliased entry org.kde.okular instead of the NoDisplay entries.
455  *
456  * @since 5.96
457  *
458  * @return QString desktopName of the aliased application (excluding .desktop suffix)
459  */
460  QString aliasFor() const;
461 
462 private:
463  friend class KBuildServiceFactory;
464 
465  /// @internal for KBuildSycoca only
466  struct KSERVICE_NO_EXPORT ServiceTypeAndPreference {
467  ServiceTypeAndPreference()
468  : preference(-1)
469  , serviceType()
470  {
471  }
472  ServiceTypeAndPreference(int pref, const QString &servType)
473  : preference(pref)
474  , serviceType(servType)
475  {
476  }
477  int preference;
478  QString serviceType; // or MIME type
479  };
480  /// @internal for KBuildSycoca only
481  QVector<ServiceTypeAndPreference> _k_accessServiceTypes();
482 
483  void setActions(const QList<KServiceAction> &actions);
484 
485  friend QDataStream &operator>>(QDataStream &, ServiceTypeAndPreference &);
486  friend QDataStream &operator<<(QDataStream &, const ServiceTypeAndPreference &);
487 
488  Q_DECLARE_PRIVATE(KService)
489 
490  friend class KServiceFactory;
491 
492  /**
493  * @internal
494  * Construct a service from a stream.
495  * The stream must already be positioned at the correct offset.
496  */
497  KSERVICE_NO_EXPORT KService(QDataStream &str, int offset);
498 };
499 #endif
QString storageId() const
QVariant property(const QString &name) const
Returns the requested property.
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Base class for all Sycoca entries.
Definition: ksycocaentry.h:32
QList< Ptr > List
A list of shared data pointers for KService.
Definition: kservice.h:50
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
QExplicitlySharedDataPointer< KService > Ptr
A shared data pointer for KService.
Definition: kservice.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Jun 6 2023 03:54:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.