aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-01-05 10:55:56 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-01-05 14:59:10 +0200
commit7918bce1422fb217b59131111c6fb1ccdf52ddc4 (patch)
tree369fce128f1b021c0c88455039c0011a2014b9d7 /src
parentDisable plugins as broken (diff)
downloadsmolbote-7918bce1422fb217b59131111c6fb1ccdf52ddc4.tar.xz
Code cleanup
- Replace Browser::getProfileManager with WebProfileManager::instance - Make WebProfileManager::profileMenu a free function - Replace Browser::loadProfile with Browser::loadProfiles
Diffstat (limited to 'src')
-rw-r--r--src/browser.cpp68
-rw-r--r--src/browser.h28
-rw-r--r--src/mainwindow/menubar.cpp24
-rw-r--r--src/session/savesessiondialog.cpp6
-rw-r--r--src/session/session.cpp12
-rw-r--r--src/webengine/webprofilemanager.cpp44
-rw-r--r--src/webengine/webprofilemanager.h23
-rw-r--r--src/webengine/webview.cpp3
8 files changed, 113 insertions, 95 deletions
diff --git a/src/browser.cpp b/src/browser.cpp
index c3cbd2f..09fc2c8 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -98,46 +98,31 @@ const QList<QPair<QString, Profile *>> Browser::profileList() const
return profiles;
}
-QPair<QString, Profile *> Browser::loadProfile(const QString &id, bool isOffTheRecord)
+void Browser::loadProfiles(const QStringList &profilePaths)
{
Configuration conf;
- const QString _id = [id]() {
- // if id contains a separator, it should be a path
- if(id.contains(QDir::separator())) {
- return QFileInfo(id).baseName();
- } else {
- return id;
- }
- }();
+ for(const QString &path : profilePaths) {
+ const QString id = QFileInfo(path).baseName();
- auto *profile = m_profileManager->profile(/* id */ _id, /* path */ (_id == id) ? QString() : id, isOffTheRecord);
- connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
-
- auto *interceptor = new UrlRequestInterceptor(profile, profile);
- for(UrlFilter *filter : m_filters) {
- interceptor->addFilter(filter);
- }
- const auto headers = conf.value<QStringList>("filter.header").value_or(QStringList());
- for(const QString &header : headers) {
- const auto h = header.split(QLatin1Literal(":"));
- if(h.length() == 2)
- interceptor->addHttpHeader(h.at(0).toLatin1(), h.at(1).toLatin1());
- }
- profile->setUrlRequestInterceptor(interceptor);
+ auto *profile = m_profileManager->add(id, path);
+ connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
- spdlog::info("Added profile: {}{}", qUtf8Printable(_id), profile->isOffTheRecord() ? " (otr)" : "");
- return QPair<QString, WebProfile *>(_id, profile);
-}
+ auto *interceptor = new UrlRequestInterceptor(profile, profile);
+ for(UrlFilter *filter : m_filters) {
+ interceptor->addFilter(filter);
+ }
-void Browser::removeProfile(const QString &id)
-{
- m_profileManager->deleteProfile(id);
-}
+ const auto headers = conf.value<QStringList>("filter.header").value_or(QStringList());
+ for(const QString &header : headers) {
+ const auto h = header.split(QLatin1Literal(":"));
+ if(h.length() == 2)
+ interceptor->addHttpHeader(h.at(0).toLatin1(), h.at(1).toLatin1());
+ }
+ profile->setUrlRequestInterceptor(interceptor);
-WebProfileManager *Browser::getProfileManager()
-{
- return m_profileManager;
+ spdlog::info("Added profile\t{}{}\t{}", qUtf8Printable(id), profile->isOffTheRecord() ? "*" : "", qUtf8Printable(profile->name()));
+ }
}
QPluginLoader *Browser::addPlugin(const QString &path)
@@ -202,19 +187,20 @@ void Browser::setup(QVector<QPluginLoader *> plugins)
// load profiles
m_profileManager = new WebProfileManager(this);
- for(const QString &profilePath : Util::files(conf.value<QString>("profile.path").value(), { "*.profile" })) {
- this->loadProfile(profilePath);
- }
+ WebProfileManager::setInstance(m_profileManager);
+ loadProfiles(Util::files(conf.value<QString>("profile.path").value(), { "*.profile" }));
// set default profile
{
const QString id = conf.value<QString>("profile.default").value();
auto *profile = m_profileManager->profile(id);
if(profile == nullptr) {
- profile = qobject_cast<WebProfile *>(loadProfile(id).second);
+ spdlog::error("Unknown default profile!");
+ //profile = qobject_cast<WebProfile *>(loadProfile(id).second);
+ } else {
+ spdlog::info("Default profile\t{}{}\t{}", qUtf8Printable(id), profile->isOffTheRecord() ? "*" : "", qUtf8Printable(profile->name()));
+ WebProfile::setDefaultProfile(profile);
}
-
- WebProfile::setDefaultProfile(profile);
}
// bookmarks
@@ -231,7 +217,7 @@ void Browser::setup(QVector<QPluginLoader *> plugins)
});
auto *openInCurrentTabWithProfile = menu->addMenu(tr("Open link in current tab with profile"));
- m_profileManager->profileMenu(openInCurrentTabWithProfile, [url, subwindow](WebProfile *profile) {
+ profileMenu(openInCurrentTabWithProfile, [url, subwindow](WebProfile *profile) {
subwindow->currentView()->setProfile(profile);
subwindow->currentView()->load(url);
});
@@ -241,7 +227,7 @@ void Browser::setup(QVector<QPluginLoader *> plugins)
});
auto *openInNewTabWithProfile = menu->addMenu(tr("Open link in new tab with profile"));
- m_profileManager->profileMenu(openInNewTabWithProfile, [url, subwindow](WebProfile *profile) {
+ profileMenu(openInNewTabWithProfile, [url, subwindow](WebProfile *profile) {
subwindow->addTab(url, profile);
});
diff --git a/src/browser.h b/src/browser.h
index fb47c46..a16bd6d 100644
--- a/src/browser.h
+++ b/src/browser.h
@@ -11,12 +11,12 @@
#include "session/session.h"
#include <QJsonObject>
+#include <QMenu>
+#include <QPluginLoader>
#include <QVector>
#include <functional>
#include <memory>
#include <singleapplication.h>
-#include <QMenu>
-#include <QPluginLoader>
class UrlFilter;
class Configuration;
@@ -38,14 +38,8 @@ public slots:
public:
// interface
- [[deprecated]]
- WebProfileManager *getProfileManager();
- [[deprecated]]
- const QList<QPair<QString, Profile *>> profileList() const;
- [[deprecated]]
- QPair<QString, Profile *> loadProfile(const QString &id, bool isOffTheRecord = true);
- [[deprecated]]
- void removeProfile(const QString &id);
+ [[deprecated]] const QList<QPair<QString, Profile *>> profileList() const;
+ void loadProfiles(const QStringList &profilePaths);
QPluginLoader *addPlugin(const QString &path = QString());
@@ -56,10 +50,12 @@ public:
return qAsConst(m_windows);
}
- BookmarksWidget *bookmarks() const {
+ BookmarksWidget *bookmarks() const
+ {
return m_bookmarks.get();
}
- DownloadsWidget *downloads() const {
+ DownloadsWidget *downloads() const
+ {
return m_downloads.get();
}
@@ -69,10 +65,12 @@ public slots:
private:
struct PluginInfo {
- explicit PluginInfo(QPluginLoader *l) {
+ explicit PluginInfo(QPluginLoader *l)
+ {
this->loader = l;
}
- ~PluginInfo() {
+ ~PluginInfo()
+ {
loader->unload();
for(auto *m : menus)
m->deleteLater();
@@ -91,7 +89,7 @@ private:
QVector<UrlFilter *> m_filters;
QVector<MainWindow *> m_windows;
- QVector<PluginInfo*> m_plugins;
+ QVector<PluginInfo *> m_plugins;
};
#endif // SMOLBOTE_BROWSER_H
diff --git a/src/mainwindow/menubar.cpp b/src/mainwindow/menubar.cpp
index 54ba102..76e08c6 100644
--- a/src/mainwindow/menubar.cpp
+++ b/src/mainwindow/menubar.cpp
@@ -12,10 +12,10 @@
#include "configuration.h"
#include "downloadswidget.h"
#include "mainwindow.h"
-#include "webengine/webprofilemanager.h"
#include "session/savesessiondialog.h"
#include "session/sessiondialog.h"
#include "subwindow/subwindow.h"
+#include "webengine/webprofilemanager.h"
#include "webengine/webview.h"
#include "widgets/menusearch.h"
#include <QApplication>
@@ -26,11 +26,11 @@
#include <QPrintDialog>
#include <QPrinter>
#include <QPrinterInfo>
+#include <QVBoxLayout>
#include <QWidgetAction>
#include <functional>
-#include <QVBoxLayout>
-inline void run_if(SubWindow *_subwindow, const std::function<void(SubWindow*, int)> &f)
+inline void run_if(SubWindow *_subwindow, const std::function<void(SubWindow *, int)> &f)
{
if(_subwindow != nullptr)
f(_subwindow, _subwindow->currentTabIndex());
@@ -292,7 +292,7 @@ MenuBar::MenuBar(MainWindow *parent)
});
subwindow->addAction(tr("Close all other tabs"), parent, [parent]() {
run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
- for(int i = _subwindow->tabCount() -1; i >= 0; i--) {
+ for(int i = _subwindow->tabCount() - 1; i >= 0; i--) {
if(i != currentIdx) {
const auto data = _subwindow->tabData(i);
if(!data.closeLocked)
@@ -342,9 +342,11 @@ MenuBar::MenuBar(MainWindow *parent)
auto *_subwindow = parent->currentSubWindow();
if(_subwindow != nullptr) {
- browser->getProfileManager()->profileMenu(subwindowProfile, [_subwindow](WebProfile *profile) {
- _subwindow->setProfile(profile);
- }, _subwindow->profile(), true);
+ profileMenu(
+ subwindowProfile, [_subwindow](WebProfile *profile) {
+ _subwindow->setProfile(profile);
+ },
+ _subwindow->profile(), true);
}
});
}
@@ -389,9 +391,11 @@ MenuBar::MenuBar(MainWindow *parent)
pageProfile->clear();
if(parent->currentView != nullptr) {
- browser->getProfileManager()->profileMenu(pageProfile, [parent](WebProfile *profile) {
- parent->currentView->setProfile(profile);
- }, parent->currentView->profile(), true);
+ profileMenu(
+ pageProfile, [parent](WebProfile *profile) {
+ parent->currentView->setProfile(profile);
+ },
+ parent->currentView->profile(), true);
}
});
diff --git a/src/session/savesessiondialog.cpp b/src/session/savesessiondialog.cpp
index ffc53f7..507cddb 100644
--- a/src/session/savesessiondialog.cpp
+++ b/src/session/savesessiondialog.cpp
@@ -9,9 +9,9 @@
#include "savesessiondialog.h"
#include "browser.h"
#include "mainwindow/mainwindow.h"
-#include "webengine/webprofilemanager.h"
#include "subwindow/subwindow.h"
#include "ui_savesessiondialog.h"
+#include "webengine/webprofilemanager.h"
#include "webengine/webview.h"
#include <QFileDialog>
#include <QPointer>
@@ -36,7 +36,7 @@ SaveSessionDialog::SaveSessionDialog(QWidget *parent)
for(const SubWindow *subwindow : window->subWindows()) {
auto *subwindowItem = new QTreeWidgetItem(windowItem);
subwindowItem->setText(0, tr("Subwindow"));
- subwindowItem->setText(1, browser->getProfileManager()->id(subwindow->profile()));
+ subwindowItem->setText(1, WebProfileManager::instance()->id(subwindow->profile()));
ui->treeWidget->expandItem(subwindowItem);
@@ -44,7 +44,7 @@ SaveSessionDialog::SaveSessionDialog(QWidget *parent)
auto *tabItem = new QTreeWidgetItem(subwindowItem);
auto *view = subwindow->view(i);
tabItem->setText(0, view->title());
- tabItem->setText(1, browser->getProfileManager()->id(view->profile()));
+ tabItem->setText(1, WebProfileManager::instance()->id(view->profile()));
}
}
}
diff --git a/src/session/session.cpp b/src/session/session.cpp
index 9e6fa7d..c97c22b 100644
--- a/src/session/session.cpp
+++ b/src/session/session.cpp
@@ -9,15 +9,15 @@
#include "session.h"
#include "../webengine/webview.h"
#include "browser.h"
+#include "configuration.h"
#include "mainwindow/mainwindow.h"
-#include "webengine/webprofilemanager.h"
#include "subwindow/subwindow.h"
+#include "webengine/webprofilemanager.h"
#include "webengine/webview.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QWebEngineHistory>
#include <memory>
-#include "configuration.h"
QJsonObject Session::fromCommandLine(const QString &profile, const QStringList &urls)
{
@@ -78,7 +78,7 @@ QJsonObject Session::_window(const MainWindow *window)
QJsonObject Session::_subwindow(const SubWindow *subwindow)
{
- auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager();
+ const auto *profileManager = WebProfileManager::instance();
Q_CHECK_PTR(profileManager);
QJsonObject subwindowData;
@@ -98,7 +98,7 @@ QJsonObject Session::_subwindow(const SubWindow *subwindow)
QJsonObject Session::view(const WebView *view)
{
- auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager();
+ const auto *profileManager = WebProfileManager::instance();
Q_CHECK_PTR(profileManager);
QByteArray historyData;
@@ -116,7 +116,7 @@ QJsonObject Session::view(const WebView *view)
void Session::restoreView(WebView *view, const QJsonObject &data)
{
- auto *profileManager = dynamic_cast<Browser *>(qApp)->getProfileManager();
+ const auto *profileManager = WebProfileManager::instance();
Q_CHECK_PTR(profileManager);
auto *profile = profileManager->profile(data["profile"].toString());
@@ -138,7 +138,7 @@ void Session::restoreSession(const QJsonObject &sessionData)
{
auto *browser = dynamic_cast<Browser *>(qApp);
Q_CHECK_PTR(browser);
- auto *profileManager = browser->getProfileManager();
+ const auto *profileManager = WebProfileManager::instance();
Q_CHECK_PTR(profileManager);
for(const auto subwindowData : sessionData["subwindows"].toArray()) {
diff --git a/src/webengine/webprofilemanager.cpp b/src/webengine/webprofilemanager.cpp
index 2fe6222..80762f7 100644
--- a/src/webengine/webprofilemanager.cpp
+++ b/src/webengine/webprofilemanager.cpp
@@ -12,6 +12,17 @@
#include <QFileInfo>
#include <QWebEngineSettings>
+static WebProfileManager *s_instance = nullptr;
+
+auto WebProfileManager::instance() -> const WebProfileManager *
+{
+ return s_instance;
+}
+void WebProfileManager::setInstance(WebProfileManager *ptr)
+{
+ s_instance = ptr;
+}
+
WebProfileManager::WebProfileManager(QObject *parent)
: QObject(parent)
{
@@ -37,11 +48,21 @@ WebProfileManager::~WebProfileManager()
}
}
-WebProfile *WebProfileManager::profile(const QString &id, const QString &path, bool isOffTheRecord)
+WebProfile *WebProfileManager::profile(const QString &id) const
{
// Check if profile exists
- if(profiles.contains(id))
+ if(profiles.contains(id)) {
return profiles.value(id).ptr;
+ }
+
+ return nullptr;
+}
+
+WebProfile *WebProfileManager::add(const QString &id, const QString &path, bool isOffTheRecord)
+{
+ if(profiles.contains(id)) {
+ return nullptr;
+ }
Configuration conf;
Profile profile;
@@ -52,12 +73,13 @@ WebProfile *WebProfileManager::profile(const QString &id, const QString &path, b
profile.settings = new QSettings;
// QWebEngineCore cleans up profiles automatically, so no need to set parent
- profile.ptr = [id, isOffTheRecord, profile]() {
- if(profile.settings->value("otr", isOffTheRecord).toBool())
- return new WebProfile(/* name */ profile.settings->value("name", id).toString(), /* parent */ nullptr);
- else
- return new WebProfile(/* storageName */ id, /* name */ profile.settings->value("name", id).toString(), /* parent */ nullptr);
- }();
+ if(profile.settings->value("otr", isOffTheRecord).toBool()) {
+ // name parent
+ profile.ptr = new WebProfile(profile.settings->value("name", id).toString(), nullptr);
+ } else {
+ // storageName name parent
+ profile.ptr = new WebProfile(id, profile.settings->value("name", id).toString(), nullptr);
+ }
profile.settings->setParent(profile.ptr);
@@ -133,12 +155,12 @@ void WebProfileManager::deleteProfile(const QString &id)
}
}
-void WebProfileManager::profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current, bool checkable) const
+void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current, bool checkable)
{
auto *group = new QActionGroup(menu);
- connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater);
+ QObject::connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater);
- for(const auto &profile : profiles) {
+ for(const auto &profile : s_instance->profiles) {
auto *action = menu->addAction(profile.ptr->name(), profile.ptr, [profile, callback]() {
callback(profile.ptr);
});
diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h
index 2d9cd29..0e18d5f 100644
--- a/src/webengine/webprofilemanager.h
+++ b/src/webengine/webprofilemanager.h
@@ -18,28 +18,34 @@
#include <QSettings>
#include <functional>
+void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false);
+
+class Browser;
class WebProfileManager : public QObject
{
Q_OBJECT
+
+ friend class Browser;
+ friend void profileMenu(QMenu *, const std::function<void(WebProfile *)> &, WebProfile *current, bool);
+
public:
explicit WebProfileManager(QObject *parent);
- ~WebProfileManager();
+ ~WebProfileManager() override;
+
+ static auto instance() -> const WebProfileManager *;
+ static void setInstance(WebProfileManager *ptr);
/** Create a profile with specified id
* param id The profile ID
- * param path The path to the profile settings
- * param isOffTheRecord Off-the-record toggle
* return WebProfile* The profile, or nullptr if one could not be created
*/
- WebProfile *profile(const QString &id, const QString &path = QString(), bool isOffTheRecord = true);
+ WebProfile *profile(const QString &id) const;
/** Set a profile for deletion
* param id The profile ID
* return void
*/
- void deleteProfile(const QString &id);
-
- void profileMenu(QMenu *menu, const std::function<void(WebProfile *)> &callback, WebProfile *current = nullptr, bool checkable = false) const;
+ [[deprecated]] void deleteProfile(const QString &id);
const QStringList idList() const
{
@@ -56,6 +62,9 @@ public:
return QString();
}
+protected:
+ WebProfile *add(const QString &id, const QString &path = QString(), bool isOffTheRecord = true);
+
private:
struct Profile {
WebProfile *ptr = nullptr;
diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp
index 4fa227f..5b2b6fd 100644
--- a/src/webengine/webview.cpp
+++ b/src/webengine/webview.cpp
@@ -7,7 +7,6 @@
*/
#include "webview.h"
-#include "browser.h"
#include "subwindow/subwindow.h"
#include "wallet/wallet.h"
#include "webpage.h"
@@ -255,7 +254,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event)
});
auto *newTabMenu = menu->addMenu(tr("Open link in new tab with profile"));
- dynamic_cast<Browser *>(qApp)->getProfileManager()->profileMenu(newTabMenu, [this, ctxdata](WebProfile *profile) {
+ profileMenu(newTabMenu, [this, ctxdata](WebProfile *profile) {
auto *view = this->createWindow(QWebEnginePage::WebBrowserTab);
view->setProfile(profile);
view->load(ctxdata.linkUrl());