KService Class

Header: #include <KService>
CMake: find_package(KF6 REQUIRED COMPONENTS Service)
target_link_libraries(mytarget PRIVATE KF6::Service)

Public Types

Public Functions

KService(const QString &fullpath)
KService(const KDesktopFile *config, const QString &entryPath = QString())
KService(const QString &name, const QString &exec, const QString &icon)
QList<KServiceAction> actions() const
QString aliasFor() const
bool allowMultipleFiles() const
QStringList categories() const
QString comment() const
QString desktopEntryName() const
QString docPath() const
QString exec() const
QString genericName() const
bool hasMimeType(const QString &mimeType) const
QString icon() const
bool isApplication() const
QStringList keywords() const
QString locateLocal() const
QString menuId() const
QStringList mimeTypes() const
bool noDisplay() const
(since KService 5.30) bool runOnDiscreteGpu() const
QStringList schemeHandlers() const
bool showInCurrentDesktop() const
bool showOnCurrentPlatform() const
std::optional<bool> startupNotify() const
QString storageId() const
bool substituteUid() const
QStringList supportedProtocols() const
bool terminal() const
QString terminalOptions() const
QString untranslatedGenericName() const
QString untranslatedName() const
QString username() const
QString workingDirectory() const

Static Public Members

KService::List allServices()
QString newServicePath(bool showInMenu, const QString &suggestedName, QString *menuId = nullptr, const QStringList *reservedMenuIds = nullptr)
KService::Ptr serviceByDesktopName(const QString &_name)
KService::Ptr serviceByDesktopPath(const QString &_path)
KService::Ptr serviceByMenuId(const QString &_menuId)
KService::Ptr serviceByStorageId(const QString &_storageId)

Detailed Description

Represents an installed application.

To obtain a KService instance for a specific application you typically use serviceByDesktopName(), e.g.:

KService::Ptr service = KService::serviceByDesktopName("org.kde.kate");

Other typical usage would be in combination with KApplicationTrader to obtain e.g. the default application for a given file type.

@see <a href="https://specifications.freedesktop.org/desktop-entry-spec/latest/">Desktop Entry Specification</a>

Member Type Documentation

KService::List

A list of shared data pointers for KService.

KService::Ptr

A shared data pointer for KService.

Member Function Documentation

[explicit] KService::KService(const QString &fullpath)

Construct a service and take all information from a .desktop file.

fullpath Full path to the .desktop file.

[explicit] KService::KService(const KDesktopFile *config, const QString &entryPath = QString())

Construct a service and take all information from a desktop file.

config the desktop file to read

optional relative path to store for findByName

KService::KService(const QString &name, const QString &exec, const QString &icon)

Construct a temporary service with a given name, exec-line and icon.

name the name of the service

exec the executable

icon the name of the icon

QList<KServiceAction> KService::actions() const

Returns the actions defined in this desktop file

QString KService::aliasFor() const

@brief A desktop file name that this application is an alias for.

This is used when a `NoDisplay` application is used to enforce specific handling for an application. In that case the `NoDisplay` application is an `AliasFor` another application and be considered roughly equal to the `AliasFor` application (which should not be `NoDisplay=true`) For example Okular supplies a desktop file for each supported format (e.g. PDF), all of which `NoDisplay` and it is merely there to selectively support specific file formats. A UI may choose to display the aliased entry org.kde.okular instead of the NoDisplay entries.

@since 5.96

@return QString desktopName of the aliased application (excluding .desktop suffix)

[static] KService::List KService::allServices()

Returns the whole list of applications.

Useful for being able to to display them in a list box, for example. More memory consuming than the ones above, don't use unless really necessary. @return the list of all applications

bool KService::allowMultipleFiles() const

Checks whether this application can handle several files as startup arguments. @return true if multiple files may be passed to this service at startup. False if only one file at a time may be passed.

QStringList KService::categories() const

Returns a list of VFolder categories. @return the list of VFolder categories

QString KService::comment() const

Returns the descriptive comment for the application, if there is one. @return the descriptive comment for the application, or QString() if not set

QString KService::desktopEntryName() const

Returns the filename of the desktop entry without any extension, e.g. "org.kde.kate" @return the name of the desktop entry without path or extension, or QString() if not set

QString KService::docPath() const

The path to the documentation for this application. @since 4.2 @return the documentation path, or QString() if not set

QString KService::exec() const

Returns the executable.

Returns the command that the service executes, or QString() if not set

QString KService::genericName() const

Returns the generic name for the application, if there is one (e.g. "Mail Client"). @return the generic name, or QString() if not set

bool KService::hasMimeType(const QString &mimeType) const

Checks whether the application supports this MIME type @param mimeType The name of the MIME type you are interested in determining whether this service supports. @since 4.6

QString KService::icon() const

Returns the name of the icon associated with the service, or QString() if not set

bool KService::isApplication() const

Whether this service is an application

Returns true if this service is an application, i.e. it has Type=Application in its .desktop file and exec() will not be empty.

QStringList KService::keywords() const

Returns a list of descriptive keywords for the application, if there are any. @return the list of keywords

QString KService::locateLocal() const

Returns a path that can be used for saving changes to this application @return path that can be used for saving changes to this application

Returns the menu ID of the application desktop entry. The menu ID is used to add or remove the entry to a menu. @return the menu ID

