From d631875b20244cb8eb0523c88cc838808e9ff4b0 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 4 Nov 2019 21:55:20 +0200 Subject: Move lib/webprofile into src/webengine --- lib/webprofile/meson.build | 12 --- lib/webprofile/webprofile.cpp | 147 ------------------------------ lib/webprofile/webprofile.h | 94 ------------------- lib/webprofile/webprofilemanager.cpp | 148 ------------------------------ lib/webprofile/webprofilemanager.h | 85 ------------------ meson.build | 1 - src/browser.cpp | 10 +-- src/mainwindow/mainwindow.cpp | 4 +- src/mainwindow/menubar.cpp | 2 +- src/mainwindow/widgets/navigationbar.cpp | 2 +- src/meson.build | 7 +- src/session/savesessiondialog.cpp | 2 +- src/session/session.cpp | 2 +- src/subwindow/subwindow.cpp | 2 +- src/subwindow/tabwidget.cpp | 2 +- src/webengine/webprofile.cpp | 147 ++++++++++++++++++++++++++++++ src/webengine/webprofile.h | 94 +++++++++++++++++++ src/webengine/webprofilemanager.cpp | 149 +++++++++++++++++++++++++++++++ src/webengine/webprofilemanager.h | 84 +++++++++++++++++ 19 files changed, 490 insertions(+), 504 deletions(-) delete mode 100644 lib/webprofile/meson.build delete mode 100644 lib/webprofile/webprofile.cpp delete mode 100644 lib/webprofile/webprofile.h delete mode 100644 lib/webprofile/webprofilemanager.cpp delete mode 100644 lib/webprofile/webprofilemanager.h create mode 100644 src/webengine/webprofile.cpp create mode 100644 src/webengine/webprofile.h create mode 100644 src/webengine/webprofilemanager.cpp create mode 100644 src/webengine/webprofilemanager.h diff --git a/lib/webprofile/meson.build b/lib/webprofile/meson.build deleted file mode 100644 index b3836cc..0000000 --- a/lib/webprofile/meson.build +++ /dev/null @@ -1,12 +0,0 @@ -webprofile_lib = static_library('webprofile', - ['webprofilemanager.cpp', 'webprofile.cpp', interfaces_moc, - mod_qt5.preprocess(moc_headers: ['webprofilemanager.h', 'webprofile.h'], dependencies: dep_qt5)], - dependencies: dep_qt5, - include_directories: [include] -) - -dep_webprofile = declare_dependency( - include_directories: include_directories('.'), - link_with: webprofile_lib -) - diff --git a/lib/webprofile/webprofile.cpp b/lib/webprofile/webprofile.cpp deleted file mode 100644 index 843b78e..0000000 --- a/lib/webprofile/webprofile.cpp +++ /dev/null @@ -1,147 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "webprofile.h" -#include -#include -#include -#include - -WebProfile *WebProfile::profile = nullptr; - -WebProfile::WebProfile(const QString &name, QObject *parent) - : Profile(parent) -{ - m_name = name; - - connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) { - m_cookies.append(cookie); - }); - connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) { - m_cookies.removeAll(cookie); - }); -} - -WebProfile::WebProfile(const QString &storageName, const QString &name, QObject *parent) - : Profile(storageName, parent) -{ - m_name = name; - - connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) { - m_cookies.append(cookie); - }); - connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) { - m_cookies.removeAll(cookie); - }); -} - -const QString WebProfile::name() const -{ - return m_name; -} - -void WebProfile::setName(const QString &name) -{ - m_name = name; - emit nameChanged(name); -} - -QString WebProfile::search() const -{ - return m_search; -} - -void WebProfile::setSearch(const QString &url) -{ - m_search = url; - emit searchChanged(m_search); -} - -QUrl WebProfile::homepage() const -{ - return m_homepage; -} - -void WebProfile::setHomepage(const QUrl &url) -{ - m_homepage = url; - emit homepageChanged(m_homepage); -} - -QUrl WebProfile::newtab() const -{ - return m_newtab; -} - -void WebProfile::setNewtab(const QUrl &url) -{ - m_newtab = url; - emit newtabChanged(m_newtab); -} - -void WebProfile::setCachePath(const QString &path) -{ - QWebEngineProfile::setCachePath(path); - emit propertyChanged("cachePath", path); -} - -void WebProfile::setPersistentStoragePath(const QString &path) -{ - QWebEngineProfile::setPersistentStoragePath(path); - emit propertyChanged("persistentStoragePath", path); -} - -void WebProfile::setPersistentCookiesPolicy(int policy) -{ - QWebEngineProfile::setPersistentCookiesPolicy(static_cast(policy)); - emit propertyChanged("persistentCookiesPolicy", policy); -} - -void WebProfile::setHttpAcceptLanguage(const QString &httpAcceptLanguage) -{ - QWebEngineProfile::setHttpAcceptLanguage(httpAcceptLanguage); - emit propertyChanged("httpAcceptLanguage", httpAcceptLanguage); -} - -void WebProfile::setHttpCacheMaximumSize(int maxSize) -{ - QWebEngineProfile::setHttpCacheMaximumSize(maxSize); - emit propertyChanged("httpCacheMaximumSize", maxSize); -} - -void WebProfile::setHttpCacheType(int type) -{ - QWebEngineProfile::setHttpCacheType(static_cast(type)); - emit propertyChanged("httpCacheType", type); -} - -void WebProfile::setHttpUserAgent(const QString &userAgent) -{ - QWebEngineProfile::setHttpUserAgent(userAgent); - emit propertyChanged("httpUserAgent", userAgent); -} - -void WebProfile::setHttpHeader(const QString &name, const QString &value) -{ - m_headers[name.toLatin1()] = value.toLatin1(); - emit headerChanged(name, value); -} - -void WebProfile::removeHttpHeader(const QString &name) -{ - if(m_headers.contains(name.toLatin1())) { - m_headers.remove(name.toLatin1()); - emit headerRemoved(name); - } -} - -void WebProfile::setSpellCheckEnabled(bool enable) -{ - QWebEngineProfile::setSpellCheckEnabled(enable); - emit propertyChanged("spellCheckEnabed", enable); -} diff --git a/lib/webprofile/webprofile.h b/lib/webprofile/webprofile.h deleted file mode 100644 index 37e3419..0000000 --- a/lib/webprofile/webprofile.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_WEBENGINEPROFILE_H -#define SMOLBOTE_WEBENGINEPROFILE_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class WebProfile : public Profile -{ - Q_OBJECT - -public: - // off-the-record constructor - explicit WebProfile(const QString &name, QObject *parent = nullptr); - // default constructor - explicit WebProfile(const QString &storageName, const QString &name, QObject *parent = nullptr); - - ~WebProfile() = default; - - static void setDefaultProfile(WebProfile *profile) - { - Q_CHECK_PTR(profile); - WebProfile::profile = profile; - } - static WebProfile *defaultProfile() - { - Q_CHECK_PTR(WebProfile::profile); - return WebProfile::profile; - } - - const QString name() const; - void setName(const QString &name); - - const QVector cookies() const - { - return qAsConst(m_cookies); - } - const QMap headers() const - { - return qAsConst(m_headers); - } - - // search url - QString search() const; - void setSearch(const QString &url); - - // homepage url - QUrl homepage() const; - void setHomepage(const QUrl &url); - - // new tab url - QUrl newtab() const; - void setNewtab(const QUrl &url); - - void setCachePath(const QString &path); - void setPersistentStoragePath(const QString &path); - void setPersistentCookiesPolicy(int policy); - - void setHttpAcceptLanguage(const QString &httpAcceptLanguage); - void setHttpCacheMaximumSize(int maxSize); - void setHttpCacheType(int type); - void setHttpUserAgent(const QString &userAgent); - void setHttpHeader(const QString &name, const QString &value); - void removeHttpHeader(const QString &name); - - void setSpellCheckEnabled(bool enable); - -private: - static WebProfile *profile; - - QString m_name; - QString m_search = QString("about:blank"); - QUrl m_homepage = QUrl("about:blank"); - QUrl m_newtab = QUrl("about:blank"); - - QVector m_cookies; - QMap m_headers; -}; - -#endif // SMOLBOTE_WEBENGINEPROFILE_H diff --git a/lib/webprofile/webprofilemanager.cpp b/lib/webprofile/webprofilemanager.cpp deleted file mode 100644 index 05808ab..0000000 --- a/lib/webprofile/webprofilemanager.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "webprofilemanager.h" -#include "webprofile.h" -#include -#include - -WebProfileManager::WebProfileManager(const ProfileDefault_t &profileSection, QObject *parent) - : QObject(parent) - , defaults(profileSection) -{ -} - -WebProfileManager::~WebProfileManager() -{ - for(Profile p : profiles) { - if(p.selfDestruct && p.settings != nullptr) { - if(!p.ptr->isOffTheRecord()) { - if(!p.ptr->persistentStoragePath().isEmpty()) - QDir(p.ptr->persistentStoragePath()).removeRecursively(); - if(!p.ptr->cachePath().isEmpty()) - QDir(p.ptr->cachePath()).removeRecursively(); - } - const QString filename = p.settings->fileName(); - delete p.settings; - QFile::remove(filename); - } else if(p.settings != nullptr) { - p.settings->sync(); - delete p.settings; - } - } -} - -WebProfile *WebProfileManager::profile(const QString &id, const QString &path, bool isOffTheRecord) -{ - // Check if profile exists - if(profiles.contains(id)) - return profiles.value(id).ptr; - - Profile profile; - - if(!path.isEmpty()) - profile.settings = new QSettings(path, QSettings::IniFormat); - - // QWebEngineCore cleans up profiles automatically, so no need to set parent - profile.ptr = [id, isOffTheRecord, profile]() { - if(profile.value("otr", isOffTheRecord).toBool()) - return new WebProfile(/* name */ profile.value("name", id).toString(), /* parent */ nullptr); - else - return new WebProfile(/* storageName */ id, /* name */ profile.value("name", id).toString(), /* parent */ nullptr); - }(); - if(profile.settings != nullptr) - profile.settings->setParent(profile.ptr); - - connect(profile.ptr, &WebProfile::nameChanged, profile.settings, [profile](const QString &name) { - profile.settings->setValue("name", name); - }); - - profile.ptr->setSearch(profile.value("search", defaults.search).toString()); - connect(profile.ptr, &WebProfile::searchChanged, profile.settings, [profile](const QString &url) { - profile.settings->setValue("search", url); - }); - - profile.ptr->setHomepage(profile.value("homepage", defaults.homepage).toUrl()); - connect(profile.ptr, &WebProfile::homepageChanged, profile.settings, [profile](const QUrl &url) { - profile.settings->setValue("homepage", url); - }); - - profile.ptr->setNewtab(profile.value("newtab", defaults.newtab).toUrl()); - connect(profile.ptr, &WebProfile::newtabChanged, profile.settings, [profile](const QUrl &url) { - profile.settings->setValue("newtab", url); - }); - - if(profile.settings != nullptr) { - profile.settings->beginGroup("properties"); - { - const auto keys = profile.settings->childKeys(); - for(const QString &key : keys) { - profile.ptr->setProperty(qUtf8Printable(key), profile.settings->value(key)); - } - } - profile.settings->endGroup(); // properties - connect(profile.ptr, &WebProfile::propertyChanged, [profile](const QString &property, const QVariant &value) { - profile.settings->setValue("properties/" + property, value); - }); - - profile.settings->beginGroup("attributes"); - { - const auto keys = profile.settings->childKeys(); - auto *settings = profile.ptr->settings(); - for(const QString &key : keys) { - auto attribute = static_cast(key.toInt()); - settings->setAttribute(attribute, profile.settings->value(key).toBool()); - } - } - profile.settings->endGroup(); - connect(profile.ptr, &WebProfile::attributeChanged, [profile](const QWebEngineSettings::WebAttribute attr, const bool value) { - profile.settings->setValue("attributes/" + QString::number(attr), value); - }); - - // headers - profile.settings->beginGroup("headers"); - for(const QString &key : profile.settings->childKeys()) { - profile.ptr->setHttpHeader(key.toLatin1(), profile.settings->value(key).toString().toLatin1()); - } - profile.settings->endGroup(); - connect(profile.ptr, &WebProfile::headerChanged, [profile](const QString &name, const QString &value) { - profile.settings->setValue("headers/" + name, value); - }); - connect(profile.ptr, &WebProfile::headerRemoved, [profile](const QString &name) { - profile.settings->remove("headers/" + name); - }); - - } // profile.settings != nullptr - - profiles[id] = profile; - return profile.ptr; -} - -void WebProfileManager::deleteProfile(const QString &id) -{ - if(profiles.contains(id)) { - profiles[id].selfDestruct = true; - } -} - -void WebProfileManager::profileMenu(QMenu *menu, const std::function &callback, WebProfile *current, bool checkable) const -{ - auto *group = new QActionGroup(menu); - connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater); - - for(const auto &profile : profiles) { - auto *action = menu->addAction(profile.ptr->name(), profile.ptr, [profile, callback]() { - callback(profile.ptr); - }); - action->setCheckable(checkable); - if(profile.ptr == current) - action->setChecked(true); - group->addAction(action); - } -} - diff --git a/lib/webprofile/webprofilemanager.h b/lib/webprofile/webprofilemanager.h deleted file mode 100644 index 3d86def..0000000 --- a/lib/webprofile/webprofilemanager.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_WEBPROFILEMANAGER_H -#define SMOLBOTE_WEBPROFILEMANAGER_H - -#include "webprofile.h" -#include -#include -#include -#include -#include -#include -#include - -struct ProfileDefault_t -{ - QString search; - QString homepage; - QString newtab; -}; - -class WebProfileManager : public QObject -{ - Q_OBJECT -public: - explicit WebProfileManager(const ProfileDefault_t &profileSection, QObject *parent); - ~WebProfileManager(); - - /** 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); - - /** Set a profile for deletion - * param id The profile ID - * return void - */ - void deleteProfile(const QString &id); - - void profileMenu(QMenu *menu, const std::function &callback, WebProfile *current = nullptr, bool checkable = false) const; - - const QStringList idList() const - { - return profiles.keys(); - } - QString id(WebProfile *profile) const - { - QMapIterator i(profiles); - while(i.hasNext()) { - i.next(); - if(i.value().ptr == profile) - return i.key(); - } - return QString(); - } - -private: - struct Profile { - WebProfile *ptr = nullptr; - QSettings *settings = nullptr; - bool selfDestruct = false; - - QVariant value(const QString &key, const QVariant &defaultValue) const - { - if(settings == nullptr) - return defaultValue; - else - return settings->value(key, defaultValue); - } - }; - - QMap profiles; - const ProfileDefault_t defaults; -}; - -#endif // SMOLBOTE_PROFILEMANAGER_H diff --git a/meson.build b/meson.build index 3894ac1..50e239d 100644 --- a/meson.build +++ b/meson.build @@ -87,7 +87,6 @@ subdir('lib/configuration') subdir('lib/downloads') subdir('lib/pluginloader') subdir('lib/urlfilter') -subdir('lib/webprofile') subdir('3rd-party/SingleApplication') subdir('3rd-party/args') diff --git a/src/browser.cpp b/src/browser.cpp index d677997..b3039a5 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -15,11 +15,11 @@ #include "configuration.h" #include "downloadswidget.h" #include "mainwindow/mainwindow.h" -#include "webprofilemanager.h" +#include "webengine/webprofilemanager.h" #include "subwindow/subwindow.h" #include "util.h" #include "webengine/urlinterceptor.h" -#include "webprofile.h" +#include "webengine/webprofile.h" #include #include #include @@ -203,11 +203,7 @@ void Browser::setup(QVector plugins) // cookie request filter // load profiles - ProfileDefault_t p; - p.search = conf.value("profile.search").value(); - p.homepage = conf.value("profile.homepage").value(); - p.newtab = conf.value("profile.newtab").value(); - m_profileManager = new WebProfileManager(p, this); + m_profileManager = new WebProfileManager(this); for(const QString &profilePath : Util::files(conf.value("profile.path").value(), { "*.profile" })) { this->loadProfile(profilePath); } diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index fa8b23a..292d922 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -12,12 +12,12 @@ #include "conf.hpp" #include "configuration.h" #include "menubar.h" -#include "webprofilemanager.h" +#include "webengine/webprofilemanager.h" #include "session/session.h" #include "session/sessiondialog.h" #include "subwindow/subwindow.h" #include "webengine/webview.h" -#include "webprofile.h" +#include "webengine/webprofile.h" #include "widgets/dockwidget.h" #include "widgets/navigationbar.h" #include "widgets/searchform.h" diff --git a/src/mainwindow/menubar.cpp b/src/mainwindow/menubar.cpp index 2ce87f7..54ba102 100644 --- a/src/mainwindow/menubar.cpp +++ b/src/mainwindow/menubar.cpp @@ -12,7 +12,7 @@ #include "configuration.h" #include "downloadswidget.h" #include "mainwindow.h" -#include "webprofilemanager.h" +#include "webengine/webprofilemanager.h" #include "session/savesessiondialog.h" #include "session/sessiondialog.h" #include "subwindow/subwindow.h" diff --git a/src/mainwindow/widgets/navigationbar.cpp b/src/mainwindow/widgets/navigationbar.cpp index f57d678..89e1e50 100644 --- a/src/mainwindow/widgets/navigationbar.cpp +++ b/src/mainwindow/widgets/navigationbar.cpp @@ -18,7 +18,7 @@ #include #include #include -#include "webprofile.h" +#include "webengine/webprofile.h" NavigationBar::NavigationBar(QWidget *parent) : QToolBar(parent) diff --git a/src/meson.build b/src/meson.build index b263b5f..3494112 100644 --- a/src/meson.build +++ b/src/meson.build @@ -4,7 +4,7 @@ poi_moc = mod_qt5.preprocess( 'mainwindow/mainwindow.h', 'mainwindow/addressbar.h', 'mainwindow/menubar.h', 'mainwindow/widgets/completer.h', 'mainwindow/widgets/urllineedit.h', 'mainwindow/widgets/dockwidget.h', 'mainwindow/widgets/menusearch.h', 'mainwindow/widgets/navigationbar.h', 'mainwindow/widgets/searchform.h', 'session/savesessiondialog.h', 'session/sessiondialog.h', 'session/sessionform.h', 'subwindow/subwindow.h', 'subwindow/tabwidget.h', - 'webengine/urlinterceptor.h', 'webengine/webpage.h', 'webengine/webview.h'], + 'webengine/urlinterceptor.h', 'webengine/webpage.h', 'webengine/webview.h', 'webengine/webprofilemanager.h', 'webengine/webprofile.h'], ui_files: ['mainwindow/addressbar.ui', 'mainwindow/widgets/searchform.ui', 'session/savesessiondialog.ui', 'session/sessiondialog.ui', 'session/sessionform.ui'], qresources: '../data/resources.qrc', rcc_extra_arguments: ['--format-version=1'], @@ -14,7 +14,7 @@ poi_moc = mod_qt5.preprocess( poi = executable(get_option('poiName'), install: true, cpp_args: ['-DQAPPLICATION_CLASS=QApplication'], dependencies: [dep_qt5, dep_spdlog, dep_SingleApplication, dep_args, optional_deps, - dep_about, dep_bookmarks, dep_configuration, dep_downloads, dep_pluginloader, dep_urlfilter, dep_webprofile], + dep_about, dep_bookmarks, dep_configuration, dep_downloads, dep_pluginloader, dep_urlfilter], include_directories: [include], sources: ['main.cpp', 'builtins.cpp', 'crashhandler.cpp', poi_moc, version_h, 'browser.cpp', @@ -41,6 +41,9 @@ poi = executable(get_option('poiName'), install: true, 'webengine/urlinterceptor.cpp', 'webengine/webpage.cpp', 'webengine/webview.cpp', + 'webengine/webprofile.cpp', + 'webengine/webprofilemanager.cpp', + interfaces_moc, 'wallet/wallet.cpp', 'wallet/wallet.h'] ) diff --git a/src/session/savesessiondialog.cpp b/src/session/savesessiondialog.cpp index 9291215..ffc53f7 100644 --- a/src/session/savesessiondialog.cpp +++ b/src/session/savesessiondialog.cpp @@ -9,7 +9,7 @@ #include "savesessiondialog.h" #include "browser.h" #include "mainwindow/mainwindow.h" -#include "webprofilemanager.h" +#include "webengine/webprofilemanager.h" #include "subwindow/subwindow.h" #include "ui_savesessiondialog.h" #include "webengine/webview.h" diff --git a/src/session/session.cpp b/src/session/session.cpp index 67cea70..9e6fa7d 100644 --- a/src/session/session.cpp +++ b/src/session/session.cpp @@ -10,7 +10,7 @@ #include "../webengine/webview.h" #include "browser.h" #include "mainwindow/mainwindow.h" -#include "webprofilemanager.h" +#include "webengine/webprofilemanager.h" #include "subwindow/subwindow.h" #include "webengine/webview.h" #include diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 869a453..7f2ae05 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -20,7 +20,7 @@ #include #include #include "configuration.h" -#include "webprofile.h" +#include "webengine/webprofile.h" SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp index ef15657..d09fffb 100644 --- a/src/subwindow/tabwidget.cpp +++ b/src/subwindow/tabwidget.cpp @@ -13,7 +13,7 @@ #include #include #include -#include "webprofile.h" +#include "webengine/webprofile.h" #include #include "subwindow.h" diff --git a/src/webengine/webprofile.cpp b/src/webengine/webprofile.cpp new file mode 100644 index 0000000..843b78e --- /dev/null +++ b/src/webengine/webprofile.cpp @@ -0,0 +1,147 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "webprofile.h" +#include +#include +#include +#include + +WebProfile *WebProfile::profile = nullptr; + +WebProfile::WebProfile(const QString &name, QObject *parent) + : Profile(parent) +{ + m_name = name; + + connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) { + m_cookies.append(cookie); + }); + connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) { + m_cookies.removeAll(cookie); + }); +} + +WebProfile::WebProfile(const QString &storageName, const QString &name, QObject *parent) + : Profile(storageName, parent) +{ + m_name = name; + + connect(this->cookieStore(), &QWebEngineCookieStore::cookieAdded, this, [this](const QNetworkCookie &cookie) { + m_cookies.append(cookie); + }); + connect(this->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this, [this](const QNetworkCookie &cookie) { + m_cookies.removeAll(cookie); + }); +} + +const QString WebProfile::name() const +{ + return m_name; +} + +void WebProfile::setName(const QString &name) +{ + m_name = name; + emit nameChanged(name); +} + +QString WebProfile::search() const +{ + return m_search; +} + +void WebProfile::setSearch(const QString &url) +{ + m_search = url; + emit searchChanged(m_search); +} + +QUrl WebProfile::homepage() const +{ + return m_homepage; +} + +void WebProfile::setHomepage(const QUrl &url) +{ + m_homepage = url; + emit homepageChanged(m_homepage); +} + +QUrl WebProfile::newtab() const +{ + return m_newtab; +} + +void WebProfile::setNewtab(const QUrl &url) +{ + m_newtab = url; + emit newtabChanged(m_newtab); +} + +void WebProfile::setCachePath(const QString &path) +{ + QWebEngineProfile::setCachePath(path); + emit propertyChanged("cachePath", path); +} + +void WebProfile::setPersistentStoragePath(const QString &path) +{ + QWebEngineProfile::setPersistentStoragePath(path); + emit propertyChanged("persistentStoragePath", path); +} + +void WebProfile::setPersistentCookiesPolicy(int policy) +{ + QWebEngineProfile::setPersistentCookiesPolicy(static_cast(policy)); + emit propertyChanged("persistentCookiesPolicy", policy); +} + +void WebProfile::setHttpAcceptLanguage(const QString &httpAcceptLanguage) +{ + QWebEngineProfile::setHttpAcceptLanguage(httpAcceptLanguage); + emit propertyChanged("httpAcceptLanguage", httpAcceptLanguage); +} + +void WebProfile::setHttpCacheMaximumSize(int maxSize) +{ + QWebEngineProfile::setHttpCacheMaximumSize(maxSize); + emit propertyChanged("httpCacheMaximumSize", maxSize); +} + +void WebProfile::setHttpCacheType(int type) +{ + QWebEngineProfile::setHttpCacheType(static_cast(type)); + emit propertyChanged("httpCacheType", type); +} + +void WebProfile::setHttpUserAgent(const QString &userAgent) +{ + QWebEngineProfile::setHttpUserAgent(userAgent); + emit propertyChanged("httpUserAgent", userAgent); +} + +void WebProfile::setHttpHeader(const QString &name, const QString &value) +{ + m_headers[name.toLatin1()] = value.toLatin1(); + emit headerChanged(name, value); +} + +void WebProfile::removeHttpHeader(const QString &name) +{ + if(m_headers.contains(name.toLatin1())) { + m_headers.remove(name.toLatin1()); + emit headerRemoved(name); + } +} + +void WebProfile::setSpellCheckEnabled(bool enable) +{ + QWebEngineProfile::setSpellCheckEnabled(enable); + emit propertyChanged("spellCheckEnabed", enable); +} diff --git a/src/webengine/webprofile.h b/src/webengine/webprofile.h new file mode 100644 index 0000000..37e3419 --- /dev/null +++ b/src/webengine/webprofile.h @@ -0,0 +1,94 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_WEBENGINEPROFILE_H +#define SMOLBOTE_WEBENGINEPROFILE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class WebProfile : public Profile +{ + Q_OBJECT + +public: + // off-the-record constructor + explicit WebProfile(const QString &name, QObject *parent = nullptr); + // default constructor + explicit WebProfile(const QString &storageName, const QString &name, QObject *parent = nullptr); + + ~WebProfile() = default; + + static void setDefaultProfile(WebProfile *profile) + { + Q_CHECK_PTR(profile); + WebProfile::profile = profile; + } + static WebProfile *defaultProfile() + { + Q_CHECK_PTR(WebProfile::profile); + return WebProfile::profile; + } + + const QString name() const; + void setName(const QString &name); + + const QVector cookies() const + { + return qAsConst(m_cookies); + } + const QMap headers() const + { + return qAsConst(m_headers); + } + + // search url + QString search() const; + void setSearch(const QString &url); + + // homepage url + QUrl homepage() const; + void setHomepage(const QUrl &url); + + // new tab url + QUrl newtab() const; + void setNewtab(const QUrl &url); + + void setCachePath(const QString &path); + void setPersistentStoragePath(const QString &path); + void setPersistentCookiesPolicy(int policy); + + void setHttpAcceptLanguage(const QString &httpAcceptLanguage); + void setHttpCacheMaximumSize(int maxSize); + void setHttpCacheType(int type); + void setHttpUserAgent(const QString &userAgent); + void setHttpHeader(const QString &name, const QString &value); + void removeHttpHeader(const QString &name); + + void setSpellCheckEnabled(bool enable); + +private: + static WebProfile *profile; + + QString m_name; + QString m_search = QString("about:blank"); + QUrl m_homepage = QUrl("about:blank"); + QUrl m_newtab = QUrl("about:blank"); + + QVector m_cookies; + QMap m_headers; +}; + +#endif // SMOLBOTE_WEBENGINEPROFILE_H diff --git a/src/webengine/webprofilemanager.cpp b/src/webengine/webprofilemanager.cpp new file mode 100644 index 0000000..3f3d5ba --- /dev/null +++ b/src/webengine/webprofilemanager.cpp @@ -0,0 +1,149 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "webprofilemanager.h" +#include "webprofile.h" +#include +#include +#include "configuration.h" + +WebProfileManager::WebProfileManager(QObject *parent) + : QObject(parent) +{ +} + +WebProfileManager::~WebProfileManager() +{ + for(Profile p : profiles) { + if(p.selfDestruct && p.settings != nullptr) { + if(!p.ptr->isOffTheRecord()) { + if(!p.ptr->persistentStoragePath().isEmpty()) + QDir(p.ptr->persistentStoragePath()).removeRecursively(); + if(!p.ptr->cachePath().isEmpty()) + QDir(p.ptr->cachePath()).removeRecursively(); + } + const QString filename = p.settings->fileName(); + delete p.settings; + QFile::remove(filename); + } else if(p.settings != nullptr) { + p.settings->sync(); + delete p.settings; + } + } +} + +WebProfile *WebProfileManager::profile(const QString &id, const QString &path, bool isOffTheRecord) +{ + // Check if profile exists + if(profiles.contains(id)) + return profiles.value(id).ptr; + + Configuration conf; + Profile profile; + + if(!path.isEmpty()) + profile.settings = new QSettings(path, QSettings::IniFormat); + + // QWebEngineCore cleans up profiles automatically, so no need to set parent + profile.ptr = [id, isOffTheRecord, profile]() { + if(profile.value("otr", isOffTheRecord).toBool()) + return new WebProfile(/* name */ profile.value("name", id).toString(), /* parent */ nullptr); + else + return new WebProfile(/* storageName */ id, /* name */ profile.value("name", id).toString(), /* parent */ nullptr); + }(); + if(profile.settings != nullptr) + profile.settings->setParent(profile.ptr); + + connect(profile.ptr, &WebProfile::nameChanged, profile.settings, [profile](const QString &name) { + profile.settings->setValue("name", name); + }); + + profile.ptr->setSearch(profile.value("search", conf.value("profile.search").value()).toString()); + connect(profile.ptr, &WebProfile::searchChanged, profile.settings, [profile](const QString &url) { + profile.settings->setValue("search", url); + }); + + profile.ptr->setHomepage(profile.value("homepage", conf.value("profile.homepage").value()).toUrl()); + connect(profile.ptr, &WebProfile::homepageChanged, profile.settings, [profile](const QUrl &url) { + profile.settings->setValue("homepage", url); + }); + + profile.ptr->setNewtab(profile.value("newtab", conf.value("profile.newtab").value()).toUrl()); + connect(profile.ptr, &WebProfile::newtabChanged, profile.settings, [profile](const QUrl &url) { + profile.settings->setValue("newtab", url); + }); + + if(profile.settings != nullptr) { + profile.settings->beginGroup("properties"); + { + const auto keys = profile.settings->childKeys(); + for(const QString &key : keys) { + profile.ptr->setProperty(qUtf8Printable(key), profile.settings->value(key)); + } + } + profile.settings->endGroup(); // properties + connect(profile.ptr, &WebProfile::propertyChanged, [profile](const QString &property, const QVariant &value) { + profile.settings->setValue("properties/" + property, value); + }); + + profile.settings->beginGroup("attributes"); + { + const auto keys = profile.settings->childKeys(); + auto *settings = profile.ptr->settings(); + for(const QString &key : keys) { + auto attribute = static_cast(key.toInt()); + settings->setAttribute(attribute, profile.settings->value(key).toBool()); + } + } + profile.settings->endGroup(); + connect(profile.ptr, &WebProfile::attributeChanged, [profile](const QWebEngineSettings::WebAttribute attr, const bool value) { + profile.settings->setValue("attributes/" + QString::number(attr), value); + }); + + // headers + profile.settings->beginGroup("headers"); + for(const QString &key : profile.settings->childKeys()) { + profile.ptr->setHttpHeader(key.toLatin1(), profile.settings->value(key).toString().toLatin1()); + } + profile.settings->endGroup(); + connect(profile.ptr, &WebProfile::headerChanged, [profile](const QString &name, const QString &value) { + profile.settings->setValue("headers/" + name, value); + }); + connect(profile.ptr, &WebProfile::headerRemoved, [profile](const QString &name) { + profile.settings->remove("headers/" + name); + }); + + } // profile.settings != nullptr + + profiles[id] = profile; + return profile.ptr; +} + +void WebProfileManager::deleteProfile(const QString &id) +{ + if(profiles.contains(id)) { + profiles[id].selfDestruct = true; + } +} + +void WebProfileManager::profileMenu(QMenu *menu, const std::function &callback, WebProfile *current, bool checkable) const +{ + auto *group = new QActionGroup(menu); + connect(menu, &QMenu::aboutToHide, group, &QActionGroup::deleteLater); + + for(const auto &profile : profiles) { + auto *action = menu->addAction(profile.ptr->name(), profile.ptr, [profile, callback]() { + callback(profile.ptr); + }); + action->setCheckable(checkable); + if(profile.ptr == current) + action->setChecked(true); + group->addAction(action); + } +} + diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h new file mode 100644 index 0000000..3e82936 --- /dev/null +++ b/src/webengine/webprofilemanager.h @@ -0,0 +1,84 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_WEBPROFILEMANAGER_H +#define SMOLBOTE_WEBPROFILEMANAGER_H + +#include "webprofile.h" +#include +#include +#include +#include +#include +#include +#include + +struct ProfileDefault_t +{ + QString search; + QString homepage; + QString newtab; +}; + +class WebProfileManager : public QObject +{ + Q_OBJECT +public: + explicit WebProfileManager(QObject *parent); + ~WebProfileManager(); + + /** 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); + + /** Set a profile for deletion + * param id The profile ID + * return void + */ + void deleteProfile(const QString &id); + + void profileMenu(QMenu *menu, const std::function &callback, WebProfile *current = nullptr, bool checkable = false) const; + + const QStringList idList() const + { + return profiles.keys(); + } + QString id(WebProfile *profile) const + { + QMapIterator i(profiles); + while(i.hasNext()) { + i.next(); + if(i.value().ptr == profile) + return i.key(); + } + return QString(); + } + +private: + struct Profile { + WebProfile *ptr = nullptr; + QSettings *settings = nullptr; + bool selfDestruct = false; + + QVariant value(const QString &key, const QVariant &defaultValue) const + { + if(settings == nullptr) + return defaultValue; + else + return settings->value(key, defaultValue); + } + }; + + QMap profiles; +}; + +#endif // SMOLBOTE_PROFILEMANAGER_H -- cgit v1.2.1