1 | /* $Id: UINetworkRequestManager.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINetworkRequestManager stuff implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2023 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 <QUrl>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UINetworkCustomer.h"
|
---|
33 | #include "UINetworkRequest.h"
|
---|
34 | #include "UINetworkRequestManager.h"
|
---|
35 |
|
---|
36 | /* Other VBox includes: */
|
---|
37 | #include "iprt/assert.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /* static */
|
---|
41 | UINetworkRequestManager *UINetworkRequestManager::s_pInstance = 0;
|
---|
42 |
|
---|
43 | /* static */
|
---|
44 | void UINetworkRequestManager::create()
|
---|
45 | {
|
---|
46 | AssertReturnVoid(!s_pInstance);
|
---|
47 | new UINetworkRequestManager;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /* static */
|
---|
51 | void UINetworkRequestManager::destroy()
|
---|
52 | {
|
---|
53 | AssertPtrReturnVoid(s_pInstance);
|
---|
54 | delete s_pInstance;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /* static */
|
---|
58 | UINetworkRequestManager *UINetworkRequestManager::instance()
|
---|
59 | {
|
---|
60 | return s_pInstance;
|
---|
61 | }
|
---|
62 |
|
---|
63 | QUuid UINetworkRequestManager::createNetworkRequest(UINetworkRequestType enmType,
|
---|
64 | const QList<QUrl> &urls,
|
---|
65 | const QString &strTarget,
|
---|
66 | const UserDictionary &requestHeaders,
|
---|
67 | UINetworkCustomer *pCustomer)
|
---|
68 | {
|
---|
69 | /* Create network-request: */
|
---|
70 | UINetworkRequest *pNetworkRequest = new UINetworkRequest(enmType, urls, strTarget, requestHeaders);
|
---|
71 | if (pNetworkRequest)
|
---|
72 | {
|
---|
73 | /* Configure request listeners: */
|
---|
74 | connect(pNetworkRequest, &UINetworkRequest::sigProgress,
|
---|
75 | this, &UINetworkRequestManager::sltHandleNetworkRequestProgress);
|
---|
76 | connect(pNetworkRequest, &UINetworkRequest::sigCanceled,
|
---|
77 | this, &UINetworkRequestManager::sltHandleNetworkRequestCancel);
|
---|
78 | connect(pNetworkRequest, &UINetworkRequest::sigFinished,
|
---|
79 | this, &UINetworkRequestManager::sltHandleNetworkRequestFinish);
|
---|
80 | connect(pNetworkRequest, &UINetworkRequest::sigFailed,
|
---|
81 | this, &UINetworkRequestManager::sltHandleNetworkRequestFailure);
|
---|
82 |
|
---|
83 | /* [Re]generate ID until unique: */
|
---|
84 | QUuid uId = QUuid::createUuid();
|
---|
85 | while (m_requests.contains(uId))
|
---|
86 | uId = QUuid::createUuid();
|
---|
87 |
|
---|
88 | /* Add request&customer to map: */
|
---|
89 | m_requests.insert(uId, pNetworkRequest);
|
---|
90 | m_customers.insert(uId, pCustomer);
|
---|
91 | connect(pCustomer, &UINetworkCustomer::sigBeingDestroyed,
|
---|
92 | this, &UINetworkRequestManager::sltHandleNetworkCustomerBeingDestroyed,
|
---|
93 | Qt::UniqueConnection);
|
---|
94 |
|
---|
95 | /* Return ID: */
|
---|
96 | return uId;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* Null ID by default: */
|
---|
100 | return QUuid();
|
---|
101 | }
|
---|
102 |
|
---|
103 | void UINetworkRequestManager::cancelNetworkRequest(const QUuid &uId)
|
---|
104 | {
|
---|
105 | /* Cleanup request: */
|
---|
106 | UINetworkRequest *pNetworkRequest = m_requests.value(uId);
|
---|
107 | if (pNetworkRequest)
|
---|
108 | pNetworkRequest->sltCancel();
|
---|
109 | }
|
---|
110 |
|
---|
111 | UINetworkRequestManager::UINetworkRequestManager()
|
---|
112 | {
|
---|
113 | s_pInstance = this;
|
---|
114 | prepare();
|
---|
115 | }
|
---|
116 |
|
---|
117 | UINetworkRequestManager::~UINetworkRequestManager()
|
---|
118 | {
|
---|
119 | cleanup();
|
---|
120 | s_pInstance = 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void UINetworkRequestManager::sltHandleNetworkRequestProgress(qint64 iReceived, qint64 iTotal)
|
---|
124 | {
|
---|
125 | /* Make sure we have this request registered: */
|
---|
126 | UINetworkRequest *pNetworkRequest = qobject_cast<UINetworkRequest*>(sender());
|
---|
127 | AssertPtrReturnVoid(pNetworkRequest);
|
---|
128 | const QUuid uId = m_requests.key(pNetworkRequest);
|
---|
129 | AssertReturnVoid(!uId.isNull());
|
---|
130 |
|
---|
131 | /* Delegate request to customer: */
|
---|
132 | UINetworkCustomer *pNetworkCustomer = m_customers.value(uId);
|
---|
133 | if (pNetworkCustomer)
|
---|
134 | pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void UINetworkRequestManager::sltHandleNetworkRequestFailure(const QString &strError)
|
---|
138 | {
|
---|
139 | /* Make sure we have this request registered: */
|
---|
140 | UINetworkRequest *pNetworkRequest = qobject_cast<UINetworkRequest*>(sender());
|
---|
141 | AssertPtrReturnVoid(pNetworkRequest);
|
---|
142 | const QUuid uId = m_requests.key(pNetworkRequest);
|
---|
143 | AssertReturnVoid(!uId.isNull());
|
---|
144 |
|
---|
145 | /* Delegate request to customer: */
|
---|
146 | UINetworkCustomer *pNetworkCustomer = m_customers.value(uId);
|
---|
147 | if (pNetworkCustomer)
|
---|
148 | pNetworkCustomer->processNetworkReplyFailed(strError);
|
---|
149 |
|
---|
150 | /* Cleanup request: */
|
---|
151 | cleanupNetworkRequest(uId);
|
---|
152 | }
|
---|
153 |
|
---|
154 | void UINetworkRequestManager::sltHandleNetworkRequestCancel()
|
---|
155 | {
|
---|
156 | /* Make sure we have this request registered: */
|
---|
157 | UINetworkRequest *pNetworkRequest = qobject_cast<UINetworkRequest*>(sender());
|
---|
158 | AssertPtrReturnVoid(pNetworkRequest);
|
---|
159 | const QUuid uId = m_requests.key(pNetworkRequest);
|
---|
160 | AssertReturnVoid(!uId.isNull());
|
---|
161 |
|
---|
162 | /* Delegate request to customer: */
|
---|
163 | UINetworkCustomer *pNetworkCustomer = m_customers.value(uId);
|
---|
164 | if (pNetworkCustomer)
|
---|
165 | pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply());
|
---|
166 |
|
---|
167 | /* Cleanup request: */
|
---|
168 | cleanupNetworkRequest(uId);
|
---|
169 | }
|
---|
170 |
|
---|
171 | void UINetworkRequestManager::sltHandleNetworkRequestFinish()
|
---|
172 | {
|
---|
173 | /* Make sure we have this request registered: */
|
---|
174 | UINetworkRequest *pNetworkRequest = qobject_cast<UINetworkRequest*>(sender());
|
---|
175 | AssertPtrReturnVoid(pNetworkRequest);
|
---|
176 | const QUuid uId = m_requests.key(pNetworkRequest);
|
---|
177 | AssertReturnVoid(!uId.isNull());
|
---|
178 |
|
---|
179 | /* Delegate request to customer: */
|
---|
180 | UINetworkCustomer *pNetworkCustomer = m_customers.value(uId);
|
---|
181 | if (pNetworkCustomer)
|
---|
182 | pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply());
|
---|
183 |
|
---|
184 | /* Cleanup request: */
|
---|
185 | cleanupNetworkRequest(uId);
|
---|
186 | }
|
---|
187 |
|
---|
188 | void UINetworkRequestManager::sltHandleNetworkCustomerBeingDestroyed(UINetworkCustomer *pNetworkCustomer)
|
---|
189 | {
|
---|
190 | /* Make sure customer was and still registered: */
|
---|
191 | const QList<QUuid> ids = m_customers.keys(pNetworkCustomer);
|
---|
192 | AssertReturnVoid(!ids.isEmpty());
|
---|
193 | /* Unregister it: */
|
---|
194 | foreach (const QUuid &uId, ids)
|
---|
195 | m_customers.remove(uId);
|
---|
196 | }
|
---|
197 |
|
---|
198 | void UINetworkRequestManager::prepare()
|
---|
199 | {
|
---|
200 | // nothing for now
|
---|
201 | }
|
---|
202 |
|
---|
203 | void UINetworkRequestManager::cleanupNetworkRequest(const QUuid &uId)
|
---|
204 | {
|
---|
205 | delete m_requests.value(uId);
|
---|
206 | m_requests.remove(uId);
|
---|
207 | }
|
---|
208 |
|
---|
209 | void UINetworkRequestManager::cleanupNetworkRequests()
|
---|
210 | {
|
---|
211 | foreach (const QUuid &uId, m_requests.keys())
|
---|
212 | cleanupNetworkRequest(uId);
|
---|
213 | }
|
---|
214 |
|
---|
215 | void UINetworkRequestManager::cleanup()
|
---|
216 | {
|
---|
217 | cleanupNetworkRequests();
|
---|
218 | }
|
---|