aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/configuration/configuration.cpp')
-rw-r--r--lib/configuration/configuration.cpp205
1 files changed, 78 insertions, 127 deletions
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp
index 952f4b2..bd7700c 100644
--- a/lib/configuration/configuration.cpp
+++ b/lib/configuration/configuration.cpp
@@ -7,146 +7,97 @@
*/
#include "configuration.h"
-#include <QStandardPaths>
-#include <fstream>
-#include <boost/algorithm/string/predicate.hpp>
+#include <sstream>
+#include <algorithm>
+#include <stdexcept>
#include <iostream>
-#include <QCoreApplication>
-#include "config.h"
+#include <QStandardPaths>
-namespace po = boost::program_options;
+static std::unique_ptr<Configuration> s_conf;
-inline std::string defaultUserConfigLocation()
+inline void strip(std::string &value)
{
-#ifdef CONFIG_PATH_CONFIG
- return CONFIG_PATH_CONFIG;
-#else
- // try to locate an existing config
- QString path = QStandardPaths::locate(QStandardPaths::ConfigLocation, "smolbote/smolbote.cfg");
+ value.erase(value.begin(), std::find_if(value.begin(), value.end(), std::bind1st(std::not_equal_to<char>(), ' ')));
+ value.erase(std::find_if(value.rbegin(), value.rend(), std::bind1st(std::not_equal_to<char>(), ' ')).base(), value.end());
+}
- // 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
+Configuration::Configuration()
+ : use_global(true)
+{
+ if(!s_conf)
+ throw new std::runtime_error("Trying to use default Configuration, but none has been set!");
}
-Configuration::Configuration(int argc, char **argv, const std::string &path, QObject *parent)
- : QObject(parent)
- , m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString())
+Configuration::Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept
+ : m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString())
{
- configuration_desc.add_options()
- ("browser.stylesheet", po::value<std::string>())
- ("browser.iconTheme", po::value<std::string>(), "Icon theme")
- ("browser.locale", po::value<std::string>(), "Set Qt localization.")
- ("browser.translation", po::value<std::string>(), "Set application localization.")
-
- // sessions
- ("browser.session.path", po::value<std::string>()->default_value(CONFIG_PATH_SESSION))
-
-#ifdef CONFIG_USEBREAKPAD
- ("browser.crash.path", po::value<std::string>()->default_value(CONFIG_PATH_CRASHDUMP))
- ("browser.crash.handler", po::value<std::string>()->default_value(CONFIG_PATH_CRASHHANDLER))
-#endif
-
- // main window ui
- ("mainwindow.height", po::value<int>()->default_value(720))
- ("mainwindow.width", po::value<int>()->default_value(1280))
- ("mainwindow.maximized", po::value<bool>()->default_value(true))
- ("mainwindow.title", po::value<std::string>()->default_value(CONFIG_POI_NAME))
-
- // main window shortcuts
- ("mainwindow.shortcuts.saveSession", po::value<std::string>()->default_value("Ctrl+S,S"))
- ("mainwindow.shortcuts.openSession", po::value<std::string>()->default_value("Ctrl+S,O"))
-
- ("mainwindow.shortcuts.newGroup", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_NEWGROUP))
- ("mainwindow.shortcuts.newWindow", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_NEWWINDOW))
-
- ("mainwindow.shortcuts.about", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_ABOUT))
- ("mainwindow.shortcuts.quit", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_QUIT))
-
- ("mainwindow.shortcuts.search", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_SEARCH))
- ("mainwindow.shortcuts.tileWindows", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_TILE))
- ("mainwindow.shortcuts.cascadeWindows", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_CASCADE))
-
- // navigation
- ("navigation.movable", po::value<bool>()->default_value(false))
- ("navigation.shortcuts.back", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_BACK))
- ("navigation.shortcuts.backMenu", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_BACKMENU))
- ("navigation.shortcuts.forward", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_FORWARD))
- ("navigation.shortcuts.forwardMenu", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_FORWARDMENU))
- ("navigation.shortcuts.refresh", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_REFRESH))
- ("navigation.shortcuts.reload", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_RELOAD))
- ("navigation.shortcuts.home", po::value<std::string>()->default_value(CONFIG_SHORTCUT_NAVIGATION_HOME))
-
- // address bar
- ("addressbar.shortcuts.focus", po::value<std::string>()->default_value(CONFIG_SHORTCUT_ADDRESS_FOCUS))
- ("addressbar.shortcuts.menu", po::value<std::string>()->default_value(CONFIG_SHORTCUT_ADDRESS_MENU))
-
- // subwindow
- ("subwindow.shortcuts.menu", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_MENU))
- ("subwindow.shortcuts.new", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_NEWTAB))
- ("subwindow.shortcuts.close", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_CLOSETAB))
- ("subwindow.shortcuts.restoreTab", po::value<std::string>()->default_value("Ctrl+Shift+T"))
- ("subwindow.shortcuts.left", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_TABLEFT))
- ("subwindow.shortcuts.moveLeft", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_MOVETABLEFT))
- ("subwindow.shortcuts.right", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_TABRIGHT))
- ("subwindow.shortcuts.moveRight", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_MOVETABRIGHT))
- ("subwindow.shortcuts.fullscreen", po::value<std::string>()->default_value(CONFIG_SHORTCUT_SUBWINDOW_FULLSCREEN))
-
- // Filter settings
- ("filter.hosts", po::value<std::string>()->default_value(CONFIG_PATH_FILTER))
- ("filter.adblock", po::value<std::string>())
- ("filter.header", po::value<std::vector<std::string>>())
-// ("filter.cookies.block.all", po::value<bool>()->default_value(false))
-// ("filter.cookies.block.thirdParty", po::value<bool>()->default_value(true))
-// ("filter.cookies.path", po::value<std::string>()->default_value("~/.config/smolbote/cookies.d"))
-
- // Plugin settings
- ("plugins.path", po::value<std::string>()->default_value(CONFIG_PATH_PLUGINS))
-
- // Profile settings
- // default profile name the browser should use; "" is off-the-record
- ("profile.default", po::value<std::string>()->default_value(CONFIG_PROFILE_DEFAULT))
- ("profile.path", po::value<std::string>()->default_value(CONFIG_PATH_PROFILES))
- ("profile.search", po::value<std::string>()->default_value(CONFIG_PROFILE_DEFAULT_SEARCH))
- ("profile.homepage", po::value<std::string>()->default_value(CONFIG_PROFILE_DEFAULT_HOMEPAGE))
- ("profile.newtab", po::value<std::string>()->default_value(CONFIG_PROFILE_DEFAULT_NEWTAB))
-
- // Bookmark settings
- ("bookmarks.path", po::value<std::string>()->default_value(CONFIG_PATH_BOOKMARKS))
- ("bookmarks.shortcut", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_BOOKMARKS))
-
- // Downloads settings
- ("downloads.path", po::value<std::string>()->default_value(CONFIG_PATH_DOWNLOADS))
- ("downloads.shortcut", po::value<std::string>()->default_value(CONFIG_SHORTCUT_WINDOW_DOWNLOADS))
- ;
-
- // po::store will only overwrite values that are default, so:
- // 1. parse command line
- {
- auto cmd = po::command_line_parser(argc, argv);
- cmd.allow_unregistered();
- cmd.options(configuration_desc);
- po::store(cmd.run(), vm);
- }
- // 2. parse config file
- {
- std::ifstream f(path, std::ifstream::in);
- // parse_config_file(file, options_description, allow_unregistered)
- po::store(po::parse_config_file(f, configuration_desc, true), vm);
- }
+ for(const auto &i : l) {
+ insert_or_assign(i.first, i.second);
+ }
}
-QHash<QString, QString> Configuration::section(const std::string &prefix) const
+void Configuration::read(std::basic_istream<char> &input)
{
- QHash<QString, QString> v;
- for(auto &s : configuration_desc.options()) {
- if(boost::starts_with(s->long_name(), prefix)) {
- v[s->long_name().c_str()] = QString::fromStdString(value<std::string>(s->long_name().c_str()).value());
+ std::string line, key, value;
+ std::istringstream is_line;
+
+ while(std::getline(input, line)) {
+ if(line[0] == '#' || line.length() == 0)
+ continue;
+
+ is_line.clear();
+ is_line.str(line);
+
+ if(std::getline(is_line, key, '=')) {
+ is_line >> value;
+
+ strip(key);
+ strip(value);
+
+ if(this->count(key) == 0) {
+ // no type has been specified for this key, assuming std::string
+ insert(std::make_pair(key, value));
+ continue;
+ }
+
+ auto v = at(key);
+ if(std::holds_alternative<std::string>(v)) {
+ at(key) = value;
+ } else if(std::holds_alternative<int>(v)) {
+ at(key) = std::stoi(value);
+ } else if(std::holds_alternative<bool>(v)) {
+ at(key) = (value == "true");
+ }
+
}
}
+}
+
- return v;
+void Configuration::move_global(std::unique_ptr<Configuration> &&conf)
+{
+ s_conf = std::move(conf);
+}
+
+Configuration* Configuration::instance()
+{
+ return s_conf.get();
+}
+
+void setShortcut(QAction *action, const char *name)
+{
+ if(!s_conf)
+ throw new std::runtime_error("Trying to set a shortcut, but no configuration has been set!");
+
+ if(const auto shortcutText = s_conf->value<QString>(name)) {
+ const QString tooltip = action->toolTip();
+ action->setShortcut(QKeySequence::fromString(shortcutText.value()));
+ action->setToolTip(QString("%1 (%2)").arg(tooltip, shortcutText.value()));
+ }
+#ifdef QT_DEBUG
+ else {
+ std::cout << "fixme: setShortcut called for " << name << ", but no such value exists!" << std::endl;
+ }
+#endif
}