File indexing completed on 2024-05-19 04:59:17
0001 /* ============================================================ 0002 * KDEFrameworksIntegration - KDE support plugin for Falkon 0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "kdeframeworksintegrationplugin.h" 0019 #include "kwalletpasswordbackend.h" 0020 #include "pluginproxy.h" 0021 #include "browserwindow.h" 0022 #include "../config.h" 0023 #include "mainapplication.h" 0024 #include "autofill.h" 0025 #include "passwordmanager.h" 0026 #include "downloadmanager.h" 0027 #include "kioschemehandler.h" 0028 #include "webpage.h" 0029 #include "webview.h" 0030 #include "downloadkjob.h" 0031 #include "downloaditem.h" 0032 #include "settings.h" 0033 0034 #include <KCrash> 0035 #include <KAboutData> 0036 #include <KProtocolInfo> 0037 #include <Purpose/AlternativesModel> 0038 #include <KUiServerJobTracker> 0039 0040 #include <QWebEngineProfile> 0041 #include <QWebEngineUrlScheme> 0042 #include <QMenu> 0043 #include <QJsonArray> 0044 0045 KDEFrameworksIntegrationPlugin::KDEFrameworksIntegrationPlugin() 0046 : QObject() 0047 { 0048 } 0049 0050 void KDEFrameworksIntegrationPlugin::init(InitState state, const QString &settingsPath) 0051 { 0052 Q_UNUSED(state); 0053 Q_UNUSED(settingsPath); 0054 0055 m_backend = new KWalletPasswordBackend; 0056 mApp->autoFill()->passwordManager()->registerBackend(QSL("KWallet"), m_backend); 0057 0058 // Enable KWallet password backend inside KDE session 0059 if (qgetenv("KDE_FULL_SESSION") == QByteArray("true")) { 0060 mApp->autoFill()->passwordManager()->switchBackend(QSL("KWallet")); 0061 } 0062 0063 m_jobTracker = new KUiServerJobTracker(this); 0064 0065 auto manager = mApp->downloadManager(); 0066 connect(manager, &DownloadManager::downloadAdded, this, [=](DownloadItem *item) { 0067 auto job = new DownloadKJob(item->url(), item->path(), item->fileName(), this); 0068 m_jobTracker->registerJob(job); 0069 job->start(); 0070 job->updateDescription(); 0071 0072 connect(item, &DownloadItem::progressChanged, job, &DownloadKJob::progress); 0073 connect(manager, QOverload<>::of(&DownloadManager::downloadFinished), m_jobTracker, [=]() { 0074 m_jobTracker->unregisterJob(job); 0075 }); 0076 }); 0077 0078 0079 QStringList newSchemes; 0080 const auto protocols = KProtocolInfo::protocols(); 0081 for (const QString &protocol : protocols) { 0082 if (WebPage::internalSchemes().contains(protocol)) { 0083 continue; 0084 } 0085 if (!QWebEngineUrlScheme::schemeByName(protocol.toUtf8()).name().isEmpty()) { 0086 auto *handler = new KIOSchemeHandler(protocol, this); 0087 m_kioSchemeHandlers.append(handler); 0088 mApp->webProfile()->installUrlSchemeHandler(protocol.toUtf8(), handler); 0089 WebPage::addSupportedScheme(protocol); 0090 } 0091 else { 0092 newSchemes.append(protocol); 0093 qInfo() << QSL("KDEFrameworksIntegration: Custom scheme '%1' will be available after browser restart.").arg(protocol); 0094 } 0095 } 0096 0097 if (!newSchemes.isEmpty()) { 0098 Settings settings; 0099 settings.beginGroup(QSL("Web-Browser-Settings")); 0100 0101 QStringList allowedSchemes = settings.value(QSL("AllowedSchemes"), QStringList()).toStringList(); 0102 allowedSchemes.append(newSchemes); 0103 allowedSchemes.removeDuplicates(); 0104 settings.setValue(QSL("AllowedSchemes"), allowedSchemes); 0105 0106 settings.endGroup(); 0107 } 0108 0109 m_sharePageMenu = new Purpose::Menu(); 0110 m_sharePageMenu->setTitle(tr("Share page")); 0111 m_sharePageMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-share"))); 0112 m_sharePageMenu->model()->setInputData(QJsonObject{ 0113 { QStringLiteral("urls"), QJsonArray {QJsonValue(QSL("falkon"))} }, 0114 { QStringLiteral("title"), QJsonValue(QSL("falkon")) } 0115 }); 0116 m_sharePageMenu->model()->setPluginType(QStringLiteral("ShareUrl")); 0117 0118 KAboutData aboutData(QSL("falkon"), QSL("Falkon"), QCoreApplication::applicationVersion()); 0119 KAboutData::setApplicationData(aboutData); 0120 0121 KCrash::initialize(); 0122 KCrash::setFlags(KCrash::KeepFDs); 0123 } 0124 0125 void KDEFrameworksIntegrationPlugin::unload() 0126 { 0127 mApp->autoFill()->passwordManager()->unregisterBackend(m_backend); 0128 delete m_backend; 0129 delete m_sharePageMenu; 0130 0131 for (KIOSchemeHandler *handler : std::as_const(m_kioSchemeHandlers)) { 0132 mApp->webProfile()->removeUrlSchemeHandler(handler); 0133 WebPage::removeSupportedScheme(handler->protocol()); 0134 delete handler; 0135 } 0136 m_kioSchemeHandlers.clear(); 0137 } 0138 0139 void KDEFrameworksIntegrationPlugin::populateWebViewMenu(QMenu *menu, WebView *view, const WebHitTestResult &r) 0140 { 0141 Q_UNUSED(r) 0142 0143 m_sharePageMenu->model()->setInputData(QJsonObject{ 0144 { QStringLiteral("urls"), QJsonArray {QJsonValue(view->url().toString())} }, 0145 { QStringLiteral("title"), QJsonValue(view->title()) } 0146 }); 0147 m_sharePageMenu->reload(); 0148 0149 menu->addAction(m_sharePageMenu->menuAction()); 0150 } 0151 0152 bool KDEFrameworksIntegrationPlugin::testPlugin() 0153 { 0154 // Require the version that the plugin was built with 0155 return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION)); 0156 }