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