1 | /* $Id: UINetworkRequest.cpp 79365 2019-06-26 15:57:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINetworkRequest class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* GUI includes: */
|
---|
19 | #include "UINetworkRequest.h"
|
---|
20 | #include "UINetworkRequestWidget.h"
|
---|
21 | #include "UINetworkManager.h"
|
---|
22 | #include "UINetworkManagerDialog.h"
|
---|
23 | #include "UINetworkManagerIndicator.h"
|
---|
24 | #include "UINetworkCustomer.h"
|
---|
25 | #include "UICommon.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | UINetworkRequest::UINetworkRequest(UINetworkRequestType enmType,
|
---|
29 | const QList<QUrl> &urls,
|
---|
30 | const QString &strTarget,
|
---|
31 | const UserDictionary &requestHeaders,
|
---|
32 | UINetworkCustomer *pCustomer,
|
---|
33 | UINetworkManager *pNetworkManager)
|
---|
34 | : QObject(pNetworkManager)
|
---|
35 | , m_enmType(enmType)
|
---|
36 | , m_urls(urls)
|
---|
37 | , m_strTarget(strTarget)
|
---|
38 | , m_requestHeaders(requestHeaders)
|
---|
39 | , m_pCustomer(pCustomer)
|
---|
40 | , m_pNetworkManager(pNetworkManager)
|
---|
41 | , m_uuid(QUuid::createUuid())
|
---|
42 | , m_iUrlIndex(-1)
|
---|
43 | , m_fRunning(false)
|
---|
44 | {
|
---|
45 | /* Prepare: */
|
---|
46 | prepare();
|
---|
47 | }
|
---|
48 |
|
---|
49 | UINetworkRequest::~UINetworkRequest()
|
---|
50 | {
|
---|
51 | /* Cleanup: */
|
---|
52 | cleanup();
|
---|
53 | }
|
---|
54 |
|
---|
55 | const QString UINetworkRequest::description() const
|
---|
56 | {
|
---|
57 | return m_pCustomer->description();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void UINetworkRequest::sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal)
|
---|
61 | {
|
---|
62 | /* Notify common network-request listeners: */
|
---|
63 | emit sigProgress(m_uuid, iReceived, iTotal);
|
---|
64 | /* Notify own network-request listeners: */
|
---|
65 | emit sigProgress(iReceived, iTotal);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void UINetworkRequest::sltHandleNetworkReplyFinish()
|
---|
69 | {
|
---|
70 | /* Mark network-reply as non-running: */
|
---|
71 | m_fRunning = false;
|
---|
72 |
|
---|
73 | /* Make sure network-reply still valid: */
|
---|
74 | if (!m_pReply)
|
---|
75 | return;
|
---|
76 |
|
---|
77 | /* If network-reply has no errors: */
|
---|
78 | if (m_pReply->error() == UINetworkReply::NoError)
|
---|
79 | {
|
---|
80 | /* Notify own network-request listeners: */
|
---|
81 | emit sigFinished();
|
---|
82 | /* Notify common network-request listeners: */
|
---|
83 | emit sigFinished(m_uuid);
|
---|
84 | }
|
---|
85 | /* If network-request was canceled: */
|
---|
86 | else if (m_pReply->error() == UINetworkReply::OperationCanceledError)
|
---|
87 | {
|
---|
88 | /* Notify network-manager: */
|
---|
89 | emit sigCanceled(m_uuid);
|
---|
90 | }
|
---|
91 | /* If some other error occured: */
|
---|
92 | else
|
---|
93 | {
|
---|
94 | /* Check if we are able to handle error: */
|
---|
95 | bool fErrorHandled = false;
|
---|
96 |
|
---|
97 | /* Handle redirection: */
|
---|
98 | switch (m_pReply->error())
|
---|
99 | {
|
---|
100 | case UINetworkReply::ContentReSendError:
|
---|
101 | {
|
---|
102 | /* Check whether redirection link was acquired: */
|
---|
103 | const QString strRedirect = m_pReply->header(UINetworkReply::LocationHeader).toString();
|
---|
104 | if (!strRedirect.isEmpty())
|
---|
105 | {
|
---|
106 | /* Cleanup current network-reply first: */
|
---|
107 | cleanupNetworkReply();
|
---|
108 |
|
---|
109 | /* Choose redirect-source as current url: */
|
---|
110 | m_url = strRedirect;
|
---|
111 |
|
---|
112 | /* Create new network-reply finally: */
|
---|
113 | prepareNetworkReply();
|
---|
114 |
|
---|
115 | /* Mark this error handled: */
|
---|
116 | fErrorHandled = true;
|
---|
117 | }
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | default:
|
---|
121 | break;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* If error still unhandled: */
|
---|
125 | if (!fErrorHandled)
|
---|
126 | {
|
---|
127 | /* Check if we have other urls in queue: */
|
---|
128 | if (m_iUrlIndex < m_urls.size() - 1)
|
---|
129 | {
|
---|
130 | /* Cleanup current network-reply first: */
|
---|
131 | cleanupNetworkReply();
|
---|
132 |
|
---|
133 | /* Choose next url as current: */
|
---|
134 | ++m_iUrlIndex;
|
---|
135 | m_url = m_urls.at(m_iUrlIndex);
|
---|
136 |
|
---|
137 | /* Create new network-reply finally: */
|
---|
138 | prepareNetworkReply();
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | /* Notify own network-request listeners: */
|
---|
143 | emit sigFailed(m_pReply->errorString());
|
---|
144 | /* Notify common network-request listeners: */
|
---|
145 | emit sigFailed(m_uuid, m_pReply->errorString());
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | void UINetworkRequest::sltRetry()
|
---|
152 | {
|
---|
153 | /* Cleanup current network-reply first: */
|
---|
154 | cleanupNetworkReply();
|
---|
155 |
|
---|
156 | /* Choose first url as current: */
|
---|
157 | m_iUrlIndex = 0;
|
---|
158 | m_url = m_urls.at(m_iUrlIndex);
|
---|
159 |
|
---|
160 | /* Create new network-reply finally: */
|
---|
161 | prepareNetworkReply();
|
---|
162 | }
|
---|
163 |
|
---|
164 | void UINetworkRequest::sltCancel()
|
---|
165 | {
|
---|
166 | /* Abort network-reply if present: */
|
---|
167 | if (m_pReply)
|
---|
168 | {
|
---|
169 | if (m_fRunning)
|
---|
170 | m_pReply->abort();
|
---|
171 | else
|
---|
172 | emit sigCanceled(m_uuid);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | void UINetworkRequest::prepare()
|
---|
177 | {
|
---|
178 | /* Prepare listeners for network-manager: */
|
---|
179 | connect(manager(), &UINetworkManager::sigCancelNetworkRequests,
|
---|
180 | this, &UINetworkRequest::sltCancel, Qt::QueuedConnection);
|
---|
181 |
|
---|
182 | /* Choose first url as current: */
|
---|
183 | m_iUrlIndex = 0;
|
---|
184 | m_url = m_urls.at(m_iUrlIndex);
|
---|
185 |
|
---|
186 | /* Register network-request in network-manager: */
|
---|
187 | manager()->registerNetworkRequest(this);
|
---|
188 |
|
---|
189 | /* Prepare network-reply: */
|
---|
190 | prepareNetworkReply();
|
---|
191 | }
|
---|
192 |
|
---|
193 | void UINetworkRequest::prepareNetworkReply()
|
---|
194 | {
|
---|
195 | /* Create network-reply: */
|
---|
196 | m_pReply = new UINetworkReply(m_enmType, m_url, m_strTarget, m_requestHeaders);
|
---|
197 | AssertPtrReturnVoid(m_pReply.data());
|
---|
198 | {
|
---|
199 | /* Prepare network-reply: */
|
---|
200 | connect(m_pReply.data(), &UINetworkReply::downloadProgress,
|
---|
201 | this, &UINetworkRequest::sltHandleNetworkReplyProgress);
|
---|
202 | connect(m_pReply.data(), &UINetworkReply::finished,
|
---|
203 | this, &UINetworkRequest::sltHandleNetworkReplyFinish);
|
---|
204 |
|
---|
205 | /* Mark network-reply as running: */
|
---|
206 | m_fRunning = true;
|
---|
207 |
|
---|
208 | /* Notify common network-request listeners: */
|
---|
209 | emit sigStarted(m_uuid);
|
---|
210 | /* Notify own network-request listeners: */
|
---|
211 | emit sigStarted();
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | void UINetworkRequest::cleanupNetworkReply()
|
---|
216 | {
|
---|
217 | /* Destroy network-reply: */
|
---|
218 | AssertPtrReturnVoid(m_pReply.data());
|
---|
219 | m_pReply->disconnect();
|
---|
220 | m_pReply->deleteLater();
|
---|
221 | m_pReply = 0;
|
---|
222 | }
|
---|
223 |
|
---|
224 | void UINetworkRequest::cleanup()
|
---|
225 | {
|
---|
226 | /* Cleanup network-reply: */
|
---|
227 | cleanupNetworkReply();
|
---|
228 |
|
---|
229 | /* Unregister network-request from network-manager: */
|
---|
230 | manager()->unregisterNetworkRequest(m_uuid);
|
---|
231 | }
|
---|
232 |
|
---|