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