File indexing completed on 2024-04-21 03:55:01

0001 /*
0002     SPDX-FileCopyrightText: 2013 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KIO_DESKTOPEXECPARSER_H
0008 #define KIO_DESKTOPEXECPARSER_H
0009 
0010 #include "kiocore_export.h"
0011 
0012 #include <QList>
0013 #include <QScopedPointer>
0014 #include <QStringList>
0015 
0016 class QUrl;
0017 class KService;
0018 
0019 namespace KIO
0020 {
0021 class DesktopExecParserPrivate;
0022 
0023 /**
0024  * @class KIO::DesktopExecParser desktopexecparser.h <KIO/DesktopExecParser>
0025  *
0026  * Parses the Exec= line from a .desktop file,
0027  * and process all the '\%' placeholders, e.g.\ handling URLs vs local files.
0028  *
0029  * The processing actually happens when calling resultingArguments(), after
0030  * setting everything up.
0031  *
0032  * @since 5.0
0033  */
0034 class KIOCORE_EXPORT DesktopExecParser
0035 {
0036 public:
0037     /**
0038      * Creates a parser for a desktop file Exec line.
0039      *
0040      * @param service the service to extract information from.
0041      * The KService instance must remain alive as long as the parser is alive.
0042      * @param urls The urls the service should open.
0043      */
0044     DesktopExecParser(const KService &service, const QList<QUrl> &urls);
0045 
0046     /**
0047      * Destructor
0048      */
0049     ~DesktopExecParser();
0050 
0051     /**
0052      * If @p tempFiles is set to true and the urls given to the constructor are local files,
0053      * they will be deleted when the application exits.
0054      */
0055     void setUrlsAreTempFiles(bool tempFiles);
0056 
0057     /**
0058      * Sets the file name to use in the case of downloading the file to a tempfile
0059      * in order to give to a non-url-aware application. Some apps rely on the extension
0060      * to determine the MIME type of the file. Usually the file name comes from the URL,
0061      * but in the case of the HTTP Content-Disposition header, we need to override the
0062      * file name.
0063      */
0064     void setSuggestedFileName(const QString &suggestedFileName);
0065 
0066     /**
0067      * @return a list of arguments suitable for QProcess.
0068      * Returns an empty list on error, check errorMessage() for details.
0069      */
0070     QStringList resultingArguments() const;
0071 
0072     /**
0073      * @return an error message for when resultingArguments() returns an empty list
0074      * @since 5.71
0075      */
0076     QString errorMessage() const;
0077 
0078     /**
0079      * Returns the list of protocols which the application supports.
0080      * This can be a list of actual protocol names, or just "KIO" for KIO-based apps.
0081      */
0082     static QStringList supportedProtocols(const KService &service);
0083 
0084     /**
0085      * Returns true if @p protocol is in the list of protocols returned by supportedProtocols().
0086      * The only reason for this method is the special handling of "KIO".
0087      */
0088     static bool isProtocolInSupportedList(const QUrl &url, const QStringList &supportedProtocols);
0089 
0090     /**
0091      * Returns true if @p protocol should be opened by a "handler" application, i.e.\ an application
0092      * associated to _all_ URLs using this protocol (a.k.a. scheme).
0093      */
0094     static bool hasSchemeHandler(const QUrl &url); // KF6 TODO move to OpenUrlJob
0095 
0096     /**
0097      * Given a full command line (e.g.\ the Exec= line from a .desktop file),
0098      * extract the name of the executable being run (removing the path, if specified).
0099      * @param execLine the full command line
0100      * @return the name of the executable to run, example: "ls"
0101      */
0102     static QString executableName(const QString &execLine);
0103 
0104     /**
0105      * Given a full command line (e.g.\ the Exec= line from a .desktop file),
0106      * extract the name of the executable being run, including its full path, if specified.
0107      * @param execLine the full command line
0108      * @return the name of the executable to run, example: "/bin/ls"
0109      */
0110     static QString executablePath(const QString &execLine);
0111 
0112 private:
0113     QScopedPointer<DesktopExecParserPrivate> d;
0114 };
0115 
0116 } // namespace KIO
0117 
0118 #endif