diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 160 |
1 files changed, 32 insertions, 128 deletions
diff --git a/src/main.cpp b/src/main.cpp index 97b529b..88fa657 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,162 +1,66 @@ /* * 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/gitea/aqua/smolbote + * location: https://neueland.iserlohn-fortress.net/cgit/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "browser.h" -#include "builtins.h" +#include "cmd/cmd.hpp" #include "configuration.h" -#include "session/sessiondialog.h" -#include "session_json.hpp" -#include "settings.h" #include "util.h" -#include "version.h" #include <QFile> #include <QPluginLoader> #include <QStandardPaths> -#include <iostream> -#include <memory> #include <pluginloader.h> #include <spdlog/spdlog.h> -// a helper function to join the keys of a command_map into a string -inline std::string join_keys(const command_map &map, const std::string sep = ", ") +namespace builtins { - std::vector<std::string> keys(map.size()); - std::transform(map.begin(), map.end(), keys.begin(), [](auto pair) { return pair.first; }); - std::sort(keys.begin(), keys.end()); - - std::string k; - std::for_each(keys.begin(), keys.end() - 1, [&k, &sep](const std::string &piece) { k += piece + sep; }); - k += keys.back(); - - return k; +int sub_configuration(const QStringList &l, Browser &); +int sub_bookmarks(const QStringList &l, Browser &); +int sub_session(const QStringList &l, Browser &); } int main(int argc, char **argv) { - // change log pattern - spdlog::set_pattern("[%^%l%$] [%P:%t] %v"); + // spdlog pattern + if(const char *env_spdlog = std::getenv("POI_LOG_PATTERN")) + spdlog::set_pattern(env_spdlog); + else + spdlog::set_pattern("[%^%l%$] [%P:%t] %v"); + #ifdef QT_DEBUG spdlog::set_level(spdlog::level::debug); // Set global log level to debug #endif - const command_map commands{ - { "configuration", builtins::configuration }, - { "bookmarks", builtins::bookmarks }, + const command_line::map<Browser> subcommands{ + { "configuration", builtins::sub_configuration }, + { "bookmarks", builtins::sub_bookmarks }, + { "session", builtins::sub_session }, }; - const std::vector<std::string> args(argv + 1, argv + argc); - args::ArgumentParser parser("smolbote: yet another no-frills browser", "Subcommands: " + join_keys(commands)); - parser.Prog(argv[0]); - - args::HelpFlag cmd_help(parser, "help", "Display this help message.", { 'h', "help" }); - args::Flag cmd_version(parser, "version", "Display version information.", { 'v', "version" }); - args::Flag cmd_build(parser, "build", "Display build commit.", { 'b', "build" }); - - args::ValueFlag<std::string> cmd_config(parser, "config", "Set the configuration file.", { 'c', "config" }); - - args::Flag cmd_noRemote(parser, "no-remote", "Do not accept or send remote commands.", { "no-remote" }); - - args::Flag cmd_pickSession(parser, "pick-session", "Show all available sessions and select which one to open", { "pick-session" }); - args::ValueFlag<std::string> cmd_session(parser, "session", "Open the specified session.", { 's', "session" }); - - args::PositionalList<std::string> cmd_args(parser, "URL(s)", "List of URLs to open"); - cmd_args.KickOut(true); - - try { - auto next = parser.ParseArgs(args); - - if(cmd_version) - return builtins::version(); - if(cmd_build) - return builtins::build(); - - // create and load configuration - auto conf = init_conf(args::get(cmd_config)); - conf.ptr->make_global(); - spdlog::debug("Loading configuration {}", conf.path); + // argc, argv, allowSecondary + Browser app(argc, argv); + // set this, otherwise the webview becomes black when using a stylesheet + app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - if(cmd_args) { - const auto front = args::get(cmd_args).front(); - const auto cmd = commands.find(front); - if(cmd != commands.end()) { - return cmd->second(argv[0], next, std::end(args)); - } - } - - // argc, argv, allowSecondary - Browser app(argc, argv); - // set this, otherwise the webview becomes black when using a stylesheet - app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - - { - // load plugins - for(const QString &path : Util::files(conf.ptr->value<QString>("plugins.path").value(), { "*.so", "*.dll" })) { - if(app.loadPlugin(path)) { - spdlog::debug("Loaded plugin [{}]", qUtf8Printable(path)); - } else { - spdlog::warn("Failed loading plugin [{}]", qUtf8Printable(path)); - } - } - } - - const auto profile = conf.ptr->value<QString>("profile.default").value(); - - QStringList urls; - for(const auto &u : args::get(cmd_args)) { - urls.append(QString::fromStdString(u)); - } - if(urls.isEmpty()) { - urls.append(QString()); - } + const auto f = command_line::process<Browser>(app, subcommands, "session"); - // if app is primary, create new sessions from received messages - if(app.isPrimary() && !cmd_noRemote) { - QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, QByteArray message) { - Q_UNUSED(instanceId); - JsonSession session(message); - app.open(session.get()); - }); - } - - { - const auto session = [&]() { - if(cmd_session) { - QFile sessionJson(QString::fromStdString(args::get(cmd_session))); - if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { - return JsonSession(sessionJson.readAll()); - } - } - if(cmd_pickSession) { - SessionDialog dlg; - if(const auto pick = dlg.pickSession()) { - return JsonSession(pick.value()); - } - } - return JsonSession(profile, urls); - }(); - - if(app.isPrimary() || cmd_noRemote) { - app.open(session.get()); + { + Configuration conf; + const auto plugins_path = conf.value<QString>("plugins.path").value(); + // load plugins + /*for(const QString &path : Util::files(conf.value<QString>("plugins.path").value(), { "*.so", "*.dll" })) { + if(app.loadPlugin(path)) { + spdlog::debug("Loaded plugin [{}]", qUtf8Printable(path)); } else { - // app is secondary and not standalone - return app.sendMessage(session.serialize()); + spdlog::warn("Failed loading plugin [{}]", qUtf8Printable(path)); } - } - - return app.exec(); + }*/ + } - } catch(args::Help &) { - std::cout << parser; - return 0; + return f(app); - } catch(args::Error &e) { - std::cerr << e.what() << std::endl; - std::cerr << parser; - return -1; - } } |