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

浏览器

开发平台:

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 "networkaccessmanager.h"
  38. #include "browserapplication.h"
  39. #include "browsermainwindow.h"
  40. #include "ui_passworddialog.h"
  41. #include "ui_proxy.h"
  42. #include <QtCore/QSettings>
  43. #include <QtGui/QDialog>
  44. #include <QtGui/QMessageBox>
  45. #include <QtGui/QStyle>
  46. #include <QtGui/QTextDocument>
  47. #include <QtNetwork/QAuthenticator>
  48. #include <QtNetwork/QNetworkProxy>
  49. #include <QtNetwork/QNetworkReply>
  50. #include <QtNetwork/QSslError>
  51. NetworkAccessManager::NetworkAccessManager(QObject *parent)
  52.     : QNetworkAccessManager(parent)
  53. {
  54.     connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
  55.             SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
  56.     connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
  57.             SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
  58. #ifndef QT_NO_OPENSSL
  59.     connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
  60.             SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&)));
  61. #endif
  62.     loadSettings();
  63. }
  64. void NetworkAccessManager::loadSettings()
  65. {
  66.     QSettings settings;
  67.     settings.beginGroup(QLatin1String("proxy"));
  68.     QNetworkProxy proxy;
  69.     if (settings.value(QLatin1String("enabled"), false).toBool()) {
  70.         if (settings.value(QLatin1String("type"), 0).toInt() == 0)
  71.             proxy.setType(QNetworkProxy::Socks5Proxy);
  72.         else
  73.             proxy.setType(QNetworkProxy::HttpProxy);
  74.         proxy.setHostName(settings.value(QLatin1String("hostName")).toString());
  75.         proxy.setPort(settings.value(QLatin1String("port"), 1080).toInt());
  76.         proxy.setUser(settings.value(QLatin1String("userName")).toString());
  77.         proxy.setPassword(settings.value(QLatin1String("password")).toString());
  78.     }
  79.     setProxy(proxy);
  80. }
  81. void NetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth)
  82. {
  83.     BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
  84.     QDialog dialog(mainWindow);
  85.     dialog.setWindowFlags(Qt::Sheet);
  86.     Ui::PasswordDialog passwordDialog;
  87.     passwordDialog.setupUi(&dialog);
  88.     passwordDialog.iconLabel->setText(QString());
  89.     passwordDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
  90.     QString introMessage = tr("<qt>Enter username and password for "%1" at %2</qt>");
  91.     introMessage = introMessage.arg(Qt::escape(reply->url().toString())).arg(Qt::escape(reply->url().toString()));
  92.     passwordDialog.introLabel->setText(introMessage);
  93.     passwordDialog.introLabel->setWordWrap(true);
  94.     if (dialog.exec() == QDialog::Accepted) {
  95.         auth->setUser(passwordDialog.userNameLineEdit->text());
  96.         auth->setPassword(passwordDialog.passwordLineEdit->text());
  97.     }
  98. }
  99. void NetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth)
  100. {
  101.     BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
  102.     QDialog dialog(mainWindow);
  103.     dialog.setWindowFlags(Qt::Sheet);
  104.     Ui::ProxyDialog proxyDialog;
  105.     proxyDialog.setupUi(&dialog);
  106.     proxyDialog.iconLabel->setText(QString());
  107.     proxyDialog.iconLabel->setPixmap(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
  108.     QString introMessage = tr("<qt>Connect to proxy "%1" using:</qt>");
  109.     introMessage = introMessage.arg(Qt::escape(proxy.hostName()));
  110.     proxyDialog.introLabel->setText(introMessage);
  111.     proxyDialog.introLabel->setWordWrap(true);
  112.     if (dialog.exec() == QDialog::Accepted) {
  113.         auth->setUser(proxyDialog.userNameLineEdit->text());
  114.         auth->setPassword(proxyDialog.passwordLineEdit->text());
  115.     }
  116. }
  117. #ifndef QT_NO_OPENSSL
  118. void NetworkAccessManager::sslErrors(QNetworkReply *reply, const QList<QSslError> &error)
  119. {
  120.     BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
  121.     QStringList errorStrings;
  122.     for (int i = 0; i < error.count(); ++i)
  123.         errorStrings += error.at(i).errorString();
  124.     QString errors = errorStrings.join(QLatin1String("n"));
  125.     int ret = QMessageBox::warning(mainWindow, QCoreApplication::applicationName(),
  126.                            tr("SSL Errors:nn%1nn%2nn"
  127.                               "Do you want to ignore these errors?").arg(reply->url().toString()).arg(errors),
  128.                            QMessageBox::Yes | QMessageBox::No,
  129.                            QMessageBox::No);
  130.     if (ret == QMessageBox::Yes)
  131.         reply->ignoreSslErrors();
  132. }
  133. #endif