diff options
-rw-r--r-- | lib/configuration/configuration.cpp | 13 | ||||
-rw-r--r-- | lib/configuration/configuration.h | 2 | ||||
-rw-r--r-- | lib/configuration/test/main.cpp | 2 | ||||
-rw-r--r-- | plugins/AdblockFilter/options.cpp | 2 | ||||
-rw-r--r-- | plugins/AdblockFilter/options.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 149 | ||||
-rw-r--r-- | src/settings.h.in | 29 |
7 files changed, 100 insertions, 99 deletions
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index 75f863c..6686520 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -17,7 +17,7 @@ #include <QStandardPaths> #endif -static std::unique_ptr<Configuration> s_conf; +static Configuration *s_conf = nullptr; Configuration::Configuration() : use_global(true) @@ -131,14 +131,19 @@ void Configuration::read(std::basic_istream<char> &input) } } -void Configuration::move_global(std::unique_ptr<Configuration> &&conf) +bool Configuration::make_global() { - s_conf = std::move(conf); + if(use_global || s_conf != nullptr) { + return false; + } + + s_conf = this; + return true; } Configuration *Configuration::instance() { - return s_conf.get(); + return s_conf; } std::ostream &operator<<(std::ostream &out, const Configuration &obj) diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index cd3c244..c2400b6 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -126,7 +126,7 @@ public: return T{}; } - static void move_global(std::unique_ptr<Configuration> && conf); + bool make_global(); private: static Configuration *instance(); diff --git a/lib/configuration/test/main.cpp b/lib/configuration/test/main.cpp index fa04f10..06f7cb0 100644 --- a/lib/configuration/test/main.cpp +++ b/lib/configuration/test/main.cpp @@ -151,7 +151,7 @@ SCENARIO("Configuration") output << *global_conf; REQUIRE(output.str() == "name=global\nnumber=123\ntoggle=true\n"); - Configuration::move_global(std::move(global_conf)); + global_conf->make_global(); Configuration g; REQUIRE(g.value<std::string>("name")); REQUIRE(g.value<std::string>("name").value() == "global"); diff --git a/plugins/AdblockFilter/options.cpp b/plugins/AdblockFilter/options.cpp index 08f30ee..ad231bf 100644 --- a/plugins/AdblockFilter/options.cpp +++ b/plugins/AdblockFilter/options.cpp @@ -34,7 +34,7 @@ constexpr std::array abpTypeOptions = { "__unknown" // ResourceTypeUnknown 255 Unknown request type. }; -auto parseTypeOption(QStringRef &option) +inline auto parseTypeOption(QStringRef &option) { struct { bool found = false; diff --git a/plugins/AdblockFilter/options.h b/plugins/AdblockFilter/options.h index efc47a6..9f7a374 100644 --- a/plugins/AdblockFilter/options.h +++ b/plugins/AdblockFilter/options.h @@ -37,7 +37,7 @@ struct Options { bool parseAbp(const QStringRef &options); // TODO private: - std::bitset<32> flags; + std::bitset<32> flags = 0; }; } // namespace AdblockPlus diff --git a/src/main.cpp b/src/main.cpp index dad1f73..3976f53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,7 +77,9 @@ int main(int argc, char **argv) return builtins::build(); // create and load configuration - spdlog::debug("Loading configuration {}", init_conf(args::get(cmd_config))); + auto conf = init_conf(args::get(cmd_config)); + conf.ptr->make_global(); + spdlog::debug("Loading configuration {}", conf.path); if(cmd_args) { const auto front = args::get(cmd_args).front(); @@ -87,92 +89,87 @@ int main(int argc, char **argv) } } - } catch(args::Help &) { - std::cout << parser; - return 0; - - } catch(args::Error &e) { - std::cerr << e.what() << std::endl; - std::cerr << parser; - return -1; - } - - // argc, argv, allowSecondary - Browser app(argc, argv); - // set this, otherwise the webview becomes black when using a stylesheet - app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - - { - Configuration conf; - - if(conf.value<bool>("usebreakpad").value()) { - CrashHandler::Context ctx( - conf.value<std::string>("path.crashdump").value(), - conf.value<std::string>("path.crashhandler").value()); - - if(CrashHandler::install_handler(ctx)) { - spdlog::info("Installed breakpad crash handler: {}", ctx.dumppath); - } else { - spdlog::warn("Failed to install breakpad crash handler: {}", ctx.dumppath); + // 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(conf.ptr->value<bool>("usebreakpad").value()) { + CrashHandler::Context ctx( + conf.ptr->value<std::string>("path.crashdump").value(), + conf.ptr->value<std::string>("path.crashhandler").value()); + + if(CrashHandler::install_handler(ctx)) { + spdlog::info("Installed breakpad crash handler: {}", ctx.dumppath); + } else { + spdlog::warn("Failed to install breakpad crash handler: {}", ctx.dumppath); + } } - } - // 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 { - spdlog::warn("Failed loading plugin [{}]", qUtf8Printable(path)); + // 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 = []() { - Configuration c; - return c.value<QString>("profile.default").value(); - }(); + 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()); - } + QStringList urls; + for(const auto &u : args::get(cmd_args)) { + urls.append(QString::fromStdString(u)); + } + if(urls.isEmpty()) { + urls.append(QString()); + } - // 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()); - }); - } + // 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()); + { + 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()); + 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()); + } else { + // app is secondary and not standalone + return app.sendMessage(session.serialize()); } - return JsonSession(profile, urls); - }(); - - if(app.isPrimary() || cmd_noRemote) { - app.open(session.get()); - } else { - // app is secondary and not standalone - return app.sendMessage(session.serialize()); } - } - return app.exec(); + return app.exec(); + + } catch(args::Help &) { + std::cout << parser; + return 0; + + } catch(args::Error &e) { + std::cerr << e.what() << std::endl; + std::cerr << parser; + return -1; + } } diff --git a/src/settings.h.in b/src/settings.h.in index 88fbcf5..605288e 100644 --- a/src/settings.h.in +++ b/src/settings.h.in @@ -4,29 +4,28 @@ #include <configuration.h> #include <fstream> -inline const std::string init_conf(const std::string &path) +inline auto init_conf(const std::string &path) { - auto value_map = std::make_unique<Configuration, std::initializer_list<std::pair<std::string, conf_value_t>>>({ + struct { + std::string path; + std::unique_ptr<Configuration> ptr; + } conf; + + conf.ptr = std::make_unique<Configuration, std::initializer_list<std::pair<std::string, conf_value_t>>>({ @__DEFAULT_CFG__ }); - const std::string cfgpath = [&]() { - if(path.empty()) - return value_map->value<std::string>("poi.cfg.path").value(); - - auto p = path; - if(p.front() == '~') - p.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()); - return p; - }(); + conf.path = path.empty() ? conf.ptr->value<std::string>("poi.cfg.path").value() : path; + if(conf.path.front() == '~') { + conf.path.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()); + }; std::fstream fs; - fs.open(cfgpath, std::fstream::in); + fs.open(conf.path, std::fstream::in); if(fs.is_open()) { - value_map->read(fs); + conf.ptr->read(fs); fs.close(); } - Configuration::move_global(std::move(value_map)); - return cfgpath; + return conf; } |