aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp175
1 files changed, 99 insertions, 76 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 9df24b2..837e06c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -20,121 +20,144 @@
#include <iostream>
#include "browser.h"
-#include "mainwindow.h"
-#include <QApplication>
#include <QCommandLineParser>
+#include <QFile>
+#include <QStandardPaths>
+#include "mainwindow.h"
-/**
- * @brief printPoiToml Print the built-in config
- * @param output std::ostream to print to
- */
-void printPoiToml(std::ostream &output)
+// read config into std::string, supports qrc
+inline std::string readConfig(QString path)
{
- QFile conf(":/poi.toml");
+ QFile conf(path);
+ std::string ret;
if(conf.open(QIODevice::ReadOnly)) {
- output << conf.readAll().toStdString() << std::endl;
+ 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);
}
-const QHash<QString, QVariant> parseCommandLine(QApplication &app)
+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();
- QCommandLineOption configOption(QStringList() << "c" << "config", "Set configuration file.", "PATH");
+ // 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);
- QCommandLineOption defaultConfigOption(QStringList() << "print-defaults", "Print default configuration.");
+ // default config, :/poi.cfg
+ QCommandLineOption defaultConfigOption("default-config", "Set the default configuration file.", "path");
+ defaultConfigOption.setDefaultValue(":/poi.cfg");
parser.addOption(defaultConfigOption);
- QCommandLineOption profileOption(QStringList() << "p" << "profile", "Use this profile.", "PROFILE");
- parser.addOption(profileOption);
+ // print default config, so users can easily create their overrides
+ QCommandLineOption printDefaultConfigOption("print-default-config", "Print default configuration.");
+ parser.addOption(printDefaultConfigOption);
- //QCommandLineOption nopluginsOption(QStringList() << "n" << "no-plugins", "Don't load plugins");
- //parser.addOption(nopluginsOption);
+ // generate user config
+ QCommandLineOption generateUserConfigOption("generate-user-config", "Generate user configuration and exit.");
+ parser.addOption(generateUserConfigOption);
- QCommandLineOption newInstanceOption(QStringList() << "new-instance", "Skip instance check at startup");
+ 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(QStringList() << "in-new-window", "Open URL in new window");
+ QCommandLineOption newWindowOption("in-new-window", "Open URL in new window");
parser.addOption(newWindowOption);
- QCommandLineOption newTabOption(QStringList() << "in-new-tab", "Open URL in new tab");
+ QCommandLineOption newTabOption("in-new-tab", "Open URL in new tab");
parser.addOption(newTabOption);
parser.addPositionalArgument("URL", "URL(s) to open");
- parser.process(app);
+ parser.process(instance);
- QHash<QString, QVariant> options;
- if(parser.isSet(configOption)) {
- options.insert("config", parser.value(configOption));
- }
- if(parser.isSet(defaultConfigOption)) {
- options.insert("print-defaults", true);
- }
- if(parser.isSet(profileOption)) {
- options.insert("profile", parser.value(profileOption));
- }
- if(parser.isSet(newInstanceOption)) {
- options.insert("new-instance", true);
- }
- if(parser.isSet(newWindowOption)) {
- options.insert("in-new-window", true);
- }
- if(parser.isSet(newTabOption)) {
- options.insert("in-new-tab", true);
- }
- options.insert("urls", parser.positionalArguments());
+#ifdef QT_DEBUG
+ qDebug("config=%s", qUtf8Printable(parser.value(configOption)));
+ qDebug("default-config=%s", qUtf8Printable(parser.value(defaultConfigOption)));
+#endif
- return options;
-}
+ if(parser.isSet(printDefaultConfigOption)) {
+ std::cout << readConfig(parser.value(defaultConfigOption));
+ std::cout.flush();
+ return 0;
+ }
-int main(int argc, char *argv[])
-{
- // Create application object
- Browser app(argc, argv);
-
- // program init
- {
- // parse command line options
- const QHash<QString, QVariant> options = parseCommandLine(app);
-
- // Check for another instance unless specified not to
- if(!options.contains("new-instance")) {
- app.bindLocalSocket();
- if(app.isRunning()) {
- qDebug("Another instance is running, returning...");
- app.sendMessage(options);
- return 0;
- }
+ std::shared_ptr<Configuration> config = std::make_shared<Configuration>();
+ config->readDefaultConfiguration(readConfig(parser.value(defaultConfigOption)));
+ config->readUserConfiguration(parser.value(configOption).toStdString());
+ instance.setConfiguration(config);
+
+ // 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";
}
- if(options.contains("print-defaults")) {
- printPoiToml(std::cout);
+#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;
}
+ }
- app.setWindowIcon(QIcon(QLatin1String(":/icon.svg")));
- // This lets the web view automatically scale on high-dpi displays.
- app.setAttribute(Qt::AA_EnableHighDpiScaling);
-
- // Set configuration
- app.loadSettings(options.value("config", "").toString());
+ // TODO: instance check
- // Load profiles
- app.loadProfiles();
+ instance.loadProfiles();
- // Load plugins
- // if(!parser.isSet(nopluginsOption)) {
- // app.loadPlugins();
- // }
+ MainWindow* mainWindow = instance.createWindow();
+ if(parser.isSet(profileOption)) {
+ mainWindow->setProfile(instance.profile(parser.value(profileOption)));
+ }
- app.createWindow(options);
+ 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 app.exec();
+ return instance.exec();
}