Falkon Develop
Cross-platform Qt-based web browser
kioschemehandler.cpp
Go to the documentation of this file.
1/* ============================================================
2* KDEFrameworksIntegration - KDE support plugin for Falkon
3* Copyright (C) 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* ============================================================ */
18#include "kioschemehandler.h"
19
20#include <QBuffer>
21#include <QPointer>
22#include <QNetworkReply>
23#include <QWebEngineUrlRequestJob>
24
25#include <kio_version.h>
26
27#include <QNetworkAccessManager>
28Q_GLOBAL_STATIC_WITH_ARGS(QNetworkAccessManager, s_knam, (nullptr))
29
30KIOSchemeHandler::KIOSchemeHandler(const QString &protocol, QObject *parent)
31 : QWebEngineUrlSchemeHandler(parent)
32 , m_protocol(protocol)
33{
34}
35
37{
38 return m_protocol;
39}
40
41void KIOSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
42{
43 if (job->requestMethod() != QByteArray("GET")) {
44 qWarning() << "Unsupported method" << job->requestMethod();
45 job->fail(QWebEngineUrlRequestJob::RequestFailed);
46 return;
47 }
48
49 QPointer<QWebEngineUrlRequestJob> jobPtr = job;
50 QNetworkReply *reply = s_knam()->get(QNetworkRequest(job->requestUrl()));
51 connect(reply, &QNetworkReply::finished, this, [=]() {
52 if (!jobPtr) {
53 reply->deleteLater();
54 return;
55 }
56 if (reply->error() != QNetworkReply::NoError) {
57 reply->deleteLater();
58 qWarning() << "Error:" << reply->errorString();
59 job->fail(QWebEngineUrlRequestJob::RequestFailed);
60 } else {
61 reply->setParent(job);
62 job->reply(reply->header(QNetworkRequest::ContentTypeHeader).toByteArray(), reply);
63 }
64 });
65}
void requestStarted(QWebEngineUrlRequestJob *job) override
QString protocol() const