From 2a696a57abafb25978aef1af5758fe75b706d1f0 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 16 Jan 2019 16:52:07 +0100 Subject: Rewrite lib/web to lib/webprofile - libweb was supposed to be a general QtWebEngine wrapper, but only turned out to do profiles and profile management. The new name should make this more obvious. - Renamed ProfileManager to WebProfileManager, and cut out duplicate code. - Temporary profiles: temporary profiles are not kept after closing the browser. --- lib/web/meson.build | 15 ---- lib/web/profilemanager.cpp | 171 --------------------------------------------- lib/web/profilemanager.h | 79 --------------------- lib/web/webprofile.cpp | 156 ----------------------------------------- lib/web/webprofile.h | 94 ------------------------- 5 files changed, 515 deletions(-) delete mode 100644 lib/web/meson.build delete mode 100644 lib/web/profilemanager.cpp delete mode 100644 lib/web/profilemanager.h delete mode 100644 lib/web/webprofile.cpp delete mode 100644 lib/web/webprofile.h (limited to 'lib/web') diff --git a/lib/web/meson.build b/lib/web/meson.build deleted file mode 100644 index 54d94df..0000000 --- a/lib/web/meson.build +++ /dev/null @@ -1,15 +0,0 @@ -web_inc = include_directories('.') -web_moc = qt5.preprocess( - moc_headers: ['profilemanager.h', 'webprofile.h'], - dependencies: dep_qt5 -) -web_lib = static_library('web', - ['profilemanager.cpp', 'webprofile.cpp', web_moc, interfaces_moc], - dependencies: dep_qt5, - include_directories: [include, web_inc] -) - -dep_web = declare_dependency( - include_directories: web_inc, - link_with: web_lib -) diff --git a/lib/web/profilemanager.cpp b/lib/web/profilemanager.cpp deleted file mode 100644 index 0bae251..0000000 --- a/lib/web/profilemanager.cpp +++ /dev/null @@ -1,171 +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 "profilemanager.h" -#include "webprofile.h" -#include -#include - -ProfileManager::ProfileManager(const QHash &profileSection, QObject *parent) - : QObject(parent) - , defaults(profileSection) -{ -} - -WebProfile *ProfileManager::createProfile(const QString& id, bool isOffTheRecord) -{ - QDir profileDir(defaults.value("profile.path")); - const QString path = profileDir.absoluteFilePath(id + ".profile"); - { - QSettings init(path, QSettings::IniFormat); - init.setValue("name", id); - init.setValue("otr", isOffTheRecord); - init.sync(); - } - return loadProfile(path); -} - -WebProfile *ProfileManager::loadProfile(const QString &path) -{ - std::unique_ptr ptr = std::make_unique(path); - const QString id = QFileInfo(path).baseName(); - - if(ptr->settings.value("otr", true).toBool()) { - ptr->profile = new WebProfile(ptr->settings.value("name", id).toString(), nullptr); - } else { - ptr->profile = new WebProfile(id, ptr->settings.value("name", id).toString(), nullptr); - } - Q_CHECK_PTR(ptr->profile); - connect(ptr->profile, &WebProfile::nameChanged, ptr->profile, [this, id](const QString &name) { - this->m_profiles.at(id)->settings.setValue("name", name); - }); - - ptr->profile->setSearch(ptr->settings.value("search", defaults.value("profile.search")).toString()); - connect(ptr->profile, &WebProfile::searchChanged, ptr->profile, [this, id](const QString &url) { - this->m_profiles.at(id)->settings.setValue("search", url); - }); - - ptr->profile->setHomepage(ptr->settings.value("homepage", defaults.value("profile.homepage")).toUrl()); - connect(ptr->profile, &WebProfile::homepageChanged, ptr->profile, [this, id](const QUrl &url) { - this->m_profiles.at(id)->settings.setValue("homepage", url); - }); - - ptr->profile->setNewtab(ptr->settings.value("newtab", defaults.value("profile.newtab")).toUrl()); - connect(ptr->profile, &WebProfile::newtabChanged, ptr->profile, [this, id](const QUrl &url) { - this->m_profiles.at(id)->settings.setValue("newtab", url); - }); - - ptr->settings.beginGroup("properties"); - { - const auto keys = ptr->settings.childKeys(); - for(const QString &key : keys) { - ptr->profile->setProperty(qUtf8Printable(key), ptr->settings.value(key)); - } - } - ptr->settings.endGroup(); // properties - connect(ptr->profile, &WebProfile::propertyChanged, ptr->profile, [this, id](const QString &property, const QVariant &value) { - this->m_profiles.at(id)->settings.setValue("properties/" + property, value); - }); - - ptr->settings.beginGroup("attributes"); - { - const auto keys = ptr->settings.childKeys(); - auto *settings = ptr->profile->settings(); - for(const QString &key : keys) { - auto attribute = static_cast(key.toInt()); - settings->setAttribute(attribute, ptr->settings.value(key).toBool()); - } - } - ptr->settings.endGroup(); - connect(ptr->profile, &WebProfile::attributeChanged, ptr->profile, [this, id](const QWebEngineSettings::WebAttribute attr, const bool value) { - this->m_profiles.at(id)->settings.setValue("attributes/" + QString::number(attr), value); - }); - - // headers - ptr->settings.beginGroup("headers"); - for(const QString &key : ptr->settings.childKeys()) { - ptr->profile->setHttpHeader(key.toLatin1(), ptr->settings.value(key).toString().toLatin1()); - } - ptr->settings.endGroup(); - connect(ptr->profile, &WebProfile::headerChanged, ptr->profile, [this, id](const QString &name, const QString &value) { - this->m_profiles.at(id)->settings.setValue("headers/" + name, value); - }); - connect(ptr->profile, &WebProfile::headerRemoved, ptr->profile, [this, id](const QString &name) { - this->m_profiles.at(id)->settings.remove("headers/" + name); - }); - - m_profiles[id] = std::move(ptr); - return m_profiles.at(id)->profile; -} - -void ProfileManager::deleteProfile(const QString &id) -{ - if(m_profiles.count(id) > 0) { - auto profileData = m_profiles.extract(id); - profileData.mapped()->deleteSelf = true; - } -} - -void ProfileManager::profilePickerMenu(QMenu *menu, WebProfile *current, std::function callback) const -{ - auto *profileGroup = new QActionGroup(menu); - - for(const auto &profileData : m_profiles) { - WebProfile *profile = profileData.second->profile; - - auto *action = menu->addAction(profile->name(), profile, [profile, callback]() { - callback(profile); - }); - action->setCheckable(true); - profileGroup->addAction(action); - - if(profile == current) - action->setChecked(true); - } - - connect(menu, &QMenu::aboutToHide, profileGroup, &QActionGroup::deleteLater); -} - -QMenu *ProfileManager::createProfileMenu(std::function callback, QWidget *parent) const -{ - 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, [profile, callback]() { - callback(profile); - }); - } - return menu; -} - -const QStringList ProfileManager::idList() const -{ - QStringList ids; - for(const auto &m_profile : m_profiles) { - ids.append(m_profile.first); - } - return ids; -} - -const QString ProfileManager::id(const WebProfile *profile) const -{ - for(const auto &m_profile : m_profiles) { - if(m_profile.second->profile == profile) - return m_profile.first; - } - return QString(); -} - -WebProfile *ProfileManager::profile(const QString &id) const -{ - if(m_profiles.count(id) > 0) { - return m_profiles.at(id)->profile; - } - return nullptr; -} diff --git a/lib/web/profilemanager.h b/lib/web/profilemanager.h deleted file mode 100644 index e0040a3..0000000 --- a/lib/web/profilemanager.h +++ /dev/null @@ -1,79 +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_PROFILEMANAGER_H -#define SMOLBOTE_PROFILEMANAGER_H - -#include "webprofile.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -class WebProfile; -class ProfileManager : public QObject -{ - Q_OBJECT -public: - explicit ProfileManager(const QHash &profileSection, QObject *parent); - - WebProfile *createProfile(const QString &id, bool isOffTheRecord); - WebProfile *loadProfile(const QString &path); - void deleteProfile(const QString &id); - - void profilePickerMenu(QMenu *menu, WebProfile *current, std::function callback) const; - QMenu *createProfileMenu(std::function callback, QWidget *parent = nullptr) const; - - const QStringList idList() const; - const QString id(const WebProfile *profile) const; - WebProfile *profile(const QString &id) const; - -private: - struct ProfileData { - explicit ProfileData(const QString &path = QString()) - : settings(path, QSettings::IniFormat) - { - this->path = path; - } - - ~ProfileData() - { - if(!deleteSelf) { -#ifdef QT_DEBUG - qDebug("sync %s", qUtf8Printable(settings.fileName())); -#endif - this->settings.sync(); - } else { - QFile::remove(path); - - if(!profile->isOffTheRecord()) { - if(!profile->persistentStoragePath().isEmpty()) - QDir(profile->persistentStoragePath()).removeRecursively(); - - if(!profile->cachePath().isEmpty()) - QDir(profile->cachePath()).removeRecursively(); - } - } - } - - WebProfile *profile = nullptr; - bool deleteSelf = false; - QSettings settings; - QString path; - }; - - std::map> m_profiles; - const QHash defaults; -}; - -#endif // SMOLBOTE_PROFILEMANAGER_H diff --git a/lib/web/webprofile.cpp b/lib/web/webprofile.cpp deleted file mode 100644 index 38aa7f0..0000000 --- a/lib/web/webprofile.cpp +++ /dev/null @@ -1,156 +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; - -#ifdef QT_DEBUG - qDebug("Creating otr profile %s", qUtf8Printable(m_name)); -#endif - - 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; - -#ifdef QT_DEBUG - qDebug("Creating profile %s", qUtf8Printable(m_name)); -#endif - - 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) -{ - qDebug("set httpCacheType to %i", 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/web/webprofile.h b/lib/web/webprofile.h deleted file mode 100644 index a3b5b12..0000000 --- a/lib/web/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 -- cgit v1.2.1