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

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0004     SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef __kparts_browserextension_h__
0010 #define __kparts_browserextension_h__
0011 
0012 #include <kparts/browserarguments.h>
0013 #include <kparts/openurlarguments.h>
0014 #include <kparts/readonlypart.h>
0015 #include <kparts/windowargs.h>
0016 
0017 #include <memory>
0018 
0019 #include <QAction>
0020 #include <qplatformdefs.h> //mode_t
0021 
0022 template<class Key, class T>
0023 class QMap;
0024 template<typename T>
0025 class QList;
0026 
0027 class KFileItem;
0028 class KFileItemList;
0029 class QDataStream;
0030 class QPoint;
0031 
0032 namespace KParts
0033 {
0034 class BrowserInterface;
0035 
0036 /**
0037  * @class BrowserExtension browserextension.h <KParts/BrowserExtension>
0038  *
0039  * @short The Browser Extension is an extension (yes, no kidding) to
0040  * KParts::ReadOnlyPart, which allows a better integration of parts
0041  * with browsers (in particular Konqueror).
0042  *
0043  * Remember that ReadOnlyPart only has openUrl(QUrl) and a few arguments() but not much more.
0044  * For full-fledged browsing, we need much more than that, including
0045  * enabling/disabling of standard actions (print, copy, paste...),
0046  * allowing parts to save and restore their data into the back/forward history,
0047  * allowing parts to control the location bar URL, to requests URLs
0048  * to be opened by the hosting browser, etc.
0049  *
0050  * The part developer needs to define its own class derived from BrowserExtension,
0051  * to implement the virtual methods [and the standard-actions slots, see below].
0052  *
0053  * The way to associate the BrowserExtension with the part is to simply
0054  * create the BrowserExtension as a child of the part (in QObject's terms).
0055  * The hosting application will look for it automatically.
0056  *
0057  * Another aspect of the browser integration is that a set of standard
0058  * actions are provided by the browser, but implemented by the part
0059  * (for the actions it supports).
0060  *
0061  * The following standard actions are defined by the host of the view:
0062  *
0063  * [selection-dependent actions]
0064  * @li @p cut : Copy selected items to clipboard and store 'not cut' in clipboard.
0065  * @li @p copy : Copy selected items to clipboard and store 'cut' in clipboard.
0066  * @li @p paste : Paste clipboard into view URL.
0067  * @li @p pasteTo(const QUrl &) : Paste clipboard into given URL.
0068  * @li @p searchProvider : Lookup selected text at default search provider
0069  *
0070  * [normal actions]
0071  * @li None anymore.
0072  *
0073  *
0074  * The view defines a slot with the name of the action in order to implement the action.
0075  * The browser will detect the slot automatically and connect its action to it when
0076  * appropriate (i.e. when the view is active).
0077  *
0078  *
0079  * The selection-dependent actions are disabled by default and the view should
0080  * enable them when the selection changes, emitting enableAction().
0081  *
0082  * The normal actions do not depend on the selection.
0083  *
0084  * A special case is the configuration slots, not connected to any action directly.
0085  *
0086  * [configuration slot]
0087  * @li @p reparseConfiguration : Re-read configuration and apply it.
0088  * @li @p disableScrolling: no scrollbars
0089  */
0090 class KPARTS_EXPORT BrowserExtension : public QObject // TODO KF6, rename class and header to NavigationExtension. See https://phabricator.kde.org/T12224
0091 {
0092     Q_OBJECT
0093     Q_PROPERTY(bool urlDropHandling READ isURLDropHandlingEnabled WRITE setURLDropHandlingEnabled)
0094 public:
0095     /**
0096      * Constructor
0097      *
0098      * @param parent The KParts::ReadOnlyPart that this extension ... "extends" :)
0099      */
0100     explicit BrowserExtension(KParts::ReadOnlyPart *parent);
0101 
0102     ~BrowserExtension() override;
0103 
0104     /**
0105      * Set of flags passed via the popupMenu signal, to ask for some items in the popup menu.
0106      * @see PopupFlags
0107      */
0108     enum PopupFlag {
0109         DefaultPopupItems = 0x0000, /**< default value, no additional menu item */
0110 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 27)
0111         ShowNavigationItems KPARTS_ENUMERATOR_DEPRECATED_VERSION_BELATED(5, 82, 5, 27, "No effect anymore") =
0112             0x0001, /**< @deprecated since 5.27, no effect anymore */
0113         ShowUp KPARTS_ENUMERATOR_DEPRECATED_VERSION_BELATED(5, 82, 5, 27, "No effect anymore") = 0x0002, /**< @deprecated since 5.27, no effect anymore */
0114         ShowReload KPARTS_ENUMERATOR_DEPRECATED_VERSION_BELATED(5, 82, 5, 27, "No effect anymore") = 0x0004, /**< @deprecated since 5.27, no effect anymore */
0115 #endif
0116         ShowBookmark = 0x0008, /**< show "add to bookmarks" (usually not done on the local filesystem) */
0117         ShowCreateDirectory = 0x0010, /**<  show "create directory" (usually only done on the background of the view, or
0118                                        *                      in hierarchical views like directory trees, where the new dir would be visible) */
0119         ShowTextSelectionItems = 0x0020, /**< set when selecting text, for a popup that only contains text-related items. */
0120         NoDeletion = 0x0040, /**< deletion, trashing and renaming not allowed (e.g. parent dir not writeable).
0121                               *            (this is only needed if the protocol itself supports deletion, unlike e.g. HTTP) */
0122         IsLink = 0x0080, /**< show "Bookmark This Link" and other link-related actions (linkactions merging group) */
0123         ShowUrlOperations = 0x0100, /**< show copy, paste, as well as cut if NoDeletion is not set. */
0124         ShowProperties = 0x200, /**< show "Properties" action (usually done by directory views) */
0125     };
0126 
0127     /**
0128      * Stores a combination of #PopupFlag values.
0129      */
0130     Q_DECLARE_FLAGS(PopupFlags, PopupFlag)
0131 
0132     /**
0133      * Set the parameters to use for opening the next URL.
0134      * This is called by the "hosting" application, to pass parameters to the part.
0135      * @see BrowserArguments
0136      */
0137     virtual void setBrowserArguments(const BrowserArguments &args);
0138 
0139     /**
0140      * Retrieve the set of parameters to use for opening the URL
0141      * (this must be called from openUrl() in the part).
0142      * @see BrowserArguments
0143      */
0144     BrowserArguments browserArguments() const;
0145 
0146     /**
0147      * Returns the current x offset.
0148      *
0149      * For a scrollview, implement this using contentsX().
0150      */
0151     virtual int xOffset();
0152     /**
0153      * Returns the current y offset.
0154      *
0155      * For a scrollview, implement this using contentsY().
0156      */
0157     virtual int yOffset();
0158 
0159     /**
0160      * Used by the browser to save the current state of the view
0161      * (in order to restore it if going back in navigation).
0162      *
0163      * If you want to save additional properties, reimplement it
0164      * but don't forget to call the parent method (probably first).
0165      */
0166     virtual void saveState(QDataStream &stream);
0167 
0168     /**
0169      * Used by the browser to restore the view in the state
0170      * it was when we left it.
0171      *
0172      * If you saved additional properties, reimplement it
0173      * but don't forget to call the parent method (probably first).
0174      */
0175     virtual void restoreState(QDataStream &stream);
0176 
0177     /**
0178      * Returns whether url drop handling is enabled.
0179      * See setURLDropHandlingEnabled for more information about this
0180      * property.
0181      */
0182     bool isURLDropHandlingEnabled() const;
0183 
0184     /**
0185      * Enables or disables url drop handling. URL drop handling is a property
0186      * describing whether the hosting shell component is allowed to install an
0187      * event filter on the part's widget, to listen for URI drop events.
0188      * Set it to true if you are exporting a BrowserExtension implementation and
0189      * do not provide any special URI drop handling. If set to false you can be
0190      * sure to receive all those URI drop events unfiltered. Also note that the
0191      * implementation as of Konqueror installs the event filter only on the part's
0192      * widget itself, not on child widgets.
0193      */
0194     void setURLDropHandlingEnabled(bool enable);
0195 
0196     void setBrowserInterface(BrowserInterface *impl);
0197     BrowserInterface *browserInterface() const;
0198 
0199     /**
0200      * @return the status (enabled/disabled) of an action.
0201      * When the enableAction signal is emitted, the browserextension
0202      * stores the status of the action internally, so that it's possible
0203      * to query later for the status of the action, using this method.
0204      */
0205     bool isActionEnabled(const char *name) const;
0206 
0207     /**
0208      * @return the text of an action, if it was set explicitly by the part.
0209      * When the setActionText signal is emitted, the browserextension
0210      * stores the text of the action internally, so that it's possible
0211      * to query later for the text of the action, using this method.
0212      */
0213     QString actionText(const char *name) const;
0214 
0215     typedef QMap<QByteArray, QByteArray> ActionSlotMap;
0216 
0217 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 83)
0218     /**
0219      * Returns a map containing the action names as keys and corresponding
0220      * SLOT()'ified method names as data entries.
0221      *
0222      * This is very useful for
0223      * the host component, when connecting the own signals with the
0224      * extension's slots.
0225      * Basically you iterate over the map, check if the extension implements
0226      * the slot and connect to the slot using the data value of your map
0227      * iterator.
0228      * Checking if the extension implements a certain slot can be done like this:
0229      *
0230      * \code
0231      *   extension->metaObject()->slotNames().contains( actionName + "()" )
0232      * \endcode
0233      *
0234      * (note that @p actionName is the iterator's key value if already
0235      *  iterating over the action slot map, returned by this method)
0236      *
0237      * Connecting to the slot can be done like this:
0238      *
0239      * \code
0240      *   connect( yourObject, SIGNAL( yourSignal() ),
0241      *            extension, mapIterator.data() )
0242      * \endcode
0243      *
0244      * (where "mapIterator" is your ActionSlotMap iterator)
0245      */
0246     KPARTS_DEPRECATED_VERSION(5, 83, "Use actionSlotMapPtr instead")
0247     static ActionSlotMap actionSlotMap();
0248 #endif
0249 
0250     /**
0251      * @return a pointer to the static action-slot map. Preferred method to get it.
0252      * The map is created if it doesn't exist yet
0253      */
0254     static ActionSlotMap *actionSlotMapPtr(); // TODO KF6 Rename to actionSlotMap
0255 
0256     /**
0257      * Queries @p obj for a child object which inherits from this
0258      * BrowserExtension class. Convenience method.
0259      */
0260     static BrowserExtension *childObject(QObject *obj);
0261 
0262     /**
0263      * Asks the hosting browser to perform a paste (using openUrlRequestDelayed())
0264      */
0265     void pasteRequest();
0266 
0267     /**
0268      * Associates a list of actions with a predefined name known by the host's popupmenu:
0269      * "editactions" for actions related text editing,
0270      * "linkactions" for actions related to hyperlinks,
0271      * "partactions" for any other actions provided by the part
0272      */
0273     typedef QMap<QString, QList<QAction *>> ActionGroupMap;
0274 
0275 Q_SIGNALS:
0276     /**
0277      * Enables or disable a standard action held by the browser.
0278      *
0279      * See class documentation for the list of standard actions.
0280      */
0281     void enableAction(const char *name, bool enabled);
0282 
0283     /**
0284      * Change the text of a standard action held by the browser.
0285      * This can be used to change "Paste" into "Paste Image" for instance.
0286      *
0287      * See class documentation for the list of standard actions.
0288      */
0289     void setActionText(const char *name, const QString &text);
0290 
0291     /**
0292      * Asks the host (browser) to open @p url.
0293      * To set a reload, the x and y offsets, the service type etc., fill in the
0294      * appropriate fields in the @p args structure.
0295      * Hosts should not connect to this signal but to openUrlRequestDelayed().
0296      */
0297     void openUrlRequest(const QUrl &url,
0298                         const KParts::OpenUrlArguments &arguments = KParts::OpenUrlArguments(),
0299                         const KParts::BrowserArguments &browserArguments = KParts::BrowserArguments());
0300 
0301     /**
0302      * This signal is emitted when openUrlRequest() is called, after a 0-seconds timer.
0303      * This allows the caller to terminate what it's doing first, before (usually)
0304      * being destroyed. Parts should never use this signal, hosts should only connect
0305      * to this signal.
0306      */
0307     void openUrlRequestDelayed(const QUrl &url, const KParts::OpenUrlArguments &arguments, const KParts::BrowserArguments &browserArguments);
0308 
0309     /**
0310      * Tells the hosting browser that the part opened a new URL (which can be
0311      * queried via KParts::Part::url().
0312      *
0313      * This helps the browser to update/create an entry in the history.
0314      * The part may @em not emit this signal together with openUrlRequest().
0315      * Emit openUrlRequest() if you want the browser to handle a URL the user
0316      * asked to open (from within your part/document). This signal however is
0317      * useful if you want to handle URLs all yourself internally, while still
0318      * telling the hosting browser about new opened URLs, in order to provide
0319      * a proper history functionality to the user.
0320      * An example of usage is a html rendering component which wants to emit
0321      * this signal when a child frame document changed its URL.
0322      * Conclusion: you probably want to use openUrlRequest() instead.
0323      */
0324     void openUrlNotify();
0325 
0326     /**
0327      * Updates the URL shown in the browser's location bar to @p url.
0328      */
0329     void setLocationBarUrl(const QString &url);
0330 
0331     /**
0332      * Sets the URL of an icon for the currently displayed page.
0333      */
0334     void setIconUrl(const QUrl &url);
0335 
0336     /**
0337      * Asks the hosting browser to open a new window for the given @p url
0338      * and return a reference to the content part.
0339      *
0340      * @p arguments is optional additional information about how to open the url,
0341      * @see KParts::OpenUrlArguments
0342      *
0343      * @p browserArguments is optional additional information for web browsers,
0344      * @see KParts::BrowserArguments
0345      *
0346      * The request for a pointer to the part is only fulfilled/processed
0347      * if the mimeType is set in the @p browserArguments.
0348      * (otherwise the request cannot be processed synchronously).
0349      */
0350     void createNewWindow(const QUrl &url,
0351                          const KParts::OpenUrlArguments &arguments = KParts::OpenUrlArguments(),
0352                          const KParts::BrowserArguments &browserArguments = KParts::BrowserArguments(),
0353                          const KParts::WindowArgs &windowArgs = KParts::WindowArgs(),
0354                          KParts::ReadOnlyPart **part = nullptr); // TODO consider moving to BrowserHostExtension?
0355 
0356     /**
0357      * Since the part emits the jobid in the started() signal,
0358      * progress information is automatically displayed.
0359      *
0360      * However, if you don't use a KIO::Job in the part,
0361      * you can use loadingProgress() and speedProgress()
0362      * to display progress information.
0363      */
0364     void loadingProgress(int percent);
0365     /**
0366      * @see loadingProgress
0367      */
0368     void speedProgress(int bytesPerSecond);
0369 
0370     void infoMessage(const QString &);
0371 
0372     /**
0373      * Emit this to make the browser show a standard popup menu for the files @p items.
0374      *
0375      * @param global global coordinates where the popup should be shown
0376      * @param items list of file items which the popup applies to
0377      * @param args OpenUrlArguments, mostly for metadata here
0378      * @param browserArguments BrowserArguments, mostly for referrer
0379      * @param flags enables/disables certain builtin actions in the popupmenu
0380      * @param actionGroups named groups of actions which should be inserted into the popup, see ActionGroupMap
0381      */
0382     void popupMenu(const QPoint &global,
0383                    const KFileItemList &items,
0384                    const KParts::OpenUrlArguments &args = KParts::OpenUrlArguments(),
0385                    const KParts::BrowserArguments &browserArguments = KParts::BrowserArguments(),
0386                    KParts::BrowserExtension::PopupFlags flags = KParts::BrowserExtension::DefaultPopupItems,
0387                    const KParts::BrowserExtension::ActionGroupMap &actionGroups = ActionGroupMap());
0388 
0389     /**
0390      * Emit this to make the browser show a standard popup menu for the given @p url.
0391      *
0392      * Give as much information about this URL as possible,
0393      * like @p args.mimeType and the file type @p mode
0394      *
0395      * @param global global coordinates where the popup should be shown
0396      * @param url the URL this popup applies to
0397      * @param mode the file type of the url (S_IFREG, S_IFDIR...)
0398      * @param args OpenUrlArguments, set the mimetype of the URL using setMimeType()
0399      * @param browserArguments BrowserArguments, mostly for referrer
0400      * @param flags enables/disables certain builtin actions in the popupmenu
0401      * @param actionGroups named groups of actions which should be inserted into the popup, see ActionGroupMap
0402      */
0403     void popupMenu(const QPoint &global,
0404                    const QUrl &url,
0405                    mode_t mode = static_cast<mode_t>(-1),
0406                    const KParts::OpenUrlArguments &args = KParts::OpenUrlArguments(),
0407                    const KParts::BrowserArguments &browserArguments = KParts::BrowserArguments(),
0408                    KParts::BrowserExtension::PopupFlags flags = KParts::BrowserExtension::DefaultPopupItems,
0409                    const KParts::BrowserExtension::ActionGroupMap &actionGroups = ActionGroupMap());
0410 
0411     /**
0412      * Inform the hosting application about the current selection.
0413      * Used when a set of files/URLs is selected (with full information
0414      * about those URLs, including size, permissions etc.)
0415      */
0416     void selectionInfo(const KFileItemList &items);
0417 
0418 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 88)
0419     /**
0420      * Inform the hosting application about the current selection.
0421      * Used when some text is selected.
0422      * @deprecated Since 5.88, deprecated for lack of usage
0423      */
0424     KPARTS_DEPRECATED_VERSION(5, 88, "Deprecated for lack of usage")
0425     void selectionInfo(const QString &text);
0426 #endif
0427 
0428 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 88)
0429     /**
0430      * Inform the hosting application about the current selection.
0431      * Used when a set of URLs is selected.
0432      * @deprecated Since 5.88, use selectionInfo(KFileItemList) instead
0433      */
0434     KPARTS_DEPRECATED_VERSION(5, 88, "Use selectionInfo(KFileItemList) instead")
0435     void selectionInfo(const QList<QUrl> &urls);
0436 #endif
0437 
0438     /**
0439      * Inform the hosting application that the user moved the mouse over an item.
0440      * Used when the mouse is on an URL.
0441      */
0442     void mouseOverInfo(const KFileItem &item);
0443 
0444     /**
0445      * Ask the hosting application to add a new HTML (aka Mozilla/Netscape)
0446      * SideBar entry.
0447      */
0448     void addWebSideBar(const QUrl &url, const QString &name);
0449 
0450     /**
0451      * Ask the hosting application to move the top level widget.
0452      */
0453     void moveTopLevelWidget(int x, int y);
0454 
0455     /**
0456      * Ask the hosting application to resize the top level widget.
0457      */
0458     void resizeTopLevelWidget(int w, int h);
0459 
0460     /**
0461      * Ask the hosting application to focus @p part.
0462      */
0463     void requestFocus(KParts::ReadOnlyPart *part);
0464 
0465     /**
0466      * Tell the host (browser) about security state of current page
0467      * enum PageSecurity { NotCrypted, Encrypted, Mixed };
0468      */
0469     void setPageSecurity(int);
0470 
0471     /**
0472      * Inform the host about items that have been removed.
0473      */
0474     void itemsRemoved(const KFileItemList &items);
0475 
0476 private Q_SLOTS:
0477     KPARTS_NO_EXPORT void slotCompleted();
0478     KPARTS_NO_EXPORT void slotOpenUrlRequest(const QUrl &url,
0479                                              const KParts::OpenUrlArguments &arguments = KParts::OpenUrlArguments(),
0480                                              const KParts::BrowserArguments &browserArguments = KParts::BrowserArguments());
0481 
0482     KPARTS_NO_EXPORT void slotEmitOpenUrlRequestDelayed();
0483     KPARTS_NO_EXPORT void slotEnableAction(const char *, bool);
0484     KPARTS_NO_EXPORT void slotSetActionText(const char *, const QString &);
0485 
0486 public:
0487     typedef QMap<QByteArray, int> ActionNumberMap;
0488 
0489 private:
0490     std::unique_ptr<class BrowserExtensionPrivate> const d;
0491 };
0492 
0493 Q_DECLARE_OPERATORS_FOR_FLAGS(BrowserExtension::PopupFlags)
0494 
0495 }
0496 
0497 #endif