From 9abf7a9690163737d3e70b3b52d814135858d0d5 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 29 Sep 2018 14:56:39 +0200 Subject: ProfileManager: move initial profile loading to ProfileManager --- src/browser.cpp | 58 ++++++++++++++++--------------------------- src/browser.h | 1 + src/mainwindow/mainwindow.cpp | 1 + src/session.cpp | 3 ++- src/subwindow/subwindow.cpp | 1 + src/webengine/webview.cpp | 2 ++ 6 files changed, 29 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/browser.cpp b/src/browser.cpp index 6cdfdab..e93123c 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -16,16 +16,16 @@ #include #include #include +#include #include +#include #include #include #include #include #include -#include #include -#include -#include +#include Browser::Browser(int &argc, char *argv[], bool allowSecondary) : SingleApplication(argc, argv, allowSecondary, SingleApplication::User | SingleApplication::SecondaryNotification | SingleApplication::ExcludeAppVersion) @@ -55,11 +55,11 @@ void Browser::about() QPair Browser::loadProfile(const QString &id) { - WebProfile *profile = profileManager->loadProfile(id); + WebProfile *profile = m_profileManager->loadProfile(id); connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); profile->setRequestInterceptor(m_urlFilter.get()); - return QPair(profileManager->id(profile), profile); + return QPair(m_profileManager->id(profile), profile); } void Browser::setConfiguration(std::unique_ptr &config) @@ -76,27 +76,24 @@ Configuration *Browser::getConfiguration() const ProfileManager *Browser::getProfileManager() { - return ProfileManager::instance(); + return m_profileManager; } void Browser::registerPlugin(const Plugin &plugin) { Q_ASSERT(m_config); - auto *p = qobject_cast(plugin.instance); - Q_CHECK_PTR(p); - p->setBrowserInterface(this); - - m_plugins.append(plugin); + if(p != nullptr) { + p->setBrowserInterface(this); + m_plugins.append(plugin); + } } void Browser::setup(const QString &defaultProfile) { Q_ASSERT(m_config); - ProfileManager::setInstance(new ProfileManager(m_config->section("profile"), this)); - auto stylesheet = m_config->value("browser.stylesheet"); if(stylesheet) { QFile f(stylesheet.value()); @@ -113,25 +110,15 @@ void Browser::setup(const QString &defaultProfile) // cookie request filter // load profiles - { - 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, QDir::Time); - - for(const QFileInfo &f : entries) { - loadProfile(f.absoluteFilePath()); - } - } - - // set default profile - if(profileManager->profile(defaultProfile) == nullptr) { - // if this profile has not been added, it doesn't have a path - loadProfile(QString()); - } - WebProfile::setDefaultProfile(profileManager->profile(defaultProfile)); + m_profileManager = new ProfileManager(m_config->section("profile"), defaultProfile, this); + // connect profiles + for(const QString &id : m_profileManager->idList()) { + auto *profile = m_profileManager->profile(id); + connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); + profile->setRequestInterceptor(m_urlFilter.get()); } + // set default profile + WebProfile::setDefaultProfile(m_profileManager->profile(defaultProfile)); // bookmarks m_bookmarks = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); @@ -142,7 +129,7 @@ void Browser::setup(const QString &defaultProfile) auto *timer = new QTimer(this); connect(timer, &QTimer::timeout, m_bookmarks.get(), &BookmarksWidget::save); // 5min * 60sec * 1000ms - timer->start(5*60*1000); + timer->start(5 * 60 * 1000); } void Browser::createSession(const QJsonObject &object) @@ -158,7 +145,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 = m_profileManager->profile(profileId); if(profile == nullptr) profile = WebProfile::defaultProfile(); Q_CHECK_PTR(profile); @@ -180,7 +167,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 = m_profileManager->profile(tab.value("profile").toString()); window->addTab(url, p); } } @@ -190,7 +177,7 @@ void Browser::createSession(const QJsonObject &object) MainWindow *Browser::createWindow() { // the window will delete itself when it closes, so we don't need to delete it - MainWindow *window = new MainWindow(m_config); + auto *window = new MainWindow(m_config); connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search); connect(window, &MainWindow::createBookmark, m_bookmarks.get(), &BookmarksWidget::addBookmark); @@ -231,7 +218,6 @@ MainWindow *Browser::createWindow() plugin->createWidget(window)->exec(); }); window->addAction(MainWindow::ToolsMenu, pluginAction); - } m_windows.append(window); diff --git a/src/browser.h b/src/browser.h index 007b0a1..bf91cc8 100644 --- a/src/browser.h +++ b/src/browser.h @@ -63,6 +63,7 @@ private: std::unique_ptr m_config; std::shared_ptr m_bookmarks; std::unique_ptr m_downloads; + ProfileManager *m_profileManager; std::unique_ptr m_urlFilter; QVector m_windows; diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index e1b8fd8..8a33e23 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -390,6 +390,7 @@ void MainWindow::updatePageLoadProfileMenu() if(currentView == nullptr) return; + auto *profileManager = dynamic_cast(qApp)->getProfileManager(); pageLoadProfileMenu->addActions(profileManager->createProfileMenu([this](WebProfile *profile) { this->currentView->setProfile(profile); }, diff --git a/src/session.cpp b/src/session.cpp index 70e1deb..3c40a2a 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -13,7 +13,7 @@ #include #include "webengine/webview.h" #include - +#include "browser.h" QJsonObject Session::session(QVector windows) { @@ -31,6 +31,7 @@ QJsonObject Session::session(QVector windows) QJsonObject Session::window(const MainWindow *window) { QJsonObject obj; + auto *profileManager = dynamic_cast(qApp)->getProfileManager(); QJsonArray subwindows; for(const SubWindow *subwindow : window->subWindows()) { diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 7dc1cdf..44ac1b1 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -50,6 +50,7 @@ SubWindow::SubWindow(const std::unique_ptr &config, QWidget *pare Browser *browser = qobject_cast(qApp); Q_CHECK_PTR(browser); + auto *profileManager = dynamic_cast(qApp)->getProfileManager(); loadProfile_menu->addActions(profileManager->createProfileMenu([this, profileName_action](WebProfile *profile) { this->setProfile(profile); profileName_action->setText(tr("Profile: %1").arg(profile->name())); diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index c2c0f75..f8be48e 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -22,6 +22,7 @@ #include #include #include +#include "browser.h" inline QAction *historyAction(QWebEngineView *view, const QWebEngineHistoryItem &item) { @@ -243,6 +244,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) createWindow(QWebEnginePage::WebBrowserTab)->load(ctxdata.linkUrl()); }); + auto *profileManager = dynamic_cast(qApp)->getProfileManager(); QMenu *newTabMenu = profileManager->createProfileMenu([this, ctxdata](WebProfile *profile) { auto *view = this->createWindow(QWebEnginePage::WebBrowserTab); view->setProfile(profile); -- cgit v1.2.1