aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-09-29 14:56:39 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-09-29 14:56:39 +0200
commit9abf7a9690163737d3e70b3b52d814135858d0d5 (patch)
tree037b1d3a12115221182cb283c14736229c52a8d8
parentUpdate repository path in license headers (diff)
downloadsmolbote-9abf7a9690163737d3e70b3b52d814135858d0d5.tar.xz
ProfileManager: move initial profile loading to ProfileManager
-rw-r--r--lib/web/profilemanager.cpp54
-rw-r--r--lib/web/profilemanager.h10
-rw-r--r--src/browser.cpp58
-rw-r--r--src/browser.h1
-rw-r--r--src/mainwindow/mainwindow.cpp1
-rw-r--r--src/session.cpp3
-rw-r--r--src/subwindow/subwindow.cpp1
-rw-r--r--src/webengine/webview.cpp2
8 files changed, 54 insertions, 76 deletions
diff --git a/lib/web/profilemanager.cpp b/lib/web/profilemanager.cpp
index 97d5f8d..75fd413 100644
--- a/lib/web/profilemanager.cpp
+++ b/lib/web/profilemanager.cpp
@@ -11,23 +11,25 @@
#include <QFileInfo>
#include <QWebEngineSettings>
-ProfileManager *ProfileManager::s_instance = nullptr;
-
-ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent) : QObject(parent)
- , defaults(profileSection)
+ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, const QString &defaultId, QObject *parent)
+ : QObject(parent)
+ , defaults(profileSection)
{
-}
+ // load profiles from path
+ const QDir profilesDir(defaults.value("profile.path"));
-void ProfileManager::setInstance(ProfileManager *instance)
-{
- Q_CHECK_PTR(instance);
- s_instance = instance;
-}
+ if(profilesDir.exists()) {
+ const auto entries = profilesDir.entryInfoList({ "*.profile" }, QDir::Files | QDir::Readable, QDir::Time);
-ProfileManager *ProfileManager::instance()
-{
- Q_CHECK_PTR(s_instance);
- return s_instance;
+ for(const QFileInfo &f : entries) {
+ loadProfile(f.absoluteFilePath());
+ }
+ }
+
+ const QString defaultProfile = defaults.value("profile.default", defaultId);
+ if(m_profiles.count(defaultProfile) == 0) {
+ loadProfile(defaultProfile);
+ }
}
WebProfile *ProfileManager::loadProfile(const QString &path)
@@ -100,9 +102,9 @@ void ProfileManager::deleteProfile(const QString &id)
QMenu *ProfileManager::createProfileMenu(std::function<void(WebProfile *)> callback, QWidget *parent) const
{
- QMenu *menu = new QMenu(parent);
- for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
- WebProfile *profile = it->second->profile;
+ auto *menu = new QMenu(parent);
+ for(const auto &m_profile : m_profiles) {
+ WebProfile *profile = m_profile.second->profile;
QAction *action = menu->addAction(profile->name());
connect(action, &QAction::triggered, [profile, callback]() {
callback(profile);
@@ -114,17 +116,17 @@ QMenu *ProfileManager::createProfileMenu(std::function<void(WebProfile *)> callb
const QStringList ProfileManager::idList() const
{
QStringList ids;
- for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
- ids.append(it->first);
+ for(const auto &m_profile : m_profiles) {
+ ids.append(m_profile.first);
}
return ids;
}
const QString ProfileManager::id(const WebProfile *profile) const
{
- for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
- if(it->second->profile == profile)
- return it->first;
+ for(const auto &m_profile : m_profiles) {
+ if(m_profile.second->profile == profile)
+ return m_profile.first;
}
return QString();
}
@@ -136,11 +138,3 @@ WebProfile *ProfileManager::profile(const QString &id) const
}
return nullptr;
}
-
-const QString ProfileManager::configurationPath(const QString &id) const
-{
- if(m_profiles.count(id) > 0) {
- return m_profiles.at(id)->path;
- }
- return QString();
-}
diff --git a/lib/web/profilemanager.h b/lib/web/profilemanager.h
index 1cb5dd5..9cd2cbd 100644
--- a/lib/web/profilemanager.h
+++ b/lib/web/profilemanager.h
@@ -20,17 +20,12 @@
#include <map>
#include <memory>
-#define profileManager ProfileManager::instance()
-
class WebProfile;
class ProfileManager : public QObject
{
Q_OBJECT
public:
- explicit ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent = nullptr);
-
- static void setInstance(ProfileManager *instance);
- static ProfileManager *instance();
+ explicit ProfileManager(const QHash<QString, QString> &profileSection, const QString &defaultId, QObject *parent = nullptr);
WebProfile *loadProfile(const QString &path);
void deleteProfile(const QString &id);
@@ -40,7 +35,6 @@ public:
const QStringList idList() const;
const QString id(const WebProfile *profile) const;
WebProfile *profile(const QString &id) const;
- const QString configurationPath(const QString &id) const;
private:
struct ProfileData {
@@ -68,8 +62,6 @@ private:
};
std::map<QString, std::unique_ptr<ProfileData>> m_profiles;
-
- static ProfileManager *s_instance;
const QHash<QString, QString> defaults;
};
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 <QFileInfo>
#include <QFileInfoList>
#include <QJsonArray>
+#include <QJsonDocument>
#include <QPluginLoader>
+#include <QTimer>
#include <about/aboutdialog.h>
#include <bookmarks/bookmarkswidget.h>
#include <configuration/configuration.h>
#include <downloads/downloadswidget.h>
#include <version.h>
-#include <web/webprofile.h>
#include <web/profilemanager.h>
-#include <QJsonDocument>
-#include <QTimer>
+#include <web/webprofile.h>
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<QString, WebProfile *> 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<QString, WebProfile *>(profileManager->id(profile), profile);
+ return QPair<QString, WebProfile *>(m_profileManager->id(profile), profile);
}
void Browser::setConfiguration(std::unique_ptr<Configuration> &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<PluginInterface *>(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<QString>("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<QString>("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<BookmarksWidget>(QString::fromStdString(m_config->value<std::string>("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<Configuration> m_config;
std::shared_ptr<BookmarksWidget> m_bookmarks;
std::unique_ptr<DownloadsWidget> m_downloads;
+ ProfileManager *m_profileManager;
std::unique_ptr<UrlRequestInterceptor> m_urlFilter;
QVector<MainWindow *> 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<Browser *>(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 <QJsonArray>
#include "webengine/webview.h"
#include <web/profilemanager.h>
-
+#include "browser.h"
QJsonObject Session::session(QVector<MainWindow *> windows)
{
@@ -31,6 +31,7 @@ QJsonObject Session::session(QVector<MainWindow *> windows)
QJsonObject Session::window(const MainWindow *window)
{
QJsonObject obj;
+ auto *profileManager = dynamic_cast<Browser *>(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<Configuration> &config, QWidget *pare
Browser *browser = qobject_cast<Browser *>(qApp);
Q_CHECK_PTR(browser);
+ auto *profileManager = dynamic_cast<Browser *>(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 <QWidgetAction>
#include <web/profilemanager.h>
#include <web/webprofile.h>
+#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<Browser *>(qApp)->getProfileManager();
QMenu *newTabMenu = profileManager->createProfileMenu([this, ctxdata](WebProfile *profile) {
auto *view = this->createWindow(QWebEnginePage::WebBrowserTab);
view->setProfile(profile);