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
|
73
|
45 import qt.network.QNetworkAccessManager;
|
|
46
|
|
47 import qt.core.QSettings;
|
|
48
|
|
49 import qt.gui.QDesktopServices;
|
|
50 import qt.gui.QDialog;
|
|
51 import qt.gui.QMessageBox;
|
|
52 import qt.gui.QStyle;
|
|
53 import qt.gui.QTextDocument;
|
|
54
|
|
55 import qt.network.QAuthenticator;
|
|
56 import qt.network.QNetworkDiskCache;
|
|
57 import qt.network.QNetworkProxy;
|
|
58 import qt.network.QNetworkReply;
|
|
59 import qt.network.QSslError;
|
45
|
60
|
|
61 import browserapplication;
|
|
62 import browsermainwindow;
|
|
63 import ui_passworddialog;
|
|
64 import ui_proxy;
|
|
65
|
|
66
|
|
67 class NetworkAccessManager : public QNetworkAccessManager
|
|
68 {
|
73
|
69 public:
|
|
70 this(QObject parent = null)
|
|
71 {
|
|
72 super(parent);
|
45
|
73
|
73
|
74 this.authenticationRequired.connect(&authenticationRequired);
|
|
75 this.proxyAuthenticationRequired.connect(&proxyAuthenticationRequired);
|
|
76 version(QT_NO_OPENSSL) {
|
|
77 this.sslErrors.connect(&sslErrors);
|
|
78 }
|
|
79 loadSettings();
|
45
|
80
|
73
|
81 QNetworkDiskCache diskCache = new QNetworkDiskCache(this);
|
|
82 QString location = QDesktopServices.storageLocation(QDesktopServices.CacheLocation);
|
|
83 diskCache.setCacheDirectory(location);
|
|
84 setCache(diskCache);
|
|
85 }
|
45
|
86
|
|
87 private:
|
|
88 QList<QString> sslTrustedHostList;
|
|
89
|
73
|
90 public:
|
45
|
91
|
73
|
92 void loadSettings()
|
|
93 {
|
|
94 QSettings settings;
|
|
95 settings.beginGroup(QLatin1String("proxy"));
|
|
96 QNetworkProxy proxy;
|
|
97 if (settings.value(QLatin1String("enabled"), false).toBool()) {
|
|
98 if (settings.value(QLatin1String("type"), 0).toInt() == 0)
|
|
99 proxy = QNetworkProxy.Socks5Proxy;
|
|
100 else
|
|
101 proxy = QNetworkProxy.HttpProxy;
|
|
102 proxy.setHostName(settings.value(QLatin1String("hostName")).toString());
|
|
103 proxy.setPort(settings.value(QLatin1String("port"), 1080).toInt());
|
|
104 proxy.setUser(settings.value(QLatin1String("userName")).toString());
|
|
105 proxy.setPassword(settings.value(QLatin1String("password")).toString());
|
|
106 }
|
|
107 setProxy(proxy);
|
|
108 }
|
45
|
109
|
73
|
110 private:
|
|
111
|
|
112 void authenticationRequired(QNetworkReply reply, QAuthenticator auth)
|
|
113 {
|
|
114 BrowserMainWindow mainWindow = BrowserApplication.instance().mainWindow();
|
45
|
115
|
73
|
116 QDialog dialog(mainWindow);
|
|
117 dialog.setWindowFlags(Qt.Sheet);
|
45
|
118
|
73
|
119 Ui.PasswordDialog passwordDialog;
|
|
120 passwordDialog.setupUi(&dialog);
|
|
121
|
|
122 passwordDialog.iconLabel.setText(QString());
|
|
123 passwordDialog.iconLabel.setPixmap(mainWindow.style().standardIcon(QStyle.SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
|
45
|
124
|
73
|
125 QString introMessage = tr("<qt>Enter username and password for \"%1\" at %2</qt>");
|
|
126 introMessage = introMessage.arg(Qt.escape(reply.url().toString())).arg(Qt.escape(reply.url().toString()));
|
|
127 passwordDialog.introLabel.setText(introMessage);
|
|
128 passwordDialog.introLabel.setWordWrap(true);
|
45
|
129
|
73
|
130 if (dialog.exec() == QDialog.Accepted) {
|
|
131 auth.setUser(passwordDialog.userNameLineEdit.text());
|
|
132 auth.setPassword(passwordDialog.passwordLineEdit.text());
|
|
133 }
|
|
134 }
|
45
|
135
|
73
|
136 void proxyAuthenticationRequired(const QNetworkProxy proxy, QAuthenticator auth)
|
|
137 {
|
|
138 BrowserMainWindow mainWindow = BrowserApplication.instance().mainWindow();
|
45
|
139
|
73
|
140 QDialog dialog(mainWindow);
|
|
141 dialog.setWindowFlags(Qt.Sheet);
|
45
|
142
|
73
|
143 Ui.ProxyDialog proxyDialog;
|
|
144 proxyDialog.setupUi(&dialog);
|
45
|
145
|
73
|
146 proxyDialog.iconLabel.setText(QString());
|
|
147 proxyDialog.iconLabel.setPixmap(mainWindow.style().standardIcon(QStyle.SP_MessageBoxQuestion, 0, mainWindow).pixmap(32, 32));
|
45
|
148
|
73
|
149 QString introMessage = tr("<qt>Connect to proxy \"%1\" using:</qt>");
|
|
150 introMessage = introMessage.arg(Qt.escape(proxy.hostName()));
|
|
151 proxyDialog.introLabel.setText(introMessage);
|
|
152 proxyDialog.introLabel.setWordWrap(true);
|
45
|
153
|
73
|
154 if (dialog.exec() == QDialog.Accepted) {
|
|
155 auth.setUser(proxyDialog.userNameLineEdit.text());
|
|
156 auth.setPassword(proxyDialog.passwordLineEdit.text());
|
|
157 }
|
|
158 }
|
45
|
159
|
|
160 version(QT_NO_OPENSSL) {
|
73
|
161 void sslErrors(QNetworkReply reply, QList<QSslError> error)
|
|
162 {
|
|
163 // check if SSL certificate has been trusted already
|
|
164 QString replyHost = reply.url().host() + ":" + reply.url().port();
|
|
165 if(! sslTrustedHostList.contains(replyHost)) {
|
|
166 BrowserMainWindow mainWindow = BrowserApplication.instance().mainWindow();
|
45
|
167
|
73
|
168 QStringList errorStrings;
|
|
169 for (int i = 0; i < error.count(); ++i)
|
|
170 errorStrings += error.at(i).errorString();
|
|
171 QString errors = errorStrings.join(QLatin1String("\n"));
|
|
172 int ret = QMessageBox.warning(mainWindow, QCoreApplication.applicationName(),
|
|
173 tr("SSL Errors:\n\n%1\n\n%2\n\n"
|
|
174 "Do you want to ignore these errors for this host?").arg(reply.url().toString()).arg(errors),
|
|
175 QMessageBox.Yes | QMessageBox.No, QMessageBox.No);
|
|
176 if (ret == QMessageBox.Yes) {
|
|
177 reply.ignoreSslErrors();
|
|
178 sslTrustedHostList.append(replyHost);
|
|
179 }
|
|
180 }
|
|
181 }
|
45
|
182 }
|
|
183
|
|
184 }
|