aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 60c30311d4996a8ba69fc891c8895872aa953f0e (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
/*
 * 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 "settings.h"
#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

    init_conf("");

    // argc, argv, allowSecondary
    Browser app(argc, argv);
    // set this, otherwise the webview becomes black when using a stylesheet
    app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true);

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

    const auto profile = []() {
        Configuration c;
        return c.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() /*&& !cmd_noRemote*/) {
        QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, QByteArray message) {
            Q_UNUSED(instanceId);
            JsonSession session(message);
            app.open(session.get());
        });
    }

    {
        const auto session = [&]() {
            /*if(cmd_session) {
                QFile sessionJson(QString::fromStdString(args::get(cmd_session)));
                if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    return JsonSession(sessionJson.readAll());
                }
            }
            if(cmd_pickSession) {
                SessionDialog dlg;
                if(const auto pick = dlg.pickSession()) {
                    return JsonSession(pick.value());
                }
            }*/
            return JsonSession(profile, urls);
        }();

        if(app.isPrimary() /*|| cmd_noRemote */) {
            app.open(session.get());
        } else {
            // app is secondary and not standalone
            return app.sendMessage(session.serialize());
        }
    }

    return app.exec();
}