From de6225509a584df31be1b28146e73d8ecf72c7eb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 2 Sep 2018 12:38:21 +0200 Subject: Clean up ProfileManager --- src/browser.cpp | 29 ++++++++++++----------------- src/mainwindow/mainwindow.cpp | 2 +- src/profilemanager.cpp | 19 ++++++++++++++++--- src/profilemanager.h | 27 +++++++++++++++++++++------ src/session.cpp | 4 ++-- src/subwindow/subwindow.cpp | 2 +- src/webengine/webview.cpp | 2 +- 7 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/browser.cpp b/src/browser.cpp index 46e55d2..0af050f 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -54,14 +54,11 @@ void Browser::about() QPair Browser::loadProfile(const QString &id) { - Q_ASSERT(m_config); - const QDir profilesDir(m_config->value("profile.path").value()); - - auto *profile = ProfileManager::loadProfile(profilesDir.absoluteFilePath(id + ".profile"), m_config->section("profile")); + WebProfile *profile = profileManager->loadProfile(id); connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); profile->setRequestInterceptor(m_urlFilter.get()); - return QPair(id, profile); + return QPair(profileManager->id(profile), profile); } void Browser::setConfiguration(std::unique_ptr &config) @@ -90,7 +87,9 @@ void Browser::registerPlugin(const Plugin &plugin) void Browser::setup(const QString &defaultProfile) { - Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set"); + Q_ASSERT(m_config); + + ProfileManager::setInstance(new ProfileManager(m_config->section("profile"), this)); auto stylesheet = m_config->value("browser.stylesheet"); if(stylesheet) { @@ -116,20 +115,16 @@ void Browser::setup(const QString &defaultProfile) const auto entries = profilesDir.entryInfoList({ "*.profile" }, QDir::Files | QDir::Readable, QDir::Time); for(const QFileInfo &f : entries) { - auto *profile = ProfileManager::loadProfile(f.absoluteFilePath(), defaults); - connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); - profile->setRequestInterceptor(m_urlFilter.get()); + loadProfile(f.absoluteFilePath()); } } // set default profile - if(ProfileManager::profile(defaultProfile) == nullptr) { + if(profileManager->profile(defaultProfile) == nullptr) { // if this profile has not been added, it doesn't have a path - auto *profile = ProfileManager::loadProfile(QString(), defaults); - connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); - profile->setRequestInterceptor(m_urlFilter.get()); + loadProfile(QString()); } - WebProfile::setDefaultProfile(ProfileManager::profile(defaultProfile)); + WebProfile::setDefaultProfile(profileManager->profile(defaultProfile)); } // bookmarks @@ -141,7 +136,7 @@ void Browser::setup(const QString &defaultProfile) const QVector Browser::profiles() const { - const QMap profileList = ProfileManager::profileList(); + const QMap profileList = profileManager->profileList(); return QVector::fromList(profileList.values()); } @@ -158,7 +153,7 @@ void Browser::createSession(const QJsonObject &object) for(const QJsonValue &s : subwindows) { const QJsonObject subwindow = s.toObject(); const QString profileId = subwindow.value("profile").toString(); - WebProfile *profile = ProfileManager::profile(profileId); + WebProfile *profile = profileManager->profile(profileId); if(profile == nullptr) profile = WebProfile::defaultProfile(); Q_CHECK_PTR(profile); @@ -180,7 +175,7 @@ void Browser::createSession(const QJsonObject &object) for(const QJsonValue &t : subwindow.value("tabs").toArray()) { const QJsonObject tab = t.toObject(); const QUrl url = QUrl::fromUserInput(tab.value("url").toString()); - WebProfile *p = ProfileManager::profile(tab.value("profile").toString()); + WebProfile *p = profileManager->profile(tab.value("profile").toString()); window->addTab(url, p); } } diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index ccc5f1e..91e820a 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -382,7 +382,7 @@ void MainWindow::updatePageLoadProfileMenu() auto *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); - ProfileIterator it(ProfileManager::profileList()); + ProfileIterator it(profileManager->profileList()); while(it.hasNext()) { it.next(); auto *profile = it.value(); diff --git a/src/profilemanager.cpp b/src/profilemanager.cpp index 1038598..5b0f76e 100644 --- a/src/profilemanager.cpp +++ b/src/profilemanager.cpp @@ -12,13 +12,26 @@ #include #include -QMap ProfileManager::profiles; +ProfileManager *ProfileManager::s_instance = nullptr; -ProfileManager::ProfileManager(QObject *parent) : QObject(parent) +ProfileManager::ProfileManager(const QHash &profileSection, QObject *parent) : QObject(parent) + , defaults(profileSection) { } -WebProfile *ProfileManager::loadProfile(const QString &path, const QHash &defaults) +void ProfileManager::setInstance(ProfileManager *instance) +{ + Q_CHECK_PTR(instance); + s_instance = instance; +} + +ProfileManager *ProfileManager::instance() +{ + Q_CHECK_PTR(s_instance); + return s_instance; +} + +WebProfile *ProfileManager::loadProfile(const QString &path) { WebProfile *profile = nullptr; #ifdef QT_DEBUG diff --git a/src/profilemanager.h b/src/profilemanager.h index b2509a1..5ece510 100644 --- a/src/profilemanager.h +++ b/src/profilemanager.h @@ -15,21 +15,36 @@ typedef QMapIterator ProfileIterator; +#define profileManager ProfileManager::instance() + class WebProfile; class ProfileManager : public QObject { Q_OBJECT public: - explicit ProfileManager(QObject *parent = nullptr); - static WebProfile *loadProfile(const QString &path, const QHash &defaults); + struct Profile + { + QString id; + WebProfile *profile = nullptr; + }; + + explicit ProfileManager(const QHash &profileSection, QObject *parent = nullptr); + + static void setInstance(ProfileManager *instance); + static ProfileManager *instance(); - static const QString id(WebProfile *profile); - static WebProfile *profile(const QString &id); - static const QMap& profileList(); + WebProfile *loadProfile(const QString &path); + + const QString id(WebProfile *profile); + WebProfile *profile(const QString &id); + const QMap& profileList(); private: - static QMap profiles; + QMap profiles; + + static ProfileManager *s_instance; + const QHash defaults; }; #endif // SMOLBOTE_PROFILEMANAGER_H diff --git a/src/session.cpp b/src/session.cpp index 0781cd7..414e8aa 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -25,13 +25,13 @@ QJsonObject Session::toJsonObject(MainWindow *window) QJsonArray subwindows; for(const SubWindow *subwindow : window->subWindows()) { QJsonObject window; - window.insert("profile", ProfileManager::id(subwindow->profile())); + window.insert("profile", profileManager->id(subwindow->profile())); QJsonArray tabs; for(int i = 0; i < subwindow->tabCount(); ++i) { QJsonObject tab; tab.insert("url", subwindow->view(i)->url().toString()); - tab.insert("profile", ProfileManager::id(subwindow->view(i)->profile())); + tab.insert("profile", profileManager->id(subwindow->view(i)->profile())); tabs.append(tab); } window.insert("tabs", tabs); diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 56bf821..4e79a03 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -50,7 +50,7 @@ SubWindow::SubWindow(const std::unique_ptr &config, QWidget *pare Browser *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); - ProfileIterator it(ProfileManager::profileList()); + ProfileIterator it(profileManager->profileList()); while(it.hasNext()) { it.next(); auto *profile = it.value(); diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index d7281e9..94f73bc 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -244,7 +244,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) }); auto *newTabMenu = new QMenu(tr("Open link in new tab with profile"), this); - ProfileIterator it(ProfileManager::profileList()); + ProfileIterator it(profileManager->profileList()); while(it.hasNext()) { it.next(); connect(newTabMenu->addAction(it.key()), &QAction::triggered, this, [this, ctxdata, it]() { -- cgit v1.2.1