File indexing completed on 2024-04-14 03:53:18

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KIO_OPENURLJOB_H
0009 #define KIO_OPENURLJOB_H
0010 
0011 #include "applicationlauncherjob.h"
0012 #include "kiogui_export.h"
0013 #include <KCompositeJob>
0014 #include <QScopedPointer>
0015 
0016 class QUrl;
0017 
0018 namespace KIO
0019 {
0020 class OpenUrlJobPrivate;
0021 
0022 /**
0023  * @class OpenUrlJob openurljob.h <KIO/OpenUrlJob>
0024  *
0025  * @brief OpenUrlJob finds out the right way to "open" a URL.
0026  * This includes finding out its MIME type, and then the associated application,
0027  * or running desktop files, executables, etc.
0028  * It also honours the "use this webbrowser for all http(s) URLs" setting.
0029  *
0030  * For the "Open With" dialog functionality to work, make sure to set
0031  * KIO::JobUiDelegate as the delegate for this job (in widgets applications).
0032  * @code
0033  *    // Since 5.98 use:
0034  *    job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, window));
0035  *    // For older releases use:
0036  *    job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, window));
0037  * @endcode
0038  *
0039  * @since 5.71
0040  */
0041 class KIOGUI_EXPORT OpenUrlJob : public KCompositeJob
0042 {
0043     Q_OBJECT
0044 public:
0045     /**
0046      * @brief Creates an OpenUrlJob in order to open a URL.
0047      * @param url the URL of the file/directory to open
0048      */
0049     explicit OpenUrlJob(const QUrl &url, QObject *parent = nullptr);
0050 
0051     /**
0052      * @brief Creates an OpenUrlJob for the case where the MIME type is already known.
0053      * @param url the URL of the file/directory to open
0054      * @param mimeType the type of file/directory. See QMimeType.
0055      */
0056     explicit OpenUrlJob(const QUrl &url, const QString &mimeType, QObject *parent = nullptr);
0057 
0058     /**
0059      * Destructor
0060      *
0061      * Note that by default jobs auto-delete themselves after emitting result.
0062      */
0063     ~OpenUrlJob() override;
0064 
0065     /**
0066      * Specifies that the URL passed to the application will be deleted when it exits (if the URL is a local file)
0067      */
0068     void setDeleteTemporaryFile(bool b);
0069 
0070     /**
0071      * Sets the file name to use in the case of downloading the file to a tempfile,
0072      * in order to give it to a non-URL-aware application.
0073      * Some apps rely on the extension to determine the MIME type of the file.
0074      * Usually the file name comes from the URL, but in the case of the
0075      * HTTP Content-Disposition header, we need to override the file name.
0076      * @param suggestedFileName the file name
0077      */
0078     void setSuggestedFileName(const QString &suggestedFileName);
0079 
0080     /**
0081      * Sets the platform-specific startup id of the application launch.
0082      * @param startupId startup id, if any (otherwise "").
0083      * For X11, this would be the id for the Startup Notification protocol.
0084      * For Wayland, this would be the token for the XDG Activation protocol.
0085      */
0086     void setStartupId(const QByteArray &startupId);
0087 
0088     /**
0089      * Set this to true if this class should allow the user to run executables.
0090      * Unlike KF5's KRun, this setting is OFF by default here for security reasons.
0091      * File managers can enable this, but e.g. web browsers, mail clients etc. shouldn't.
0092      */
0093     void setRunExecutables(bool allow);
0094 
0095     /**
0096      * Set this to @c true if this class should show a dialog to ask the user about how
0097      * to handle various types of executable files; note that executing/running remote
0098      * files is disallowed as that is not secure (in the case of remote shell scripts
0099      * and .desktop files, they are always opened as text in the default application):
0100      *  - For native binaries: whether to execute or cancel
0101      *  - For .exe files: whether to execute or cancel, ("execute" on Linux in this
0102      *    context means running the file with the default application (e.g. WINE))
0103      *  - For executable shell scripts: whether to execute the file or open it as
0104      *    text in the default application; note that if the file doesn't have the
0105      *    execute bit, it'll always be opened as text
0106      *  - For .desktop files: whether to run the file or open it as text in the default
0107      *    application; note that if the .desktop file is located in a non-standard
0108      *    location (on Linux standard locations are /usr/share/applications or
0109      *    ~/.local/share/applications) and does not have the execute bit, another dialog
0110      *    (see UntrustedProgramHandlerInterface) will be launched to ask the user whether
0111      *    they trust running the application (the one the .desktop file launches based on
0112      *    the Exec line)
0113      *
0114      * Note that the dialog, ExecutableFileOpenDialog (from KIOWidgets), provides an option
0115      * to remember the last value used and not ask again, if that is set, then the dialog will
0116      * not be shown.
0117      *
0118      * When set to @c true this will take precedence over setRunExecutables (the latter can be
0119      * used to allow running executables without first asking the user for confirmation).
0120      *
0121      * @since 5.73
0122      */
0123     void setShowOpenOrExecuteDialog(bool b);
0124 
0125     /**
0126      * Sets whether the external webbrowser setting should be honoured.
0127      * This is enabled by default.
0128      * This should only be disabled in webbrowser applications.
0129      * @param b whether to let the external browser handle the URL or not
0130      */
0131     void setEnableExternalBrowser(bool b);
0132 
0133     /**
0134      * Sets whether the job should follow URL redirections.
0135      * This is enabled by default.
0136      * @param b whether to follow redirections or not.
0137      */
0138     void setFollowRedirections(bool b);
0139 
0140     /**
0141      * Starts the job.
0142      * You must call this, after having called all the needed setters.
0143      * This is a GUI job, never use exec(), it would block user interaction.
0144      */
0145     void start() override;
0146 
0147     /**
0148      * Returns whether the @p url of @p mimetype is executable.
0149      * To be executable the file must pass the following rules:
0150      * -# Must reside on the local filesystem.
0151      * -# Must be marked as executable for the user by the filesystem.
0152      * -# The MIME type must inherit application/x-executable, application/x-executable-script
0153      */
0154     static bool isExecutableFile(const QUrl &url, const QString &mimetypeName);
0155 
0156 Q_SIGNALS:
0157     /**
0158      * Emitted when the MIME type is determined.
0159      * This can be used for special cases like webbrowsers
0160      * who want to embed the URL in some cases, rather than starting a different
0161      * application. In that case they can kill the job.
0162      */
0163     void mimeTypeFound(const QString &mimeType);
0164 
0165 protected:
0166     bool doKill() override;
0167 
0168 private:
0169     void slotResult(KJob *job) override;
0170 
0171     friend class OpenUrlJobPrivate;
0172     QScopedPointer<OpenUrlJobPrivate> d;
0173 };
0174 
0175 } // namespace KIO
0176 
0177 #endif // OPENURLJOB_H