From 307a43ada021b99f17c976354f255ff11389d641 Mon Sep 17 00:00:00 2001 From: Andras Becsi Date: Wed, 5 Aug 2015 18:06:17 +0200 Subject: Make settings propagate to webViews This also adds a check for consistency of the loaded settings' index and the default settings to prevent inconsistent states. --- src/engine.cpp | 5 ++++ src/engine.h | 2 ++ src/qml/PageView.qml | 2 +- src/qml/SettingsView.qml | 78 +++++++++++++++++++++++++++--------------------- 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index fae28f5..b6db85c 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -47,6 +47,11 @@ Engine::Engine(QObject *parent) { } +QString Engine::settingsPath() +{ + return m_settings.fileName(); +} + QUrl Engine::fromUserInput(const QString& userInput) { QFileInfo fileInfo(userInput); diff --git a/src/engine.h b/src/engine.h index e905d72..777dd74 100644 --- a/src/engine.h +++ b/src/engine.h @@ -77,6 +77,7 @@ class Engine : public QObject { Q_OBJECT Q_PROPERTY(QObject * rootWindow READ rootWindow FINAL CONSTANT) + Q_PROPERTY(QString settingsPath READ settingsPath FINAL CONSTANT) QSettings m_settings; @@ -86,6 +87,7 @@ public: { return parent(); } + QString settingsPath(); Q_INVOKABLE QUrl fromUserInput(const QString& userInput); Q_INVOKABLE QString domainFromString(const QString& urlString); diff --git a/src/qml/PageView.qml b/src/qml/PageView.qml index 58bdfb8..c4b2603 100644 --- a/src/qml/PageView.qml +++ b/src/qml/PageView.qml @@ -121,7 +121,7 @@ Rectangle { top: permBar.bottom } - profile: defaultProfile + profile: settingsView.privateBrowsingEnabled ? otrProfile : defaultProfile enabled: root.interactive function takeSnapshot() { diff --git a/src/qml/SettingsView.qml b/src/qml/SettingsView.qml index e373960..1eeb3a3 100644 --- a/src/qml/SettingsView.qml +++ b/src/qml/SettingsView.qml @@ -44,26 +44,21 @@ import Qt.labs.settings 1.0 Rectangle { id: root - property bool autoLoadImages: get(0) - property bool javaScriptDisabled: get(1) - property bool httpDiskCacheEnabled: get(2) - property bool pluginsEnabled: get(3) - property bool privateBrowsingEnabled: get(4) + property bool privateBrowsingEnabled: appSettings[0].active + property bool httpDiskCacheEnabled: appSettings[1].active + property bool autoLoadImages: appSettings[2].active + property bool javaScriptDisabled: appSettings[3].active + property bool pluginsEnabled: appSettings[4].active - property var defaultValues: [{ "name": "Auto Load Images", "active": true }, - { "name": "Disable JavaScript", "active": false }, - { "name": "Enable HTTP Disk Cache", "active": true }, - { "name": "Enable Plugins", "active": false }, - { "name": "Private Browsing", "active": false }] - - function get(index) { - var elem = listModel.get(index) - if (!elem) - return defaultValues[index].active - return elem.active - } + property var appSettings: [ + { "name": "Private Browsing", "active": false, "notify": function(v) { privateBrowsingEnabled = v; } }, + { "name": "Enable HTTP Disk Cache", "active": true, "notify": function(v) { httpDiskCacheEnabled = v; } }, + { "name": "Auto Load Images", "active": true, "notify": function(v) { autoLoadImages = v; } }, + { "name": "Disable JavaScript", "active": false, "notify": function(v) { javaScriptDisabled = v; } }, + { "name": "Enable Plugins", "active": false, "notify": function(v) { pluginsEnabled = v; } } + ] - state: "enabled" + state: "disabled" states: [ State { @@ -112,7 +107,7 @@ Rectangle { font.family: defaultFontFamily font.pixelSize: 28 text: name - color: tch.checked ? "black" : "#929495" + color: sw.enabled ? "black" : "#929495" } Rectangle { anchors { @@ -120,11 +115,22 @@ Rectangle { verticalCenter: parent.verticalCenter } Switch { - id: tch + id: sw + enabled: { + var ok = appSettings[index].name.indexOf("Disk Cache") < 0 + return ok || !privateBrowsingEnabled + } anchors.centerIn: parent - checked: active + checked: { + if (enabled) + return active + return false + } onClicked: { + var setting = appSettings[index] + setting.active = checked listModel.get(index).active = checked + setting.notify(checked) listView.save() } style: SwitchStyle { @@ -149,25 +155,29 @@ Rectangle { } } } - function save() { - var list = [] - for (var i = 0; i < listModel.count; ++i) { - var elem = listModel.get(i) - list[i] = { "name": elem.name, "active": elem.active } - } - - if (!list.length) - return - engine.saveSetting("settings", JSON.stringify(list)) + function save() { + // Do not persist private browsing mode + appSettings[0].active = false + engine.saveSetting("settings", JSON.stringify(appSettings)) } Component.onCompleted: { - var string = engine.restoreSetting("settings", JSON.stringify(defaultValues)) + var string = engine.restoreSetting("settings", JSON.stringify(appSettings)) var list = JSON.parse(string) for (var i = 0; i < list.length; ++i) { - var elem = list[i] - listModel.append({ "name": elem.name, "active": elem.active }) + var persistentSetting = list[i] + var localSetting = appSettings[i] + + if (localSetting.name !== persistentSetting.name) { + console.error("Conflicting configuration layout detected, using default setting!\nIf the problem persists please remove " + engine.settingsPath +" and restart the application.") + listModel.append(localSetting) + continue + } + + listModel.append({ "name": persistentSetting.name, "active": persistentSetting.active }) + localSetting.active = persistentSetting.active + localSetting.notify(persistentSetting.active) } listView.forceLayout() } -- cgit v1.2.3