aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 75691f997ecd4b7be4e3583ecf36701a789d4b41 (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
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "browser.h"
#include "commandline.h"
#include "mainwindow/mainwindow.h"
#include "version.h"
#include <iomanip>
#include <iostream>
#include "configuration/configuration.h"
// startup time measuring
#ifdef QT_DEBUG
#include <QElapsedTimer>
#endif

int main(int argc, char **argv)
{
    // Create application object
    Browser instance(argc, argv);
    instance.setApplicationVersion(SMOLBOTE_VERSION);

#ifdef QT_DEBUG
    QElapsedTimer timer;
    timer.start();
#endif

    CommandLine parser;
    parser.parseCommandLine(instance);

#ifdef QT_DEBUG
    qDebug("config=%s", qUtf8Printable(parser.value(parser.configOption)));
    qDebug("profile=%s", qUtf8Printable(parser.value(parser.profileOption)));
    qDebug("socket=%s", qUtf8Printable(parser.value(parser.socketOption)));

#endif

    std::shared_ptr<Configuration> config = std::make_shared<Configuration>();

    // load user configuration
    if(!parser.value(parser.configOption).isEmpty()) {
        qDebug("Reading configuration [%s]: %s",
            qUtf8Printable(parser.value(parser.configOption)),
            config->read(parser.value(parser.configOption)) ? "ok" : "failed");
    }

    // parse command-line overrides
    // we assume the users knows what they're doing, so we only pass the unknown options to program_options
    // passing any unknown options though will cause it to fail, so we need to filter out the regular options
    // unfortunately, QCommandLineParser will only give us the unknown option
    // names, so we need to build a list, add them as options, reparse, and then
    // we get their values
    if(!parser.unknownOptions.isEmpty()) {
        int _argc = parser.unknownOptions.length() + 1;
        const char *_argv[_argc];

        // program_options requires 0 to be the program name, otherwise it seems to fail
        _argv[0] = qUtf8Printable(instance.arguments().at(0));

        for(int i = 1; i < _argc; ++i) {
            _argv[i] = qUtf8Printable(QString("--%1=%2").arg(parser.unknownOptions[i - 1].names().at(0), parser.value(parser.unknownOptions[i - 1])));
        }

        qDebug("Parsing command-line overrides: %s", config->parse(_argc, _argv) ? "ok" : "failed");
    }

    // check for other instances
    bool isSingleInstance = instance.bindLocalSocket(parser.value(parser.socketOption));
    if(isSingleInstance) {
        qDebug("Bound to local socket %s", qUtf8Printable(instance.serverName()));
        instance.setConfiguration(config);
    }

    // create session
    {
        QString profile;
        if(parser.isSet(parser.profileOption))
            profile = parser.value(parser.profileOption);
        else
            profile = QString::fromStdString(config->value<std::string>("browser.profile").value());

        instance.sendMessage(profile, parser.isSet(parser.newWindowOption), parser.positionalArguments());
    }

    if(!isSingleInstance) {
        return 0;
    }

#ifdef QT_DEBUG
    qDebug("Startup complete in %lldms", timer.elapsed());
#endif

    // Normally we'd use
    //return instance.exec();
    // but, Call to "exec" is ambiguous
    return static_cast<QApplication *>(&instance)->exec();
}