From 4e3c479a0f279926e0bd9a359a0ee57b334e976e Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 8 Dec 2017 14:22:19 +0100 Subject: Replaced tinytoml with libconfig --- data/poi.cfg | 58 ++ data/poi.toml | 86 -- data/resources.qrc | 2 +- lib/settings/configuration.cpp | 70 +- lib/settings/configuration.h | 11 +- lib/settings/settings.qbs | 1 + smolbote.qbs | 28 +- src/3rd-party/toml/LICENSE | 24 - src/3rd-party/toml/toml.h | 1958 ------------------------------------ src/browser.cpp | 222 +--- src/browser.h | 59 +- src/forms/aboutdialog.cpp | 64 +- src/forms/aboutdialog.h | 4 +- src/forms/profilesdialog.cpp | 22 +- src/forms/profilesdialog.h | 2 +- src/main.cpp | 175 ++-- src/mainwindow.cpp | 52 +- src/mainwindow.h | 8 +- src/singleapplication.cpp | 8 +- src/singleapplication.h | 2 +- src/webengine/webengineprofile.cpp | 157 ++- src/webengine/webengineprofile.h | 7 +- src/widgets/mainwindowmenubar.cpp | 58 +- src/widgets/mainwindowmenubar.h | 8 +- src/widgets/webviewtabbar.cpp | 7 +- 25 files changed, 503 insertions(+), 2590 deletions(-) create mode 100644 data/poi.cfg delete mode 100644 data/poi.toml delete mode 100644 src/3rd-party/toml/LICENSE delete mode 100644 src/3rd-party/toml/toml.h diff --git a/data/poi.cfg b/data/poi.cfg new file mode 100644 index 0000000..68503e1 --- /dev/null +++ b/data/poi.cfg @@ -0,0 +1,58 @@ +// smolbote default configuration file +// Uses libconfig format, for details refer to: +// https://hyperrealm.github.io/libconfig/libconfig_manual.html#Configuration-Files + +// Browser default settings +browser = { + // true to generate user settings when run + firstRun = true; + + // default profile name the browser should use; "" is off-the-record + profile = ""; + + // default window size + window = { + height = 720; + width = 1280; + maximized = true; + title = "title — smolbote [profile]"; + }; + + ui = { + navtoolbarMovable = false; + tabtoolbarMovable = false; + }; + + shortcuts = { + // browser menu + newWindow = "Ctrl+N"; + newTab = "Ctrl+T"; + about = "F1"; + quit = "Ctrl+Q"; + + focusAddress = "F4"; + fullscreen = "F11"; + + // tabs + tabClose = "Ctrl+X"; + tabLeft = "Shift+Left"; + tabRight = "Shift+Right"; + }; +}; + +// Profile settings +profile = { + path = "~/.config/smolbote/Profiles"; + homepage = "about:blank"; + newtab = "about:blank"; +}; + +// Bookmark settings +bookmarks = { + path = "~/.config/smolbote/bookmarks.xbel"; +}; + +// Downloads settings +downloads = { + path = "~/Downloads" +}; diff --git a/data/poi.toml b/data/poi.toml deleted file mode 100644 index 4a84692..0000000 --- a/data/poi.toml +++ /dev/null @@ -1,86 +0,0 @@ -# -# poi.conf -# -## Settings and default settings -# There are two parts to the settings - default values and user overrides. -# The default settings are read from: -# - /usr/local/share/smolbote/poi.conf -# - /usr/share/smolbote/poi.conf -# - /etc/smolbote.d/poi.conf -# - :/poi.toml -# The user settings are read from: -# - any location specified with -c/--config -# - QStandardPaths::AppConfigLocation + "/poi.conf" -# -## Variables -# §home is QStandardPaths::HomeLocation, usually /home/username -# $cache is QStandardPaths::CacheLocation, usually /home/username/.cache/smolbote -# $settings is the directory where the settings file is located, usually /home/username/.config/smolbote - -# General -[general] -search="https://duckduckgo.com/?q=$term" # FIXME remove; move to profile - -# Browser: application-wide settings -[browser] -#sessionPath="$cache/session.json" - -# Profile -[browser.profile] -# Which profile should be used by default for new windows -# A nameless ("") profile is off-the-record -default="" -# Where to look for profiles -path="$home/.config/smolbote/profiles/" -storagePath="$home/.config/smolbote/profiles/" -cachePath="$home/.cache/smolbote/profiles/" - -[browser.profile.new] -homepage="https://duckduckgo.com" -newtab="about:blank" -search="https://duckduckgo.com/lite?q=$term" # FIXME - -# Main window settings -[window] -height=720 -width=1280 -maximized=true -title="title — smolbote [profile]" - -# Main window shortcuts -[window.shortcuts] -focusAddress="F4" -fullscreen="F11" -tabNew="Ctrl+T" -tabClose="Ctrl+X" -tabLeft="Shift+Left" -tabRight="Shift+Right" -windowNew="Ctrl+N" -windowClose="Ctrl+Q" - -# Main window UI -[window.ui] -navtoolbarMovable=false -tabtoolbarMovable=false - -# URL blocker -[blocker] -shortcut="Ctrl+Shift+F" -path="$settings/" -subscriptions=[ - "filter.json" -# "https://easylist.to/easylist/easylist.txt", -# "https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt", -# "https://easylist.to/easylist/easyprivacy.txt" -] - -# Bookmark manager -[bookmarks] -dialogShortcut="Ctrl+Shift+B" -path="$settings/bookmarks.xbel" - -# Download manager -[downloads] -dialogShortcut="Ctrl+Shift+D" -path="$home/Downloads" -auto=false diff --git a/data/resources.qrc b/data/resources.qrc index b2b6be5..529bf43 100644 --- a/data/resources.qrc +++ b/data/resources.qrc @@ -1,6 +1,6 @@ poi.svg - poi.toml + poi.cfg diff --git a/lib/settings/configuration.cpp b/lib/settings/configuration.cpp index 543ea60..9d53581 100644 --- a/lib/settings/configuration.cpp +++ b/lib/settings/configuration.cpp @@ -22,10 +22,18 @@ #include #include +using namespace libconfig; + Configuration::Configuration() { - m_userCfg = new libconfig::Config(); - m_defaultCfg = new libconfig::Config(); + m_userCfg = new Config(); +#ifdef C_LIKE_CONFIG + m_userCfg->setOptions(Config::OptionSemicolonSeparators | Config::OptionOpenBraceOnSeparateLine); +#endif + // fsync after writing and before closing + m_userCfg->setOption(Config::OptionFsync, true); + + m_defaultCfg = new Config(); } Configuration::~Configuration() @@ -43,19 +51,30 @@ bool Configuration::readUserConfiguration(const std::string &path) m_userCfgPath = path; try { m_userCfg->readFile(path.c_str()); - } catch (libconfig::FileIOException) { + } catch (FileIOException) { return false; - } catch (libconfig::ParseException) { + } catch (ParseException) { return false; } return true; } +bool Configuration::writeUserConfiguration(const std::string &path) +{ + m_userCfgPath = path; + try { + m_userCfg->writeFile(path.c_str()); + } catch (FileIOException) { + return false; + } + return true; +} + bool Configuration::readDefaultConfiguration(const std::string &data) { try { m_defaultCfg->readString(data); - } catch(libconfig::ParseException) { + } catch(ParseException) { return false; } return true; @@ -64,7 +83,7 @@ bool Configuration::readDefaultConfiguration(const std::string &data) std::vector Configuration::children(const char* name) { std::vector groupNames; - const libconfig::Setting &root = m_defaultCfg->lookup(name); + const Setting &root = m_defaultCfg->lookup(name); for(auto i = root.begin(); i != root.end(); ++i) { groupNames.push_back(std::string(i->getName())); @@ -76,23 +95,23 @@ std::vector Configuration::children(const char* name) template std::optional Configuration::value(const char* path) const { - libconfig::Config *conf = getConfig(path); + Config *conf = getConfig(path); if(conf == nullptr) { return std::nullopt; } // setting was found - const libconfig::Setting &setting = conf->lookup(path); + const Setting &setting = conf->lookup(path); std::optional r; // cast depending on type // type checks are done during compile time switch (setting.getType()) { - case libconfig::Setting::TypeNone: + case Setting::TypeNone: r = std::nullopt; break; - case libconfig::Setting::TypeInt: + case Setting::TypeInt: // int, unsigned int, long, unsigned long if constexpr(std::is_same_v) { r = std::optional(std::to_string(static_cast(setting))); @@ -103,7 +122,7 @@ std::optional Configuration::value(const char* path) const } break; - case libconfig::Setting::TypeInt64: + case Setting::TypeInt64: // int, unsigned int; long long, unsigned long long if constexpr(std::is_same_v) { r = std::optional(std::to_string(static_cast(setting))); @@ -114,7 +133,7 @@ std::optional Configuration::value(const char* path) const } break; - case libconfig::Setting::TypeFloat: + case Setting::TypeFloat: // float, double if constexpr(std::is_same_v) { r = std::optional(std::to_string(static_cast(setting))); @@ -125,7 +144,7 @@ std::optional Configuration::value(const char* path) const } break; - case libconfig::Setting::TypeString: + case Setting::TypeString: // const char*, std::string if constexpr(std::is_same::value) { r = std::optional(static_cast(setting)); @@ -134,7 +153,7 @@ std::optional Configuration::value(const char* path) const } break; - case libconfig::Setting::TypeBoolean: + case Setting::TypeBoolean: // bool if constexpr(std::is_same::value) { r = std::optional(static_cast(setting) ? "true" : "false"); @@ -145,15 +164,15 @@ std::optional Configuration::value(const char* path) const } break; - case libconfig::Setting::TypeGroup: + case Setting::TypeGroup: r = std::nullopt; break; - case libconfig::Setting::TypeArray: + case Setting::TypeArray: r = std::nullopt; break; - case libconfig::Setting::TypeList: + case Setting::TypeList: r = std::nullopt; break; } @@ -181,7 +200,7 @@ template void Configuration::setValue(std::string path, const T &val) { if(m_userCfg->exists(path)) { - libconfig::Setting &setting = m_userCfg->lookup(path); + Setting &setting = m_userCfg->lookup(path); // compiler complained about operator= not taking unsinged ints, longs and long longs if constexpr(std::is_unsigned_v && !std::is_same_v) { setting = static_cast>(val); @@ -197,8 +216,8 @@ void Configuration::setValue(std::string path, const T &val) // this will error out if entry being added is not in the default config std::istringstream p(path); - libconfig::Setting *userSetting = &m_userCfg->getRoot(); - libconfig::Setting *defaultSetting = &m_defaultCfg->getRoot(); + Setting *userSetting = &m_userCfg->getRoot(); + Setting *defaultSetting = &m_defaultCfg->getRoot(); std::string i; while (std::getline(p, i, '.')) { @@ -232,7 +251,7 @@ template void Configuration::setValue(std::string path, const bool &val); template void Configuration::setValue(std::string path, const std::string &val); -libconfig::Config *Configuration::getConfig(const char* path) const +Config *Configuration::getConfig(const char* path) const { if(m_userCfg->exists(path)) { return m_userCfg; @@ -242,3 +261,12 @@ libconfig::Config *Configuration::getConfig(const char* path) const return nullptr; } } + +std::string &patchHome(std::string &path, const std::string &home) +{ + const size_t location = path.find("~"); + if(location != std::string::npos) { + return path.replace(location, 1, home); + } + return path; +} diff --git a/lib/settings/configuration.h b/lib/settings/configuration.h index e0f1872..548b816 100644 --- a/lib/settings/configuration.h +++ b/lib/settings/configuration.h @@ -18,8 +18,8 @@ ** ******************************************************************************/ -#ifndef SETTINGS_H -#define SETTINGS_H +#ifndef CONFIGURATION_H +#define CONFIGURATION_H #include #include @@ -36,6 +36,8 @@ public: ~Configuration(); bool readUserConfiguration(const std::string &path); + bool writeUserConfiguration(const std::string &path); + bool readDefaultConfiguration(const std::string &data); std::vector children(const char *name = ""); @@ -54,6 +56,9 @@ private: libconfig::Config *m_defaultCfg; }; +// replace ~ with home +std::string& patchHome(std::string &path, const std::string &home); + // instantiate functions // this needs to be done because the implementation is in the cpp file @@ -89,4 +94,4 @@ extern template void Configuration::setValue(std::string path, const bool extern template void Configuration::setValue(std::string path, const std::string &val); -#endif // SETTINGS_H +#endif // CONFIGURATION_H diff --git a/lib/settings/settings.qbs b/lib/settings/settings.qbs index e49339f..77e97fb 100644 --- a/lib/settings/settings.qbs +++ b/lib/settings/settings.qbs @@ -8,6 +8,7 @@ Project { Depends { name: "cpp" } + cpp.defines: "C_LIKE_CONFIG" cpp.cxxLanguageVersion: "c++17" files: [ diff --git a/smolbote.qbs b/smolbote.qbs index 6d991eb..d18af60 100644 --- a/smolbote.qbs +++ b/smolbote.qbs @@ -49,6 +49,9 @@ Project { Depends { name: "downloads" } + Depends { + name: "settings" + } Probe { id: git @@ -65,24 +68,23 @@ Project { } // global includes - cpp.includePaths: ['src', 'src/3rd-party', 'src/lib'] + cpp.includePaths: ['src', 'src/3rd-party', 'src/lib', 'lib'] // global defines cpp.defines: { if(project.deprecatedWarnings) defines.push("QT_DEPRECATED_WARNINGS", "QT_DISABLE_DEPRECATED_BEFORE="+project.deprecatedBefore); return defines; } - cpp.cxxLanguageVersion: "c++11" + cpp.cxxLanguageVersion: "c++17" + + cpp.linkerFlags: libconfig.libs Group { name: "main" files: [ "src/browser.cpp", "src/browser.h", - "src/interfaces.h", "src/main.cpp", - "src/settings.cpp", - "src/settings.h", "src/singleapplication.cpp", "src/singleapplication.h", ] @@ -114,7 +116,7 @@ Project { "src/widgets/webviewtabbar.h", ] } - +/* Group { name: "Request Filter" files: [ @@ -140,7 +142,7 @@ Project { return defines; } } - +*/ Group { name: "Profile" files: [ @@ -168,18 +170,6 @@ Project { qbs.install: true qbs.installDir: "bin" } - - Group { - name: "Configuration" - files: [ - "data/poi.toml", - ] - qbs.install: true - qbs.installDir: "share/smolbote" - } } // CppApplication poi - //SubProject { - // filePath: "src/plugins/plugins.qbs" - //} } diff --git a/src/3rd-party/toml/LICENSE b/src/3rd-party/toml/LICENSE deleted file mode 100644 index 1514196..0000000 --- a/src/3rd-party/toml/LICENSE +++ /dev/null @@ -1,24 +0,0 @@ -Copyright (c) 2014, MAYAH -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - diff --git a/src/3rd-party/toml/toml.h b/src/3rd-party/toml/toml.h deleted file mode 100644 index ec8c489..0000000 --- a/src/3rd-party/toml/toml.h +++ /dev/null @@ -1,1958 +0,0 @@ -#ifndef TINYTOML_H_ -#define TINYTOML_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace toml { - -// ---------------------------------------------------------------------- -// Declarations - -class Value; -typedef std::chrono::system_clock::time_point Time; -typedef std::vector Array; -typedef std::map Table; - -namespace internal { -template struct call_traits_value { - typedef T return_type; -}; -template struct call_traits_ref { - typedef const T& return_type; -}; -} // namespace internal - -template struct call_traits; -template<> struct call_traits : public internal::call_traits_value {}; -template<> struct call_traits : public internal::call_traits_value {}; -template<> struct call_traits : public internal::call_traits_value {}; -template<> struct call_traits : public internal::call_traits_value {}; -template<> struct call_traits : public internal::call_traits_ref {}; -template<> struct call_traits