]> rtime.felk.cvut.cz Git - coffee/qtwebbrowser.git/blob - src/qml/SettingsView.qml
1eeb3a38ac923802e6a756696792825cac8f3dc5
[coffee/qtwebbrowser.git] / src / qml / SettingsView.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtBrowser project.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 2 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.GPLv2 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU General Public License version 2 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3.0 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 3.0 requirements will be
31 ** met: http://www.gnu.org/copyleft/gpl.html.
32 **
33 **
34 ** $QT_END_LICENSE$
35 **
36 ****************************************************************************/
37
38 import QtQuick 2.5
39 import QtQuick.Layouts 1.0
40 import QtQuick.Controls 1.4
41 import QtQuick.Controls.Styles 1.4
42 import Qt.labs.settings 1.0
43
44 Rectangle {
45     id: root
46
47     property bool privateBrowsingEnabled: appSettings[0].active
48     property bool httpDiskCacheEnabled: appSettings[1].active
49     property bool autoLoadImages: appSettings[2].active
50     property bool javaScriptDisabled: appSettings[3].active
51     property bool pluginsEnabled: appSettings[4].active
52
53     property var appSettings: [
54         { "name": "Private Browsing",       "active": false, "notify": function(v) { privateBrowsingEnabled = v; } },
55         { "name": "Enable HTTP Disk Cache", "active": true,  "notify": function(v) { httpDiskCacheEnabled = v; } },
56         { "name": "Auto Load Images",       "active": true,  "notify": function(v) { autoLoadImages = v; } },
57         { "name": "Disable JavaScript",     "active": false, "notify": function(v) { javaScriptDisabled = v; } },
58         { "name": "Enable Plugins",         "active": false, "notify": function(v) { pluginsEnabled = v; } }
59     ]
60
61     state: "disabled"
62
63     states: [
64         State {
65             name: "enabled"
66             AnchorChanges {
67                 target: root
68                 anchors.top: navigation.bottom
69             }
70             PropertyChanges {
71                 target: settingsToolBar
72                 opacity: 1.0
73             }
74         },
75         State {
76             name: "disabled"
77             AnchorChanges {
78                 target: root
79                 anchors.top: root.parent.bottom
80             }
81             PropertyChanges {
82                 target: settingsToolBar
83                 opacity: 0.0
84             }
85         }
86     ]
87
88     transitions: Transition {
89         AnchorAnimation { duration: animationDuration; easing.type : Easing.InSine }
90     }
91
92     ListModel {
93         id: listModel
94     }
95
96     ListView {
97         id: listView
98         leftMargin: 230
99         rightMargin: leftMargin
100         anchors.fill: parent
101         model: listModel
102         delegate: Rectangle {
103             height: 100
104             width: 560
105             Text {
106                 anchors.verticalCenter: parent.verticalCenter
107                 font.family: defaultFontFamily
108                 font.pixelSize: 28
109                 text: name
110                 color: sw.enabled ? "black" : "#929495"
111             }
112             Rectangle {
113                 anchors {
114                     right: parent.right
115                     verticalCenter: parent.verticalCenter
116                 }
117                 Switch {
118                     id: sw
119                     enabled: {
120                         var ok = appSettings[index].name.indexOf("Disk Cache") < 0
121                         return ok || !privateBrowsingEnabled
122                     }
123                     anchors.centerIn: parent
124                     checked: {
125                         if (enabled)
126                             return active
127                         return false
128                     }
129                     onClicked: {
130                         var setting = appSettings[index]
131                         setting.active = checked
132                         listModel.get(index).active = checked
133                         setting.notify(checked)
134                         listView.save()
135                     }
136                     style: SwitchStyle {
137                         handle: Rectangle {
138                             width: 42
139                             height: 42
140                             radius: height / 2
141                             color: "white"
142                             border.color: control.checked ? "#5caa14" : "#9b9b9b"
143                             border.width: 1
144                         }
145
146                         groove: Rectangle {
147                             implicitWidth: 72
148                             height: 42
149                             radius: height / 2
150                             border.color: control.checked ? "#5caa14" : "#9b9b9b"
151                             color: control.checked ? "#5cff14" : "white"
152                             border.width: 1
153                         }
154                     }
155                 }
156             }
157         }
158
159         function save() {
160             // Do not persist private browsing mode
161             appSettings[0].active = false
162             engine.saveSetting("settings", JSON.stringify(appSettings))
163         }
164
165         Component.onCompleted: {
166             var string = engine.restoreSetting("settings", JSON.stringify(appSettings))
167             var list = JSON.parse(string)
168             for (var i = 0; i < list.length; ++i) {
169                 var persistentSetting = list[i]
170                 var localSetting = appSettings[i]
171
172                 if (localSetting.name !== persistentSetting.name) {
173                     console.error("Conflicting configuration layout detected, using default setting!\nIf the problem persists please remove " + engine.settingsPath +" and restart the application.")
174                     listModel.append(localSetting)
175                     continue
176                 }
177
178                 listModel.append({ "name": persistentSetting.name, "active": persistentSetting.active })
179                 localSetting.active = persistentSetting.active
180                 localSetting.notify(persistentSetting.active)
181             }
182             listView.forceLayout()
183         }
184         Component.onDestruction: save()
185     }
186 }