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