Falkon Develop
Cross-platform Qt-based web browser
qmlwebengineurlrequestjob.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@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* ============================================================ */
19#include "qztools.h"
20
21#include <QBuffer>
22#include <QVariantMap>
23#include <QtWebEngineWidgetsVersion>
24
25QmlWebEngineUrlRequestJob::QmlWebEngineUrlRequestJob(QWebEngineUrlRequestJob *job, QObject *parent)
26 : QObject(parent)
27 , m_job(job)
28{
29}
30
32{
33 if (!m_job) {
34 return;
35 }
36 m_job->fail(QWebEngineUrlRequestJob::Error(error));
37}
38
39void QmlWebEngineUrlRequestJob::redirect(const QString &urlString)
40{
41 if (!m_job) {
42 return;
43 }
44 return m_job->redirect(QUrl::fromEncoded(urlString.toUtf8()));
45}
46
47void QmlWebEngineUrlRequestJob::reply(const QVariantMap &map)
48{
49 if (!m_job) {
50 return;
51 }
52 if (!map.contains(QSL("content")) || !map.contains(QSL("contentType"))) {
53 qWarning() << "Unable to call" << __FUNCTION__ << ": invalid parameters";
54 return;
55 }
56 QByteArray content = map.value(QSL("content")).toString().toUtf8();
57 QByteArray contentType = map.value(QSL("contentType")).toString().toUtf8();
58 auto *buffer = new QBuffer();
59 buffer->open(QIODevice::ReadWrite);
60 buffer->write(content);
61 buffer->seek(0);
62 m_job->reply(contentType, buffer);
63}
65{
66 if (!m_job) {
67 return {};
68 }
69 QString initiatorString;
70 initiatorString = QString::fromUtf8(m_job->initiator().toEncoded());
71 return initiatorString;
72}
73
75{
76 if (!m_job) {
77 return {};
78 }
79 return QString::fromUtf8(m_job->requestUrl().toEncoded());
80}
81
83{
84 if (!m_job) {
85 return {};
86 }
87 return QString::fromUtf8(m_job->requestMethod());
88}
QString requestUrl
request url of the QWebEngineUrlRequestJob
Q_INVOKABLE void redirect(const QString &urlString)
Redirects the request to the url.
QmlWebEngineUrlRequestJob(QWebEngineUrlRequestJob *job=nullptr, QObject *parent=nullptr)
QString requestMethod
request method of the QWebEngineUrlRequestJob
Q_INVOKABLE void fail(QmlWebEngineUrlRequestJob::Error error)
Fails the request with the error.
Q_INVOKABLE void reply(const QVariantMap &map)
Replies to the request.
Error
The Error enum, exposes QWebEngineUrlRequestJob::Error to QML.
QString initiator
initiator of the QWebEngineUrlRequestJob
#define QSL(x)
Definition: qzcommon.h:40