1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
44 #include "browserapplication.h"
45 #include "browsermainwindow.h"
46 #include "cookiejar.h"
48 #include "networkaccessmanager.h"
51 #include <QtCore/QSettings>
52 #include <QtGui/QtGui>
53 #include <QtWebKit/QtWebKit>
55 SettingsDialog::SettingsDialog(QWidget *parent)
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()));
69 void SettingsDialog::loadDefaults()
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()));
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()));
82 downloadsLocation->setText(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
84 enableJavascript->setChecked(defaultSettings->testAttribute(QWebSettings::JavascriptEnabled));
85 enablePlugins->setChecked(defaultSettings->testAttribute(QWebSettings::PluginsEnabled));
88 void SettingsDialog::loadFromSettings()
91 settings.beginGroup(QLatin1String("MainWindow"));
92 QString defaultHome = QLatin1String("http://doc.qt.nokia.com");
93 homeLineEdit->setText(settings.value(QLatin1String("home"), defaultHome).toString());
96 settings.beginGroup(QLatin1String("history"));
97 int historyExpire = settings.value(QLatin1String("historyExpire")).toInt();
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;
109 expireHistory->setCurrentIndex(idx);
112 settings.beginGroup(QLatin1String("downloadmanager"));
113 QString downloadDirectory = settings.value(QLatin1String("downloadDirectory"), downloadsLocation->text()).toString();
114 downloadsLocation->setText(downloadDirectory);
117 settings.beginGroup(QLatin1String("general"));
118 openLinksIn->setCurrentIndex(settings.value(QLatin1String("openLinksIn"), openLinksIn->currentIndex()).toInt());
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));
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()));
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());
136 settings.beginGroup(QLatin1String("cookies"));
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);
148 case CookieJar::AcceptNever:
149 acceptCombo->setCurrentIndex(1);
151 case CookieJar::AcceptOnlyFromSitesNavigatedTo:
152 acceptCombo->setCurrentIndex(2);
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);
165 case CookieJar::KeepUntilExit:
166 keepUntilCombo->setCurrentIndex(1);
168 case CookieJar::KeepUntilTimeLimit:
169 keepUntilCombo->setCurrentIndex(2);
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());
186 void SettingsDialog::saveToSettings()
189 settings.beginGroup(QLatin1String("MainWindow"));
190 settings.setValue(QLatin1String("home"), homeLineEdit->text());
193 settings.beginGroup(QLatin1String("general"));
194 settings.setValue(QLatin1String("openLinksIn"), openLinksIn->currentIndex());
197 settings.beginGroup(QLatin1String("history"));
198 int historyExpire = expireHistory->currentIndex();
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;
208 settings.setValue(QLatin1String("historyExpire"), idx);
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));
221 settings.setValue(QLatin1String("userStyleSheet"), QUrl(userStyleSheetString));
225 settings.beginGroup(QLatin1String("cookies"));
227 CookieJar::KeepPolicy keepCookies;
228 switch(acceptCombo->currentIndex()) {
231 keepCookies = CookieJar::KeepUntilExpire;
234 keepCookies = CookieJar::KeepUntilExit;
237 keepCookies = CookieJar::KeepUntilTimeLimit;
240 CookieJar *jar = BrowserApplication::cookieJar();
241 QMetaEnum acceptPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("AcceptPolicy"));
242 settings.setValue(QLatin1String("acceptCookies"), QLatin1String(acceptPolicyEnum.valueToKey(keepCookies)));
244 CookieJar::KeepPolicy keepPolicy;
245 switch(keepUntilCombo->currentIndex()) {
248 keepPolicy = CookieJar::KeepUntilExpire;
251 keepPolicy = CookieJar::KeepUntilExit;
254 keepPolicy = CookieJar::KeepUntilTimeLimit;
258 QMetaEnum keepPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("KeepPolicy"));
259 settings.setValue(QLatin1String("keepCookiesUntil"), QLatin1String(keepPolicyEnum.valueToKey(keepPolicy)));
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());
273 BrowserApplication::instance()->loadSettings();
274 BrowserApplication::networkAccessManager()->loadSettings();
275 BrowserApplication::cookieJar()->loadSettings();
276 BrowserApplication::historyManager()->loadSettings();
279 void SettingsDialog::accept()
285 void SettingsDialog::showCookies()
287 CookiesDialog *dialog = new CookiesDialog(BrowserApplication::cookieJar(), this);
291 void SettingsDialog::showExceptions()
293 CookiesExceptionsDialog *dialog = new CookiesExceptionsDialog(BrowserApplication::cookieJar(), this);
297 void SettingsDialog::chooseFont()
300 QFont font = QFontDialog::getFont(&ok, standardFont, this);
303 standardLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
307 void SettingsDialog::chooseFixedFont()
310 QFont font = QFontDialog::getFont(&ok, fixedFont, this);
313 fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
317 void SettingsDialog::setHomeToCurrentPage()
319 BrowserMainWindow *mw = static_cast<BrowserMainWindow*>(parent());
320 WebView *webView = mw->currentTab();
322 homeLineEdit->setText(webView->url().toString());