File indexing completed on 2024-05-19 05:01:21

0001 /*
0002     This file is part of the KDE project.
0003 
0004     SPDX-FileCopyrightText: 2007 Trolltech ASA
0005     SPDX-FileCopyrightText: 2008 Urs Wolfer <uwolfer @ kde.org>
0006     SPDX-FileCopyrightText: 2009 Dawit Alemayehu <adawit@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.1-or-later
0009 */
0010 #ifndef WEBENGINEPART_H
0011 #define WEBENGINEPART_H
0012 
0013 #include "kwebenginepartlib_export.h"
0014 
0015 #include "browserextension.h"
0016 
0017 #include <QWebEnginePage>
0018 
0019 #include <kparts_version.h>
0020 #include <KParts/ReadOnlyPart>
0021 #include <QUrl>
0022 #include <QWebEngineScript>
0023 
0024 namespace KParts {
0025 //TODO KF6: when removing compatibility with KF5, uncomment the line below
0026   // class NavigationExtension;
0027   class StatusBarExtension;
0028 }
0029 
0030 class QWebEngineView;
0031 class QWebEngineProfile;
0032 class WebEngineView;
0033 class WebEnginePage;
0034 class SearchBar;
0035 class PasswordBar;
0036 class FeaturePermissionBar;
0037 class KUrlLabel;
0038 class WebEngineNavigationExtension;
0039 class WebEngineWallet;
0040 class KPluginMetaData;
0041 class WebEnginePartControls;
0042 class WebEngineDownloaderExtension;
0043 
0044 /**
0045  * A KPart wrapper for the QtWebEngine's browser rendering engine.
0046  *
0047  * This class attempts to provide the same type of integration into KPart
0048  * plugin applications, such as Konqueror, in much the same way as KHTML.
0049  *
0050  * Unlink the KHTML part however, access into the internals of the rendering
0051  * engine are provided through existing QtWebEngine class ; @see QWebEngineView.
0052  *
0053  */
0054 class KWEBENGINEPARTLIB_EXPORT WebEnginePart : public KParts::ReadOnlyPart
0055 {
0056     Q_OBJECT
0057     Q_PROPERTY( bool modified READ isModified )
0058 public:
0059     explicit WebEnginePart(QWidget* parentWidget, QObject* parent,
0060                          const KPluginMetaData& metaData,
0061                          const QByteArray& cachedHistory = QByteArray(),
0062                          const QStringList& = QStringList());
0063     ~WebEnginePart() override;
0064 
0065     /**
0066      * Re-implemented for internal reasons. API remains unaffected.
0067      *
0068      * @see KParts::ReadOnlyPart::openUrl
0069      */
0070     bool openUrl(const QUrl &) override;
0071 
0072     /**
0073      * Actually loads the URL in the page
0074      */
0075     void loadUrl(const QUrl & _u);
0076 
0077     /**
0078      * Re-implemented for internal reasons. API remains unaffected.
0079      *
0080      * @see KParts::ReadOnlyPart::closeUrl
0081      */
0082     bool closeUrl() override;
0083 
0084     /**
0085      * Returns a pointer to the render widget used to display a web page.
0086      *
0087      * @see QWebEngineView.
0088      */
0089     QWebEngineView *view() const;
0090 
0091     /**
0092      * Checks whether the page contains unsubmitted form changes.
0093      *
0094      * @return @p true if form changes exist.
0095      */
0096     bool isModified() const;
0097 
0098     class SpellCheckerManager* spellCheckerManager();
0099 
0100     class WebEnginePartDownloadManager* downloadManager();
0101 
0102     /**
0103      * Connects the appropriate signals from the given page to the slots
0104      * in this class.
0105      */
0106     void connectWebEnginePageSignals(WebEnginePage* page);
0107 
0108     void slotShowFeaturePermissionBar(const QUrl &origin, QWebEnginePage::Feature);
0109 
0110     void setWallet(WebEngineWallet* wallet);
0111 
0112     WebEngineWallet* wallet() const;
0113 
0114 #if QT_VERSION_MAJOR < 6
0115     KParts::NavigationExtension* navigationExtension() const;
0116 #endif
0117 
0118     /**
0119      * @brief Changes the page object associated with the part
0120      *
0121      * This currently includes:
0122      * - calling WebEngineView::setPage
0123      * - making the part a child of the view
0124      * - connecting signals between part and page
0125      * - injecting scripts into the page
0126      * @param newPage the new page
0127      */
0128     void setPage(WebEnginePage *newPage);
0129 
0130     WebEnginePage* page();
0131     const WebEnginePage* page() const;
0132     BrowserExtension *browserExtension() const;
0133 
0134     QWebEngineProfile *profile() const;
0135 
0136     bool isWebEnginePart() const {return true;}
0137 
0138     Q_PROPERTY(bool isWebEnginePart READ isWebEnginePart)
0139 
0140     WebEngineDownloaderExtension* downloader() const {return m_downloader;}
0141 
0142 public Q_SLOTS:
0143     void exitFullScreen();
0144     void setInspectedPart(KParts::ReadOnlyPart *part);
0145 
0146 protected:
0147     /**
0148      * Re-implemented for internal reasons. API remains unaffected.
0149      *
0150      * @see KParts::ReadOnlyPart::guiActivateEvent
0151      */
0152     void guiActivateEvent(KParts::GUIActivateEvent *) override;
0153 
0154     /**
0155      * Re-implemented for internal reasons. API remains unaffected.
0156      *
0157      * @see KParts::ReadOnlyPart::openFile
0158      */
0159     bool openFile() override;
0160 
0161 private Q_SLOTS:
0162     void slotShowSecurity();
0163     void slotShowSearchBar();
0164     void slotLoadStarted();
0165     void slotLoadAborted(const QUrl &);
0166     void slotLoadFinished(bool);
0167 
0168     void slotSearchForText(const QString &text, bool backward);
0169     void slotLinkHovered(const QString &);
0170     //void slotSaveFrameState(QWebFrame *frame, QWebHistoryItem *item);
0171     //void slotRestoreFrameState(QWebFrame *frame);
0172     void slotLinkMiddleOrCtrlClicked(const QUrl&);
0173     void slotSelectionClipboardUrlPasted(const QUrl&, const QString&);
0174 
0175     void slotUrlChanged(const QUrl &);
0176     void resetWallet();
0177     void slotShowWalletMenu();
0178     void slotLaunchWalletManager();
0179     void togglePasswordStorableState(bool on);
0180     void slotRemoveCachedPasswords();
0181     void slotSetTextEncoding(const QString &codecName);
0182     void slotSetStatusBarText(const QString& text);
0183     void slotWindowCloseRequested();
0184     void slotSaveFormDataRequested(const QString &, const QUrl &);
0185     void slotSaveFormDataDone();
0186     void slotWalletSavedForms(const QUrl &url, bool success);
0187     void slotFillFormRequestCompleted(bool);
0188 
0189     void slotFeaturePolicyChosen(FeaturePermissionBar *bar, QWebEnginePage::Feature feature, QWebEnginePage::PermissionPolicy policy);
0190     void deleteFeaturePermissionBar(FeaturePermissionBar *bar);
0191 
0192     void updateWalletStatusBarIcon();
0193     void walletFinishedFormDetection(const QUrl &url, bool found, bool autoFillableFound);
0194     void updateWalletActions();
0195     void reloadAfterUAChange(const QString &);
0196 
0197 private:
0198     static void initWebEngineUrlSchemes();
0199 
0200     void deleteStatusBarWalletLabel();
0201 
0202     struct WalletData{
0203         enum Member{HasForms, HasAutofillableForms, HasCachedData};
0204         bool hasForms;
0205         bool hasAutoFillableForms;
0206         bool hasCachedData;
0207     };
0208     //Always use the following functions to change the values of m_walletData, as they automatically update the UI
0209     void updateWalletData(WalletData::Member which, bool status);
0210     void updateWalletData(std::initializer_list<bool> data);
0211 
0212     void attemptInstallKIOSchemeHandler(const QUrl &url);
0213 
0214     void initActions();
0215     void createWalletActions();
0216     void updateActions();
0217 
0218     bool m_emitOpenUrlNotify;
0219 
0220     WalletData m_walletData;
0221     bool m_doLoadFinishedActions;
0222     KUrlLabel* m_statusBarWalletLabel;
0223     SearchBar* m_searchBar;
0224     PasswordBar* m_passwordBar;
0225     QVector<FeaturePermissionBar*> m_permissionBars;
0226     WebEngineNavigationExtension* m_browserExtension;
0227     KParts::StatusBarExtension* m_statusBarExtension;
0228     WebEngineView* m_webView;
0229     WebEngineWallet* m_wallet;
0230     WebEngineDownloaderExtension* m_downloader;
0231 };
0232 
0233 #endif // WEBENGINEPART_H