From f3a4607d6a722a862af0eb9747a15dcdf624b6fb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 3 Nov 2019 00:18:10 +0200 Subject: Drop boost dependency - wrote not-invented-here config file parser and conf class - spent obscene amount of time plugging in said conf class --- src/main.cpp | 98 +++++++++++++++++++++++------------------------------------- 1 file changed, 38 insertions(+), 60 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 02a1168..c6f4120 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,15 +15,15 @@ #include "util.h" #include "version.h" #include -#include #include -#include #include #include #include #include #include #include +#include "conf.hpp" +#include typedef std::function::const_iterator, std::vector::const_iterator)> subcommand_func; typedef std::unordered_map command_map; @@ -42,24 +42,6 @@ inline std::string join_keys(const command_map &map, const std::string sep = ", return k; } -#include -inline std::string defaultUserConfigLocation() -{ -#ifdef CONFIG_PATH_CONFIG - return CONFIG_PATH_CONFIG; -#else - // try to locate an existing config - QString path = QStandardPaths::locate(QStandardPaths::ConfigLocation, "smolbote/smolbote.cfg"); - - // it's possible there is no config, so set the path properly - if(path.isEmpty()) - path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/smolbote/smolbote.cfg"; - - return path.toStdString(); -#endif -} - -#include "config.h" #if defined(CONFIG_USEPLASMA) && !defined(PLASMA) #error "You have enabled Plasma integration, but Frameworks was not found." #endif @@ -94,7 +76,7 @@ int main(int argc, char **argv) args::PositionalList cmd_args(parser, "URL(s)", "List of URLs to open"); try { - auto next = parser.ParseArgs(args); + /*auto next = */parser.ParseArgs(args); if(cmd_version) return builtins::version(); @@ -121,52 +103,48 @@ int main(int argc, char **argv) // create and load configuration const std::string config_path = [&]() { + std::string path; if(cmd_config) - return args::get(cmd_config); + path = args::get(cmd_config); else - return defaultUserConfigLocation(); + path = std::string(CONFIG_POI_CFG_PATH); + + if(path.front() == '~') + path.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()); + + return path; }(); - std::unique_ptr config = std::make_unique(argc, argv, config_path); - QTranslator translator; - if(config->exists("browser.translation")) { - translator.load(config->value("browser.translation").value()); - } + spdlog::debug("Opening config file {}", config_path); + init_conf(config_path); + QVector plugins; CommandHash_t pluginCommands; // Load plugins - for(const QString &path : Util::files(config->value("plugins.path").value(), { "*.so", "*.dll" })) { - auto *loader = new PluginLoader(path); - const bool loaded = loader->load(); - spdlog::info("{} plugin {}", loaded ? "Loaded" : "Failed to load", qUtf8Printable(path)); - - if(loaded) { - plugins.append(loader); - auto *plugin = qobject_cast(loader->instance()); - pluginCommands.unite(plugin->commands()); - } else { - spdlog::warn("{}", qUtf8Printable(loader->errorString())); - delete loader; - } + [&]() { + Configuration conf; + spdlog::debug("plugins.path={}", conf.value("plugins.path").value()); + for(const QString &path : Util::files(conf.value("plugins.path").value(), { "*.so", "*.dll" })) { + auto *loader = new PluginLoader(path); + const bool loaded = loader->load(); + spdlog::info("{} plugin {}", loaded ? "Loaded" : "Failed to load", qUtf8Printable(path)); + + if(loaded) { + plugins.append(loader); + auto *plugin = qobject_cast(loader->instance()); + pluginCommands.unite(plugin->commands()); + } else { + spdlog::warn("{}", qUtf8Printable(loader->errorString())); + delete loader; + } } + }(); // argc, argv, allowSecondary Browser app(argc, argv); // set this, otherwise the webview becomes black when using a stylesheet app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - app.installTranslator(&translator); - if(config->exists("browser.locale")) { - auto *locale = new QTranslator(&app); - if(locale->load("qt_" + config->value("browser.locale").value(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) - app.installTranslator(locale); - else - delete locale; - } - - if(auto iconTheme = config->value("browser.iconTheme")) { - QIcon::setThemeName(iconTheme.value()); - } #ifdef CONFIG_USEBREAKPAD const std::string crashpath = config->value("browser.crash.path").value_or("/tmp"); @@ -186,14 +164,14 @@ int main(int argc, char **argv) // minidump descriptor, filter callback, minidump callback, callback_context, install handler, server_fd google_breakpad::ExceptionHandler eh(descriptor, nullptr, CrashHandler::dumpCallback, &ctx, true, -1); -#ifdef QT_DEBUG - spdlog::info("Installed breakpad exception handler (path {})", crashpath); -#endif + spdlog::debug("Installed breakpad exception handler (path {})", crashpath); #endif // CONFIG_USEBREAKPAD - const auto profile = config->value("profile.default"); + const auto profile = [](){ + Configuration c; + return c.value("profile.default").value(); + }(); - app.setConfiguration(config); // app takes ownership of config app.setup(plugins); QStringList urls; @@ -225,7 +203,7 @@ int main(int argc, char **argv) if(const auto pick = dlg->pickSession()) sessionData = pick.value(); else - sessionData = Session::fromCommandLine(profile.value(), urls); + sessionData = Session::fromCommandLine(profile, urls); } else if(cmd_session) { QFile sessionJson(QString::fromStdString(args::get(cmd_session))); if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -233,7 +211,7 @@ int main(int argc, char **argv) sessionJson.close(); } } else { - sessionData = Session::fromCommandLine(profile.value(), urls); + sessionData = Session::fromCommandLine(profile, urls); } if(app.isPrimary() || cmd_noRemote) { -- cgit v1.2.1