From 44421abbe89be2c6a6290182571fff82dfec9651 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 15 Mar 2018 16:26:38 +0100 Subject: Moved Configuration class into library --- CMakeLists.txt | 1 + config/CMakeLists.txt | 11 ++-- config/settingsdialog.cpp | 3 + config/settingsdialog.h | 8 +-- lib/configuration/CMakeLists.txt | 8 +++ lib/configuration/configuration.cpp | 108 ++++++++++++++++++++++++++++++++++++ lib/configuration/configuration.h | 76 +++++++++++++++++++++++++ lib/settings/CMakeLists.txt | 12 ---- src/CMakeLists.txt | 7 +-- src/configuration.cpp | 108 ------------------------------------ src/configuration.h | 76 ------------------------- 11 files changed, 210 insertions(+), 208 deletions(-) create mode 100644 lib/configuration/CMakeLists.txt create mode 100644 lib/configuration/configuration.cpp create mode 100644 lib/configuration/configuration.h delete mode 100644 lib/settings/CMakeLists.txt delete mode 100644 src/configuration.cpp delete mode 100644 src/configuration.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c0ab789..a7cd876 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ endif (UseLibCpp) add_subdirectory(lib/bookmarks) add_subdirectory(lib/downloads) +add_subdirectory(lib/configuration) add_subdirectory(plugins/ProfileEditor) diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index cde4648..9ae0381 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -10,12 +10,15 @@ add_executable(poi-config main.cpp settingsdialog.cpp settingsdialog.h - ../src/configuration.h - ../src/configuration.cpp - ) +) + +target_include_directories(poi-config + PRIVATE ../lib/configuration +) target_link_libraries(poi-config Qt5::Core Qt5::Widgets - ${Boost_LIBRARIES}) + configuration +) install(TARGETS poi-config RUNTIME DESTINATION bin CONFIGURATIONS Release) diff --git a/config/settingsdialog.cpp b/config/settingsdialog.cpp index 56acc01..ef21a47 100644 --- a/config/settingsdialog.cpp +++ b/config/settingsdialog.cpp @@ -24,6 +24,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) setCentralWidget(&treeWidget); treeWidget.setColumnCount(2); + treeWidget.setHeaderLabels({ tr("setting"), tr("value") }); //config.read() @@ -32,6 +33,8 @@ SettingsDialog::SettingsDialog(QWidget *parent) item->setText(0, QString::fromStdString(option->long_name())); item->setText(1, QString::fromStdString(config.value(option->long_name().c_str()).value_or(std::string()))); } + + treeWidget.resizeColumnToContents(0); } SettingsDialog::~SettingsDialog() = default; diff --git a/config/settingsdialog.h b/config/settingsdialog.h index 82cd1be..dc3cc08 100644 --- a/config/settingsdialog.h +++ b/config/settingsdialog.h @@ -6,13 +6,13 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef SETTINGSDIALOG_H -#define SETTINGSDIALOG_H +#ifndef SMOLBOTE_SETTINGSDIALOG_H +#define SMOLBOTE_SETTINGSDIALOG_H -#include "../src/configuration.h" #include #include #include +#include class SettingsDialog : public QMainWindow { @@ -28,4 +28,4 @@ private: Configuration config; }; -#endif // SETTINGSDIALOG_H +#endif // SMOLBOTE_SETTINGSDIALOG_H 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 +#include + +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()->default_value("")) + + // default window size + ("browser.window.height", po::value()->default_value(720)) + ("browser.window.width", po::value()->default_value(1280)) + ("browser.window.maximized", po::value()->default_value(true)) + ("browser.window.title", po::value()->default_value("title — smolbote [profile]")) + + // window ui + ("browser.ui.navtoolbarMovable", po::value()->default_value(false)) + ("browser.ui.tabtoolbarMovable", po::value()->default_value(false)) + + // browser shortcuts + + // browser menu + ("browser.shortcuts.newWindow", po::value()->default_value("Ctrl+N")) + ("browser.shortcuts.newTab", po::value()->default_value("Ctrl+T")) + ("browser.shortcuts.about", po::value()->default_value("F1")) + ("browser.shortcuts.quit", po::value()->default_value("Ctrl+Q")) + + // navigation + ("browser.shortcuts.back", po::value()->default_value("Ctrl+Left")) + ("browser.shortcuts.forward", po::value()->default_value("Ctrl+Right")) + ("browser.shortcuts.refresh", po::value()->default_value("F5")) + ("browser.shortcuts.reload", po::value()->default_value("Ctrl+F5")) + ("browser.shortcuts.home", po::value()->default_value("Ctrl+Home")) + + // tabs + ("browser.shortcuts.tabClose", po::value()->default_value("Ctrl+X")) + ("browser.shortcuts.tabLeft", po::value()->default_value("Ctrl+O")) + ("browser.shortcuts.tabRight", po::value()->default_value("Ctrl+P")) + + // page + ("browser.shortcuts.toggleSearchBox", po::value()->default_value("F3")) + ("browser.shortcuts.focusAddress", po::value()->default_value("F4")) + ("browser.shortcuts.fullscreen", po::value()->default_value("F11")) + + // Filter settings + ("filter.path", po::value()->default_value("~/.config/smolbote/hosts.d")) + + // Plugin settings + ("plugins.path", po::value()->default_value("~/.config/smolbote/plugins.d")) + + // Profile settings + ("profile.path", po::value()->default_value("~/.config/smolbote/profiles.d")) + ("profile.search", po::value()->default_value("https://duckduckgo.com/?q=$term&ia=web")) + ("profile.homepage", po::value()->default_value("about:blank")) + ("profile.newtab", po::value()->default_value("about:blank")) + + // Bookmark settings + ("bookmarks.path", po::value()->default_value("~/.config/smolbote/bookmarks.xbel")) + ("bookmarks.shortcut", po::value()->default_value("Ctrl+B")) + + // Downloads settings + ("downloads.path", po::value()->default_value("~/Downloads")) + ("downloads.shortcut", po::value()->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 +#include +#include +#include +#include +#include + +class Configuration +{ +public: + explicit Configuration(); + ~Configuration(); + + bool read(const QString &path); + bool parse(int argc, const char **argv); + + template + std::optional 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) { + std::string r; + try { + r = vm[path].as(); + } catch (boost::bad_any_cast &) { + // try int + try { + r = std::to_string(vm[path].as()); + } catch (boost::bad_any_cast &) { + + // try bool, and crash if not that either + r = vm[path].as() ? "true" : "false"; + } + + } + + // check if it's a path + if(r.front() == '~') { + r.replace(0, 1, m_homePath); + } + + return std::optional(r); + } else + return std::optional(vm[path].as()); + } + + const std::vector> & 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 diff --git a/lib/settings/CMakeLists.txt b/lib/settings/CMakeLists.txt deleted file mode 100644 index 20c78ae..0000000 --- a/lib/settings/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -cmake_minimum_required(VERSION 3.1.0) - -add_library(configuration - settingsdialog.cpp - settingsdialog.h - settingsdialog.ui) - -if (CLikeConfig) - target_compile_definitions(configuration PRIVATE C_LIKE_CONFIG) -endif (CLikeConfig) - -target_link_libraries(configuration config++ Qt5::Widgets) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d2febde..f017d6b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,8 +21,6 @@ add_executable(poi singleapplication.h browser.cpp browser.h - configuration.cpp - configuration.h ../data/resources.qrc # main window @@ -74,11 +72,12 @@ add_executable(poi ../plugins/interfaces.h commandline.cpp commandline.h) target_include_directories(poi - PRIVATE ../lib ../plugins) + PRIVATE ../lib ../plugins + PRIVATE ../lib/configuration) target_link_libraries(poi Qt5::Core Qt5::Widgets Qt5::Concurrent Qt5::WebEngineWidgets - ${Boost_LIBRARIES} + configuration bookmarks downloads) install(TARGETS poi RUNTIME DESTINATION bin CONFIGURATIONS Release) diff --git a/src/configuration.cpp b/src/configuration.cpp deleted file mode 100644 index c114155..0000000 --- a/src/configuration.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 -#include - -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()->default_value("")) - - // default window size - ("browser.window.height", po::value()->default_value(720)) - ("browser.window.width", po::value()->default_value(1280)) - ("browser.window.maximized", po::value()->default_value(true)) - ("browser.window.title", po::value()->default_value("title — smolbote [profile]")) - - // window ui - ("browser.ui.navtoolbarMovable", po::value()->default_value(false)) - ("browser.ui.tabtoolbarMovable", po::value()->default_value(false)) - - // browser shortcuts - - // browser menu - ("browser.shortcuts.newWindow", po::value()->default_value("Ctrl+N")) - ("browser.shortcuts.newTab", po::value()->default_value("Ctrl+T")) - ("browser.shortcuts.about", po::value()->default_value("F1")) - ("browser.shortcuts.quit", po::value()->default_value("Ctrl+Q")) - - // navigation - ("browser.shortcuts.back", po::value()->default_value("Ctrl+Left")) - ("browser.shortcuts.forward", po::value()->default_value("Ctrl+Right")) - ("browser.shortcuts.refresh", po::value()->default_value("F5")) - ("browser.shortcuts.reload", po::value()->default_value("Ctrl+F5")) - ("browser.shortcuts.home", po::value()->default_value("Ctrl+Home")) - - // tabs - ("browser.shortcuts.tabClose", po::value()->default_value("Ctrl+X")) - ("browser.shortcuts.tabLeft", po::value()->default_value("Ctrl+O")) - ("browser.shortcuts.tabRight", po::value()->default_value("Ctrl+P")) - - // page - ("browser.shortcuts.toggleSearchBox", po::value()->default_value("F3")) - ("browser.shortcuts.focusAddress", po::value()->default_value("F4")) - ("browser.shortcuts.fullscreen", po::value()->default_value("F11")) - - // Filter settings - ("filter.path", po::value()->default_value("~/.config/smolbote/hosts.d")) - - // Plugin settings - ("plugins.path", po::value()->default_value("~/.config/smolbote/plugins.d")) - - // Profile settings - ("profile.path", po::value()->default_value("~/.config/smolbote/profiles.d")) - ("profile.search", po::value()->default_value("https://duckduckgo.com/?q=$term&ia=web")) - ("profile.homepage", po::value()->default_value("about:blank")) - ("profile.newtab", po::value()->default_value("about:blank")) - - // Bookmark settings - ("bookmarks.path", po::value()->default_value("~/.config/smolbote/bookmarks.xbel")) - ("bookmarks.shortcut", po::value()->default_value("Ctrl+B")) - - // Downloads settings - ("downloads.path", po::value()->default_value("~/Downloads")) - ("downloads.shortcut", po::value()->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/src/configuration.h b/src/configuration.h deleted file mode 100644 index 5038a4d..0000000 --- a/src/configuration.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 CONFIGURATION_H -#define CONFIGURATION_H - -#include -#include -#include -#include -#include -#include - -class Configuration -{ -public: - explicit Configuration(); - ~Configuration(); - - bool read(const QString &path); - bool parse(int argc, const char **argv); - - template - std::optional 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) { - std::string r; - try { - r = vm[path].as(); - } catch (boost::bad_any_cast &) { - // try int - try { - r = std::to_string(vm[path].as()); - } catch (boost::bad_any_cast &) { - - // try bool, and crash if not that either - r = vm[path].as() ? "true" : "false"; - } - - } - - // check if it's a path - if(r.front() == '~') { - r.replace(0, 1, m_homePath); - } - - return std::optional(r); - } else - return std::optional(vm[path].as()); - } - - const std::vector> & options() { - return desc.options(); - } - -private: - boost::program_options::options_description desc; - boost::program_options::variables_map vm; - - std::string m_homePath; -}; - -#endif // CONFIGURATION_H -- cgit v1.2.1