From 6c3a4e1bdbedc0d96c3693c9deddc29db3c208b2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 21 Mar 2017 14:17:18 +0100 Subject: Profile saving and loading --- data/poi.conf | 28 ++++---- src/mainwindow.cpp | 4 +- src/settings.cpp | 13 ++-- src/settings.h | 1 + src/webengine/webengineprofile.cpp | 128 ++++++++++++++++++++++++++++++++++++- 5 files changed, 154 insertions(+), 20 deletions(-) diff --git a/data/poi.conf b/data/poi.conf index 1e4965f..c9461f5 100644 --- a/data/poi.conf +++ b/data/poi.conf @@ -4,18 +4,29 @@ # See https://gitlab.com/xiannox/smolbote/wikis/settings for options # +# General +[general] +homepage="https://duckduckgo.com" +newtab="about:blank" +#search="https://duckduckgo.com/?q=$term&kp=-1" + # Browser: application-wide settings [browser] singleInstance=true localSocket="smolbote-singlelock" -#sessionPath="~settings/session.ini" +#sessionPath="$cache/session.json" + +# Profile +[browser.profile] +default="Default" +path="$cache/Profiles/" # Main window settings [window] height=720 width=1280 maximized=true -title="$title — smolbote [$profile]" +title="title — smolbote [profile]" # Main window shortcuts [window.shortcuts] @@ -32,17 +43,10 @@ windowClose="Ctrl+Q" navtoolbarMovable=false tabtoolbarMovable=false -# General -[general] -homepage="https://duckduckgo.com" -newtab="about:blank" -profile="Default" -#search="https://duckduckgo.com/?q=$term&kp=-1" - # URL blocker [blocker] shortcut="Ctrl+Shift+A" -path="~settings/" +path="$settings/" subscriptions=[ # "https://easylist.to/easylist/easylist.txt", "https://easylist-downloads.adblockplus.org/easylist_noelemhide.txt", @@ -52,10 +56,10 @@ subscriptions=[ # Bookmark manager [bookmarks] dialogShortcut="Ctrl+Shift+B" -path="~settings/bookmarks.xbel" +path="$settings/bookmarks.xbel" # Download manager [downloads] dialogShortcut="Ctrl+Shift+D" -path="~/Downloads" +path="$home/Downloads" auto=false diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eaf061e..5620bb3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -96,7 +96,7 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) : connect(tabBar, SIGNAL(currentTabChanged(WebView*)), this, SLOT(handleTabChanged(WebView*))); // Load profile - loadProfile(sSettings->value("general.profile").toString()); + loadProfile(sSettings->value("browser.profile.default").toString()); // loading bar ui->statusBar->addPermanentWidget(progressBar); @@ -253,7 +253,7 @@ void MainWindow::handleUrlChanged() void MainWindow::handleTitleUpdated(const QString &title) { - setWindowTitle(sSettings->value("window.title").toString().replace("$title", title).replace("$profile", m_profile->storageName())); + setWindowTitle(sSettings->value("window.title").toString().replace("title", title).replace("profile", m_profile->storageName())); } void MainWindow::execProfileEditor() diff --git a/src/settings.cpp b/src/settings.cpp index aee0eb5..5d78cb7 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -46,10 +46,12 @@ Settings::Settings(const QString &configFile) path = configFile; homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); settingsLocation = QFileInfo(configFile).dir().absolutePath(); + cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); qDebug("Read configuration: [%s]", qUtf8Printable(configFile)); - qDebug("~ => '%s'", qUtf8Printable(homeLocation)); - qDebug("~settings => '%s'", qUtf8Printable(settingsLocation)); + qDebug("$home | '%s'", qUtf8Printable(homeLocation)); + qDebug("$settings | '%s'", qUtf8Printable(settingsLocation)); + qDebug("$cache | '%s'", qUtf8Printable(cacheLocation)); } Settings::~Settings() @@ -120,8 +122,11 @@ QVariant Settings::value(const QString &key, const QVariant &defaultValue) const // check if key is a path, in which case replace '~' with the home location if(key.endsWith(QLatin1String("path"), Qt::CaseInsensitive)) { QString value = r.toString(); - value.replace("~settings", settingsLocation); - value.replace('~', homeLocation); + while(value.contains('$')) { + value.replace("$settings", settingsLocation); + value.replace("$cache", cacheLocation); + value.replace("$home", homeLocation); + } r = QVariant(value); } diff --git a/src/settings.h b/src/settings.h index 54a3917..32eb4b4 100644 --- a/src/settings.h +++ b/src/settings.h @@ -42,6 +42,7 @@ private: QString path; QString homeLocation; QString settingsLocation; + QString cacheLocation; }; #endif // SETTINGS_H diff --git a/src/webengine/webengineprofile.cpp b/src/webengine/webengineprofile.cpp index de806df..cd6291f 100644 --- a/src/webengine/webengineprofile.cpp +++ b/src/webengine/webengineprofile.cpp @@ -19,7 +19,9 @@ ******************************************************************************/ #include "webengineprofile.h" +#include "browser.h" #include +#include WebEngineProfile::WebEngineProfile(QObject *parent) : QWebEngineProfile(parent) @@ -30,13 +32,73 @@ WebEngineProfile::WebEngineProfile(QObject *parent) : WebEngineProfile::WebEngineProfile(const QString &storageName, QObject *parent) : QWebEngineProfile(storageName, parent) { + setPersistentStoragePath(sSettings->value("browser.profile.path").toString() + storageName); + setCachePath(sSettings->value("browser.profile.path").toString() + storageName); + QString profilePath = persistentStoragePath() + "/profile.ini"; qDebug("Reading profile from [%s]", qUtf8Printable(profilePath)); QSettings config(profilePath, QSettings::IniFormat); config.beginGroup("http"); setHttpUserAgent(config.value("userAgent").toString()); - config.endGroup(); + setHttpAcceptLanguage(config.value("accept-lang").toString()); + { + QString cacheType = config.value("cacheType").toString(); + if(cacheType == "memory") { + setHttpCacheType(MemoryHttpCache); + } else if(cacheType == "disk") { + setHttpCacheType(DiskHttpCache); + } else if(cacheType == "disabled") { + setHttpCacheType(NoCache); + } + } + setHttpCacheMaximumSize(config.value("cacheSize").toInt()); + config.endGroup(); // http + + config.beginGroup("policy"); + { + QString cookies = config.value("cookies").toString(); + if(cookies == "disabled") { + setPersistentCookiesPolicy(NoPersistentCookies); + } else if(cookies == "allow") { + setPersistentCookiesPolicy(AllowPersistentCookies); + } else if(cookies == "force") { + setPersistentCookiesPolicy(ForcePersistentCookies); + } + } + config.endGroup(); // policy + + config.beginGroup("attributes"); + settings()->setAttribute(QWebEngineSettings::AutoLoadImages, config.value("autoLoadImages", true).toBool()); + settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, config.value("javascriptEnabled", true).toBool()); + settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, config.value("javascriptCanOpenWindows", true).toBool()); + settings()->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, config.value("javascriptCanAccessClipboard", false).toBool()); + settings()->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain, config.value("linksIncludedInFocusChain", true).toBool()); + settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, config.value("localStorageEnabled", true).toBool()); + settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, config.value("localContentCanAccessRemoteUrls", false).toBool()); + settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, config.value("xssAuditingEnabled", false).toBool()); + settings()->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, config.value("spatialNavigationEnabled", false).toBool()); + settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, config.value("localContentCanAccessFileUrls", true).toBool()); + settings()->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled, config.value("hyperlinkAuditingEnabled", false).toBool()); + settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled, config.value("scrollAnimatorEnabled", false).toBool()); + settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, config.value("errorPageEnabled", true).toBool()); +#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) + settings()->setAttribute(QWebEngineSettings::PluginsEnabled, config.value("pluginsEnabled", false).toBool()); + settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, config.value("fullscreenSupportEnabled", false).toBool()); +#endif +#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0) + settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, config.value("screenCaptureEnabled", false).toBool()); + settings()->setAttribute(QWebEngineSettings::WebGLEnabled, config.value("webglEnabled", true).toBool()); + settings()->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled, config.value("accelerated2dCanvasEnabled", true).toBool()); + settings()->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, config.value("autoLoadIconsForPage", true).toBool()); + settings()->setAttribute(QWebEngineSettings::TouchIconsEnabled, config.value("touchIconsEnabled", false).toBool()); +#endif +#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) + settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, config.value("focusOnNavigationEnabled", true).toBool()); + settings()->setAttribute(QWebEngineSettings::PrintElementBackgrounds, config.value("printElementBackgrounds", true).toBool()); + settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, config.value("allowRunningInsecureContent", false).toBool()); +#endif + config.endGroup(); // attributes } WebEngineProfile::~WebEngineProfile() @@ -49,8 +111,70 @@ WebEngineProfile::~WebEngineProfile() void WebEngineProfile::saveProfile() { QSettings config(persistentStoragePath() + "/profile.ini", QSettings::IniFormat); + config.beginGroup("http"); config.setValue("userAgent", httpUserAgent()); - config.endGroup(); + config.setValue("accept-lang", httpAcceptLanguage()); + switch (httpCacheType()) { + case MemoryHttpCache: + config.setValue("cacheType", "memory"); + break; + case DiskHttpCache: + config.setValue("cacheType", "disk"); + break; + case NoCache: + config.setValue("cacheType", "disabled"); + break; + } + config.setValue("cacheSize", httpCacheMaximumSize()); + config.endGroup(); // http + + config.beginGroup("policy"); + switch (persistentCookiesPolicy()) { + case NoPersistentCookies: + config.setValue("cookies", "disabled"); + break; + case AllowPersistentCookies: + config.setValue("cookies", "allow"); + break; + case ForcePersistentCookies: + config.setValue("cookies", "force"); + break; + } + config.endGroup(); // policy + + QWebEngineSettings *s = settings(); + config.beginGroup("attributes"); + config.setValue("autoLoadImages", s->testAttribute(QWebEngineSettings::AutoLoadImages)); + config.setValue("javascriptEnabled", s->testAttribute(QWebEngineSettings::JavascriptEnabled)); + config.setValue("javascriptCanOpenWindows", s->testAttribute(QWebEngineSettings::JavascriptCanOpenWindows)); + config.setValue("javascriptCanAccessClipboard", s->testAttribute(QWebEngineSettings::JavascriptCanAccessClipboard)); + config.setValue("linksIncludedInFocusChain", s->testAttribute(QWebEngineSettings::LinksIncludedInFocusChain)); + config.setValue("localStorageEnabled", s->testAttribute(QWebEngineSettings::LocalStorageEnabled)); + config.setValue("localContentCanAccessRemoteUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls)); + config.setValue("xssAuditingEnabled", s->testAttribute(QWebEngineSettings::XSSAuditingEnabled)); + config.setValue("spatialNavigationEnabled", s->testAttribute(QWebEngineSettings::SpatialNavigationEnabled)); + config.setValue("localContentCanAccessFileUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls)); + config.setValue("hyperlinkAuditingEnabled", s->testAttribute(QWebEngineSettings::HyperlinkAuditingEnabled)); + config.setValue("scrollAnimatorEnabled", s->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled)); + config.setValue("errorPageEnabled", s->testAttribute(QWebEngineSettings::ErrorPageEnabled)); +#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) + config.setValue("pluginsEnabled", s->testAttribute(QWebEngineSettings::PluginsEnabled)); + config.setValue("fullscreenSupportEnabled", s->testAttribute(QWebEngineSettings::FullScreenSupportEnabled)); +#endif +#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0) + config.setValue("screenCaptureEnabled", s->testAttribute(QWebEngineSettings::ScreenCaptureEnabled)); + config.setValue("webglEnabled", s->testAttribute(QWebEngineSettings::WebGLEnabled)); + config.setValue("accelerated2dCanvasEnabled", s->testAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled)); + config.setValue("autoLoadIconsForPage", s->testAttribute(QWebEngineSettings::AutoLoadIconsForPage)); + config.setValue("touchIconsEnabled", s->testAttribute(QWebEngineSettings::TouchIconsEnabled)); +#endif +#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) + config.setValue("focusOnNavigationEnabled", s->testAttribute(QWebEngineSettings::FocusOnNavigationEnabled)); + config.setValue("printElementBackgrounds", s->testAttribute(QWebEngineSettings::PrintElementBackgrounds)); + config.setValue("allowRunningInsecureContent", s->testAttribute(QWebEngineSettings::AllowRunningInsecureContent)); +#endif + config.endGroup(); // attributes + config.sync(); } -- cgit v1.2.1