aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-03-15 16:26:38 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-03-15 16:26:38 +0100
commit44421abbe89be2c6a6290182571fff82dfec9651 (patch)
tree69d7980953968f900411db190254a4d05acd2cae /lib/configuration
parentAdd missing AUTOUIC in ProfileEditor (diff)
downloadsmolbote-44421abbe89be2c6a6290182571fff82dfec9651.tar.xz
Moved Configuration class into library
Diffstat (limited to 'lib/configuration')
-rw-r--r--lib/configuration/CMakeLists.txt8
-rw-r--r--lib/configuration/configuration.cpp108
-rw-r--r--lib/configuration/configuration.h76
3 files changed, 192 insertions, 0 deletions
diff --git a/lib/configuration/CMakeLists.txt b/lib/configuration/CMakeLists.txt
new file mode 100644
index 0000000..f68a5b1
--- /dev/null
+++ b/lib/configuration/CMakeLists.txt
@@ -0,0 +1,8 @@
+add_library(configuration
+ configuration.cpp
+ configuration.h)
+
+target_link_libraries(configuration
+ Qt5::Core
+ ${Boost_LIBRARIES}
+)
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp
new file mode 100644
index 0000000..c114155
--- /dev/null
+++ b/lib/configuration/configuration.cpp
@@ -0,0 +1,108 @@
+/*
+ * 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/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "configuration.h"
+#include <QStandardPaths>
+#include <fstream>
+
+namespace po = boost::program_options;
+
+Configuration::Configuration()
+{
+ 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
+ {
+ const char* argv[0];
+ po::store(po::parse_command_line(0, argv, desc), vm);
+ }
+
+}
+
+Configuration::~Configuration() = default;
+
+bool Configuration::read(const QString &path)
+{
+ std::ifstream f(path.toStdString(), std::ifstream::in);
+ po::store(po::parse_config_file(f, desc, false), vm);
+ return true;
+}
+
+bool Configuration::parse(int argc, const char **argv)
+{
+ try {
+ po::store(po::parse_command_line(argc, argv, desc), vm);
+ } catch (const po::error &e) {
+ return false;
+ }
+
+ return true;
+}
diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h
new file mode 100644
index 0000000..4d58a90
--- /dev/null
+++ b/lib/configuration/configuration.h
@@ -0,0 +1,76 @@
+/*
+ * 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/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef SMOLBOTE_CONFIGURATION_H
+#define SMOLBOTE_CONFIGURATION_H
+
+#include <optional>
+#include <string>
+#include <vector>
+#include <QString>
+#include <boost/program_options.hpp>
+#include <QStringList>
+
+class Configuration
+{
+public:
+ explicit Configuration();
+ ~Configuration();
+
+ bool read(const QString &path);
+ bool parse(int argc, const char **argv);
+
+ template <typename T>
+ std::optional<T> value(const char *path) const
+ {
+ // if setting doesn't exist, we crash
+ // in debug builds, check if setting exists
+#ifdef QT_DEBUG
+ if(vm.count(path) == 0) {
+ qWarning("value(%s) does not exist, probably crashing now", path);
+ }
+#endif
+
+ if constexpr(std::is_same_v<T, std::string>) {
+ std::string r;
+ try {
+ r = vm[path].as<std::string>();
+ } catch (boost::bad_any_cast &) {
+ // try int
+ try {
+ r = std::to_string(vm[path].as<int>());
+ } catch (boost::bad_any_cast &) {
+
+ // try bool, and crash if not that either
+ r = vm[path].as<bool>() ? "true" : "false";
+ }
+
+ }
+
+ // check if it's a path
+ if(r.front() == '~') {
+ r.replace(0, 1, m_homePath);
+ }
+
+ return std::optional<std::string>(r);
+ } else
+ return std::optional<T>(vm[path].as<T>());
+ }
+
+ const std::vector<boost::shared_ptr<boost::program_options::option_description>> & options() {
+ return desc.options();
+ }
+
+private:
+ boost::program_options::options_description desc;
+ boost::program_options::variables_map vm;
+
+ std::string m_homePath;
+};
+
+#endif // SMOLBOTE_CONFIGURATION_H