aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 8cd1d348402fb0ea865d82f226dda64270e9e922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "browser.h"
#include "configuration.h"
#include "session/sessiondialog.h"
#include "session_json.hpp"
#include <QCommandLineParser>
#include <QFile>
#include <QPluginLoader>
#include <QStandardPaths>
#include <spdlog/spdlog.h>

int main(int argc, char **argv)
{
    // change log pattern
    spdlog::set_pattern("[%^%l%$] [%P:%t] %v");
#ifdef QT_DEBUG
    spdlog::set_level(spdlog::level::debug); // Set global log level to debug
#endif

    QCommandLineParser parser;
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("url", "URLs to open");

    // generic options
    QCommandLineOption cmd_config({ "c", "config" }, "Set the configuration file.", "path");
    parser.addOption(cmd_config);

    QCommandLineOption cmd_session({ "s", "session" }, "Open the specified session.", "path");
    parser.addOption(cmd_session);

    QCommandLineOption cmd_pick_session("pick-session", "Show all available sessions and select which one to open.");
    parser.addOption(cmd_pick_session);

    // Qt options
    QCommandLineOption cmd_renderer("renderer", "Select the OpenGL renderer used by the application. Available options are: desktop, gles, software", "value");
    parser.addOption(cmd_renderer);

    // SingleApplication options
    QCommandLineOption cmd_no_remote("no-remote", "Start a new instance that won't accept or send remote commands.");
    parser.addOption(cmd_no_remote);

    { // handle command line options that need to be set before the QApplication object is created
        QStringList args;
        for(int i = 0; i < argc; ++i)
            args.append(argv[i]);
        parser.parse(args);

        // the OpenGL implementation needs to be manually set to software if it's not properly configured
        if(parser.isSet(cmd_renderer)) {
            const auto value = parser.value(cmd_renderer);
            if(value == QLatin1String("desktop")) {
                QApplication::setAttribute(Qt::AA_UseDesktopOpenGL, true);
            } else if(value == QLatin1String("gles")) {
                QApplication::setAttribute(Qt::AA_UseOpenGLES, true);
            } else if(value == QLatin1String("software")) {
                QApplication::setAttribute(Qt::AA_UseSoftwareOpenGL, true);
            } else {
                spdlog::error("Unknown cmd_renderer flag: {}", qUtf8Printable(value));
            }
        }
    }

    // set this, otherwise the webview becomes black when using a stylesheet
    QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true);

    spdlog::info("Loaded configuration: {}", Configuration::init_global(parser.value(cmd_config).toStdString()));

    Browser app(argc, argv);
    parser.process(app);

    const auto profile = []() {
        Configuration conf;
        return conf.value<QString>("profile/default").value();
    }();

    QStringList urls = parser.positionalArguments();
    if(urls.isEmpty()) {
        urls.append(QString());
    }

    // if app is primary, create new sessions from received messages
    if(app.isPrimary() && !parser.isSet(cmd_no_remote)) {
        QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, const QByteArray &message) {
            Q_UNUSED(instanceId);
            JsonSession session(message);
            app.open(session.get());
        });
    }

    {
        const auto session = [&]() {
            if(parser.isSet(cmd_session)) {
                QFile sessionJson(parser.value(cmd_session));
                if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    return JsonSession(sessionJson.readAll());
                }
            }
            if(parser.isSet(cmd_pick_session)) {
                SessionDialog dlg;
                if(const auto pick = dlg.pickSession()) {
                    return JsonSession(pick.value());
                }
            }
            return JsonSession(profile, urls);
        }();

        if(app.isPrimary() || parser.isSet(cmd_no_remote)) {
            app.open(session.get());
        } else {
            // app is secondary and not standalone
            return app.sendMessage(session.serialize()) ? EXIT_SUCCESS : EXIT_FAILURE;
        }
    }

    return QApplication::exec();
}