xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/allwinner/browser/settings.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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:LGPL21$
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 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33 
34 #include "settings.h"
35 
36 #include "browserapplication.h"
37 #include "browsermainwindow.h"
38 #include "cookiejar.h"
39 #include "history.h"
40 #include "networkaccessmanager.h"
41 #include "webview.h"
42 
43 #include <QtCore/QSettings>
44 #include <QtWidgets/QtWidgets>
45 #include <QtWebKitWidgets>
46 
SettingsDialog(QWidget * parent)47 SettingsDialog::SettingsDialog(QWidget *parent)
48     : QDialog(parent)
49 {
50     setupUi(this);
51     connect(exceptionsButton, SIGNAL(clicked()), this, SLOT(showExceptions()));
52     connect(setHomeToCurrentPageButton, SIGNAL(clicked()), this, SLOT(setHomeToCurrentPage()));
53     connect(cookiesButton, SIGNAL(clicked()), this, SLOT(showCookies()));
54     connect(standardFontButton, SIGNAL(clicked()), this, SLOT(chooseFont()));
55     connect(fixedFontButton, SIGNAL(clicked()), this, SLOT(chooseFixedFont()));
56 
57     loadDefaults();
58     loadFromSettings();
59 }
60 
loadDefaults()61 void SettingsDialog::loadDefaults()
62 {
63     QWebSettings *defaultSettings = QWebSettings::globalSettings();
64     QString standardFontFamily = defaultSettings->fontFamily(QWebSettings::StandardFont);
65     int standardFontSize = defaultSettings->fontSize(QWebSettings::DefaultFontSize);
66     standardFont = QFont(standardFontFamily, standardFontSize);
67     standardLabel->setText(QString(QLatin1String("%1 %2")).arg(standardFont.family()).arg(standardFont.pointSize()));
68 
69     QString fixedFontFamily = defaultSettings->fontFamily(QWebSettings::FixedFont);
70     int fixedFontSize = defaultSettings->fontSize(QWebSettings::DefaultFixedFontSize);
71     fixedFont = QFont(fixedFontFamily, fixedFontSize);
72     fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(fixedFont.family()).arg(fixedFont.pointSize()));
73 
74     downloadsLocation->setText(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
75 
76     enableJavascript->setChecked(defaultSettings->testAttribute(QWebSettings::JavascriptEnabled));
77     enablePlugins->setChecked(defaultSettings->testAttribute(QWebSettings::PluginsEnabled));
78 }
79 
loadFromSettings()80 void SettingsDialog::loadFromSettings()
81 {
82     QSettings settings;
83     settings.beginGroup(QLatin1String("MainWindow"));
84     QString defaultHome = QLatin1String("http://qt-project.org/");
85     homeLineEdit->setText(settings.value(QLatin1String("home"), defaultHome).toString());
86     settings.endGroup();
87 
88     settings.beginGroup(QLatin1String("history"));
89     int historyExpire = settings.value(QLatin1String("historyExpire")).toInt();
90     int idx = 0;
91     switch (historyExpire) {
92     case 1: idx = 0; break;
93     case 7: idx = 1; break;
94     case 14: idx = 2; break;
95     case 30: idx = 3; break;
96     case 365: idx = 4; break;
97     case -1: idx = 5; break;
98     default:
99         idx = 5;
100     }
101     expireHistory->setCurrentIndex(idx);
102     settings.endGroup();
103 
104     settings.beginGroup(QLatin1String("downloadmanager"));
105     QString downloadDirectory = settings.value(QLatin1String("downloadDirectory"), downloadsLocation->text()).toString();
106     downloadsLocation->setText(downloadDirectory);
107     settings.endGroup();
108 
109     settings.beginGroup(QLatin1String("general"));
110     openLinksIn->setCurrentIndex(settings.value(QLatin1String("openLinksIn"), openLinksIn->currentIndex()).toInt());
111 
112     settings.endGroup();
113 
114     // Appearance
115     settings.beginGroup(QLatin1String("websettings"));
116     fixedFont = qvariant_cast<QFont>(settings.value(QLatin1String("fixedFont"), fixedFont));
117     standardFont = qvariant_cast<QFont>(settings.value(QLatin1String("standardFont"), standardFont));
118 
119     standardLabel->setText(QString(QLatin1String("%1 %2")).arg(standardFont.family()).arg(standardFont.pointSize()));
120     fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(fixedFont.family()).arg(fixedFont.pointSize()));
121 
122     enableJavascript->setChecked(settings.value(QLatin1String("enableJavascript"), enableJavascript->isChecked()).toBool());
123     enablePlugins->setChecked(settings.value(QLatin1String("enablePlugins"), enablePlugins->isChecked()).toBool());
124     userStyleSheet->setText(settings.value(QLatin1String("userStyleSheet")).toUrl().toString());
125     settings.endGroup();
126 
127     // Privacy
128     settings.beginGroup(QLatin1String("cookies"));
129 
130     QByteArray value = settings.value(QLatin1String("acceptCookies"), QLatin1String("AcceptOnlyFromSitesNavigatedTo")).toByteArray();
131     QMetaEnum acceptPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("AcceptPolicy"));
132     CookieJar::AcceptPolicy acceptCookies = acceptPolicyEnum.keyToValue(value) == -1 ?
133                         CookieJar::AcceptOnlyFromSitesNavigatedTo :
134                         static_cast<CookieJar::AcceptPolicy>(acceptPolicyEnum.keyToValue(value));
135     switch(acceptCookies) {
136     case CookieJar::AcceptAlways:
137         acceptCombo->setCurrentIndex(0);
138         break;
139     case CookieJar::AcceptNever:
140         acceptCombo->setCurrentIndex(1);
141         break;
142     case CookieJar::AcceptOnlyFromSitesNavigatedTo:
143         acceptCombo->setCurrentIndex(2);
144         break;
145     }
146 
147     value = settings.value(QLatin1String("keepCookiesUntil"), QLatin1String("Expire")).toByteArray();
148     QMetaEnum keepPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("KeepPolicy"));
149     CookieJar::KeepPolicy keepCookies = keepPolicyEnum.keyToValue(value) == -1 ?
150                         CookieJar::KeepUntilExpire :
151                         static_cast<CookieJar::KeepPolicy>(keepPolicyEnum.keyToValue(value));
152     switch(keepCookies) {
153     case CookieJar::KeepUntilExpire:
154         keepUntilCombo->setCurrentIndex(0);
155         break;
156     case CookieJar::KeepUntilExit:
157         keepUntilCombo->setCurrentIndex(1);
158         break;
159     case CookieJar::KeepUntilTimeLimit:
160         keepUntilCombo->setCurrentIndex(2);
161         break;
162     }
163     settings.endGroup();
164 
165 
166     // Proxy
167     settings.beginGroup(QLatin1String("proxy"));
168     proxySupport->setChecked(settings.value(QLatin1String("enabled"), false).toBool());
169     proxyType->setCurrentIndex(settings.value(QLatin1String("type"), 0).toInt());
170     proxyHostName->setText(settings.value(QLatin1String("hostName")).toString());
171     proxyPort->setValue(settings.value(QLatin1String("port"), 1080).toInt());
172     proxyUserName->setText(settings.value(QLatin1String("userName")).toString());
173     proxyPassword->setText(settings.value(QLatin1String("password")).toString());
174     settings.endGroup();
175 }
176 
saveToSettings()177 void SettingsDialog::saveToSettings()
178 {
179     QSettings settings;
180     settings.beginGroup(QLatin1String("MainWindow"));
181     settings.setValue(QLatin1String("home"), homeLineEdit->text());
182     settings.endGroup();
183 
184     settings.beginGroup(QLatin1String("general"));
185     settings.setValue(QLatin1String("openLinksIn"), openLinksIn->currentIndex());
186     settings.endGroup();
187 
188     settings.beginGroup(QLatin1String("history"));
189     int historyExpire = expireHistory->currentIndex();
190     int idx = -1;
191     switch (historyExpire) {
192     case 0: idx = 1; break;
193     case 1: idx = 7; break;
194     case 2: idx = 14; break;
195     case 3: idx = 30; break;
196     case 4: idx = 365; break;
197     case 5: idx = -1; break;
198     }
199     settings.setValue(QLatin1String("historyExpire"), idx);
200     settings.endGroup();
201 
202     // Appearance
203     settings.beginGroup(QLatin1String("websettings"));
204     settings.setValue(QLatin1String("fixedFont"), fixedFont);
205     settings.setValue(QLatin1String("standardFont"), standardFont);
206     settings.setValue(QLatin1String("enableJavascript"), enableJavascript->isChecked());
207     settings.setValue(QLatin1String("enablePlugins"), enablePlugins->isChecked());
208     QString userStyleSheetString = userStyleSheet->text();
209     if (QFile::exists(userStyleSheetString))
210         settings.setValue(QLatin1String("userStyleSheet"), QUrl::fromLocalFile(userStyleSheetString));
211     else
212         settings.setValue(QLatin1String("userStyleSheet"), QUrl(userStyleSheetString));
213     settings.endGroup();
214 
215     //Privacy
216     settings.beginGroup(QLatin1String("cookies"));
217 
218     CookieJar::KeepPolicy keepCookies;
219     switch(acceptCombo->currentIndex()) {
220     default:
221     case 0:
222         keepCookies = CookieJar::KeepUntilExpire;
223         break;
224     case 1:
225         keepCookies = CookieJar::KeepUntilExit;
226         break;
227     case 2:
228         keepCookies = CookieJar::KeepUntilTimeLimit;
229         break;
230     }
231     QMetaEnum acceptPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("AcceptPolicy"));
232     settings.setValue(QLatin1String("acceptCookies"), QLatin1String(acceptPolicyEnum.valueToKey(keepCookies)));
233 
234     CookieJar::KeepPolicy keepPolicy;
235     switch(keepUntilCombo->currentIndex()) {
236         default:
237     case 0:
238         keepPolicy = CookieJar::KeepUntilExpire;
239         break;
240     case 1:
241         keepPolicy = CookieJar::KeepUntilExit;
242         break;
243     case 2:
244         keepPolicy = CookieJar::KeepUntilTimeLimit;
245         break;
246     }
247 
248     QMetaEnum keepPolicyEnum = CookieJar::staticMetaObject.enumerator(CookieJar::staticMetaObject.indexOfEnumerator("KeepPolicy"));
249     settings.setValue(QLatin1String("keepCookiesUntil"), QLatin1String(keepPolicyEnum.valueToKey(keepPolicy)));
250 
251     settings.endGroup();
252 
253     // proxy
254     settings.beginGroup(QLatin1String("proxy"));
255     settings.setValue(QLatin1String("enabled"), proxySupport->isChecked());
256     settings.setValue(QLatin1String("type"), proxyType->currentIndex());
257     settings.setValue(QLatin1String("hostName"), proxyHostName->text());
258     settings.setValue(QLatin1String("port"), proxyPort->text());
259     settings.setValue(QLatin1String("userName"), proxyUserName->text());
260     settings.setValue(QLatin1String("password"), proxyPassword->text());
261     settings.endGroup();
262 
263     BrowserApplication::instance()->loadSettings();
264     BrowserApplication::networkAccessManager()->loadSettings();
265     BrowserApplication::cookieJar()->loadSettings();
266     BrowserApplication::historyManager()->loadSettings();
267 }
268 
accept()269 void SettingsDialog::accept()
270 {
271     saveToSettings();
272     QDialog::accept();
273 }
274 
showCookies()275 void SettingsDialog::showCookies()
276 {
277     CookiesDialog *dialog = new CookiesDialog(BrowserApplication::cookieJar(), this);
278     dialog->exec();
279 }
280 
showExceptions()281 void SettingsDialog::showExceptions()
282 {
283     CookiesExceptionsDialog *dialog = new CookiesExceptionsDialog(BrowserApplication::cookieJar(), this);
284     dialog->exec();
285 }
286 
chooseFont()287 void SettingsDialog::chooseFont()
288 {
289     bool ok;
290     QFont font = QFontDialog::getFont(&ok, standardFont, this);
291     if ( ok ) {
292         standardFont = font;
293         standardLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
294     }
295 }
296 
chooseFixedFont()297 void SettingsDialog::chooseFixedFont()
298 {
299     bool ok;
300     QFont font = QFontDialog::getFont(&ok, fixedFont, this);
301     if ( ok ) {
302         fixedFont = font;
303         fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
304     }
305 }
306 
setHomeToCurrentPage()307 void SettingsDialog::setHomeToCurrentPage()
308 {
309     BrowserMainWindow *mw = static_cast<BrowserMainWindow*>(parent());
310     WebView *webView = mw->currentTab();
311     if (webView)
312         homeLineEdit->setText(webView->url().toString());
313 }
314 
315