aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 170765703f93d0583d670c4f925645f0adfeca75 (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
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include <iostream>
#include "browser.h"
#include <QCommandLineParser>
#include <QFile>
#include <QStandardPaths>
#include "mainwindow.h"

// read config into std::string, supports qrc
inline std::string readConfig(QString path)
{
    QFile conf(path);
    std::string ret;
    if(conf.open(QIODevice::ReadOnly)) {
        ret = conf.readAll().toStdString();
        conf.close();
    }
    return ret;
}

bool writeUserConfig(const std::string &path, Configuration &config)
{
    // set firstRun to false so we don't re-init on every run
    config.setValue<bool>("browser.firstRun", false);

    // The .path's need to be overriden because ~ doesn't translate to home
    const QString &home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

    // profile.path
    std::string profilePath = config.value<std::string>("profile.path").value();
    config.setValue<std::string>("profile.path", patchHome(profilePath, home.toStdString()));

    // bookmarks.path
    std::string bookmarksPath = config.value<std::string>("bookmarks.path").value();
    config.setValue<std::string>("bookmarks.path", patchHome(bookmarksPath, home.toStdString()));

    // downloads.path
    std::string downloadsPath = config.value<std::string>("downloads.path").value();
    config.setValue<std::string>("downloads.path", patchHome(downloadsPath, home.toStdString()));

    return config.writeUserConfiguration(path);
}

int main(int argc, char *argv[])
{
    // Create application object
    Browser instance(argc, argv);

    QCommandLineParser parser;
    parser.setApplicationDescription("yet another Qt 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");
    configOption.setDefaultValue(QStandardPaths::locate(QStandardPaths::AppConfigLocation, "smolbote.cfg"));
    parser.addOption(configOption);

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

    // print default config, so users can easily create their overrides
    QCommandLineOption printDefaultConfigOption("print-default-config", "Print default configuration.");
    parser.addOption(printDefaultConfigOption);

    // generate user config
    QCommandLineOption generateUserConfigOption("generate-user-config", "Generate user configuration and exit.");
    parser.addOption(generateUserConfigOption);

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

    QCommandLineOption newInstanceOption("new-instance", "Skip instance check at startup");
    parser.addOption(newInstanceOption);

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

    QCommandLineOption newTabOption("in-new-tab", "Open URL in new tab");
    parser.addOption(newTabOption);

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

    parser.process(instance);

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

    if(parser.isSet(printDefaultConfigOption)) {
        std::cout << readConfig(parser.value(defaultConfigOption));
        std::cout.flush();
        return 0;
    }

    std::shared_ptr<Configuration> config = std::make_shared<Configuration>();
    config->readDefaultConfiguration(readConfig(parser.value(defaultConfigOption)));
    config->readUserConfiguration(parser.value(configOption).toStdString());

    // check if first run
    if(config->value<bool>("browser.firstRun").value() || parser.isSet(generateUserConfigOption)) {
        // create a user config file
        QString path = parser.value(configOption);
        // there may be no smolbote.cfg
        if(path.isEmpty()) {
            path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/smolbote.cfg";
        }

#ifdef QT_DEBUG
        qDebug("Generating user config on first run to %s", qUtf8Printable(path));
#endif

        qDebug("Saving config to: %s - %s", qUtf8Printable(path), writeUserConfig(path.toStdString(), *config) ? "ok" : "failed");

        if(parser.isSet(generateUserConfigOption)) {
            return 0;
        }
    }

    instance.setConfiguration(config);

    // TODO: instance check

    instance.loadProfiles();

    MainWindow* mainWindow = instance.createWindow();
    if(parser.isSet(profileOption)) {
        mainWindow->setProfile(instance.profile(parser.value(profileOption)));
    }

    if(parser.positionalArguments().isEmpty()) {
        // no URLs were given
        mainWindow->newTab(QUrl::fromUserInput(config->value<std::string>("profile.homepage").value().c_str()));
    } else {
        for(const QString &url : parser.positionalArguments()) {
            mainWindow->newTab(QUrl::fromUserInput(url));
        }
    }

    return instance.exec();
}