From f3a4607d6a722a862af0eb9747a15dcdf624b6fb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 3 Nov 2019 00:18:10 +0200 Subject: Drop boost dependency - wrote not-invented-here config file parser and conf class - spent obscene amount of time plugging in said conf class --- src/browser.cpp | 91 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 45 deletions(-) (limited to 'src/browser.cpp') diff --git a/src/browser.cpp b/src/browser.cpp index 0b076ca..d677997 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -9,9 +9,9 @@ #include "browser.h" #include "aboutdialog.h" #include "aboutplugin.h" -#include "addressbar.h" +#include "mainwindow/addressbar.h" #include "bookmarkswidget.h" -#include "config.h" +#include "conf.hpp" #include "configuration.h" #include "downloadswidget.h" #include "mainwindow/mainwindow.h" @@ -39,6 +39,8 @@ #include "hostlist/hostlist.h" #include #include +#include +#include Browser::Browser(int &argc, char *argv[], bool allowSecondary) : SingleApplication(argc, argv, allowSecondary, SingleApplication::User | SingleApplication::SecondaryNotification | SingleApplication::ExcludeAppVersion) @@ -46,6 +48,29 @@ Browser::Browser(int &argc, char *argv[], bool allowSecondary) setApplicationName(CONFIG_POI_NAME); setWindowIcon(QIcon(CONFIG_POI_ICON)); setApplicationVersion(QVersionNumber::fromString(QLatin1String(poi_Version)).toString()); + + Configuration conf; + + if(const auto _translation = conf.value("browser.translation")) { + auto *translator = new QTranslator(this); + if(translator->load(_translation.value())) + installTranslator(translator); + else + delete translator; + } + + if(const auto _locale = conf.value("browser.locale")) { + auto *locale = new QTranslator(this); + if(locale->load("qt_" + _locale.value(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) + installTranslator(locale); + else + delete locale; + } + + if(auto iconTheme = conf.value("browser.iconTheme")) { + QIcon::setThemeName(iconTheme.value()); + } + } Browser::~Browser() @@ -66,25 +91,6 @@ void Browser::about() dlg->exec(); } -const QStringList Browser::configurationOptions() const -{ - QStringList options; - for(const auto &option : m_config->description().options()) { - options.append(QString::fromStdString(option->long_name())); - } - return options; -} - -const QString Browser::configuration(const QString &key) const -{ - return m_config->value(qUtf8Printable(key)).value_or(QString()); -} - -void Browser::setConfiguration(const QString &key, const QString &value) -{ - m_config->setValue(qUtf8Printable(key), value); -} - const QList> Browser::profileList() const { QList> profiles; @@ -96,6 +102,8 @@ const QList> Browser::profileList() const QPair Browser::loadProfile(const QString &id, bool isOffTheRecord) { + Configuration conf; + const QString _id = [id](){ // if id contains a separator, it should be a path if(id.contains(QDir::separator())) { @@ -112,7 +120,7 @@ QPair Browser::loadProfile(const QString &id, bool isOffTheR for(UrlFilter *filter : m_filters) { interceptor->addFilter(filter); } - const auto headers = m_config->value("filter.header").value_or(QStringList()); + const auto headers = conf.value("filter.header").value_or(QStringList()); for(const QString &header : headers) { const auto h = header.split(QLatin1Literal(":")); if(h.length() == 2) @@ -129,18 +137,6 @@ void Browser::removeProfile(const QString &id) m_profileManager->deleteProfile(id); } -void Browser::setConfiguration(std::unique_ptr &config) -{ - Q_ASSERT(config); - m_config = std::move(config); -} - -Configuration *Browser::getConfiguration() const -{ - Q_ASSERT(m_config); - return m_config.get(); -} - WebProfileManager *Browser::getProfileManager() { return m_profileManager; @@ -172,13 +168,13 @@ QPluginLoader *Browser::addPlugin(const QString &path) void Browser::setup(QVector plugins) { - Q_ASSERT(m_config); + Configuration conf; + for(QPluginLoader *loader : plugins) { m_plugins.append(new PluginInfo(loader)); } - auto stylesheet = m_config->value("browser.stylesheet"); - if(stylesheet) { + if(auto stylesheet = conf.value("browser.stylesheet")) { QFile f(stylesheet.value()); if(f.open(QIODevice::ReadOnly)) { setStyleSheet(f.readAll()); @@ -187,16 +183,17 @@ void Browser::setup(QVector plugins) } // downloads - m_downloads = std::make_unique(m_config->value("downloads.path").value()); + m_downloads = std::make_unique(conf.value("downloads.path").value()); + // url request filter - for(const QString &hostlist : Util::files(m_config->value("filter.hosts").value_or(QString()))) { + for(const QString &hostlist : Util::files(conf.value("filter.hosts").value_or(QString()))) { QFile f(hostlist); if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { m_filters.append(new HostList(&f)); f.close(); } } - for(const QString &adblock : Util::files(m_config->value("filter.adblock").value_or(QString()))) { + for(const QString &adblock : Util::files(conf.value("filter.adblock").value_or(QString()))) { QFile f(adblock); if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { m_filters.append(new AdBlockList(&f)); @@ -206,14 +203,18 @@ void Browser::setup(QVector plugins) // cookie request filter // load profiles - m_profileManager = new WebProfileManager(m_config->section("profile"), this); - for(const QString &profilePath : Util::files(m_config->value("profile.path").value(), { "*.profile" })) { + ProfileDefault_t p; + p.search = conf.value("profile.search").value(); + p.homepage = conf.value("profile.homepage").value(); + p.newtab = conf.value("profile.newtab").value(); + m_profileManager = new WebProfileManager(p, this); + for(const QString &profilePath : Util::files(conf.value("profile.path").value(), { "*.profile" })) { this->loadProfile(profilePath); } // set default profile { - const QString id = m_config->value("profile.default").value(); + const QString id = conf.value("profile.default").value(); auto *profile = m_profileManager->profile(id); if(profile == nullptr) { profile = qobject_cast(loadProfile(id).second); @@ -223,7 +224,7 @@ void Browser::setup(QVector plugins) } // bookmarks - m_bookmarks = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); + m_bookmarks = std::make_shared(QString::fromStdString(conf.value("bookmarks.path").value())); connect(m_bookmarks.get(), &BookmarksWidget::showContextMenu, this, [this](const QUrl &url, const QPoint &pos) { auto *subwindow = m_windows.last()->currentSubWindow(); if(subwindow == nullptr) @@ -276,7 +277,7 @@ void Browser::showWidget(QWidget *widget, MainWindow *where) const MainWindow *Browser::createWindow() { // the window will delete itself when it closes, so we don't need to delete it - auto *window = new MainWindow(m_config); + auto *window = new MainWindow(); connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search); for(auto *info : m_plugins) { -- cgit v1.2.1