aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: a419775d1b47cb85324bdd3ce388b453fe6a3b96 (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
/*
 * 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 "version.h"
#include <QFile>
#include <configuration/configuration.h>
#include <memory>
#include <iostream>
#include "plugin.h"
#include "session.h"

int main(int argc, char **argv)
{
    // create and load configuration
    std::shared_ptr<Configuration> config = std::make_shared<Configuration>();
#ifdef QT_DEBUG
    QObject::connect(config.get(), &Configuration::settingChanged, [](const std::string &path, const QString &value) {
        qDebug("!!! setting changed %s=[%s]", path.c_str(), qUtf8Printable(value));
    });
#endif

    if(!config->parse(argc, argv)) {
        qWarning("Error parsing command line, check --help for usage.");
        return -1;
    }
    if(!config->parse(config->value<std::string>("config").value())) {
        qWarning("Error parsing config file.");
    }

    // --version
    if(config->exists("version")) {
        std::cout << "smolbote " << SMOLBOTE_VERSION << std::endl;
        return 0;
    }

    // --build
    if(config->exists("build")) {
        std::cout << SMOLBOTE_BRANCH << ":" << SMOLBOTE_COMMIT;
        return 0;
    }

    QVector<Plugin> plugins = loadPlugins(config->value<QString>("plugins.path").value());
    QMap<QString, std::function<int()>> pluginCommands;
    for(const auto &plugin : plugins) {
        auto *pluginInterface = qobject_cast<PluginInterface*>(plugin.instance);
        Q_CHECK_PTR(pluginInterface);

        QHashIterator<QString, std::function<int()>> i(pluginInterface->commands());
        while(i.hasNext()) {
            i.next();
            pluginCommands.insert(i.key(), i.value());
        }
    }

    if(config->exists("help")) {
        std::cout << "smolbote " << SMOLBOTE_VERSION << ": yet another no-frills browser" << std::endl;
        std::cout << "Usage: " << argv[0] << " [options] [command/URL(s)]" << std::endl << std::endl;

        std::cout << "Command-line Options: " << std::endl << config->commandlineOptions() << std::endl;

        std::cout << "Commands: " << std::endl;
        for(auto it = pluginCommands.constBegin(); it != pluginCommands.constEnd(); ++it) {
            std::cout << it.key().toStdString() << std::endl;
        }
        std::cout << std::endl;

        std::cout << "Configuration Options: " << std::endl << config->configurationOptions() << std::endl;

#ifdef Q_OS_LINUX
        std::cout << std::endl << "For more information refer to the manual page smolbote.7" << std::endl;
#endif
        return 0;
    }

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

    app.setup(QString::fromStdString(config->value<std::string>("profile.default").value()));
    for(const Plugin &plugin : plugins) {
        app.registerPlugin(plugin);
    }

    auto arguments = config->value<std::vector<std::string>>("args");
    QStringList urls;

    if(arguments) {
        for(const auto &u : arguments.value()) {
            if(pluginCommands.contains(QString::fromStdString(u))) {
                return pluginCommands.value(QString::fromStdString(u))();
            } else {
                urls.append(QString::fromStdString(u));
            }
        }
    }

    // set up socket
    bool isSingleInstance = app.bindLocalSocket(QString::fromStdString(config->value<std::string>("socket").value()));
#ifdef QT_DEBUG
    qDebug("bindLocalSocket(%s) = %s", qUtf8Printable(QString::fromStdString(config->value<std::string>("socket").value())), isSingleInstance ? "true" : "false");
#endif

    // if we are the only instance, set up the browser
    if(isSingleInstance) {
        auto stylesheet = config->value<std::string>("browser.stylesheet");
        if(stylesheet) {
            QFile f(QString::fromStdString(stylesheet.value()));
            if(f.open(QIODevice::ReadOnly)) {
                app.setStyleSheet(f.readAll());
                f.close();
            }
        }

        QObject::connect(&app, &Browser::messageAvailable, &app, &Browser::createSession);
    }

    if(config->exists("session")) {
        QFile sessionJson(config->value<QString>("session").value());
        if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) {
            app.sendMessage(sessionJson.readAll());
            sessionJson.close();
        } else {
            qWarning("Could not open session [%s].", qUtf8Printable(sessionJson.fileName()));
        }
    } else if(config->exists("browser.session")) {
        QFile sessionJson(config->value<QString>("browser.session").value());
        if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) {
            app.sendMessage(sessionJson.readAll());
            sessionJson.close();
        } else {
            qWarning("Could not open browser.session [%s].", qUtf8Printable(sessionJson.fileName()));
        }
    } else
        app.sendMessage(Session::toJsonObject(config->value<QString>("profile.default").value(), urls));

    if(isSingleInstance)
        return app.exec();
    else
        return EXIT_SUCCESS;
}