File indexing completed on 2024-12-22 04:41:13

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@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 "qmlwebengineurlrequestjob.h"
0019 #include "qztools.h"
0020 
0021 #include <QBuffer>
0022 #include <QVariantMap>
0023 #include <QtWebEngineWidgetsVersion>
0024 
0025 QmlWebEngineUrlRequestJob::QmlWebEngineUrlRequestJob(QWebEngineUrlRequestJob *job, QObject *parent)
0026     : QObject(parent)
0027     , m_job(job)
0028 {
0029 }
0030 
0031 void QmlWebEngineUrlRequestJob::fail(QmlWebEngineUrlRequestJob::Error error)
0032 {
0033     if (!m_job) {
0034         return;
0035     }
0036     m_job->fail(QWebEngineUrlRequestJob::Error(error));
0037 }
0038 
0039 void QmlWebEngineUrlRequestJob::redirect(const QString &urlString)
0040 {
0041     if (!m_job) {
0042         return;
0043     }
0044     return m_job->redirect(QUrl::fromEncoded(urlString.toUtf8()));
0045 }
0046 
0047 void QmlWebEngineUrlRequestJob::reply(const QVariantMap &map)
0048 {
0049     if (!m_job) {
0050         return;
0051     }
0052     if (!map.contains(QSL("content")) || !map.contains(QSL("contentType"))) {
0053         qWarning() << "Unable to call" << __FUNCTION__ << ": invalid parameters";
0054         return;
0055     }
0056     QByteArray content = map.value(QSL("content")).toString().toUtf8();
0057     QByteArray contentType = map.value(QSL("contentType")).toString().toUtf8();
0058     auto *buffer = new QBuffer();
0059     buffer->open(QIODevice::ReadWrite);
0060     buffer->write(content);
0061     buffer->seek(0);
0062     m_job->reply(contentType, buffer);
0063 }
0064 QString QmlWebEngineUrlRequestJob::initiator() const
0065 {
0066     if (!m_job) {
0067         return {};
0068     }
0069     QString initiatorString;
0070     initiatorString = QString::fromUtf8(m_job->initiator().toEncoded());
0071     return initiatorString;
0072 }
0073 
0074 QString QmlWebEngineUrlRequestJob::requestUrl() const
0075 {
0076     if (!m_job) {
0077         return {};
0078     }
0079     return QString::fromUtf8(m_job->requestUrl().toEncoded());
0080 }
0081 
0082 QString QmlWebEngineUrlRequestJob::requestMethod() const
0083 {
0084     if (!m_job) {
0085         return {};
0086     }
0087     return QString::fromUtf8(m_job->requestMethod());
0088 }