Commit 80064d09 authored by Stefano Crocco's avatar Stefano Crocco Committed by David Faure
Browse files

Move all one-time WebEnginePart initialiation in a single place

parent dfdf724c
......@@ -33,6 +33,7 @@ set(kwebenginepartlib_LIB_SRCS
ui/credentialsdetailswidget.cpp
webengineurlrequestinterceptor.cpp
spellcheckermanager.cpp
webenginepartcontrols.cpp
)
ki18n_wrap_ui(kwebenginepartlib_LIB_SRCS webenginecustomizecacheablefieldsdlg.ui ui/credentialsdetailswidget.ui)
......
......@@ -42,7 +42,7 @@
#endif
SpellCheckerManager::SpellCheckerManager(): QObject()
SpellCheckerManager::SpellCheckerManager(QWebEngineProfile *profile, QObject *parent): QObject(parent), m_profile(profile)
{
m_dictionaryDir = QString(WEBENGINEPART_DICTIONARY_DIR);
connect(KonqSpellCheckingConfigurationDispatcher::self(), &KonqSpellCheckingConfigurationDispatcher::spellCheckingConfigurationChanged,
......@@ -53,12 +53,6 @@ SpellCheckerManager::~SpellCheckerManager()
{
}
SpellCheckerManager * SpellCheckerManager::self()
{
static SpellCheckerManager s_self;
return &s_self;
}
void SpellCheckerManager::detectDictionaries()
{
if (m_dictionaryDir.isEmpty()) {
......@@ -86,9 +80,8 @@ void SpellCheckerManager::detectDictionaries()
void SpellCheckerManager::updateConfiguration(bool spellCheckingEnabled)
{
detectDictionaries();
QWebEngineProfile * prof = QWebEngineProfile::defaultProfile();
prof->setSpellCheckEnabled(spellCheckingEnabled);
prof->setSpellCheckLanguages(m_enabledDicts);
m_profile->setSpellCheckEnabled(spellCheckingEnabled);
m_profile->setSpellCheckLanguages(m_enabledDicts);
}
void SpellCheckerManager::setup()
......@@ -107,8 +100,7 @@ QMenu * SpellCheckerManager::spellCheckingMenu(const QStringList &suggestions, K
QMenu *menu = new QMenu();
menu->setTitle(i18n("Spelling"));
QWebEngineProfile *prof = QWebEngineProfile::defaultProfile();
bool spellingEnabled = prof->isSpellCheckEnabled();
bool spellingEnabled = m_profile->isSpellCheckEnabled();
QAction *a = new QAction(i18n("Spell Checking Enabled"), coll);
a->setCheckable(true);
......@@ -130,7 +122,7 @@ QMenu * SpellCheckerManager::spellCheckingMenu(const QStringList &suggestions, K
QMenu *langs = new QMenu(menu);
langs->setTitle(i18n("&Languages"));
menu->addMenu(langs);
QStringList enabledLangs = prof->spellCheckLanguages();
QStringList enabledLangs = m_profile->spellCheckLanguages();
for (auto it = m_dicts.constBegin(); it != m_dicts.constEnd(); ++it) {
a = new QAction(it.value(), coll);
a->setCheckable(true);
......@@ -146,23 +138,21 @@ QMenu * SpellCheckerManager::spellCheckingMenu(const QStringList &suggestions, K
void SpellCheckerManager::addLanguage(const QString& lang)
{
QWebEngineProfile *prof = QWebEngineProfile::defaultProfile();
QStringList langs = prof->spellCheckLanguages();
QStringList langs = m_profile->spellCheckLanguages();
if (!langs.contains(lang)) {
langs << lang;
prof->setSpellCheckLanguages(langs);
m_profile->setSpellCheckLanguages(langs);
}
}
void SpellCheckerManager::removeLanguage(const QString& lang)
{
QWebEngineProfile *prof = QWebEngineProfile::defaultProfile();
QStringList langs = prof->spellCheckLanguages();
QStringList langs = m_profile->spellCheckLanguages();
langs.removeAll(lang);
prof->setSpellCheckLanguages(langs);
m_profile->setSpellCheckLanguages(langs);
}
void SpellCheckerManager::spellCheckingToggled(bool on)
{
QWebEngineProfile::defaultProfile()->setSpellCheckEnabled(on);
m_profile->setSpellCheckEnabled(on);
}
......@@ -33,17 +33,16 @@ class QMenu;
class KActionCollection;
class QWidget;
class WebEnginePage;
class QWebEngineProfile;
class SpellCheckerManager : public QObject
{
Q_OBJECT
public:
SpellCheckerManager();
SpellCheckerManager(QWebEngineProfile *profile, QObject *parent=nullptr);
~SpellCheckerManager();
static SpellCheckerManager* self();
QMenu *spellCheckingMenu(const QStringList &suggestions, KActionCollection *coll, WebEnginePage *page);
void setup();
......@@ -65,6 +64,7 @@ private:
QMap<QString, QString> m_dicts;
QStringList m_enabledDicts;
Sonnet::Speller m_speller;
QWebEngineProfile *m_profile;
};
#endif // SPELLCHECKERMANAGER_H
......@@ -98,7 +98,7 @@ WebEnginePage::WebEnginePage(WebEnginePart *part, QWidget *parent)
this->profile()->setHttpUserAgent(this->profile()->httpUserAgent() + " Konqueror (WebEnginePart)");
}
WebEnginePartDownloadManager::instance()->addPage(this);
m_part->downloadManager()->addPage(this);
m_wallet = new WebEngineWallet(this, parent ? parent->window()->winId() : 0);
}
......
......@@ -43,6 +43,8 @@
#include "webenginepartcookiejar.h"
#include "webengineurlrequestinterceptor.h"
#include "spellcheckermanager.h"
#include "webenginepartdownloadmanager.h"
#include "webenginepartcontrols.h"
#include "ui/searchbar.h"
#include "ui/passwordbar.h"
......@@ -75,7 +77,6 @@
#include <KSharedConfig>
#include <KSslInfoDialog>
#include <KProtocolManager>
#include <KProtocolInfo>
#include <KParts/PartActivateEvent>
#include <KParts/BrowserInterface>
#include <KIO/ApplicationLauncherJob>
......@@ -88,34 +89,12 @@
#include <QMenu>
#include <QStatusBar>
#include <QWebEngineScriptCollection>
#include <QWebEngineUrlScheme>
#include <QWebEngineScript>
#include <QDir>
#include "utils.h"
#include <kio_version.h>
void WebEnginePart::initWebEngineUrlSchemes()
{
static bool needToInitUrlSchemes = true;
if (needToInitUrlSchemes) {
needToInitUrlSchemes = false;
QVector<QByteArray> localSchemes = {"error", "konq", "tar"};
const QStringList protocols = KProtocolInfo::protocols();
for(const QString &prot : protocols){
if (KProtocolInfo::defaultMimetype(prot) == "text/html") {
localSchemes.append(QByteArray(prot.toLatin1()));
}
}
for (const QByteArray &name : qAsConst(localSchemes)){
QWebEngineUrlScheme scheme(name);
scheme.setFlags(QWebEngineUrlScheme::LocalScheme|QWebEngineUrlScheme::LocalAccessAllowed);
scheme.setSyntax(QWebEngineUrlScheme::Syntax::Path);
QWebEngineUrlScheme::registerScheme(scheme);
}
}
}
static QWebEngineScript detectRefreshScript() {
static QWebEngineScript s_detectRefreshScript;
if (s_detectRefreshScript.isNull()) {
......@@ -142,19 +121,9 @@ WebEnginePart::WebEnginePart(QWidget *parentWidget, QObject *parent,
m_passwordBar(nullptr),
m_wallet(nullptr)
{
initWebEngineUrlSchemes();
QWebEngineProfile *prof = QWebEngineProfile::defaultProfile();
if (!prof->urlSchemeHandler("error")) {
prof->installUrlSchemeHandler("error", new WebEnginePartErrorSchemeHandler(prof));
prof->installUrlSchemeHandler("konq", new KonqUrlSchemeHandler(prof));
prof->installUrlSchemeHandler("help", new WebEnginePartKIOHandler(prof));
prof->installUrlSchemeHandler("tar", new WebEnginePartKIOHandler(prof));
if (!WebEnginePartControls::self()->isReady()) {
WebEnginePartControls::self()->setup(QWebEngineProfile::defaultProfile());
}
prof->setUrlRequestInterceptor(new WebEngineUrlRequestInterceptor(this));
static WebEnginePartCookieJar s_cookieJar(prof, nullptr);
//It's safe calling this multiple times as it does nothing after the first call
SpellCheckerManager::self()->setup();
#if KPARTS_VERSION >= QT_VERSION_CHECK(5, 77, 0)
setMetaData(metaData);
......@@ -271,6 +240,16 @@ const WebEnginePage* WebEnginePart::page() const
return nullptr;
}
WebEnginePartDownloadManager * WebEnginePart::downloadManager()
{
return WebEnginePartControls::self()->downloadManager();
}
SpellCheckerManager * WebEnginePart::spellCheckerManager()
{
return WebEnginePartControls::self()->spellCheckerManager();
}
void WebEnginePart::initActions()
{
KStandardAction::create(KStandardAction::SaveAs, m_browserExtension, &WebEngineBrowserExtension::slotSaveDocument,
......@@ -1153,8 +1132,3 @@ void WebEnginePart::updateWalletData(std::initializer_list<bool> data)
updateWalletActions();
updateWalletStatusBarIcon();
}
void WebEnginePart::updateSpellCheckingConfiguration(bool enabled)
{
SpellCheckerManager::self()->updateConfiguration(enabled);
}
......@@ -45,6 +45,7 @@ class KUrlLabel;
class WebEngineBrowserExtension;
class WebEngineWallet;
class KPluginMetaData;
class WebEnginePartControls;
/**
* A KPart wrapper for the QtWebEngine's browser rendering engine.
......@@ -97,6 +98,10 @@ public:
*/
bool isModified() const;
class SpellCheckerManager* spellCheckerManager();
class WebEnginePartDownloadManager* downloadManager();
/**
* Connects the appropriate signals from the given page to the slots
* in this class.
......@@ -109,7 +114,6 @@ public:
public Q_SLOTS:
void exitFullScreen();
void updateSpellCheckingConfiguration(bool enabled);
protected:
/**
......@@ -165,6 +169,7 @@ private:
WebEnginePage* page();
const WebEnginePage* page() const;
static void initWebEngineUrlSchemes();
void deleteStatusBarWalletLabel();
struct WalletData{
......
/*
* This file is part of the KDE project.
*
* Copyright 2021 Stefano Crocco <posta@stefanocrocco.it>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "webenginepartcontrols.h"
#include "webengineparterrorschemehandler.h"
#include "webenginepartcookiejar.h"
#include "webengineurlrequestinterceptor.h"
#include "webenginepartkiohandler.h"
#include "about/konq_aboutpage.h"
#include "webenginepartcookiejar.h"
#include "spellcheckermanager.h"
#include "webenginepartdownloadmanager.h"
#include <KProtocolInfo>
#include <QWebEngineProfile>
#include <QWebEngineUrlScheme>
WebEnginePartControls::WebEnginePartControls(): QObject(),
m_profile(nullptr), m_cookieJar(nullptr), m_spellCheckerManager(nullptr), m_downloadManager(nullptr)
{
QVector<QByteArray> localSchemes = {"error", "konq", "tar"};
const QStringList protocols = KProtocolInfo::protocols();
for(const QString &prot : protocols){
if (KProtocolInfo::defaultMimetype(prot) == "text/html") {
localSchemes.append(QByteArray(prot.toLatin1()));
}
}
for (const QByteArray &name : qAsConst(localSchemes)){
QWebEngineUrlScheme scheme(name);
scheme.setFlags(QWebEngineUrlScheme::LocalScheme|QWebEngineUrlScheme::LocalAccessAllowed);
scheme.setSyntax(QWebEngineUrlScheme::Syntax::Path);
QWebEngineUrlScheme::registerScheme(scheme);
}
}
WebEnginePartControls::~WebEnginePartControls()
{
}
WebEnginePartControls* WebEnginePartControls::self()
{
static WebEnginePartControls s_self;
return &s_self;
}
bool WebEnginePartControls::isReady() const
{
return m_profile;
}
void WebEnginePartControls::setup(QWebEngineProfile* profile)
{
if (!profile || isReady()) {
return;
}
m_profile = profile;
m_profile->installUrlSchemeHandler("error", new WebEnginePartErrorSchemeHandler(m_profile));
m_profile->installUrlSchemeHandler("konq", new KonqUrlSchemeHandler(m_profile));
m_profile->installUrlSchemeHandler("help", new WebEnginePartKIOHandler(m_profile));
m_profile->installUrlSchemeHandler("tar", new WebEnginePartKIOHandler(m_profile));
m_profile->setUrlRequestInterceptor(new WebEngineUrlRequestInterceptor(this));
m_cookieJar = new WebEnginePartCookieJar(profile, this);
m_spellCheckerManager = new SpellCheckerManager(profile, this);
m_downloadManager= new WebEnginePartDownloadManager(profile, this);
}
WebEnginePartDownloadManager* WebEnginePartControls::downloadManager() const
{
return m_downloadManager;
}
SpellCheckerManager* WebEnginePartControls::spellCheckerManager() const
{
return m_spellCheckerManager;
}
/*
* This file is part of the KDE project.
*
* Copyright 2021 Stefano Crocco <posta@stefanocrocco.it>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WEBENGINEPARTCONTROLS_H
#define WEBENGINEPARTCONTROLS_H
#include <QObject>
class QWebEngineProfile;
class WebEnginePartCookieJar;
class SpellCheckerManager;
class WebEnginePartDownloadManager;
class WebEnginePartControls : public QObject
{
Q_OBJECT
public:
static WebEnginePartControls *self();
~WebEnginePartControls();
bool isReady() const;
void setup(QWebEngineProfile *profile);
SpellCheckerManager* spellCheckerManager() const;
WebEnginePartDownloadManager* downloadManager() const;
private:
WebEnginePartControls();
QWebEngineProfile *m_profile;
WebEnginePartCookieJar *m_cookieJar;
SpellCheckerManager *m_spellCheckerManager;
WebEnginePartDownloadManager *m_downloadManager;
};
#endif // WEBENGINEPARTCONTROLS_H
......@@ -41,22 +41,16 @@
#include <KFileUtils>
#include <KIO/JobUiDelegate>
WebEnginePartDownloadManager::WebEnginePartDownloadManager()
: QObject(), m_tempDownloadDir(QDir(QDir::tempPath()).filePath("WebEnginePartDownloadManager"))
WebEnginePartDownloadManager::WebEnginePartDownloadManager(QWebEngineProfile *profile, QObject *parent)
: QObject(parent), m_tempDownloadDir(QDir(QDir::tempPath()).filePath("WebEnginePartDownloadManager"))
{
connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePartDownloadManager::performDownload);
connect(profile, &QWebEngineProfile::downloadRequested, this, &WebEnginePartDownloadManager::performDownload);
}
WebEnginePartDownloadManager::~WebEnginePartDownloadManager()
{
}
WebEnginePartDownloadManager * WebEnginePartDownloadManager::instance()
{
static WebEnginePartDownloadManager inst;
return &inst;
}
void WebEnginePartDownloadManager::addPage(WebEnginePage* page)
{
if (!m_pages.contains(page)) {
......
......@@ -30,18 +30,18 @@
#include <KJob>
class WebEnginePage;
class QWebEngineProfile;
class WebEnginePartDownloadManager : public QObject
{
Q_OBJECT
public:
static WebEnginePartDownloadManager* instance();
~WebEnginePartDownloadManager() override;
WebEnginePartDownloadManager(QWebEngineProfile *profile, QObject *parent = nullptr);
private:
WebEnginePartDownloadManager();
void downloadBlob(QWebEngineDownloadItem *it);
QString generateBlobTempFileName(QString const &suggestedName, const QString &ext) const;
......@@ -84,7 +84,6 @@ private slots:
void downloadFinished();
private:
QWebEngineDownloadItem *m_downloadItem;
QDateTime m_startTime;
};
......
......@@ -335,7 +335,8 @@ void WebEngineView::editableContentActionPopupMenu(KParts::BrowserExtension::Act
editableContentActions.append(pageAction(QWebEnginePage::InspectElement));
m_spellCheckMenu = SpellCheckerManager::self()->spellCheckingMenu(page()->contextMenuData().spellCheckerSuggestions(), m_actionCollection, dynamic_cast<WebEnginePage*>(page()));
SpellCheckerManager *manager = m_part->spellCheckerManager();
m_spellCheckMenu = manager->spellCheckingMenu(page()->contextMenuData().spellCheckerSuggestions(), m_actionCollection, dynamic_cast<WebEnginePage*>(page()));
if (m_spellCheckMenu) {
editableContentActions.append(m_spellCheckMenu->menuAction());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment