File indexing completed on 2024-05-12 05:46:51

0001 /*
0002  *   Copyright 2013 David Faure <faure@kde.org>
0003  *
0004  *   This library is free software; you can redistribute it and/or
0005  *   modify it under the terms of the GNU Lesser General Public
0006  *   License as published by the Free Software Foundation; either
0007  *   version 2.1 of the License, or (at your option) version 3, or any
0008  *   later version accepted by the membership of KDE e.V. (or its
0009  *   successor approved by the membership of KDE e.V.), which shall
0010  *   act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  *   This library is distributed in the hope that it will be useful,
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  *   Lesser General Public License for more details.
0016  *
0017  *   You should have received a copy of the GNU Lesser General Public
0018  *   License along with this library.  If not, see <http://www.gnu.org/licenses/>
0019  */
0020 
0021 #ifndef KIO_DESKTOPEXECPARSER_H
0022 #define KIO_DESKTOPEXECPARSER_H
0023 
0024 #include "kiocore_export.h"
0025 
0026 #include <QList>
0027 #include <QScopedPointer>
0028 class QUrl;
0029 class QStringList;
0030 class KService;
0031 
0032 namespace KIO
0033 {
0034 
0035 class DesktopExecParserPrivate;
0036 
0037 /**
0038  * @class KIO::DesktopExecParser desktopexecparser.h <KIO/DesktopExecParser>
0039  *
0040  * Parses the Exec= line from a .desktop file,
0041  * and process all the '\%' placeholders, e.g. handling URLs vs local files.
0042  *
0043  * The processing actually happens when calling resultingArguments(), after
0044  * setting everything up.
0045  *
0046  * @since 5.0
0047  */
0048 class KIOCORE_EXPORT DesktopExecParser
0049 {
0050 public:
0051     /**
0052      * Creates a parser for a desktop file Exec line.
0053      *
0054      * @param service the service to extract information from.
0055      * The KService instance must remain alive as long as the parser is alive.
0056      * @param urls The urls the service should open.
0057      */
0058     DesktopExecParser(const KService &service, const QList<QUrl> &urls);
0059 
0060     /**
0061      * Destructor
0062      */
0063     ~DesktopExecParser();
0064 
0065     /**
0066      * If @p tempFiles is set to true and the urls given to the constructor are local files,
0067      * they will be deleted when the application exits.
0068      */
0069     void setUrlsAreTempFiles(bool tempFiles);
0070 
0071     /**
0072      * Sets the file name to use in the case of downloading the file to a tempfile
0073      * in order to give to a non-url-aware application. Some apps rely on the extension
0074      * to determine the mimetype of the file. Usually the file name comes from the URL,
0075      * but in the case of the HTTP Content-Disposition header, we need to override the
0076      * file name.
0077      */
0078     void setSuggestedFileName(const QString &suggestedFileName);
0079 
0080     /**
0081      * @return a list of arguments suitable for QProcess.
0082      */
0083     QStringList resultingArguments() const;
0084 
0085     /**
0086      * Returns the list of protocols which the application supports.
0087      * This can be a list of actual protocol names, or just "KIO" for KIO-based apps.
0088      */
0089     static QStringList supportedProtocols(const KService &service);
0090 
0091     /**
0092      * Returns true if @p protocol is in the list of protocols returned by supportedProtocols().
0093      * The only reason for this method is the special handling of "KIO".
0094      */
0095     static bool isProtocolInSupportedList(const QUrl &url, const QStringList &supportedProtocols);
0096 
0097     /**
0098      * Returns true if @p protocol should be opened by a "handler" application, i.e. an application
0099      * associated to _all_ URLs using this protocol (a.k.a. scheme).
0100      */
0101     static bool hasSchemeHandler(const QUrl &url);
0102 
0103     /**
0104      * Given a full command line (e.g. the Exec= line from a .desktop file),
0105      * extract the name of the executable being run (removing the path, if specified).
0106      * @param execLine the full command line
0107      * @return the name of the executable to run, example: "ls"
0108      */
0109     static QString executableName(const QString &execLine);
0110 
0111     /**
0112      * Given a full command line (e.g. the Exec= line from a .desktop file),
0113      * extract the name of the executable being run, including its full path, if specified.
0114      * @param execLine the full command line
0115      * @return the name of the executable to run, example: "/bin/ls"
0116      */
0117     static QString executablePath(const QString &execLine);
0118 
0119 private:
0120     QScopedPointer<DesktopExecParserPrivate> d;
0121 };
0122 
0123 } // namespace KIO
0124 
0125 #endif