File indexing completed on 2024-05-12 03:54:27

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