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