settings.cpp
上传用户:huahtool
上传日期:2015-12-10
资源大小:1089k
文件大小:13k
源码类别:

浏览器

开发平台:

Visual C++

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  4. ** Contact: Qt Software Information (qt-info@nokia.com)
  5. **
  6. ** This file is part of the demonstration applications of the Qt Toolkit.
  7. **
  8. ** Commercial Usage
  9. ** Licensees holding valid Qt Commercial licenses may use this file in
  10. ** accordance with the Qt Commercial License Agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and Nokia.
  13. **
  14. **
  15. ** GNU General Public License Usage
  16. ** Alternatively, this file may be used under the terms of the GNU
  17. ** General Public License versions 2.0 or 3.0 as published by the Free
  18. ** Software Foundation and appearing in the file LICENSE.GPL included in
  19. ** the packaging of this file.  Please review the following information
  20. ** to ensure GNU General Public Licensing requirements will be met:
  21. ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
  22. ** http://www.gnu.org/copyleft/gpl.html.  In addition, as a special
  23. ** exception, Nokia gives you certain additional rights. These rights
  24. ** are described in the Nokia Qt GPL Exception version 1.3, included in
  25. ** the file GPL_EXCEPTION.txt in this package.
  26. **
  27. ** Qt for Windows(R) Licensees
  28. ** As a special exception, Nokia, as the sole copyright holder for Qt
  29. ** Designer, grants users of the Qt/Eclipse Integration plug-in the
  30. ** right for the Qt/Eclipse Integration to link to functionality
  31. ** provided by Qt Designer and its related libraries.
  32. **
  33. ** If you are unsure which license is appropriate for your use, please
  34. ** contact the sales department at qt-sales@nokia.com.
  35. **
  36. ****************************************************************************/
  37. #include "settings.h"
  38. #include "browserapplication.h"
  39. #include "browsermainwindow.h"
  40. #include "cookiejar.h"
  41. #include "history.h"
  42. #include "networkaccessmanager.h"
  43. #include "webview.h"
  44. #include <QtCore/QSettings>
  45. #include <QtGui/QtGui>
  46. #include <QtWebKit/QtWebKit>
  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.     loadDefaults();
  57.     loadFromSettings();
  58. }
  59. void SettingsDialog::loadDefaults()
  60. {
  61.     QWebSettings *defaultSettings = QWebSettings::globalSettings();
  62.     QString standardFontFamily = defaultSettings->fontFamily(QWebSettings::StandardFont);
  63.     int standardFontSize = defaultSettings->fontSize(QWebSettings::DefaultFontSize);
  64.     standardFont = QFont(standardFontFamily, standardFontSize);
  65.     standardLabel->setText(QString(QLatin1String("%1 %2")).arg(standardFont.family()).arg(standardFont.pointSize()));
  66.     QString fixedFontFamily = defaultSettings->fontFamily(QWebSettings::FixedFont);
  67.     int fixedFontSize = defaultSettings->fontSize(QWebSettings::DefaultFixedFontSize);
  68.     fixedFont = QFont(fixedFontFamily, fixedFontSize);
  69.     fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(fixedFont.family()).arg(fixedFont.pointSize()));
  70.     downloadsLocation->setText(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
  71.     enableJavascript->setChecked(defaultSettings->testAttribute(QWebSettings::JavascriptEnabled));
  72.     enablePlugins->setChecked(defaultSettings->testAttribute(QWebSettings::PluginsEnabled));
  73. }
  74. void SettingsDialog::loadFromSettings()
  75. {
  76.     QSettings settings;
  77.     settings.beginGroup(QLatin1String("MainWindow"));
  78.     QString defaultHome = QLatin1String("http://www.trolltech.com");
  79.     homeLineEdit->setText(settings.value(QLatin1String("home"), defaultHome).toString());
  80.     settings.endGroup();
  81.     settings.beginGroup(QLatin1String("history"));
  82.     int historyExpire = settings.value(QLatin1String("historyExpire")).toInt();
  83.     int idx = 0;
  84.     switch (historyExpire) {
  85.     case 1: idx = 0; break;
  86.     case 7: idx = 1; break;
  87.     case 14: idx = 2; break;
  88.     case 30: idx = 3; break;
  89.     case 365: idx = 4; break;
  90.     case -1: idx = 5; break;
  91.     default:
  92.         idx = 5;
  93.     }
  94.     expireHistory->setCurrentIndex(idx);
  95.     settings.endGroup();
  96.     settings.beginGroup(QLatin1String("downloadmanager"));
  97.     QString downloadDirectory = settings.value(QLatin1String("downloadDirectory"), downloadsLocation->text()).toString();
  98.     downloadsLocation->setText(downloadDirectory);
  99.     settings.endGroup();
  100.     settings.beginGroup(QLatin1String("general"));
  101.     openLinksIn->setCurrentIndex(settings.value(QLatin1String("openLinksIn"), openLinksIn->currentIndex()).toInt());
  102.     settings.endGroup();
  103.     // Appearance
  104.     settings.beginGroup(QLatin1String("websettings"));
  105.     fixedFont = qVariantValue<QFont>(settings.value(QLatin1String("fixedFont"), fixedFont));
  106.     standardFont = qVariantValue<QFont>(settings.value(QLatin1String("standardFont"), standardFont));
  107.     standardLabel->setText(QString(QLatin1String("%1 %2")).arg(standardFont.family()).arg(standardFont.pointSize()));
  108.     fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(fixedFont.family()).arg(fixedFont.pointSize()));
  109.     enableJavascript->setChecked(settings.value(QLatin1String("enableJavascript"), enableJavascript->isChecked()).toBool());
  110.     enablePlugins->setChecked(settings.value(QLatin1String("enablePlugins"), enablePlugins->isChecked()).toBool());
  111.     userStyleSheet->setText(settings.value(QLatin1String("userStyleSheet")).toUrl().toString());
  112.     settings.endGroup();
  113.     // Privacy
  114.     settings.beginGroup(QLatin1String("cookies"));
  115.     CookieJar *jar = BrowserApplication::cookieJar();
  116.     QByteArray value = settings.value(QLatin1String("acceptCookies"), QLatin1String("AcceptOnlyFromSitesNavigatedTo")).toByteArray();
  117.     QMetaEnum acceptPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("AcceptPolicy"));
  118.     CookieJar::AcceptPolicy acceptCookies = acceptPolicyEnum.keyToValue(value) == -1 ?
  119.                         CookieJar::AcceptOnlyFromSitesNavigatedTo :
  120.                         static_cast<CookieJar::AcceptPolicy>(acceptPolicyEnum.keyToValue(value));
  121.     switch(acceptCookies) {
  122.     case CookieJar::AcceptAlways:
  123.         acceptCombo->setCurrentIndex(0);
  124.         break;
  125.     case CookieJar::AcceptNever:
  126.         acceptCombo->setCurrentIndex(1);
  127.         break;
  128.     case CookieJar::AcceptOnlyFromSitesNavigatedTo:
  129.         acceptCombo->setCurrentIndex(2);
  130.         break;
  131.     }
  132.     value = settings.value(QLatin1String("keepCookiesUntil"), QLatin1String("Expire")).toByteArray();
  133.     QMetaEnum keepPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("KeepPolicy"));
  134.     CookieJar::KeepPolicy keepCookies = keepPolicyEnum.keyToValue(value) == -1 ?
  135.                         CookieJar::KeepUntilExpire :
  136.                         static_cast<CookieJar::KeepPolicy>(keepPolicyEnum.keyToValue(value));
  137.     switch(keepCookies) {
  138.     case CookieJar::KeepUntilExpire:
  139.         keepUntilCombo->setCurrentIndex(0);
  140.         break;
  141.     case CookieJar::KeepUntilExit:
  142.         keepUntilCombo->setCurrentIndex(1);
  143.         break;
  144.     case CookieJar::KeepUntilTimeLimit:
  145.         keepUntilCombo->setCurrentIndex(2);
  146.         break;
  147.     }
  148.     settings.endGroup();
  149.     // Proxy
  150.     settings.beginGroup(QLatin1String("proxy"));
  151.     proxySupport->setChecked(settings.value(QLatin1String("enabled"), false).toBool());
  152.     proxyType->setCurrentIndex(settings.value(QLatin1String("type"), 0).toInt());
  153.     proxyHostName->setText(settings.value(QLatin1String("hostName")).toString());
  154.     proxyPort->setValue(settings.value(QLatin1String("port"), 1080).toInt());
  155.     proxyUserName->setText(settings.value(QLatin1String("userName")).toString());
  156.     proxyPassword->setText(settings.value(QLatin1String("password")).toString());
  157.     settings.endGroup();
  158. }
  159. void SettingsDialog::saveToSettings()
  160. {
  161.     QSettings settings;
  162.     settings.beginGroup(QLatin1String("MainWindow"));
  163.     settings.setValue(QLatin1String("home"), homeLineEdit->text());
  164.     settings.endGroup();
  165.     settings.beginGroup(QLatin1String("general"));
  166.     settings.setValue(QLatin1String("openLinksIn"), openLinksIn->currentIndex());
  167.     settings.endGroup();
  168.     settings.beginGroup(QLatin1String("history"));
  169.     int historyExpire = expireHistory->currentIndex();
  170.     int idx = -1;
  171.     switch (historyExpire) {
  172.     case 0: idx = 1; break;
  173.     case 1: idx = 7; break;
  174.     case 2: idx = 14; break;
  175.     case 3: idx = 30; break;
  176.     case 4: idx = 365; break;
  177.     case 5: idx = -1; break;
  178.     }
  179.     settings.setValue(QLatin1String("historyExpire"), idx);
  180.     settings.endGroup();
  181.     // Appearance
  182.     settings.beginGroup(QLatin1String("websettings"));
  183.     settings.setValue(QLatin1String("fixedFont"), fixedFont);
  184.     settings.setValue(QLatin1String("standardFont"), standardFont);
  185.     settings.setValue(QLatin1String("enableJavascript"), enableJavascript->isChecked());
  186.     settings.setValue(QLatin1String("enablePlugins"), enablePlugins->isChecked());
  187.     QString userStyleSheetString = userStyleSheet->text();
  188.     if (QFile::exists(userStyleSheetString))
  189.         settings.setValue(QLatin1String("userStyleSheet"), QUrl::fromLocalFile(userStyleSheetString));
  190.     else
  191.         settings.setValue(QLatin1String("userStyleSheet"), QUrl(userStyleSheetString));
  192.     settings.endGroup();
  193.     //Privacy
  194.     settings.beginGroup(QLatin1String("cookies"));
  195.     CookieJar::KeepPolicy keepCookies;
  196.     switch(acceptCombo->currentIndex()) {
  197.     default:
  198.     case 0:
  199.         keepCookies = CookieJar::KeepUntilExpire;
  200.         break;
  201.     case 1:
  202.         keepCookies = CookieJar::KeepUntilExit;
  203.         break;
  204.     case 2:
  205.         keepCookies = CookieJar::KeepUntilTimeLimit;
  206.         break;
  207.     }
  208.     CookieJar *jar = BrowserApplication::cookieJar();
  209.     QMetaEnum acceptPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("AcceptPolicy"));
  210.     settings.setValue(QLatin1String("acceptCookies"), QLatin1String(acceptPolicyEnum.valueToKey(keepCookies)));
  211.     CookieJar::KeepPolicy keepPolicy;
  212.     switch(keepUntilCombo->currentIndex()) {
  213.         default:
  214.     case 0:
  215.         keepPolicy = CookieJar::KeepUntilExpire;
  216.         break;
  217.     case 1:
  218.         keepPolicy = CookieJar::KeepUntilExit;
  219.         break;
  220.     case 2:
  221.         keepPolicy = CookieJar::KeepUntilTimeLimit;
  222.         break;
  223.     }
  224.     QMetaEnum keepPolicyEnum = jar->staticMetaObject.enumerator(jar->staticMetaObject.indexOfEnumerator("KeepPolicy"));
  225.     settings.setValue(QLatin1String("keepCookiesUntil"), QLatin1String(keepPolicyEnum.valueToKey(keepPolicy)));
  226.     settings.endGroup();
  227.     // proxy
  228.     settings.beginGroup(QLatin1String("proxy"));
  229.     settings.setValue(QLatin1String("enabled"), proxySupport->isChecked());
  230.     settings.setValue(QLatin1String("type"), proxyType->currentIndex());
  231.     settings.setValue(QLatin1String("hostName"), proxyHostName->text());
  232.     settings.setValue(QLatin1String("port"), proxyPort->text());
  233.     settings.setValue(QLatin1String("userName"), proxyUserName->text());
  234.     settings.setValue(QLatin1String("password"), proxyPassword->text());
  235.     settings.endGroup();
  236.     BrowserApplication::instance()->loadSettings();
  237.     BrowserApplication::networkAccessManager()->loadSettings();
  238.     BrowserApplication::cookieJar()->loadSettings();
  239.     BrowserApplication::historyManager()->loadSettings();
  240. }
  241. void SettingsDialog::accept()
  242. {
  243.     saveToSettings();
  244.     QDialog::accept();
  245. }
  246. void SettingsDialog::showCookies()
  247. {
  248.     CookiesDialog *dialog = new CookiesDialog(BrowserApplication::cookieJar(), this);
  249.     dialog->exec();
  250. }
  251. void SettingsDialog::showExceptions()
  252. {
  253.     CookiesExceptionsDialog *dialog = new CookiesExceptionsDialog(BrowserApplication::cookieJar(), this);
  254.     dialog->exec();
  255. }
  256. void SettingsDialog::chooseFont()
  257. {
  258.     bool ok;
  259.     QFont font = QFontDialog::getFont(&ok, standardFont, this);
  260.     if ( ok ) {
  261.         standardFont = font;
  262.         standardLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
  263.     }
  264. }
  265. void SettingsDialog::chooseFixedFont()
  266. {
  267.     bool ok;
  268.     QFont font = QFontDialog::getFont(&ok, fixedFont, this);
  269.     if ( ok ) {
  270.         fixedFont = font;
  271.         fixedLabel->setText(QString(QLatin1String("%1 %2")).arg(font.family()).arg(font.pointSize()));
  272.     }
  273. }
  274. void SettingsDialog::setHomeToCurrentPage()
  275. {
  276.     BrowserMainWindow *mw = static_cast<BrowserMainWindow*>(parent());
  277.     WebView *webView = mw->currentTab();
  278.     if (webView)
  279.         homeLineEdit->setText(webView->url().toString());
  280. }