diff --git a/CMakeLists.txt b/CMakeLists.txt index b0b41dd15..01d330e46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,10 +95,12 @@ if (PKG_CONFIG_FOUND) endif() endif() -# Optional: KWallet +# Optional: KWallet + KIO set(KF5_MIN_VERSION "5.27.0") find_package(KF5Wallet ${KF5_MIN_VERSION} CONFIG) -set_package_properties(KF5Wallet PROPERTIES DESCRIPTION "KWallet password backend plugin" TYPE OPTIONAL) +set_package_properties(KF5Wallet PROPERTIES DESCRIPTION "KDESupport plugin" TYPE OPTIONAL) +find_package(KF5KIO ${KF5_MIN_VERSION} CONFIG) +set_package_properties(KF5KIO PROPERTIES DESCRIPTION "KDESupport plugin" TYPE OPTIONAL) # Optional: PySide2 find_package(PySide2 "2.0.0") diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt index ea8dcf49f..10771276a 100644 --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -14,8 +14,8 @@ if (GNOME_KEYRING_FOUND) add_subdirectory(GnomeKeyringPasswords) endif() -if (KF5Wallet_FOUND) - add_subdirectory(KWalletPasswords) +if (KF5Wallet_FOUND AND KF5KIO_FOUND) + add_subdirectory(KDESupport) endif() if (ENABLE_PYTHON_PLUGINS) diff --git a/src/plugins/KDESupport/CMakeLists.txt b/src/plugins/KDESupport/CMakeLists.txt index 04b7bf258..479b7e53d 100644 --- a/src/plugins/KDESupport/CMakeLists.txt +++ b/src/plugins/KDESupport/CMakeLists.txt @@ -1,6 +1,7 @@ set(KDESupport_SRCS kdesupportplugin.cpp kwalletpasswordbackend.cpp + kioschemehandler.cpp ) ecm_create_qm_loader(KDESupport_SRCS falkon_kdesupport_qt) @@ -12,4 +13,4 @@ qt5_add_resources(RSCS ${KDESupport_RSCS}) add_library(KDESupport MODULE ${KDESupport_SRCS} ${RSCS}) install(TARGETS KDESupport DESTINATION ${FALKON_INSTALL_PLUGINDIR}) -target_link_libraries(KDESupport FalkonPrivate KF5::Wallet) +target_link_libraries(KDESupport FalkonPrivate KF5::Wallet KF5::KIOCore KF5::KIOWidgets) diff --git a/src/plugins/KDESupport/kdesupportplugin.cpp b/src/plugins/KDESupport/kdesupportplugin.cpp index d47e3fd60..deeae6d4f 100644 --- a/src/plugins/KDESupport/kdesupportplugin.cpp +++ b/src/plugins/KDESupport/kdesupportplugin.cpp @@ -24,6 +24,12 @@ #include "autofill.h" #include "passwordmanager.h" #include "desktopfile.h" +#include "kioschemehandler.h" +#include "webpage.h" + +#include + +#include KDESupportPlugin::KDESupportPlugin() : QObject() @@ -43,12 +49,30 @@ void KDESupportPlugin::init(InitState state, const QString &settingsPath) m_backend = new KWalletPasswordBackend; mApp->autoFill()->passwordManager()->registerBackend(QSL("KWallet"), m_backend); + + const auto protocols = KProtocolInfo::protocols(); + for (const QString &protocol : protocols) { + if (WebPage::internalSchemes().contains(protocol)) { + continue; + } + KIOSchemeHandler *handler = new KIOSchemeHandler(protocol, this); + m_kioSchemeHandlers.append(handler); + mApp->webProfile()->installUrlSchemeHandler(protocol.toUtf8(), handler); + WebPage::addSupportedScheme(protocol); + } } void KDESupportPlugin::unload() { mApp->autoFill()->passwordManager()->unregisterBackend(m_backend); delete m_backend; + + for (KIOSchemeHandler *handler : qAsConst(m_kioSchemeHandlers)) { + mApp->webProfile()->removeUrlSchemeHandler(handler); + WebPage::removeSupportedScheme(handler->protocol()); + delete handler; + } + m_kioSchemeHandlers.clear(); } bool KDESupportPlugin::testPlugin() diff --git a/src/plugins/KDESupport/kdesupportplugin.h b/src/plugins/KDESupport/kdesupportplugin.h index e8631bb58..c0fdd82e5 100644 --- a/src/plugins/KDESupport/kdesupportplugin.h +++ b/src/plugins/KDESupport/kdesupportplugin.h @@ -21,6 +21,7 @@ #include "plugininterface.h" class KWalletPasswordBackend; +class KIOSchemeHandler; class KDESupportPlugin : public QObject, public PluginInterface { @@ -38,7 +39,7 @@ public: private: KWalletPasswordBackend* m_backend; - + QVector m_kioSchemeHandlers; }; #endif // KDESUPPORTPLUGIN_H diff --git a/src/plugins/KDESupport/kioschemehandler.cpp b/src/plugins/KDESupport/kioschemehandler.cpp new file mode 100644 index 000000000..62564ff5b --- /dev/null +++ b/src/plugins/KDESupport/kioschemehandler.cpp @@ -0,0 +1,63 @@ +/* ============================================================ +* KDESupport - KDE support plugin for Falkon +* Copyright (C) 2018 David Rosca +* +* 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 3 of the License, or +* (at your option) any later version. +* +* 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 . +* ============================================================ */ +#include "kioschemehandler.h" + +#include +#include +#include +#include + +#include + +Q_GLOBAL_STATIC_WITH_ARGS(KIO::Integration::AccessManager, s_knam, (nullptr)) + +KIOSchemeHandler::KIOSchemeHandler(const QString &protocol, QObject *parent) + : QWebEngineUrlSchemeHandler(parent) + , m_protocol(protocol) +{ +} + +QString KIOSchemeHandler::protocol() const +{ + return m_protocol; +} + +void KIOSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job) +{ + if (job->requestMethod() != QByteArray("GET")) { + qWarning() << "Unsupported method" << job->requestMethod(); + job->fail(QWebEngineUrlRequestJob::RequestFailed); + return; + } + + QPointer jobPtr = job; + QNetworkReply *reply = s_knam()->get(QNetworkRequest(job->requestUrl())); + connect(reply, &QNetworkReply::finished, this, [=]() { + if (!jobPtr) { + reply->deleteLater(); + return; + } + if (reply->error() != QNetworkReply::NoError) { + reply->deleteLater(); + qWarning() << "Error:" << reply->errorString(); + job->fail(QWebEngineUrlRequestJob::RequestFailed); + } else { + job->reply(reply->header(QNetworkRequest::ContentTypeHeader).toByteArray(), reply); + } + }); +} diff --git a/src/plugins/KDESupport/kioschemehandler.h b/src/plugins/KDESupport/kioschemehandler.h new file mode 100644 index 000000000..f4011e479 --- /dev/null +++ b/src/plugins/KDESupport/kioschemehandler.h @@ -0,0 +1,33 @@ +/* ============================================================ +* KDESupport - KDE support plugin for Falkon +* Copyright (C) 2018 David Rosca +* +* 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 3 of the License, or +* (at your option) any later version. +* +* 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 . +* ============================================================ */ +#pragma once + +#include + +class KIOSchemeHandler : public QWebEngineUrlSchemeHandler +{ +public: + explicit KIOSchemeHandler(const QString &protocol, QObject *parent = nullptr); + + QString protocol() const; + + void requestStarted(QWebEngineUrlRequestJob *job) override; + +private: + QString m_protocol; +}; diff --git a/src/plugins/KDESupport/metadata.desktop b/src/plugins/KDESupport/metadata.desktop index 4be6e4025..2d2fcfc14 100644 --- a/src/plugins/KDESupport/metadata.desktop +++ b/src/plugins/KDESupport/metadata.desktop @@ -1,6 +1,6 @@ [Desktop Entry] Name=KDE Support -Comment=Provides support for storing passwords in KWallet +Comment=Provides support for KIO and storing passwords in KWallet Icon=:kdesupport/data/icon.svg Type=Service