Update copyright headers
[qt:qt.git] / demos / browser / settings.cpp
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 demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "settings.h"
43
44 #include "browserapplication.h"
45 #include "browsermainwindow.h"
46 #include "cookiejar.h"
47 #include "history.h"
48 #include "networkaccessmanager.h"
49 #include "webview.h"
50
51 #include <QtCore/QSettings>
52 #include <QtGui/QtGui>
53 #include <QtWebKit/QtWebKit>
54
55 SettingsDialog::SettingsDialog(QWidget *parent)
56     : QDialog(parent)
57 {
58     setupUi(this);
59     connect(exceptionsButton, SIGNAL(clicked()), this, SLOT(showExceptions()));
60     connect(setHomeToCurrentPageButton, SIGNAL(clicked()), this, SLOT(setHomeToCurrentPage()));
61     connect(cookiesButton, SIGNAL(clicked()), this, SLOT(showCookies()));
62     connect(standardFontButton, SIGNAL(clicked()), this, SLOT(chooseFont()));
63     connect(fixedFontButton, SIGNAL(clicked()), this, SLOT(chooseFixedFont()));
64
65     loadDefaults();
66     loadFromSettings();
67 }
68
69 void SettingsDialog::loadDefaults()
70 {
71     QWebSettings *defaultSettings = QWebSettings::globalSettings();
72     QString standardFontFamily = defaultSettings->fontFamily(QWebSettings::StandardFont);
73     int standardFontSize = defaultSettings->fontSize(QWebSettings::DefaultFontSize);
74     standardFont = QFont(standardFontFamily, standardFontSize);
75     standardLabel->setText(QString(QLatin1String("%1 %2")).arg(standardFont.family()).arg(standardFont.pointSize()));
76
77     QString fixedFontFamily = defaultSettings->fontFamily(QWebSettings::FixedFont);
78     int fixedFontSize = defaultSettings->fontSize(QWebSettings::DefaultFixedFontSize);
79     fixedFont = QFont(fixedFontFamily, fixedFontSize);
80     fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(fixedFont.family()).arg(fixedFont.pointSize()));
81
82     downloadsLocation->setText(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
83
84     enableJavascript->setChecked(defaultSettings->testAttribute(QWebSettings::JavascriptEnabled));
85     enablePlugins->setChecked(defaultSettings->testAttribute(QWebSettings::PluginsEnabled));
86 }
87
88 void SettingsDialog::loadFromSettings()
89 {
90     QSettings settings;
91     settings.beginGroup(QLatin1String("MainWindow"));
92     QString defaultHome = QLatin1String("http://doc.qt.nokia.com");
93     homeLineEdit->setText(settings.value(QLatin1String("home"), defaultHome).toString());
94     settings.endGroup();
95
96     settings.beginGroup(QLatin1String("history"));
97     int historyExpire = settings.value(QLatin1String("historyExpire")).toInt();
98     int idx = 0;
99     switch (historyExpire) {
100     case 1: idx = 0; break;
101     case 7: idx = 1; break;
102     case 14: idx = 2; break;
103     case 30: idx = 3; break;
104     case 365: idx = 4; break;
105     case -1: idx = 5; break;
106     default:
107         idx = 5;
108     }
109     expireHistory->setCurrentIndex(idx);
110     settings.endGroup();
111
112     settings.beginGroup(QLatin1String("downloadmanager"));
113     QString downloadDirectory = settings.value(QLatin1String("downloadDirectory"), downloadsLocation->text()).toString();
114     downloadsLocation->setText(downloadDirectory);
115     settings.endGroup();
116
117     settings.beginGroup(QLatin1String("general"));
118     openLinksIn->setCurrentIndex(settings.value(QLatin1String("openLinksIn"), openLinksIn->currentIndex()).toInt());
119
120     settings.endGroup();
121
122     // Appearance
123     settings.beginGroup(QLatin1String("websettings"));
124     fixedFont = qvariant_cast<QFont>(settings.value(QLatin1String("fixedFont"), fixedFont));
125     standardFont = qvariant_cast<QFont>(settings.value(QLatin1String("standardFont"), standardFont));
126
127     standardLabel->setText(QString(QLatin1String("%1 %2")).arg(standardFont.family()).arg(standardFont.pointSize()));
128     fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(fixedFont.family()).arg(fixedFont.pointSize()));
129
130     enableJavascript->setChecked(settings.value(QLatin1String("enableJavascript"), enableJavascript->isChecked()).toBool());
131     enablePlugins->setChecked(settings.value(QLatin1String("enablePlugins"), enablePlugins->isChecked()).toBool());
132     userStyleSheet->setText(settings.value(QLatin1String("userStyleSheet")).toUrl().toString());
133     settings.endGroup();
134
135     // Privacy
136     settings.beginGroup(QLatin1String("cookies"));
137
138     CookieJar *jar = BrowserApplication::cookieJar();
139     QByteArray value = settings.value(QLatin1String("acceptCookies"), QLatin1String("AcceptOnlyFromSitesNavigatedTo")).toByteArray();
140     QMetaEnum acceptPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("AcceptPolicy"));
141     CookieJar::AcceptPolicy acceptCookies = acceptPolicyEnum.keyToValue(value) == -1 ?
142                         CookieJar::AcceptOnlyFromSitesNavigatedTo :
143                         static_cast<CookieJar::AcceptPolicy>(acceptPolicyEnum.keyToValue(value));
144     switch(acceptCookies) {
145     case CookieJar::AcceptAlways:
146         acceptCombo->setCurrentIndex(0);
147         break;
148     case CookieJar::AcceptNever:
149         acceptCombo->setCurrentIndex(1);
150         break;
151     case CookieJar::AcceptOnlyFromSitesNavigatedTo:
152         acceptCombo->setCurrentIndex(2);
153         break;
154     }
155
156     value = settings.value(QLatin1String("keepCookiesUntil"), QLatin1String("Expire")).toByteArray();
157     QMetaEnum keepPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("KeepPolicy"));
158     CookieJar::KeepPolicy keepCookies = keepPolicyEnum.keyToValue(value) == -1 ?
159                         CookieJar::KeepUntilExpire :
160                         static_cast<CookieJar::KeepPolicy>(keepPolicyEnum.keyToValue(value));
161     switch(keepCookies) {
162     case CookieJar::KeepUntilExpire:
163         keepUntilCombo->setCurrentIndex(0);
164         break;
165     case CookieJar::KeepUntilExit:
166         keepUntilCombo->setCurrentIndex(1);
167         break;
168     case CookieJar::KeepUntilTimeLimit:
169         keepUntilCombo->setCurrentIndex(2);
170         break;
171     }
172     settings.endGroup();
173
174
175     // Proxy
176     settings.beginGroup(QLatin1String("proxy"));
177     proxySupport->setChecked(settings.value(QLatin1String("enabled"), false).toBool());
178     proxyType->setCurrentIndex(settings.value(QLatin1String("type"), 0).toInt());
179     proxyHostName->setText(settings.value(QLatin1String("hostName")).toString());
180     proxyPort->setValue(settings.value(QLatin1String("port"), 1080).toInt());
181     proxyUserName->setText(settings.value(QLatin1String("userName")).toString());
182     proxyPassword->setText(settings.value(QLatin1String("password")).toString());
183     settings.endGroup();
184 }
185
186 void SettingsDialog::saveToSettings()
187 {
188     QSettings settings;
189     settings.beginGroup(QLatin1String("MainWindow"));
190     settings.setValue(QLatin1String("home"), homeLineEdit->text());
191     settings.endGroup();
192
193     settings.beginGroup(QLatin1String("general"));
194     settings.setValue(QLatin1String("openLinksIn"), openLinksIn->currentIndex());
195     settings.endGroup();
196
197     settings.beginGroup(QLatin1String("history"));
198     int historyExpire = expireHistory->currentIndex();
199     int idx = -1;
200     switch (historyExpire) {
201     case 0: idx = 1; break;
202     case 1: idx = 7; break;
203     case 2: idx = 14; break;
204     case 3: idx = 30; break;
205     case 4: idx = 365; break;
206     case 5: idx = -1; break;
207     }
208     settings.setValue(QLatin1String("historyExpire"), idx);
209     settings.endGroup();
210
211     // Appearance
212     settings.beginGroup(QLatin1String("websettings"));
213     settings.setValue(QLatin1String("fixedFont"), fixedFont);
214     settings.setValue(QLatin1String("standardFont"), standardFont);
215     settings.setValue(QLatin1String("enableJavascript"), enableJavascript->isChecked());
216     settings.setValue(QLatin1String("enablePlugins"), enablePlugins->isChecked());
217     QString userStyleSheetString = userStyleSheet->text();
218     if (QFile::exists(userStyleSheetString))
219         settings.setValue(QLatin1String("userStyleSheet"), QUrl::fromLocalFile(userStyleSheetString));
220     else
221         settings.setValue(QLatin1String("userStyleSheet"), QUrl(userStyleSheetString));
222     settings.endGroup();
223
224     //Privacy
225     settings.beginGroup(QLatin1String("cookies"));
226
227     CookieJar::KeepPolicy keepCookies;
228     switch(acceptCombo->currentIndex()) {
229     default:
230     case 0:
231         keepCookies = CookieJar::KeepUntilExpire;
232         break;
233     case 1:
234         keepCookies = CookieJar::KeepUntilExit;
235         break;
236     case 2:
237         keepCookies = CookieJar::KeepUntilTimeLimit;
238         break;
239     }
240     CookieJar *jar = BrowserApplication::cookieJar();
241     QMetaEnum acceptPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("AcceptPolicy"));
242     settings.setValue(QLatin1String("acceptCookies"), QLatin1String(acceptPolicyEnum.valueToKey(keepCookies)));
243
244     CookieJar::KeepPolicy keepPolicy;
245     switch(keepUntilCombo->currentIndex()) {
246         default:
247     case 0:
248         keepPolicy = CookieJar::KeepUntilExpire;
249         break;
250     case 1:
251         keepPolicy = CookieJar::KeepUntilExit;
252         break;
253     case 2:
254         keepPolicy = CookieJar::KeepUntilTimeLimit;
255         break;
256     }
257
258     QMetaEnum keepPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("KeepPolicy"));
259     settings.setValue(QLatin1String("keepCookiesUntil"), QLatin1String(keepPolicyEnum.valueToKey(keepPolicy)));
260
261     settings.endGroup();
262
263     // proxy
264     settings.beginGroup(QLatin1String("proxy"));
265     settings.setValue(QLatin1String("enabled"), proxySupport->isChecked());
266     settings.setValue(QLatin1String("type"), proxyType->currentIndex());
267     settings.setValue(QLatin1String("hostName"), proxyHostName->text());
268     settings.setValue(QLatin1String("port"), proxyPort->text());
269     settings.setValue(QLatin1String("userName"), proxyUserName->text());
270     settings.setValue(QLatin1String("password"), proxyPassword->text());
271     settings.endGroup();
272
273     BrowserApplication::instance()->loadSettings();
274     BrowserApplication::networkAccessManager()->loadSettings();
275     BrowserApplication::cookieJar()->loadSettings();
276     BrowserApplication::historyManager()->loadSettings();
277 }
278
279 void SettingsDialog::accept()
280 {
281     saveToSettings();
282     QDialog::accept();
283 }
284
285 void SettingsDialog::showCookies()
286 {
287     CookiesDialog *dialog = new CookiesDialog(BrowserApplication::cookieJar(), this);
288     dialog->exec();
289 }
290
291 void SettingsDialog::showExceptions()
292 {
293     CookiesExceptionsDialog *dialog = new CookiesExceptionsDialog(BrowserApplication::cookieJar(), this);
294     dialog->exec();
295 }
296
297 void SettingsDialog::chooseFont()
298 {
299     bool ok;
300     QFont font = QFontDialog::getFont(&ok, standardFont, this);
301     if ( ok ) {
302         standardFont = font;
303         standardLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
304     }
305 }
306
307 void SettingsDialog::chooseFixedFont()
308 {
309     bool ok;
310     QFont font = QFontDialog::getFont(&ok, fixedFont, this);
311     if ( ok ) {
312         fixedFont = font;
313         fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
314     }
315 }
316
317 void SettingsDialog::setHomeToCurrentPage()
318 {
319     BrowserMainWindow *mw = static_cast<BrowserMainWindow*>(parent());
320     WebView *webView = mw->currentTab();
321     if (webView)
322         homeLineEdit->setText(webView->url().toString());
323 }
324