File indexing completed on 2025-09-14 05:20:54

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aditya Mehra <aix.m@outlook.com>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <QGuiApplication>
0007 #include <QQmlApplicationEngine>
0008 //#include <qtwebengineglobal.h>
0009 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0010 #include <QtWebEngine/QQuickWebEngineProfile>
0011 #else
0012 #include <QtWebEngineQuick/QQuickWebEngineProfile>
0013 #endif
0014 #include <QtWebEngineCore/qwebengineurlrequestinterceptor.h>
0015 #include "plugins/virtualMouse.h"
0016 #include "plugins/virtualKeypress.h"
0017 #include "plugins/globalSettings.h"
0018 #include "plugins/keyfilter.h"
0019 #include <QQmlContext>
0020 #include <QCommandLineParser>
0021 #include <QCommandLineOption>
0022 
0023 #include <KLocalizedContext>
0024 #include <KLocalizedString>
0025 
0026 
0027 // Add Adblock Implementation
0028 #include <QThread>
0029 #include <QFile>
0030 #include <QDebug>
0031 #include "third-party/ad-block/ad_block_client.h"
0032 
0033 
0034 
0035 
0036 class WebIntercept : public QWebEngineUrlRequestInterceptor
0037 {
0038     Q_OBJECT
0039 public:
0040     WebIntercept(QObject *parent = nullptr) : QWebEngineUrlRequestInterceptor(parent)
0041     {
0042         QThread *thread = QThread::create([this]{
0043             QFile file(QStringLiteral(":/third-party/easylist.txt"));
0044             QString easyListTxt;
0045 
0046             if(!file.exists()) {
0047                 qDebug() << "No easylist.txt file found.";
0048             } else {
0049                 if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
0050                     easyListTxt = QLatin1String(file.readAll());
0051                 }
0052                 file.close();
0053                 client.parse(easyListTxt.toStdString().c_str());
0054             }
0055         });
0056         thread->start();
0057     }
0058 
0059     void interceptRequest(QWebEngineUrlRequestInfo &info) override
0060     {
0061         if (client.matches(info.requestUrl().toString().toStdString().c_str(),
0062                            FONoFilterOption, info.requestUrl().host().toStdString().c_str())) {
0063             qDebug() << "Blocked: " << info.requestUrl();
0064             info.block(true);
0065         }
0066     }
0067 
0068 private:
0069     AdBlockClient client;
0070 };
0071 
0072 static QObject *globalSettingsSingletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
0073 {
0074     Q_UNUSED(engine)
0075     Q_UNUSED(scriptEngine)
0076 
0077     return new GlobalSettings;
0078 }
0079 
0080 int main(int argc, char *argv[])
0081 {
0082     QStringList arguments;
0083     for (int a = 0; a < argc; ++a) {
0084         arguments << QString::fromLocal8Bit(argv[a]);
0085     }
0086 
0087     QCommandLineParser parser;
0088     auto urlOption = QCommandLineOption(QStringLiteral("url"), QStringLiteral("Single url to load in sandbox"), QStringLiteral("url"));
0089     auto sandboxOption = QCommandLineOption(QStringLiteral("sandbox"), QStringLiteral("Sandbox Mode"));
0090     auto helpOption = QCommandLineOption(QStringLiteral("help"), QStringLiteral("Show this help message"));
0091     parser.addOptions({urlOption, sandboxOption, helpOption});
0092     parser.process(arguments);
0093 
0094     qputenv("QT_VIRTUALKEYBOARD_DESKTOP_DISABLE", QByteArray("1"));
0095     qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
0096     QCoreApplication::setOrganizationName(QStringLiteral("AuraBrowser"));
0097     QCoreApplication::setApplicationName(QStringLiteral("AuraBrowser"));
0098 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0099     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
0100 #endif
0101     QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
0102 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0103     QtWebEngine::initialize();
0104 #else
0105     QtWebEngineQuick::initialize();
0106 #endif
0107     QGuiApplication app(argc, argv);
0108     app.setWindowIcon(QIcon(QStringLiteral(":/qml/images/logo-small.png")));
0109     KLocalizedString::setApplicationDomain(QByteArrayLiteral("aura-browser"));
0110 
0111     if (parser.isSet(helpOption)) {
0112         parser.showHelp();
0113         return 0;
0114     }
0115 
0116     QQmlApplicationEngine engine;
0117     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0118 
0119     // Adblock Implementation
0120     WebIntercept interceptor;
0121     QQuickWebEngineProfile adblockProfile;
0122     adblockProfile.setUrlRequestInterceptor(&interceptor);
0123     adblockProfile.setHttpUserAgent(QStringLiteral("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"));
0124     adblockProfile.setStorageName(QStringLiteral("Profile"));
0125     adblockProfile.setOffTheRecord(false);
0126     engine.rootContext()->setContextProperty(QStringLiteral("adblockProfile"), &adblockProfile);
0127 
0128     FakeCursor fakeCursor;
0129     engine.rootContext()->setContextProperty(QStringLiteral("Cursor"), &fakeCursor);
0130     QQmlContext* ctx = engine.rootContext();
0131     VirtualKeyPress virtualKeyPress;
0132     ctx->setContextProperty(QStringLiteral("keyEmitter"), &virtualKeyPress);
0133     auto offlineStoragePath = QUrl::fromLocalFile(engine.offlineStoragePath());
0134     engine.rootContext()->setContextProperty(QStringLiteral("offlineStoragePath"), offlineStoragePath);
0135     qmlRegisterSingletonType<GlobalSettings>("Aura", 1, 0, "GlobalSettings", globalSettingsSingletonProvider);
0136     qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/qml/NavigationSoundEffects.qml")), "Aura", 1, 0, "NavigationSoundEffects");
0137 
0138     // Install Event Filter for KeyPress
0139     KeyFilter keyFilter;
0140     app.installEventFilter(&keyFilter);
0141     engine.rootContext()->setContextProperty(QStringLiteral("keyFilter"), &keyFilter);
0142 
0143     // Connect to keyPress signal from keyFilter
0144     QObject::connect(&keyFilter, &KeyFilter::keyRelease, &app, [&fakeCursor](QEvent *event) {
0145         fakeCursor.moveEvent(event);
0146     });
0147 
0148 
0149     QString sandboxURL = parser.value(urlOption);
0150     bool sandboxMode = parser.isSet(sandboxOption);
0151     
0152     if (arguments.count() > 1) {
0153         QUrl url = QUrl::fromUserInput(arguments.at(1));
0154         if (url.isValid()) {
0155             sandboxURL = url.toString();
0156             sandboxMode = true;
0157         }
0158     }
0159 
0160     engine.rootContext()->setContextProperty(QStringLiteral("sandboxURL"), sandboxURL);
0161     engine.rootContext()->setContextProperty(QStringLiteral("sandboxMode"), sandboxMode);
0162     
0163     // Define const QUrl url here, if the user is in sandbox mode, we want to load the sandbox qml file, if not, we want to load the main qml file
0164     QUrl url;
0165     if (sandboxMode) {
0166         url = QUrl(QStringLiteral("qrc:/qml/mainSandbox.qml"));
0167     } else {
0168         url = QUrl(QStringLiteral("qrc:/qml/main.qml"));
0169     }
0170 
0171     QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
0172                      &app, [url](QObject *obj, const QUrl &objUrl) {
0173         if (!obj && url == objUrl)
0174             QCoreApplication::exit(-1);
0175     }, Qt::QueuedConnection);
0176     engine.load(url);
0177 
0178     return app.exec();
0179 }
0180 
0181 #include "main.moc"