QStringList KService::mimeTypes() const

Returns the list of MIME types that this application supports. Note that this doesn't include inherited MIME types, only the MIME types listed in the .desktop file. @since 4.8.3

[static] QString KService::newServicePath(bool showInMenu, const QString &suggestedName, QString *menuId = nullptr, const QStringList *reservedMenuIds = nullptr)

Returns a path that can be used to create a new KService based on @p suggestedName. @param showInMenu @c true, if the application should be shown in the KDE menu @c false, if the application should be hidden from the menu This argument isn't used anymore, use `NoDisplay=true` to hide the application. @param suggestedName name to base the file on, if an application with such a name already exists, a suffix will be added to make it unique (e.g. foo.desktop, foo-1.desktop, foo-2.desktop). @param menuId If provided, menuId will be set to the menu id to use for the KService @param reservedMenuIds If provided, the path and menu id will be chosen in such a way that the new menu id does not conflict with any of the reservedMenuIds @return The path to use for the new KService.

bool KService::noDisplay() const

Whether the entry should be hidden from the menu. @return @c true to hide this application from the menu

Such services still appear in trader queries, i.e. in "Open With" popup menus for instance.

[since KService 5.30] bool KService::runOnDiscreteGpu() const

Returns true if the application indicates that it's preferred to run on a discrete graphics card, otherwise return false.

In releases older than 5.86, this method checked for the X-KDE-RunOnDiscreteGpu key in the .desktop file represented by this service; starting from 5.86 this method now also checks for PrefersNonDefaultGPU key (added to the Freedesktop.org desktop entry spec in version 1.4 of the spec).

This function was introduced in KService 5.30.

QStringList KService::schemeHandlers() const

Returns the list of scheme handlers this application supports.

For example a web browser could return {"http", "https"}.

This is taken from the x-scheme-handler MIME types listed in the .desktop file.

@since 6.0

[static] KService::Ptr KService::serviceByDesktopName(const QString &_name)

Find an application by the name of its desktop file, not depending on its actual location (as long as it's under the applications or application directories). For instance "konqbrowser" or "kcookiejar". Note that the ".desktop" extension is implicit.

This is the recommended method (safe even if the user moves stuff) but note that it assumes that no two entries have the same filename.

@param _name the name of the configuration file @return a pointer to the requested application or @c nullptr if the application is unknown. @em Very @em important: Don't store the result in a KService* !

[static] KService::Ptr KService::serviceByDesktopPath(const QString &_path)

Find a application based on its path as returned by entryPath(). It's usually better to use serviceByStorageId() instead.

@param _path the path of the configuration file @return a pointer to the requested application or @c nullptr if the application is unknown. @em Very @em important: Don't store the result in a KService* !

[static] KService::Ptr KService::serviceByMenuId(const QString &_menuId)

Find a application by its menu-id

@param _menuId the menu id of the application @return a pointer to the requested application or @c nullptr if the application is unknown. @em Very @em important: Don't store the result in a KService* !

[static] KService::Ptr KService::serviceByStorageId(const QString &_storageId)

Find a application by its storage-id or desktop-file path. This function will try very hard to find a matching application.

@param _storageId the storage id or desktop-file path of the application @return a pointer to the requested application or @c nullptr if the application is unknown. @em Very @em important: Don't store the result in a KService* !

bool KService::showInCurrentDesktop() const

Whether the application should be shown in the current desktop (including in context menus). @return true if the application should be shown in the current desktop.

KApplicationTrader honors this and removes such services from its results.

@since 5.0

bool KService::showOnCurrentPlatform() const

Whether the application should be shown on the current platform (e.g. on xcb or on wayland). @return @c true if the application should be shown on the current platform.

@since 5.0

std::optional<bool> KService::startupNotify() const

Returns the value of StartupNotify for this service.

If the service doesn't define a value nullopt is returned.

See StartupNotify in the <a href="https://specifications.freedesktop.org/desktop-entry-spec/latest/">Desktop Entry Specification</a>.

@since 6.0

QString KService::storageId() const

Returns a normalized ID suitable for storing in configuration files. It will be based on the menu-id when available and otherwise falls back to entryPath() @return the storage ID

bool KService::substituteUid() const

@brief Checks whether the application needs to run under a different UID. @return @c true if the application needs to run under a different UID. @see username()

QStringList KService::supportedProtocols() const

Returns the list of protocols this application supports.

This is taken from the x-scheme-handler MIME types listed in the .desktop file as well as the 'X-KDE-Protocols' entry

For example a web browser could return {"http", "https"}. @since 6.0

bool KService::terminal() const

Checks whether the application should be run in a terminal.

This corresponds to `Terminal=true` in the .desktop file.

Returns true if the application should be run in a terminal.

QString KService::terminalOptions() const

Returns any options associated with the terminal the application runs in, if it requires a terminal.

The application must be a TTY-oriented program.

QString KService::untranslatedGenericName() const

Returns the untranslated (US English) generic name for the application, if there is one (e.g. "Mail Client"). @return the generic name, or QString() if not set

QString KService::untranslatedName() const

@return untranslated name for the given service

@since 6.0

QString KService::username() const

Returns the user name if the application runs with a different user id. @return the username under which the service has to be run, or QString() if not set @see substituteUid()

QString KService::workingDirectory() const

@return the working directory to run the program in, or QString() if not set @since 5.63