1 | /* $Id: UINetworkManager.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UINetworkManager stuff implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 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 | /* Qt includes: */
|
---|
19 | #include <QWidget>
|
---|
20 | #include <QUrl>
|
---|
21 |
|
---|
22 | /* GUI includes: */
|
---|
23 | #include "UICommon.h"
|
---|
24 | #include "UINetworkCustomer.h"
|
---|
25 | #include "UINetworkManager.h"
|
---|
26 | #include "UINetworkManagerDialog.h"
|
---|
27 | #include "UINetworkManagerIndicator.h"
|
---|
28 | #include "UINetworkRequest.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | UINetworkManager* UINetworkManager::s_pInstance = 0;
|
---|
32 |
|
---|
33 | void UINetworkManager::create()
|
---|
34 | {
|
---|
35 | /* Check that instance do NOT exist: */
|
---|
36 | if (s_pInstance)
|
---|
37 | return;
|
---|
38 |
|
---|
39 | /* Create instance: */
|
---|
40 | new UINetworkManager;
|
---|
41 |
|
---|
42 | /* Prepare instance: */
|
---|
43 | s_pInstance->prepare();
|
---|
44 | }
|
---|
45 |
|
---|
46 | void UINetworkManager::destroy()
|
---|
47 | {
|
---|
48 | /* Check that instance exists: */
|
---|
49 | if (!s_pInstance)
|
---|
50 | return;
|
---|
51 |
|
---|
52 | /* Cleanup instance: */
|
---|
53 | s_pInstance->cleanup();
|
---|
54 |
|
---|
55 | /* Destroy instance: */
|
---|
56 | delete s_pInstance;
|
---|
57 | }
|
---|
58 |
|
---|
59 | UINetworkManagerDialog *UINetworkManager::window() const
|
---|
60 | {
|
---|
61 | return m_pNetworkManagerDialog;
|
---|
62 | }
|
---|
63 |
|
---|
64 | UINetworkManagerIndicator *UINetworkManager::createIndicator() const
|
---|
65 | {
|
---|
66 | /* For Selector UI only: */
|
---|
67 | AssertReturn(uiCommon().uiType() == UICommon::UIType_SelectorUI, 0);
|
---|
68 |
|
---|
69 | /* Create network-manager state-indicator: */
|
---|
70 | UINetworkManagerIndicator *pNetworkManagerIndicator = new UINetworkManagerIndicator;
|
---|
71 | connect(pNetworkManagerIndicator, &UINetworkManagerIndicator::sigMouseDoubleClick,
|
---|
72 | this, &UINetworkManager::show);
|
---|
73 | connect(this, &UINetworkManager::sigAddNetworkManagerIndicatorDescription,
|
---|
74 | pNetworkManagerIndicator, &UINetworkManagerIndicator::sltAddNetworkManagerIndicatorDescription);
|
---|
75 | connect(this, &UINetworkManager::sigRemoveNetworkManagerIndicatorDescription,
|
---|
76 | pNetworkManagerIndicator, &UINetworkManagerIndicator::sldRemoveNetworkManagerIndicatorDescription);
|
---|
77 | return pNetworkManagerIndicator;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void UINetworkManager::registerNetworkRequest(UINetworkRequest *pNetworkRequest)
|
---|
81 | {
|
---|
82 | /* Add network-request widget to network-manager dialog: */
|
---|
83 | m_pNetworkManagerDialog->addNetworkRequestWidget(pNetworkRequest);
|
---|
84 |
|
---|
85 | /* Add network-request description to network-manager state-indicators: */
|
---|
86 | emit sigAddNetworkManagerIndicatorDescription(pNetworkRequest);
|
---|
87 | }
|
---|
88 |
|
---|
89 | void UINetworkManager::unregisterNetworkRequest(const QUuid &uuid)
|
---|
90 | {
|
---|
91 | /* Remove network-request description from network-manager state-indicator: */
|
---|
92 | emit sigRemoveNetworkManagerIndicatorDescription(uuid);
|
---|
93 |
|
---|
94 | /* Remove network-request widget from network-manager dialog: */
|
---|
95 | m_pNetworkManagerDialog->removeNetworkRequestWidget(uuid);
|
---|
96 | }
|
---|
97 |
|
---|
98 | void UINetworkManager::show()
|
---|
99 | {
|
---|
100 | /* Show network-manager dialog: */
|
---|
101 | m_pNetworkManagerDialog->showNormal();
|
---|
102 | }
|
---|
103 |
|
---|
104 | void UINetworkManager::createNetworkRequest(UINetworkRequestType enmType, const QList<QUrl> &urls, const QString &strTarget,
|
---|
105 | const UserDictionary &requestHeaders, UINetworkCustomer *pCustomer)
|
---|
106 | {
|
---|
107 | /* Create network-request: */
|
---|
108 | UINetworkRequest *pNetworkRequest = new UINetworkRequest(enmType, urls, strTarget, requestHeaders, pCustomer, this);
|
---|
109 | /* Prepare created network-request: */
|
---|
110 | prepareNetworkRequest(pNetworkRequest);
|
---|
111 | }
|
---|
112 |
|
---|
113 | UINetworkManager::UINetworkManager()
|
---|
114 | : m_pNetworkManagerDialog(0)
|
---|
115 | {
|
---|
116 | /* Prepare instance: */
|
---|
117 | s_pInstance = this;
|
---|
118 | }
|
---|
119 |
|
---|
120 | UINetworkManager::~UINetworkManager()
|
---|
121 | {
|
---|
122 | /* Cleanup instance: */
|
---|
123 | s_pInstance = 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void UINetworkManager::prepare()
|
---|
127 | {
|
---|
128 | /* Prepare network-manager dialog: */
|
---|
129 | m_pNetworkManagerDialog = new UINetworkManagerDialog;
|
---|
130 | connect(m_pNetworkManagerDialog, &UINetworkManagerDialog::sigCancelNetworkRequests, this, &UINetworkManager::sigCancelNetworkRequests);
|
---|
131 | }
|
---|
132 |
|
---|
133 | void UINetworkManager::cleanup()
|
---|
134 | {
|
---|
135 | /* Cleanup network-requests first: */
|
---|
136 | cleanupNetworkRequests();
|
---|
137 |
|
---|
138 | /* Cleanup network-manager dialog: */
|
---|
139 | delete m_pNetworkManagerDialog;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void UINetworkManager::prepareNetworkRequest(UINetworkRequest *pNetworkRequest)
|
---|
143 | {
|
---|
144 | /* Prepare listeners for network-request: */
|
---|
145 | connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid&, qint64, qint64)>(&UINetworkRequest::sigProgress),
|
---|
146 | this, &UINetworkManager::sltHandleNetworkRequestProgress);
|
---|
147 | connect(pNetworkRequest, &UINetworkRequest::sigCanceled,
|
---|
148 | this, &UINetworkManager::sltHandleNetworkRequestCancel);
|
---|
149 | connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid&)>(&UINetworkRequest::sigFinished),
|
---|
150 | this, &UINetworkManager::sltHandleNetworkRequestFinish);
|
---|
151 | connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid &uuid, const QString &strError)>(&UINetworkRequest::sigFailed),
|
---|
152 | this, &UINetworkManager::sltHandleNetworkRequestFailure);
|
---|
153 |
|
---|
154 | /* Add network-request into map: */
|
---|
155 | m_requests.insert(pNetworkRequest->uuid(), pNetworkRequest);
|
---|
156 | }
|
---|
157 |
|
---|
158 | void UINetworkManager::cleanupNetworkRequest(QUuid uuid)
|
---|
159 | {
|
---|
160 | /* Delete network-request from map: */
|
---|
161 | delete m_requests[uuid];
|
---|
162 | m_requests.remove(uuid);
|
---|
163 | }
|
---|
164 |
|
---|
165 | void UINetworkManager::cleanupNetworkRequests()
|
---|
166 | {
|
---|
167 | /* Get all the request IDs: */
|
---|
168 | const QList<QUuid> &uuids = m_requests.keys();
|
---|
169 | /* Cleanup corresponding requests: */
|
---|
170 | for (int i = 0; i < uuids.size(); ++i)
|
---|
171 | cleanupNetworkRequest(uuids[i]);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void UINetworkManager::sltHandleNetworkRequestProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
|
---|
175 | {
|
---|
176 | /* Make sure corresponding map contains received ID: */
|
---|
177 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
178 |
|
---|
179 | /* Get corresponding network-request: */
|
---|
180 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
181 |
|
---|
182 | /* Get corresponding customer: */
|
---|
183 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
184 |
|
---|
185 | /* Send to customer to process: */
|
---|
186 | pNetworkCustomer->processNetworkReplyProgress(iReceived, iTotal);
|
---|
187 | }
|
---|
188 |
|
---|
189 | void UINetworkManager::sltHandleNetworkRequestCancel(const QUuid &uuid)
|
---|
190 | {
|
---|
191 | /* Make sure corresponding map contains received ID: */
|
---|
192 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
193 |
|
---|
194 | /* Get corresponding network-request: */
|
---|
195 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
196 |
|
---|
197 | /* Get corresponding customer: */
|
---|
198 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
199 |
|
---|
200 | /* Send to customer to process: */
|
---|
201 | pNetworkCustomer->processNetworkReplyCanceled(pNetworkRequest->reply());
|
---|
202 |
|
---|
203 | /* Cleanup network-request: */
|
---|
204 | cleanupNetworkRequest(uuid);
|
---|
205 | }
|
---|
206 |
|
---|
207 | void UINetworkManager::sltHandleNetworkRequestFinish(const QUuid &uuid)
|
---|
208 | {
|
---|
209 | /* Make sure corresponding map contains received ID: */
|
---|
210 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
211 |
|
---|
212 | /* Get corresponding network-request: */
|
---|
213 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
214 |
|
---|
215 | /* Get corresponding customer: */
|
---|
216 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
217 |
|
---|
218 | /* Send to customer to process: */
|
---|
219 | pNetworkCustomer->processNetworkReplyFinished(pNetworkRequest->reply());
|
---|
220 |
|
---|
221 | /* Cleanup network-request: */
|
---|
222 | cleanupNetworkRequest(uuid);
|
---|
223 | }
|
---|
224 |
|
---|
225 | void UINetworkManager::sltHandleNetworkRequestFailure(const QUuid &uuid, const QString &)
|
---|
226 | {
|
---|
227 | /* Make sure corresponding map contains received ID: */
|
---|
228 | AssertMsg(m_requests.contains(uuid), ("Network-request NOT found!\n"));
|
---|
229 |
|
---|
230 | /* Get corresponding network-request: */
|
---|
231 | UINetworkRequest *pNetworkRequest = m_requests.value(uuid);
|
---|
232 |
|
---|
233 | /* Get corresponding customer: */
|
---|
234 | UINetworkCustomer *pNetworkCustomer = pNetworkRequest->customer();
|
---|
235 |
|
---|
236 | /* If customer made a force-call: */
|
---|
237 | if (pNetworkCustomer->isItForceCall())
|
---|
238 | {
|
---|
239 | /* Just show the dialog: */
|
---|
240 | show();
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|