aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 973ac58592d327b657a2276f3eb2e0755140bb11 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * 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 "src/mainwindow/mainwindow.h"
#include "version.h"
#include <QCommandLineParser>
#include <QStandardPaths>
#include <iostream>

// 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

    QCommandLineParser parser;
    parser.setApplicationDescription("yet another no-frills browser");
    parser.addHelpOption();
    parser.addVersionOption();

    // user config, ~/.config/smolbote/smolbote.cfg or empty if there is none
    QCommandLineOption configOption({ "c", "config" }, "Set configuration file.", "path");
    {
        // try to locate an existing config
        QString path = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "smolbote.cfg");

        // it's possible there is no config, so set the path properly
        if(path.isEmpty())
            path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/smolbote.cfg";

        configOption.setDefaultValue(path);
    }
    parser.addOption(configOption);

    // default config, :/poi.cfg
    QCommandLineOption defaultConfigOption("default-config", "Set the default configuration file.", "path");
    parser.addOption(defaultConfigOption);

    QCommandLineOption profileOption({ "p", "profile" }, "Use this profile.", "PROFILE");
    profileOption.setDefaultValue("");
    parser.addOption(profileOption);

    QCommandLineOption socketOption("socket", "Set socket to use for IPC, leave blank for default, 'none' to disable.", "name");
    socketOption.setDefaultValue("");
    parser.addOption(socketOption);

    QCommandLineOption newWindowOption("in-new-window", "Open URL in new window");
    parser.addOption(newWindowOption);

    parser.addPositionalArgument("URL", "URL(s) to open");

    // use parse instead of process
    // process calls exit() on unknown options
    parser.parse(instance.arguments());

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

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

    // first load the default configuration
    if(parser.isSet(defaultConfigOption)) {
        qDebug("Reading default configuration [%s]: %s",
            qUtf8Printable(parser.value(defaultConfigOption)),
            config->read(parser.value(defaultConfigOption)) ? "ok" : "failed");
    }

    // then load in the user configuration, which will overwrite it
    if(parser.isSet(configOption)) {
        qDebug("Reading configuration [%s]: %s",
            qUtf8Printable(parser.value(configOption)),
            config->read(parser.value(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.unknownOptionNames().isEmpty()) {
        int _argc = parser.unknownOptionNames().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));

        // create a list of unknown QCommandLineOption's
        // parser.addOptions() takes a list, so this is a QList
        QList<QCommandLineOption> opts;
        for (const QString &opt : parser.unknownOptionNames()) {
            QCommandLineOption o(opt, "dummy desc", "dummy value");
            opts.append(o);
        }

        // add list and reparse to set the new options
        parser.addOptions(opts);
        parser.parse(instance.arguments());

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

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

    // check for other instances
    // if we socket hasn't been disabled (socket is not none)
    if(parser.value(socketOption) != "none") {
        bool bindOk = instance.bindLocalSocket(parser.value(socketOption));
        if(bindOk) {
            qDebug("Connected to local socket: %s", qUtf8Printable(instance.serverName()));
        } else {
            // pass arguments to new instance
            return instance.sendMessage(parser.value(profileOption), parser.isSet(newWindowOption), parser.positionalArguments());
        }
    }

    instance.setConfiguration(config);

    if(parser.isSet(profileOption))
        instance.createSession(parser.value(profileOption), parser.isSet(newWindowOption), parser.positionalArguments());
    else
        instance.createSession(QString::fromStdString(config->value<std::string>("browser.profile").value()), parser.isSet(newWindowOption), parser.positionalArguments());

#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();
}