45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 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 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
|
42 module networkaccessmanager;
|
|
43
|
|
44
|
|
45 import QtNetwork.QNetworkAccessManager;
|
|
46
|
|
47 import browserapplication;
|
|
48 import browsermainwindow;
|
|
49 import ui_passworddialog;
|
|
50 import ui_proxy;
|
|
51
|
|
52 import QtCore.QSettings;
|
|
53
|
|
54 import QtGui.QDesktopServices;
|
|
55 import QtGui.QDialog;
|
|
56 import QtGui.QMessageBox;
|
|
57 import QtGui.QStyle;
|
|
58 import QtGui.QTextDocument;
|
|
59
|
|
60 import QtNetwork.QAuthenticator;
|
|
61 import QtNetwork.QNetworkDiskCache;
|
|
62 import QtNetwork.QNetworkProxy;
|
|
63 import QtNetwork.QNetworkReply;
|
|
64 import QtNetwork.QSslError;
|
|
65
|
|
66
|
|
67 class NetworkAccessManager : public QNetworkAccessManager
|
|
68 {
|
|
69 Q_OBJECT
|
|
70
|
|
71 public:
|
|
72 this(QObject *parent = null)
|
|
73 {
|
|
74 super(parent);
|
|
75
|
|
76 connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
|
|
77 SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
|
|
78 connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
|
79 SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
|
80 version(QT_NO_OPENSSL) {
|
|
81 connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
|
|
82 SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&)));
|
|
83 }
|
|
84 loadSettings();
|
|
85
|
|
86 QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
|
|
87 QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
|
|
88 diskCache.setCacheDirectory(location);
|
|
89 setCache(diskCache);
|
|
90 }
|
|
91
|
|
92 private:
|
|
93 QList<QString> sslTrustedHostList;
|
|
94
|
|
95 public slots:
|
|
96 void loadSettings()
|
|
97 {
|
|
98 QSettings settings;
|
|
99 settings.beginGroup(QLatin1String("proxy"));
|
|
100 QNetworkProxy proxy;
|
|
101 if (settings.value(QLatin1String("enabled"), false).toBool()) {
|
|
102 if (settings.value(QLatin1String("type"), 0).toInt() == 0)
|
|
103 proxy = QNetworkProxy::Socks5Proxy;
|
|
104 else
|
|
105 proxy = QNetworkProxy::HttpProxy;
|
|
106 proxy.setHostName(settings.value(QLatin1String("hostName")).toString());
|
|
107 proxy.setPort(settings.value(QLatin1String("port"), 1080).toInt());
|
|
108 proxy.setUser(settings.value(QLatin1String("userName")).toString());
|
|
109 proxy.setPassword(settings.value(QLatin1String("password")).toString());
|
|
110 }
|
|
111 setProxy(proxy);
|
|
112 }
|
|
113
|
|
114
|
|
115 private slots:
|
|
116 void authenticationRequired(QNetworkReply *reply, QAuthenticator *auth)
|
|
117 {
|
|
118 BrowserMainWindow *mainWindow = BrowserApplication::instance().mainWindow();
|
|
119
|
|
120 QDialog dialog(mainWindow);
|
|
121 dialog.setWindowFlags(Qt::Sheet);
|
|
122
|
|
123 Ui::PasswordDialog passwordDialog;
|
|
124 passwordDialog.setupUi(&dialog);
|
|
125
|
|
126 passwordDialog.iconLabel.setText(QString());
|
|
127 passwordDialog.iconLabel.setPixmap(mainWindow.style().standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
|
|
128
|
|
129 QString introMessage = tr("<qt>Enter username and password for \"%1\" at %2</qt>");
|
|
130 introMessage = introMessage.arg(Qt::escape(reply.url().toString())).arg(Qt::escape(reply.url().toString()));
|
|
131 passwordDialog.introLabel.setText(introMessage);
|
|
132 passwordDialog.introLabel.setWordWrap(true);
|
|
133
|
|
134 if (dialog.exec() == QDialog::Accepted) {
|
|
135 auth.setUser(passwordDialog.userNameLineEdit.text());
|
|
136 auth.setPassword(passwordDialog.passwordLineEdit.text());
|
|
137 }
|
|
138 }
|
|
139
|
|
140 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth)
|
|
141 {
|
|
142 BrowserMainWindow *mainWindow = BrowserApplication::instance().mainWindow();
|
|
143
|
|
144 QDialog dialog(mainWindow);
|
|
145 dialog.setWindowFlags(Qt::Sheet);
|
|
146
|
|
147 Ui::ProxyDialog proxyDialog;
|
|
148 proxyDialog.setupUi(&dialog);
|
|
149
|
|
150 proxyDialog.iconLabel.setText(QString());
|
|
151 proxyDialog.iconLabel.setPixmap(mainWindow.style().standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
|
|
152
|
|
153 QString introMessage = tr("<qt>Connect to proxy \"%1\" using:</qt>");
|
|
154 introMessage = introMessage.arg(Qt::escape(proxy.hostName()));
|
|
155 proxyDialog.introLabel.setText(introMessage);
|
|
156 proxyDialog.introLabel.setWordWrap(true);
|
|
157
|
|
158 if (dialog.exec() == QDialog::Accepted) {
|
|
159 auth.setUser(proxyDialog.userNameLineEdit.text());
|
|
160 auth.setPassword(proxyDialog.passwordLineEdit.text());
|
|
161 }
|
|
162 }
|
|
163
|
|
164 version(QT_NO_OPENSSL) {
|
|
165 void sslErrors(QNetworkReply *reply, const QList<QSslError> &error)
|
|
166 {
|
|
167 // check if SSL certificate has been trusted already
|
|
168 QString replyHost = reply.url().host() + ":" + reply.url().port();
|
|
169 if(! sslTrustedHostList.contains(replyHost)) {
|
|
170 BrowserMainWindow *mainWindow = BrowserApplication::instance().mainWindow();
|
|
171
|
|
172 QStringList errorStrings;
|
|
173 for (int i = 0; i < error.count(); ++i)
|
|
174 errorStrings += error.at(i).errorString();
|
|
175 QString errors = errorStrings.join(QLatin1String("\n"));
|
|
176 int ret = QMessageBox::warning(mainWindow, QCoreApplication::applicationName(),
|
|
177 tr("SSL Errors:\n\n%1\n\n%2\n\n"
|
|
178 "Do you want to ignore these errors for this host?").arg(reply.url().toString()).arg(errors),
|
|
179 QMessageBox::Yes | QMessageBox::No,
|
|
180 QMessageBox::No);
|
|
181 if (ret == QMessageBox::Yes) {
|
|
182 reply.ignoreSslErrors();
|
|
183 sslTrustedHostList.append(replyHost);
|
|
184 }
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188
|
|
189 }
|