From 99917ab581314f9517569401bc79e150e4ce0881 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 8 Jun 2018 15:00:22 +0200 Subject: Better profile loading First load all profiles from profile.path, and then the profile.default if missing, after which set the default profile. Profile names and whether they're otr can be set by .profile name=string and otr=bool. --- lib/configuration/configuration.h | 3 ++- lib/web/webprofile.cpp | 46 +++++++++++++++++++++++------------- lib/web/webprofile.h | 6 ++--- src/browser.cpp | 43 +++++++++++++-------------------- src/mainwindow/widgets/tabwidget.cpp | 3 ++- 5 files changed, 53 insertions(+), 48 deletions(-) diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index 1df6bb5..59c837c 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -49,7 +49,8 @@ public: } if constexpr(std::is_same_v) { - return std::optional(vm[path].as()); + return std::optional(QString::fromStdString(this->value(path).value())); + //return std::optional(vm[path].as()); } else if constexpr(std::is_same_v) { diff --git a/lib/web/webprofile.cpp b/lib/web/webprofile.cpp index b40a98c..67671a0 100644 --- a/lib/web/webprofile.cpp +++ b/lib/web/webprofile.cpp @@ -14,23 +14,33 @@ WebProfile *WebProfile::profile = nullptr; -void loadProfile(WebProfile *profile, const QHash &defaults, const QString &path) +WebProfile* loadProfile(const QString &name, const QHash &defaults, const QString &path) { - Q_CHECK_PTR(profile); - - profile->setSearch(defaults.value("profile.search")); - profile->setHomepage(QUrl::fromUserInput(defaults.value("profile.homepage"))); - profile->setNewtab(QUrl::fromUserInput(defaults.value("profile.newtab"))); - - // return if there is no config file - if(!QFileInfo::exists(path)) - return; - + WebProfile *profile = nullptr; #ifdef QT_DEBUG - qDebug("+ Reading config for profile '%s': %s", qUtf8Printable(profile->name()), qUtf8Printable(path)); + qDebug("+ Reading config for profile '%s': %s", qUtf8Printable(name), qUtf8Printable(path)); #endif QSettings config(path, QSettings::IniFormat); + if(name.isEmpty()) { + // a default otr profile + profile = new WebProfile(QObject::tr("Off-the-record"), nullptr); + + } else if(config.value("otr").toBool()) { + // a named otr profile + profile = new WebProfile(config.value("name", name).toString(), nullptr); + + } else { + // a named profile + profile = new WebProfile(name, config.value("name", name).toString(), nullptr); + } + + Q_CHECK_PTR(profile); + + profile->setSearch(config.value("search", defaults.value("profile.search")).toString()); + profile->setHomepage(config.value("homepage", defaults.value("profile.homepage")).toUrl()); + profile->setNewtab(config.value("newtab", defaults.value("profile.newtab")).toUrl()); + config.beginGroup("properties"); { const auto keys = config.childKeys(); @@ -56,20 +66,22 @@ void loadProfile(WebProfile *profile, const QHash &defaults, c } } config.endGroup(); + + return profile; } -WebProfile::WebProfile(QObject *parent) +WebProfile::WebProfile(const QString &name, QObject *parent) : QWebEngineProfile(parent) { - m_name = tr("Off-the-record"); + m_name = name; #ifdef QT_DEBUG - qDebug("Creating off-the-record profile"); + qDebug("Creating otr profile %s", qUtf8Printable(m_name)); #endif } -WebProfile::WebProfile(const QString &name, QObject *parent) - : QWebEngineProfile(name, parent) +WebProfile::WebProfile(const QString &storageName, const QString &name, QObject *parent) + : QWebEngineProfile(storageName, parent) { m_name = name; diff --git a/lib/web/webprofile.h b/lib/web/webprofile.h index 31c5b44..a421359 100644 --- a/lib/web/webprofile.h +++ b/lib/web/webprofile.h @@ -34,9 +34,9 @@ class WebProfile : public QWebEngineProfile public: // off-the-record constructor - explicit WebProfile(QObject *parent = nullptr); - // default constructor explicit WebProfile(const QString &name, QObject *parent = nullptr); + // default constructor + explicit WebProfile(const QString &storageName, const QString &name, QObject *parent = nullptr); ~WebProfile() = default; @@ -108,7 +108,7 @@ private: QUrl m_newtab = QUrl("about:blank"); }; -void loadProfile(WebProfile *profile, const QHash &defaults, const QString &path); +WebProfile* loadProfile(const QString &name, const QHash &defaults, const QString &path = QString()); //WebProfile *saveProfile(WebProfile *profile, const QString &path); #endif // SMOLBOTE_WEBENGINEPROFILE_H diff --git a/src/browser.cpp b/src/browser.cpp index 99ddcc3..8abe10a 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -75,23 +75,6 @@ inline QVector loadPlugins(const QString &path) return list; } -inline QHash loadProfiles(const QHash &defaults, const QString &location) -{ - QDir profilesDir(location); - QHash list; - - if(profilesDir.exists()) { - const QFileInfoList entries = profilesDir.entryInfoList({ "*.profile" }, QDir::Files | QDir::Readable); - for(const auto &entry : entries) { - auto *profile = new WebProfile(entry.baseName()); - loadProfile(profile, defaults, entry.absoluteFilePath()); - list.insert(entry.baseName(), profile); - } - } - - return list; -} - Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { @@ -125,16 +108,24 @@ void Browser::setup(const QString &defaultProfile) // load profiles { - auto *otr = new WebProfile(this); - loadProfile(otr, m_config->section("profile"), QString::fromStdString(m_config->value("profile.path").value()) + "/otr.ini"); - m_profiles.insert(tr("Off-the-record"), otr); - m_profiles.unite(loadProfiles(m_config->section("profile"), QString::fromStdString(m_config->value("profile.path").value()))); - - if(defaultProfile == "") { - WebProfile::setDefaultProfile(otr); - } else { - WebProfile::setDefaultProfile(m_profiles.value(defaultProfile)); + const auto defaults = m_config->section("profile"); + const QDir profilesDir(m_config->value("profile.path").value()); + + if(profilesDir.exists()) { + const auto entries = profilesDir.entryInfoList({"*.profile"}, QDir::Files | QDir::Readable); + + for(const QFileInfo &f : entries) { + auto *profile = loadProfile(f.baseName(), defaults, f.absoluteFilePath()); + m_profiles.insert(f.baseName(), profile); + } + } + + // set default profile + if(!m_profiles.contains(defaultProfile)) { + // if this profile has not been added, it doesn't have a path + m_profiles.insert(defaultProfile, loadProfile(defaultProfile, defaults)); } + WebProfile::setDefaultProfile(m_profiles.value(defaultProfile)); } // url request filter diff --git a/src/mainwindow/widgets/tabwidget.cpp b/src/mainwindow/widgets/tabwidget.cpp index 2858fb0..a3fcbdd 100644 --- a/src/mainwindow/widgets/tabwidget.cpp +++ b/src/mainwindow/widgets/tabwidget.cpp @@ -13,6 +13,7 @@ #include #include #include "browser.h" +#include TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent) @@ -62,10 +63,10 @@ TabWidget::TabWidget(QWidget *parent) Q_CHECK_PTR(browser); for(const QString &name : browser->profiles()) { - auto *profileAction = loadProfile_menu->addAction(name); auto *profile = browser->profile(name); Q_CHECK_PTR(profile); + auto *profileAction = loadProfile_menu->addAction(profile->name()); connect(profileAction, &QAction::triggered, this, [this, profile]() { int index = this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos())); auto *view = qobject_cast(this->widget(index)); -- cgit v1.2.1