File indexing completed on 2024-05-12 15:34:12

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Pietro Iglio <iglio@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #ifndef KDESKTOPFILE_H
0008 #define KDESKTOPFILE_H
0009 
0010 #include <kconfig.h>
0011 
0012 class KConfigGroup;
0013 class KDesktopFilePrivate;
0014 
0015 /**
0016  * \class KDesktopFile kdesktopfile.h <KDesktopFile>
0017  *
0018  * %KDE Desktop File Management.
0019  * This class implements %KDE's support for the freedesktop.org
0020  * <em>Desktop Entry Spec</em>.
0021  *
0022  * @author Pietro Iglio <iglio@kde.org>
0023  * @see  KConfigBase  KConfig
0024  * @see <a href="https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html">Desktop Entry Spec</a>
0025  */
0026 class KCONFIGCORE_EXPORT KDesktopFile : public KConfig
0027 {
0028 public:
0029     /**
0030      * Constructs a KDesktopFile object.
0031      *
0032      * See QStandardPaths for more information on resources.
0033      *
0034      * @param resourceType   Allows you to change what sort of resource
0035      *                       to search for if @p fileName is not absolute.
0036      *                       For instance, you might want to specify GenericConfigLocation.
0037      * @param fileName       The name or path of the desktop file. If it
0038      *                       is not absolute, it will be located
0039      *                       using the resource type @p resType.
0040      */
0041     explicit KDesktopFile(QStandardPaths::StandardLocation resourceType, const QString &fileName);
0042 
0043     /**
0044      * Constructs a KDesktopFile object.
0045      *
0046      * See QStandardPaths for more information on resources.
0047      *
0048      * @param fileName       The name or path of the desktop file. If it
0049      *                       is not absolute, it will be located
0050      *                       using the resource type ApplicationsLocation
0051      */
0052     explicit KDesktopFile(const QString &fileName);
0053 
0054     /**
0055      * Destructs the KDesktopFile object.
0056      *
0057      * Writes back any changed configuration entries.
0058      */
0059     ~KDesktopFile() override;
0060 
0061     /**
0062      * Checks whether this is really a desktop file.
0063      *
0064      * The check is performed looking at the file extension (the file is not
0065      * opened).
0066      * Currently, the only valid extension is ".desktop".
0067      * @param path the path of the file to check
0068      * @return true if the file appears to be a desktop file.
0069      */
0070     static bool isDesktopFile(const QString &path);
0071 
0072     /**
0073      * Checks whether the user is authorized to run this desktop file.
0074      * By default users are authorized to run all desktop files but
0075      * the KIOSK framework can be used to activate certain restrictions.
0076      * See README.kiosk for more information.
0077      *
0078      * Note that desktop files that are not in a standard location (as
0079      * specified by XDG_DATA_DIRS) must have their executable bit set
0080      * to be authorized, regardless of KIOSK settings, to prevent users
0081      * from inadvertently running trojan desktop files.
0082      *
0083      * @param path the file to check
0084      * @return true if the user is authorized to run the file
0085      */
0086     static bool isAuthorizedDesktopFile(const QString &path);
0087 
0088     /**
0089      * Returns the location where changes for the .desktop file @p path
0090      * should be written to.
0091      */
0092     static QString locateLocal(const QString &path);
0093 
0094     /**
0095      * Returns the main config group (named "Desktop Entry") in a .desktop file.
0096      */
0097     KConfigGroup desktopGroup() const;
0098 
0099     /**
0100      * Returns the value of the "Type=" entry.
0101      * @return the type or QString() if not specified
0102      */
0103     QString readType() const;
0104 
0105     /**
0106      * Returns the value of the "Icon=" entry.
0107      * @return the icon or QString() if not specified
0108      */
0109     QString readIcon() const;
0110 
0111     /**
0112      * Returns the value of the "Name=" entry.
0113      * @return the name or QString() if not specified
0114      */
0115     QString readName() const;
0116 
0117     /**
0118      * Returns the value of the "Comment=" entry.
0119      * @return the comment or QString() if not specified
0120      */
0121     QString readComment() const;
0122 
0123     /**
0124      * Returns the value of the "GenericName=" entry.
0125      * @return the generic name or QString() if not specified
0126      */
0127     QString readGenericName() const;
0128 
0129     /**
0130      * Returns the value of the "Path=" entry.
0131      * @return the path or QString() if not specified
0132      */
0133     QString readPath() const;
0134 
0135 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 82)
0136     /**
0137      * Returns the value of the "Dev=" entry.
0138      * @return the device or QString() if not specified
0139      * @deprecated since 5.82, for lack of usage, the FSDevice .desktop template
0140      * hadn't been installed for a long time.
0141      */
0142     KCONFIGCORE_DEPRECATED_VERSION(5, 82, "For lack of usage.")
0143     QString readDevice() const;
0144 #endif
0145 
0146     /**
0147      * Returns the value of the "URL=" entry.
0148      * @return the URL or QString() if not specified
0149      */
0150     QString readUrl() const;
0151 
0152     /**
0153      * Returns a list of the "Actions=" entries.
0154      * @return the list of actions
0155      */
0156     QStringList readActions() const;
0157 
0158     /**
0159      * Returns a list of the "MimeType=" entries.
0160      * @return the list of mime types
0161      * @since 5.15
0162      */
0163     QStringList readMimeTypes() const;
0164 
0165     /**
0166      * Sets the desktop action group.
0167      * @param group the new action group
0168      */
0169     KConfigGroup actionGroup(const QString &group);
0170 
0171     // TODO KF6, don't return by const value
0172     const KConfigGroup actionGroup(const QString &group) const;
0173 
0174     /**
0175      * Returns true if the action group exists, false otherwise
0176      * @param group the action group to test
0177      * @return true if the action group exists
0178      */
0179     bool hasActionGroup(const QString &group) const;
0180 
0181     /**
0182      * Checks whether there is a "Type=Link" entry.
0183      *
0184      * The link points to the "URL=" entry.
0185      * @return true if there is a "Type=Link" entry
0186      */
0187     bool hasLinkType() const;
0188 
0189     /**
0190      * Checks whether there is an entry "Type=Application".
0191      * @return true if there is a "Type=Application" entry
0192      */
0193     bool hasApplicationType() const;
0194 
0195     /**
0196      * Checks whether there is an entry "Type=FSDevice".
0197      * @return true if there is a "Type=FSDevice" entry
0198      */
0199     bool hasDeviceType() const;
0200 
0201     /**
0202      * Checks whether the TryExec field contains a binary
0203      * which is found on the local system.
0204      * @return true if TryExec contains an existing binary
0205      */
0206     bool tryExec() const;
0207 
0208     /**
0209      * Returns the value of the "X-DocPath=" Or "DocPath=" entry.
0210      * @return The value of the "X-DocPath=" Or "DocPath=" entry.
0211      */
0212     QString readDocPath() const;
0213 
0214 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 42)
0215     /**
0216      * Returns the entry of the "SortOrder=" entry.
0217      * @return the value of the "SortOrder=" entry.
0218      * @deprecated since 5.42
0219      * SortOrder was used to specify the order of menu items, but
0220      * the Desktop Menu Specification defines another mechanism for it.
0221      */
0222     KCONFIGCORE_DEPRECATED_VERSION(5, 42, "Not recommended, Desktop Menu Specification defines another mechanism")
0223     QStringList sortOrder() const;
0224 #endif
0225 
0226     /**
0227      * Whether the entry should be suppressed in menus.
0228      * This handles the NoDisplay key, but also OnlyShowIn / NotShowIn.
0229      * @return true to suppress this service
0230      * @since 4.1
0231      */
0232     bool noDisplay() const;
0233 
0234     /**
0235      * Copies all entries from this config object to a new
0236      * KDesktopFile object that will save itself to @p file.
0237      *
0238      * Actual saving to @p file happens when the returned object is
0239      * destructed or when sync() is called upon it.
0240      *
0241      * @param file the new KDesktopFile object it will save itself to.
0242      */
0243     KDesktopFile *copyTo(const QString &file) const;
0244 
0245     /**
0246      * Returns the name of the .desktop file that was used to construct this KDesktopFile.
0247      */
0248     QString fileName() const;
0249 
0250 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 89)
0251     /**
0252      * @deprecated Since 5.89, use locationType() instead.
0253      */
0254     KCONFIGCORE_DEPRECATED_VERSION(5, 89, "Use locationType() instead.")
0255     QStandardPaths::StandardLocation resource() const;
0256 #endif
0257 
0258 private:
0259     Q_DISABLE_COPY(KDesktopFile)
0260 
0261     Q_DECLARE_PRIVATE(KDesktopFile)
0262 };
0263 
0264 #endif