File indexing completed on 2024-05-12 04:58:12

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2019 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 "protocolhandlermanager.h"
0019 #include "settings.h"
0020 
0021 #include <QWebEnginePage>
0022 #include <QtWebEngineWidgetsVersion>
0023 
0024 #include <QWebEngineRegisterProtocolHandlerRequest>
0025 
0026 ProtocolHandlerManager::ProtocolHandlerManager(QObject *parent)
0027     : QObject(parent)
0028 {
0029     init();
0030 }
0031 
0032 QHash<QString, QUrl> ProtocolHandlerManager::protocolHandlers() const
0033 {
0034     return m_protocolHandlers;
0035 }
0036 
0037 void ProtocolHandlerManager::addProtocolHandler(const QString &scheme, const QUrl &url)
0038 {
0039     if (scheme.isEmpty() || url.isEmpty()) {
0040         return;
0041     }
0042     m_protocolHandlers[scheme] = url;
0043     registerHandler(scheme, url);
0044     save();
0045 }
0046 
0047 void ProtocolHandlerManager::removeProtocolHandler(const QString &scheme)
0048 {
0049     m_protocolHandlers.remove(scheme);
0050     save();
0051 }
0052 
0053 void ProtocolHandlerManager::init()
0054 {
0055     Settings settings;
0056     settings.beginGroup(QSL("ProtocolHandlers"));
0057     const QStringList keys = settings.childKeys();
0058     for (const QString &scheme : keys) {
0059         const QUrl url = settings.value(scheme).toUrl();
0060         m_protocolHandlers[scheme] = url;
0061         registerHandler(scheme, url);
0062     }
0063     settings.endGroup();
0064 }
0065 
0066 void ProtocolHandlerManager::save()
0067 {
0068     Settings settings;
0069     settings.remove(QSL("ProtocolHandlers"));
0070     settings.beginGroup(QSL("ProtocolHandlers"));
0071     for (auto it = m_protocolHandlers.cbegin(); it != m_protocolHandlers.cend(); ++it) {
0072         settings.setValue(it.key(), it.value());
0073     }
0074     settings.endGroup();
0075 }
0076 
0077 void ProtocolHandlerManager::registerHandler(const QString &scheme, const QUrl &url)
0078 {
0079     QString urlString = url.toString();
0080     urlString.replace(QL1S("%25s"), QL1S("%s"));
0081 
0082     auto *page = new QWebEnginePage(this);
0083     connect(page, &QWebEnginePage::loadFinished, page, &QObject::deleteLater);
0084     connect(page, &QWebEnginePage::registerProtocolHandlerRequested, this, [](QWebEngineRegisterProtocolHandlerRequest request) {
0085         request.accept();
0086     });
0087     page->setHtml(QSL("<script>navigator.registerProtocolHandler('%1', '%2', '')</script>").arg(scheme, urlString), url);
0088 }