Falkon Develop
Cross-platform Qt-based web browser
kdeframeworksintegrationplugin.cpp
Go to the documentation of this file.
1/* ============================================================
2* KDEFrameworksIntegration - KDE support plugin for Falkon
3* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
20#include "pluginproxy.h"
21#include "browserwindow.h"
22#include "../config.h"
23#include "mainapplication.h"
24#include "autofill.h"
25#include "passwordmanager.h"
26#include "downloadmanager.h"
27#include "kioschemehandler.h"
28#include "webpage.h"
29#include "webview.h"
30#include "downloadkjob.h"
31#include "downloaditem.h"
32#include "settings.h"
33
34#include <KCrash>
35#include <KAboutData>
36#include <KProtocolInfo>
37#include <Purpose/AlternativesModel>
38#include <KUiServerJobTracker>
39
40#include <QWebEngineProfile>
41#include <QWebEngineUrlScheme>
42#include <QMenu>
43#include <QJsonArray>
44
46 : QObject()
47{
48}
49
50void KDEFrameworksIntegrationPlugin::init(InitState state, const QString &settingsPath)
51{
52 Q_UNUSED(state);
53 Q_UNUSED(settingsPath);
54
55 m_backend = new KWalletPasswordBackend;
56 mApp->autoFill()->passwordManager()->registerBackend(QSL("KWallet"), m_backend);
57
58 // Enable KWallet password backend inside KDE session
59 if (qgetenv("KDE_FULL_SESSION") == QByteArray("true")) {
60 mApp->autoFill()->passwordManager()->switchBackend(QSL("KWallet"));
61 }
62
63 m_jobTracker = new KUiServerJobTracker(this);
64
65 auto manager = mApp->downloadManager();
66 connect(manager, &DownloadManager::downloadAdded, this, [=](DownloadItem *item) {
67 auto job = new DownloadKJob(item->url(), item->path(), item->fileName(), this);
68 m_jobTracker->registerJob(job);
69 job->start();
70 job->updateDescription();
71
73 connect(manager, QOverload<>::of(&DownloadManager::downloadFinished), m_jobTracker, [=]() {
74 m_jobTracker->unregisterJob(job);
75 });
76 });
77
78
79 QStringList newSchemes;
80 const auto protocols = KProtocolInfo::protocols();
81 for (const QString &protocol : protocols) {
82 if (WebPage::internalSchemes().contains(protocol)) {
83 continue;
84 }
85 if (!QWebEngineUrlScheme::schemeByName(protocol.toUtf8()).name().isEmpty()) {
86 auto *handler = new KIOSchemeHandler(protocol, this);
87 m_kioSchemeHandlers.append(handler);
88 mApp->webProfile()->installUrlSchemeHandler(protocol.toUtf8(), handler);
90 }
91 else {
92 newSchemes.append(protocol);
93 qInfo() << QSL("KDEFrameworksIntegration: Custom scheme '%1' will be available after browser restart.").arg(protocol);
94 }
95 }
96
97 if (!newSchemes.isEmpty()) {
98 Settings settings;
99 settings.beginGroup(QSL("Web-Browser-Settings"));
100
101 QStringList allowedSchemes = settings.value(QSL("AllowedSchemes"), QStringList()).toStringList();
102 allowedSchemes.append(newSchemes);
103 allowedSchemes.removeDuplicates();
104 settings.setValue(QSL("AllowedSchemes"), allowedSchemes);
105
106 settings.endGroup();
107 }
108
109 m_sharePageMenu = new Purpose::Menu();
110 m_sharePageMenu->setTitle(tr("Share page"));
111 m_sharePageMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-share")));
112 m_sharePageMenu->model()->setInputData(QJsonObject{
113 { QStringLiteral("urls"), QJsonArray {QJsonValue(QSL("falkon"))} },
114 { QStringLiteral("title"), QJsonValue(QSL("falkon")) }
115 });
116 m_sharePageMenu->model()->setPluginType(QStringLiteral("ShareUrl"));
117
118 KAboutData aboutData(QSL("falkon"), QSL("Falkon"), QCoreApplication::applicationVersion());
119 KAboutData::setApplicationData(aboutData);
120
121 KCrash::initialize();
122 KCrash::setFlags(KCrash::KeepFDs);
123}
124
126{
127 mApp->autoFill()->passwordManager()->unregisterBackend(m_backend);
128 delete m_backend;
129 delete m_sharePageMenu;
130
131 for (KIOSchemeHandler *handler : std::as_const(m_kioSchemeHandlers)) {
132 mApp->webProfile()->removeUrlSchemeHandler(handler);
133 WebPage::removeSupportedScheme(handler->protocol());
134 delete handler;
135 }
136 m_kioSchemeHandlers.clear();
137}
138
140{
141 Q_UNUSED(r)
142
143 m_sharePageMenu->model()->setInputData(QJsonObject{
144 { QStringLiteral("urls"), QJsonArray {QJsonValue(view->url().toString())} },
145 { QStringLiteral("title"), QJsonValue(view->title()) }
146 });
147 m_sharePageMenu->reload();
148
149 menu->addAction(m_sharePageMenu->menuAction());
150}
151
153{
154 // Require the version that the plugin was built with
155 return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION));
156}
QString fileName() const
QString path() const
QUrl url() const
void progressChanged(double currSpeed, qint64 received, qint64 total)
void progress(double currSpeed, qint64 received, qint64 total)
void downloadFinished()
void downloadAdded(DownloadItem *item)
void populateWebViewMenu(QMenu *menu, WebView *view, const WebHitTestResult &r) override
void init(InitState state, const QString &settingsPath) override
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
static QStringList internalSchemes()
Definition: webpage.cpp:216
static void removeSupportedScheme(const QString &scheme)
Definition: webpage.cpp:249
static void addSupportedScheme(const QString &scheme)
Definition: webpage.cpp:240
QString title(bool allowEmpty=false) const
Definition: webview.cpp:107
#define mApp
FALKON_EXPORT const char * VERSION
Definition: qzcommon.cpp:26
State state
#define QSL(x)
Definition: qzcommon.h:40