1 | /* $Id: UINetworkRequest.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINetworkRequest class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QVariant>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UINetworkRequest.h"
|
---|
33 | #include "UINetworkRequestManager.h"
|
---|
34 |
|
---|
35 | /* Other VBox includes: */
|
---|
36 | #include "iprt/assert.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | UINetworkRequest::UINetworkRequest(UINetworkRequestType enmType,
|
---|
40 | const QList<QUrl> &urls,
|
---|
41 | const QString &strTarget,
|
---|
42 | const UserDictionary &requestHeaders)
|
---|
43 | : m_enmType(enmType)
|
---|
44 | , m_urls(urls)
|
---|
45 | , m_strTarget(strTarget)
|
---|
46 | , m_requestHeaders(requestHeaders)
|
---|
47 | , m_iUrlIndex(-1)
|
---|
48 | , m_fRunning(false)
|
---|
49 | {
|
---|
50 | prepare();
|
---|
51 | }
|
---|
52 |
|
---|
53 | UINetworkRequest::~UINetworkRequest()
|
---|
54 | {
|
---|
55 | cleanup();
|
---|
56 | }
|
---|
57 |
|
---|
58 | void UINetworkRequest::sltHandleNetworkReplyProgress(qint64 iReceived, qint64 iTotal)
|
---|
59 | {
|
---|
60 | /* Notify network-request listeners: */
|
---|
61 | emit sigProgress(iReceived, iTotal);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void UINetworkRequest::sltHandleNetworkReplyFinish()
|
---|
65 | {
|
---|
66 | /* Mark network-reply as non-running: */
|
---|
67 | m_fRunning = false;
|
---|
68 |
|
---|
69 | /* Make sure network-reply still valid: */
|
---|
70 | if (!m_pReply)
|
---|
71 | return;
|
---|
72 |
|
---|
73 | /* If network-reply has no errors: */
|
---|
74 | if (m_pReply->error() == UINetworkReply::NoError)
|
---|
75 | {
|
---|
76 | /* Notify network-request listeners: */
|
---|
77 | emit sigFinished();
|
---|
78 | }
|
---|
79 | /* If network-request was canceled: */
|
---|
80 | else if (m_pReply->error() == UINetworkReply::OperationCanceledError)
|
---|
81 | {
|
---|
82 | /* Notify network-request listeners: */
|
---|
83 | emit sigCanceled();
|
---|
84 | }
|
---|
85 | /* If some other error occured: */
|
---|
86 | else
|
---|
87 | {
|
---|
88 | /* Check if we are able to handle error: */
|
---|
89 | bool fErrorHandled = false;
|
---|
90 |
|
---|
91 | /* Handle redirection: */
|
---|
92 | switch (m_pReply->error())
|
---|
93 | {
|
---|
94 | case UINetworkReply::ContentReSendError:
|
---|
95 | {
|
---|
96 | /* Check whether redirection link was acquired: */
|
---|
97 | const QString strRedirect = m_pReply->header(UINetworkReply::LocationHeader).toString();
|
---|
98 | if (!strRedirect.isEmpty())
|
---|
99 | {
|
---|
100 | /* Cleanup current network-reply first: */
|
---|
101 | cleanupNetworkReply();
|
---|
102 |
|
---|
103 | /* Choose redirect-source as current url: */
|
---|
104 | m_url = strRedirect;
|
---|
105 |
|
---|
106 | /* Create new network-reply finally: */
|
---|
107 | prepareNetworkReply();
|
---|
108 |
|
---|
109 | /* Mark this error handled: */
|
---|
110 | fErrorHandled = true;
|
---|
111 | }
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | default:
|
---|
115 | break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* If error still unhandled: */
|
---|
119 | if (!fErrorHandled)
|
---|
120 | {
|
---|
121 | /* Check if we have other urls in queue: */
|
---|
122 | if (m_iUrlIndex < m_urls.size() - 1)
|
---|
123 | {
|
---|
124 | /* Cleanup current network-reply first: */
|
---|
125 | cleanupNetworkReply();
|
---|
126 |
|
---|
127 | /* Choose next url as current: */
|
---|
128 | ++m_iUrlIndex;
|
---|
129 | m_url = m_urls.at(m_iUrlIndex);
|
---|
130 |
|
---|
131 | /* Create new network-reply finally: */
|
---|
132 | prepareNetworkReply();
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | /* Notify network-request listeners: */
|
---|
137 | emit sigFailed(m_pReply->errorString());
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | void UINetworkRequest::sltCancel()
|
---|
144 | {
|
---|
145 | /* Abort network-reply if present: */
|
---|
146 | if (m_pReply)
|
---|
147 | {
|
---|
148 | if (m_fRunning)
|
---|
149 | m_pReply->abort();
|
---|
150 | else
|
---|
151 | emit sigCanceled();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | void UINetworkRequest::prepare()
|
---|
156 | {
|
---|
157 | /* Choose first url as current: */
|
---|
158 | m_iUrlIndex = 0;
|
---|
159 | m_url = m_urls.at(m_iUrlIndex);
|
---|
160 |
|
---|
161 | /* Prepare network-reply: */
|
---|
162 | prepareNetworkReply();
|
---|
163 | }
|
---|
164 |
|
---|
165 | void UINetworkRequest::prepareNetworkReply()
|
---|
166 | {
|
---|
167 | /* Create network-reply: */
|
---|
168 | m_pReply = new UINetworkReply(m_enmType, m_url, m_strTarget, m_requestHeaders);
|
---|
169 | AssertPtrReturnVoid(m_pReply.data());
|
---|
170 | {
|
---|
171 | /* Prepare network-reply: */
|
---|
172 | connect(m_pReply.data(), &UINetworkReply::downloadProgress,
|
---|
173 | this, &UINetworkRequest::sltHandleNetworkReplyProgress);
|
---|
174 | connect(m_pReply.data(), &UINetworkReply::finished,
|
---|
175 | this, &UINetworkRequest::sltHandleNetworkReplyFinish);
|
---|
176 |
|
---|
177 | /* Mark network-reply as running: */
|
---|
178 | m_fRunning = true;
|
---|
179 |
|
---|
180 | /* Notify network-request listeners: */
|
---|
181 | emit sigStarted();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | void UINetworkRequest::cleanupNetworkReply()
|
---|
186 | {
|
---|
187 | /* Destroy network-reply: */
|
---|
188 | AssertPtrReturnVoid(m_pReply.data());
|
---|
189 | m_pReply->disconnect();
|
---|
190 | m_pReply->deleteLater();
|
---|
191 | m_pReply = 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void UINetworkRequest::cleanup()
|
---|
195 | {
|
---|
196 | /* Cleanup network-reply: */
|
---|
197 | cleanupNetworkReply();
|
---|
198 | }
|
---|