File indexing completed on 2024-04-28 15:29:20

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2002 David Faure <faure@kde.org>
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef kparts_browserrun_h
0009 #define kparts_browserrun_h
0010 
0011 #include <kparts/browserextension.h>
0012 
0013 #include <KRun>
0014 #include <KService>
0015 #include <memory>
0016 
0017 #if KIOWIDGETS_ENABLE_DEPRECATED_SINCE(5, 71)
0018 namespace KParts
0019 {
0020 /**
0021  * @class BrowserRun browserrun.h <KParts/BrowserRun>
0022  *
0023  * @short This class extends KRun to provide additional functionality for browsers:
0024  * - "save or open" dialog boxes
0025  * - "save" functionality
0026  * - support for HTTP POST (including saving the result to a temp file if
0027  *   opening a separate application)
0028  * - warning before launching executables off the web
0029  * - custom error handling (i.e. treating errors as HTML pages)
0030  * - generation of SSL metadata depending on the previous URL shown by the part
0031  * @author David Faure <faure@kde.org>
0032  */
0033 class KPARTS_EXPORT BrowserRun : public KRun
0034 {
0035     Q_OBJECT
0036 public:
0037     /**
0038      * @param url the URL we're probing
0039      * @param args URL args - includes reload, metaData, etc.
0040      * @param browserArgs browser-related args - includes data for a HTTP POST, etc.
0041      * @param part the part going to open this URL - can be @c nullptr if not created yet
0042      * @param window the mainwindow - passed to KIO::Job::setWindow()
0043      * @param removeReferrer if true, the "referrer" metadata from @p args isn't passed on
0044      * @param trustedSource if false, a warning will be shown before launching an executable.
0045       Always pass false for @p trustedSource, except for local directory views.
0046      * @param hideErrorDialog if true, no dialog will be shown in case of errors.
0047      */
0048     BrowserRun(const QUrl &url,
0049                const KParts::OpenUrlArguments &args,
0050                const KParts::BrowserArguments &browserArgs,
0051                KParts::ReadOnlyPart *part,
0052                QWidget *window,
0053                bool removeReferrer,
0054                bool trustedSource,
0055                bool hideErrorDialog = false);
0056 
0057     ~BrowserRun() override;
0058 
0059     KParts::OpenUrlArguments &arguments();
0060     KParts::BrowserArguments &browserArguments();
0061     KParts::ReadOnlyPart *part() const;
0062     QUrl url() const;
0063 
0064     bool hideErrorDialog() const;
0065 
0066     /**
0067      * @return Suggested disposition by the server (e.g. HTTP content-disposition)
0068      */
0069     QString contentDisposition() const;
0070 
0071     /**
0072      * @return Whether the returned disposition suggests saving or opening inline
0073      */
0074     bool serverSuggestsSave() const;
0075 
0076 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 0)
0077     enum AskSaveResult { Save, Open, Cancel };
0078     /**
0079      * Ask the user whether to save or open a url in another application.
0080      * @param url the URL in question
0081      * @param offer the application that will be used to open the URL
0082      * @param mimeType the mimetype of the URL
0083      * @param suggestedFileName optional file name suggested by the server
0084      * @return Save, Open or Cancel.
0085      * @deprecated Since 5.0, use BrowserOpenOrSaveQuestion
0086      * @code
0087      *  BrowserOpenOrSaveQuestion dlg(parent, url, mimeType, suggestedFileName);
0088      *  const BrowserOpenOrSaveQuestion::Result res = dlg.askOpenOrSave();
0089      * @endcode
0090      */
0091     KPARTS_DEPRECATED_VERSION(5, 0, "Use KParts::BrowserOpenOrSaveQuestion")
0092     static AskSaveResult askSave(const QUrl &url, KService::Ptr offer, const QString &mimeType, const QString &suggestedFileName = QString());
0093 #endif
0094 
0095 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 65)
0096     /** @deprecated Since 5.65, use BrowserOpenOrSaveQuestion::AskEmbedOrSaveFlags */
0097     enum AskEmbedOrSaveFlags { InlineDisposition = 0, AttachmentDisposition = 1 };
0098 #endif
0099 
0100 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 0)
0101     /**
0102      * Similar to askSave but for the case where the current application is
0103      * able to embed the url itself (instead of passing it to another app).
0104      * @param url the URL in question
0105      * @param mimeType the mimetype of the URL
0106      * @param suggestedFileName optional filename suggested by the server
0107      * @param flags set to AttachmentDisposition if suggested by the server
0108      * @return Save, Open or Cancel.
0109      * @deprecated Since 5.0, use BrowserOpenOrSaveQuestion
0110      * @code
0111      *  BrowserOpenOrSaveQuestion dlg(parent, url, mimeType, suggestedFileName);
0112      *  const BrowserOpenOrSaveQuestion::Result res = dlg.askEmbedOrSave(flags);
0113      *  // Important: returns Embed now, not Open!
0114      * @endcode
0115      */
0116     KPARTS_DEPRECATED_VERSION(5, 0, "Use KParts::BrowserOpenOrSaveQuestion")
0117     static AskSaveResult askEmbedOrSave(const QUrl &url, const QString &mimeType, const QString &suggestedFileName = QString(), int flags = 0);
0118 #endif
0119 
0120     // virtual so that KHTML can implement differently (HTML cache)
0121     virtual void save(const QUrl &url, const QString &suggestedFileName);
0122 
0123 #if KPARTS_ENABLE_DEPRECATED_SINCE(4, 4)
0124     /**
0125      * static so that it can be called from other classes
0126      * @deprecated Since 4.4, use saveUrl(const QUrl &, const QString &, QWidget *, const KParts::OpenUrlArguments &)
0127      */
0128     KPARTS_DEPRECATED_VERSION(4, 4, "Use BrowserRun::saveUrl(const QUrl &, const QString &, QWidget *, const KParts::OpenUrlArguments &)")
0129     static void simpleSave(const QUrl &url, const QString &suggestedFileName, QWidget *window = nullptr);
0130 #endif
0131 
0132     /**
0133      * If kget integration is enabled, passes the url to kget.
0134      * Otherwise, asks the user for a destination url, and calls saveUrlUsingKIO.
0135      * @since 4.4
0136      */
0137     static void saveUrl(const QUrl &url, const QString &suggestedFileName, QWidget *window, const KParts::OpenUrlArguments &args);
0138 
0139     /**
0140      * Starts the KIO file copy job to download @p srcUrl into @p destUrl.
0141      * @since 4.4
0142      */
0143     static void saveUrlUsingKIO(const QUrl &srcUrl, const QUrl &destUrl, QWidget *window, const QMap<QString, QString> &metaData);
0144 
0145     static bool allowExecution(const QString &mimeType, const QUrl &url);
0146 
0147     static bool isTextExecutable(const QString &mimeType);
0148 
0149     /**
0150      * KDE webbrowsing kparts support error urls to display errors in-line in the browser component.
0151      * This helper method creates the error URL from its parameters.
0152      * @param error the KIO error code (or KIO::ERR_WORKER_DEFINED if not from KIO)
0153      * @param errorText the text of the error message
0154      * @param initialUrl the URL that we were trying to open (as a string, so that this can
0155      *                   support invalid URLs as well) (changed from QString to QUrl in KF5)
0156      * @since 4.6
0157      */
0158     static QUrl makeErrorUrl(int error, const QString &errorText, const QUrl &initialUrl);
0159 
0160 protected:
0161     /**
0162      * Reimplemented from KRun
0163      */
0164     void scanFile() override;
0165     /**
0166      * Reimplemented from KRun
0167      */
0168     void init() override;
0169     /**
0170      * Reimplemented from KRun
0171      */
0172     void handleError(KJob *job) override;
0173 
0174     /**
0175      * NotHandled means that foundMimeType should call KRun::foundMimeType,
0176      * i.e. launch an external app.
0177      */
0178     enum NonEmbeddableResult { Handled, NotHandled, Delayed };
0179 
0180     /**
0181      * Helper for foundMimeType: call this if the mimetype couldn't be embedded
0182      */
0183     NonEmbeddableResult handleNonEmbeddable(const QString &mimeType); // TODO KDE5: remove, and add =0 to the other overload
0184 
0185     /**
0186      * Helper for foundMimeType: call this if the mimetype couldn't be embedded
0187      * @param mimeType the mimetype found for the URL
0188      * @param pSelectedService Output variable: pointer to a KService::Ptr, which will be set
0189      *        to the service selected in the BrowserOpenOrSaveQuestion dialog, if any.
0190      *
0191      * How to handle this properly: if pSelectedService is non-zero, then the dialog will show
0192      * additional "open with" buttons. In your code, you should write:
0193      * @code
0194         if (selectedService) {
0195             KRun::setPreferredService(selectedService->desktopEntryName()); // not necessary since 4.9.3
0196             KRun::foundMimeType(mimeType);
0197         } else { // the user requested an open-with dialog
0198             KRun::displayOpenWithDialog(url(), m_window, false, suggestedFileName());
0199             setFinished(true);
0200         }
0201      * @endcode
0202      *
0203      * @since 4.5
0204      */
0205     NonEmbeddableResult handleNonEmbeddable(const QString &mimeType, KService::Ptr *pSelectedService);
0206 
0207 protected Q_SLOTS:
0208     void slotBrowserScanFinished(KJob *job);
0209     void slotBrowserMimetype(KIO::Job *job, const QString &type);
0210     void slotCopyToTempFileResult(KJob *job);
0211 
0212 private:
0213     void redirectToError(int error, const QString &errorText);
0214 
0215 private:
0216     std::unique_ptr<class BrowserRunPrivate> const d;
0217 };
0218 }
0219 
0220 #endif
0221 
0222 #endif