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 #pragma once
0019 
0020 #include <QWebEngineUrlRequestJob>
0021 
0022 /**
0023  * @brief The QmlWebEngineUrlRequestJob class
0024  */
0025 class QmlWebEngineUrlRequestJob : public QObject
0026 {
0027     Q_OBJECT
0028     /**
0029      * @brief initiator of the QWebEngineUrlRequestJob
0030      */
0031     Q_PROPERTY(QString initiator READ initiator CONSTANT)
0032     /**
0033      * @brief request url of the QWebEngineUrlRequestJob
0034      */
0035     Q_PROPERTY(QString requestUrl READ requestUrl CONSTANT)
0036     /**
0037      * @brief request method of the QWebEngineUrlRequestJob
0038      */
0039     Q_PROPERTY(QString requestMethod READ requestMethod CONSTANT)
0040 public:
0041     /**
0042      * @brief The Error enum, exposes QWebEngineUrlRequestJob::Error to QML
0043      */
0044     enum Error {
0045         NoError = QWebEngineUrlRequestJob::NoError,               //!< No error
0046         UrlNotFound = QWebEngineUrlRequestJob::UrlNotFound,       //!< Url not found error
0047         UrlInvaild = QWebEngineUrlRequestJob::UrlInvalid,         //!< Url invalid error
0048         RequestAborted = QWebEngineUrlRequestJob::RequestAborted, //!< Request aborted
0049         RequestDenied = QWebEngineUrlRequestJob::RequestDenied,   //!< Request denied
0050         RequestFailed = QWebEngineUrlRequestJob::RequestFailed    //!< Request failed
0051     };
0052     Q_ENUM(Error)
0053     explicit QmlWebEngineUrlRequestJob(QWebEngineUrlRequestJob *job = nullptr, QObject *parent = nullptr);
0054     /**
0055      * @brief Fails the request with the error
0056      * @param error
0057      */
0058     Q_INVOKABLE void fail(QmlWebEngineUrlRequestJob::Error error);
0059     /**
0060      * @brief Redirects the request to the url
0061      * @param urlString, represents the url to which the request is to be redirected
0062      */
0063     Q_INVOKABLE void redirect(const QString &urlString);
0064     /**
0065      * @brief Replies to the request
0066      * @param A JavaScript object containing
0067      *        - content: String representing the reply data
0068      *        - contentType: String representing the contentType of reply data
0069      */
0070     Q_INVOKABLE void reply(const QVariantMap &map);
0071 private:
0072     QWebEngineUrlRequestJob *m_job = nullptr;
0073     QString initiator() const;
0074     QString requestUrl() const;
0075     QString requestMethod() const;
0076 };