diff options
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r-- | src/configuration.cpp | 228 |
1 files changed, 82 insertions, 146 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp index b281b70..4e72d18 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -7,163 +7,99 @@ */ #include "configuration.h" -#include <QtCore/QStandardPaths> -#include <QFile> -#include <sstream> +#include <QStandardPaths> +#include <fstream> -using namespace libconfig; +namespace po = boost::program_options; -Configuration::Configuration(const std::string &path, const std::string &home) +Configuration::Configuration(const QStringList &options) { - m_userCfg = new Config(); - // prettier output - m_userCfg->setOption(Config::OptionSemicolonSeparators, true); - m_userCfg->setOption(Config::OptionColonAssignmentForGroups, false); - m_userCfg->setOption(Config::OptionColonAssignmentForNonGroups, false); - m_userCfg->setOption(Config::OptionOpenBraceOnSeparateLine, false); - m_userCfg->setOption(Config::OptionFsync, true); - - m_userCfgPath = path; - m_homePath = home; -} - -Configuration::~Configuration() -{ - delete m_userCfg; -} - -bool Configuration::read(const QString &path) -{ - QFile conf(path); - - if(!conf.open(QIODevice::ReadOnly)) { - return false; - } - - try { - m_userCfg->readString(conf.readAll().toStdString()); - conf.close(); - } catch(const ParseException &e) { - return false; - } - - return true; -} - -bool Configuration::writeIfNeeded(const std::string &path) -{ - if(!path.empty()) - m_userCfgPath = path; - - if(!changed) - return true; - - try { - m_userCfg->writeFile(m_userCfgPath.c_str()); - } catch(const FileIOException &e) { - return false; - } - - changed = false; - return true; -} - -std::vector<std::string> Configuration::childrenSettings(const char *name) -{ - std::vector<std::string> groupNames; - const Setting &root = m_userCfg->lookup(name); - - for(const Setting &setting : root) { - if(setting.getType() != Setting::TypeGroup) { - groupNames.emplace_back(setting.getName()); - //groupNames.push_back(setting.getName()); + m_homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString(); + + // create description + desc.add_options() + + // Browser default settings + // default profile name the browser should use; "" is off-the-record + ("browser.profile", po::value<std::string>()->default_value("")) + + // default window size + ("browser.window.height", po::value<int>()->default_value(720)) + ("browser.window.width", po::value<int>()->default_value(1280)) + ("browser.window.maximized", po::value<bool>()->default_value(true)) + ("browser.window.title", po::value<std::string>()->default_value("title — smolbote [profile]")) + + // window ui + ("browser.ui.navtoolbarMovable", po::value<bool>()->default_value(false)) + ("browser.ui.tabtoolbarMovable", po::value<bool>()->default_value(false)) + + // browser shortcuts + + // browser menu + ("browser.shortcuts.newWindow", po::value<std::string>()->default_value("Ctrl+N")) + ("browser.shortcuts.newTab", po::value<std::string>()->default_value("Ctrl+T")) + ("browser.shortcuts.about", po::value<std::string>()->default_value("F1")) + ("browser.shortcuts.quit", po::value<std::string>()->default_value("Ctrl+Q")) + + // navigation + ("browser.shortcuts.back", po::value<std::string>()->default_value("Ctrl+Left")) + ("browser.shortcuts.forward", po::value<std::string>()->default_value("Ctrl+Right")) + ("browser.shortcuts.refresh", po::value<std::string>()->default_value("F5")) + ("browser.shortcuts.reload", po::value<std::string>()->default_value("Ctrl+F5")) + ("browser.shortcuts.home", po::value<std::string>()->default_value("Ctrl+Home")) + + // tabs + ("browser.shortcuts.tabClose", po::value<std::string>()->default_value("Ctrl+X")) + ("browser.shortcuts.tabLeft", po::value<std::string>()->default_value("Ctrl+O")) + ("browser.shortcuts.tabRight", po::value<std::string>()->default_value("Ctrl+P")) + + // page + ("browser.shortcuts.toggleSearchBox", po::value<std::string>()->default_value("F3")) + ("browser.shortcuts.focusAddress", po::value<std::string>()->default_value("F4")) + ("browser.shortcuts.fullscreen", po::value<std::string>()->default_value("F11")) + + // Filter settings + ("filter.path", po::value<std::string>()->default_value("~/.config/smolbote/hosts.d")) + + // Plugin settings + ("plugins.path", po::value<std::string>()->default_value("~/.config/smolbote/plugins.d")) + + // Profile settings + ("profile.path", po::value<std::string>()->default_value("~/.config/smolbote/profiles.d")) + ("profile.search", po::value<std::string>()->default_value("https://duckduckgo.com/?q=$term&ia=web")) + ("profile.homepage", po::value<std::string>()->default_value("about:blank")) + ("profile.newtab", po::value<std::string>()->default_value("about:blank")) + + // Bookmark settings + ("bookmarks.path", po::value<std::string>()->default_value("~/.config/smolbote/bookmarks.xbel")) + ("bookmarks.shortcut", po::value<std::string>()->default_value("Ctrl+B")) + + // Downloads settings + ("downloads.path", po::value<std::string>()->default_value("~/Downloads")) + ("downloads.shortcut", po::value<std::string>()->default_value("Ctrl+D")) + ; + + // store the defaults into the vm + { + int argc = options.length(); + const char* argv[argc]; + for(int i = 0; i < argc; ++i) { + argv[i] = qUtf8Printable(options.at(i)); } + + po::store(po::parse_command_line(argc, argv, desc), vm); } - return groupNames; } -std::vector<std::string> Configuration::childrenGroups(const char *name) +Configuration::~Configuration() { - std::vector<std::string> groupNames; - const Setting &root = m_userCfg->lookup(name); - - for(const Setting &setting : root) { - if(setting.getType() == Setting::TypeGroup) { - groupNames.emplace_back(setting.getName()); - //groupNames.push_back(setting.getName()); - } - } - return groupNames; } -std::string castToString(const libconfig::Setting &v) +bool Configuration::read(const QString &path) { - // cast depending on type - // type checks are done during compile time - switch(v.getType()) { - case Setting::TypeNone: - return std::string(); - - case Setting::TypeInt: - // int, unsigned int, long, unsigned long - return std::to_string(static_cast<int32_t>(v)); - - case Setting::TypeInt64: - // int, unsigned int; long long, unsigned long long - return std::to_string(static_cast<int64_t>(v)); - - case Setting::TypeFloat: - // float, double - return std::to_string(static_cast<double>(v)); - - case Setting::TypeString: - // const char*, std::string - return std::string(static_cast<const char *>(v)); - - case Setting::TypeBoolean: - // bool - return std::string(static_cast<bool>(v) ? "true" : "false"); - - case Setting::TypeGroup: - case Setting::TypeArray: - case Setting::TypeList: - return std::string(); - } + std::ifstream f(path.toStdString(), std::ifstream::in); + po::store(po::parse_config_file(f, desc, false), vm); + return true; } -void setFromString(libconfig::Setting &setting, const std::string &value) -{ - switch(setting.getType()) { - case libconfig::Setting::TypeNone: - break; - - case libconfig::Setting::TypeInt: - case libconfig::Setting::TypeInt64: - setting = std::stoi(value); - break; - - case libconfig::Setting::TypeFloat: - setting = std::stod(value); - break; - - case libconfig::Setting::TypeString: - setting = value.c_str(); - break; - - case libconfig::Setting::TypeBoolean: - if(value == "true") { - setting = true; - } else if(value == "false") { - setting = false; - } - break; - - case libconfig::Setting::TypeGroup: - break; - case libconfig::Setting::TypeArray: - break; - case libconfig::Setting::TypeList: - break; - } -} |