1 | /* $Id: UIVirtualBoxEventHandler.cpp 79365 2019-06-26 15:57:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIVirtualBoxEventHandler class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2019 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 | /* GUI includes: */
|
---|
19 | #include "UICommon.h"
|
---|
20 | #include "UIExtraDataManager.h"
|
---|
21 | #include "UIMainEventListener.h"
|
---|
22 | #include "UIVirtualBoxEventHandler.h"
|
---|
23 |
|
---|
24 | /* COM includes: */
|
---|
25 | #include "CEventListener.h"
|
---|
26 | #include "CEventSource.h"
|
---|
27 | #include "CVirtualBox.h"
|
---|
28 | #include "CVirtualBoxClient.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | /** Private QObject extension
|
---|
32 | * providing UIVirtualBoxEventHandler with the CVirtualBoxClient and CVirtualBox event-sources. */
|
---|
33 | class UIVirtualBoxEventHandlerProxy : public QObject
|
---|
34 | {
|
---|
35 | Q_OBJECT;
|
---|
36 |
|
---|
37 | signals:
|
---|
38 |
|
---|
39 | /** Notifies about the VBoxSVC become @a fAvailable. */
|
---|
40 | void sigVBoxSVCAvailabilityChange(bool fAvailable);
|
---|
41 |
|
---|
42 | /** Notifies about @a state change event for the machine with @a uId. */
|
---|
43 | void sigMachineStateChange(const QUuid &uId, const KMachineState state);
|
---|
44 | /** Notifies about data change event for the machine with @a uId. */
|
---|
45 | void sigMachineDataChange(const QUuid &uId);
|
---|
46 | /** Notifies about machine with @a uId was @a fRegistered. */
|
---|
47 | void sigMachineRegistered(const QUuid &uId, const bool fRegistered);
|
---|
48 | /** Notifies about @a state change event for the session of the machine with @a uId. */
|
---|
49 | void sigSessionStateChange(const QUuid &uId, const KSessionState state);
|
---|
50 | /** Notifies about snapshot with @a uSnapshotId was taken for the machine with @a uId. */
|
---|
51 | void sigSnapshotTake(const QUuid &uId, const QUuid &uSnapshotId);
|
---|
52 | /** Notifies about snapshot with @a uSnapshotId was deleted for the machine with @a uId. */
|
---|
53 | void sigSnapshotDelete(const QUuid &uId, const QUuid &uSnapshotId);
|
---|
54 | /** Notifies about snapshot with @a uSnapshotId was changed for the machine with @a uId. */
|
---|
55 | void sigSnapshotChange(const QUuid &uId, const QUuid &uSnapshotId);
|
---|
56 | /** Notifies about snapshot with @a uSnapshotId was restored for the machine with @a uId. */
|
---|
57 | void sigSnapshotRestore(const QUuid &uId, const QUuid &uSnapshotId);
|
---|
58 |
|
---|
59 | /** Notifies about storage controller change.
|
---|
60 | * @param uMachineId Brings the ID of machine corresponding controller belongs to.
|
---|
61 | * @param strControllerName Brings the name of controller this event is related to. */
|
---|
62 | void sigStorageControllerChange(const QUuid &uMachineId, const QString &strControllerName);
|
---|
63 | /** Notifies about storage device change.
|
---|
64 | * @param comAttachment Brings corresponding attachment.
|
---|
65 | * @param fRemoved Brings whether medium is removed or added.
|
---|
66 | * @param fSilent Brings whether this change has gone silent for guest. */
|
---|
67 | void sigStorageDeviceChange(CMediumAttachment comAttachment, bool fRemoved, bool fSilent);
|
---|
68 | /** Notifies about storage medium @a comAttachment state change. */
|
---|
69 | void sigMediumChange(CMediumAttachment comAttachment);
|
---|
70 | /** Notifies about storage @a comMedium config change. */
|
---|
71 | void sigMediumConfigChange(CMedium comMedium);
|
---|
72 | /** Notifies about storage medium is (un)registered.
|
---|
73 | * @param uMediumId Brings corresponding medium ID.
|
---|
74 | * @param enmMediumType Brings corresponding medium type.
|
---|
75 | * @param fRegistered Brings whether medium is registered or unregistered. */
|
---|
76 | void sigMediumRegistered(const QUuid &uMediumId, KDeviceType enmMediumType, bool fRegistered);
|
---|
77 |
|
---|
78 | public:
|
---|
79 |
|
---|
80 | /** Constructs event proxy object on the basis of passed @a pParent. */
|
---|
81 | UIVirtualBoxEventHandlerProxy(QObject *pParent);
|
---|
82 | /** Destructs event proxy object. */
|
---|
83 | ~UIVirtualBoxEventHandlerProxy();
|
---|
84 |
|
---|
85 | protected:
|
---|
86 |
|
---|
87 | /** @name Prepare/Cleanup cascade.
|
---|
88 | * @{ */
|
---|
89 | /** Prepares all. */
|
---|
90 | void prepare();
|
---|
91 | /** Prepares listener. */
|
---|
92 | void prepareListener();
|
---|
93 | /** Prepares connections. */
|
---|
94 | void prepareConnections();
|
---|
95 |
|
---|
96 | /** Cleanups connections. */
|
---|
97 | void cleanupConnections();
|
---|
98 | /** Cleanups listener. */
|
---|
99 | void cleanupListener();
|
---|
100 | /** Cleanups all. */
|
---|
101 | void cleanup();
|
---|
102 | /** @} */
|
---|
103 |
|
---|
104 | private:
|
---|
105 |
|
---|
106 | /** Holds the COM event source instance. */
|
---|
107 | CEventSource m_comEventSource;
|
---|
108 |
|
---|
109 | /** Holds the Qt event listener instance. */
|
---|
110 | ComObjPtr<UIMainEventListenerImpl> m_pQtListener;
|
---|
111 | /** Holds the COM event listener instance. */
|
---|
112 | CEventListener m_comEventListener;
|
---|
113 | };
|
---|
114 |
|
---|
115 |
|
---|
116 | /*********************************************************************************************************************************
|
---|
117 | * Class UIVirtualBoxEventHandlerProxy implementation. *
|
---|
118 | *********************************************************************************************************************************/
|
---|
119 |
|
---|
120 | UIVirtualBoxEventHandlerProxy::UIVirtualBoxEventHandlerProxy(QObject *pParent)
|
---|
121 | : QObject(pParent)
|
---|
122 | {
|
---|
123 | /* Prepare: */
|
---|
124 | prepare();
|
---|
125 | }
|
---|
126 |
|
---|
127 | UIVirtualBoxEventHandlerProxy::~UIVirtualBoxEventHandlerProxy()
|
---|
128 | {
|
---|
129 | /* Cleanup: */
|
---|
130 | cleanup();
|
---|
131 | }
|
---|
132 |
|
---|
133 | void UIVirtualBoxEventHandlerProxy::prepare()
|
---|
134 | {
|
---|
135 | /* Prepare: */
|
---|
136 | prepareListener();
|
---|
137 | prepareConnections();
|
---|
138 | }
|
---|
139 |
|
---|
140 | void UIVirtualBoxEventHandlerProxy::prepareListener()
|
---|
141 | {
|
---|
142 | /* Create Main event listener instance: */
|
---|
143 | m_pQtListener.createObject();
|
---|
144 | m_pQtListener->init(new UIMainEventListener, this);
|
---|
145 | m_comEventListener = CEventListener(m_pQtListener);
|
---|
146 |
|
---|
147 | /* Get VirtualBoxClient: */
|
---|
148 | const CVirtualBoxClient comVBoxClient = uiCommon().virtualBoxClient();
|
---|
149 | AssertWrapperOk(comVBoxClient);
|
---|
150 | /* Get VirtualBoxClient event source: */
|
---|
151 | CEventSource comEventSourceVBoxClient = comVBoxClient.GetEventSource();
|
---|
152 | AssertWrapperOk(comEventSourceVBoxClient);
|
---|
153 |
|
---|
154 | /* Get VirtualBox: */
|
---|
155 | const CVirtualBox comVBox = uiCommon().virtualBox();
|
---|
156 | AssertWrapperOk(comVBox);
|
---|
157 | /* Get VirtualBox event source: */
|
---|
158 | CEventSource comEventSourceVBox = comVBox.GetEventSource();
|
---|
159 | AssertWrapperOk(comEventSourceVBox);
|
---|
160 |
|
---|
161 | /* Create event source aggregator: */
|
---|
162 | m_comEventSource = comEventSourceVBoxClient.CreateAggregator(QVector<CEventSource>()
|
---|
163 | << comEventSourceVBoxClient
|
---|
164 | << comEventSourceVBox);
|
---|
165 |
|
---|
166 | /* Enumerate all the required event-types: */
|
---|
167 | QVector<KVBoxEventType> eventTypes;
|
---|
168 | eventTypes
|
---|
169 | /* For VirtualBoxClient: */
|
---|
170 | << KVBoxEventType_OnVBoxSVCAvailabilityChanged
|
---|
171 | /* For VirtualBox: */
|
---|
172 | << KVBoxEventType_OnMachineStateChanged
|
---|
173 | << KVBoxEventType_OnMachineDataChanged
|
---|
174 | << KVBoxEventType_OnMachineRegistered
|
---|
175 | << KVBoxEventType_OnSessionStateChanged
|
---|
176 | << KVBoxEventType_OnSnapshotTaken
|
---|
177 | << KVBoxEventType_OnSnapshotDeleted
|
---|
178 | << KVBoxEventType_OnSnapshotChanged
|
---|
179 | << KVBoxEventType_OnSnapshotRestored
|
---|
180 | << KVBoxEventType_OnStorageControllerChanged
|
---|
181 | << KVBoxEventType_OnStorageDeviceChanged
|
---|
182 | << KVBoxEventType_OnMediumChanged
|
---|
183 | << KVBoxEventType_OnMediumConfigChanged
|
---|
184 | << KVBoxEventType_OnMediumRegistered;
|
---|
185 |
|
---|
186 | /* Register event listener for event source aggregator: */
|
---|
187 | m_comEventSource.RegisterListener(m_comEventListener, eventTypes,
|
---|
188 | gEDataManager->eventHandlingType() == EventHandlingType_Active ? TRUE : FALSE);
|
---|
189 | AssertWrapperOk(m_comEventSource);
|
---|
190 |
|
---|
191 | /* If event listener registered as passive one: */
|
---|
192 | if (gEDataManager->eventHandlingType() == EventHandlingType_Passive)
|
---|
193 | {
|
---|
194 | /* Register event sources in their listeners as well: */
|
---|
195 | m_pQtListener->getWrapped()->registerSource(m_comEventSource, m_comEventListener);
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | void UIVirtualBoxEventHandlerProxy::prepareConnections()
|
---|
200 | {
|
---|
201 | /* Create direct (sync) connections for signals of main event listener.
|
---|
202 | * Keep in mind that the abstract Qt4 connection notation should be used here. */
|
---|
203 | connect(m_pQtListener->getWrapped(), SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
204 | this, SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
205 | Qt::DirectConnection);
|
---|
206 | connect(m_pQtListener->getWrapped(), SIGNAL(sigMachineStateChange(QUuid, KMachineState)),
|
---|
207 | this, SIGNAL(sigMachineStateChange(QUuid, KMachineState)),
|
---|
208 | Qt::DirectConnection);
|
---|
209 | connect(m_pQtListener->getWrapped(), SIGNAL(sigMachineDataChange(QUuid)),
|
---|
210 | this, SIGNAL(sigMachineDataChange(QUuid)),
|
---|
211 | Qt::DirectConnection);
|
---|
212 | connect(m_pQtListener->getWrapped(), SIGNAL(sigMachineRegistered(QUuid, bool)),
|
---|
213 | this, SIGNAL(sigMachineRegistered(QUuid, bool)),
|
---|
214 | Qt::DirectConnection);
|
---|
215 | connect(m_pQtListener->getWrapped(), SIGNAL(sigSessionStateChange(QUuid, KSessionState)),
|
---|
216 | this, SIGNAL(sigSessionStateChange(QUuid, KSessionState)),
|
---|
217 | Qt::DirectConnection);
|
---|
218 | connect(m_pQtListener->getWrapped(), SIGNAL(sigSnapshotTake(QUuid, QUuid)),
|
---|
219 | this, SIGNAL(sigSnapshotTake(QUuid, QUuid)),
|
---|
220 | Qt::DirectConnection);
|
---|
221 | connect(m_pQtListener->getWrapped(), SIGNAL(sigSnapshotDelete(QUuid, QUuid)),
|
---|
222 | this, SIGNAL(sigSnapshotDelete(QUuid, QUuid)),
|
---|
223 | Qt::DirectConnection);
|
---|
224 | connect(m_pQtListener->getWrapped(), SIGNAL(sigSnapshotChange(QUuid, QUuid)),
|
---|
225 | this, SIGNAL(sigSnapshotChange(QUuid, QUuid)),
|
---|
226 | Qt::DirectConnection);
|
---|
227 | connect(m_pQtListener->getWrapped(), SIGNAL(sigSnapshotRestore(QUuid, QUuid)),
|
---|
228 | this, SIGNAL(sigSnapshotRestore(QUuid, QUuid)),
|
---|
229 | Qt::DirectConnection);
|
---|
230 | connect(m_pQtListener->getWrapped(), SIGNAL(sigStorageControllerChange(QUuid, QString)),
|
---|
231 | this, SIGNAL(sigStorageControllerChange(QUuid, QString)),
|
---|
232 | Qt::DirectConnection);
|
---|
233 | connect(m_pQtListener->getWrapped(), SIGNAL(sigStorageDeviceChange(CMediumAttachment, bool, bool)),
|
---|
234 | this, SIGNAL(sigStorageDeviceChange(CMediumAttachment, bool, bool)),
|
---|
235 | Qt::DirectConnection);
|
---|
236 | connect(m_pQtListener->getWrapped(), SIGNAL(sigMediumChange(CMediumAttachment)),
|
---|
237 | this, SIGNAL(sigMediumChange(CMediumAttachment)),
|
---|
238 | Qt::DirectConnection);
|
---|
239 | connect(m_pQtListener->getWrapped(), SIGNAL(sigMediumConfigChange(CMedium)),
|
---|
240 | this, SIGNAL(sigMediumConfigChange(CMedium)),
|
---|
241 | Qt::DirectConnection);
|
---|
242 | connect(m_pQtListener->getWrapped(), SIGNAL(sigMediumRegistered(QUuid, KDeviceType, bool)),
|
---|
243 | this, SIGNAL(sigMediumRegistered(QUuid, KDeviceType, bool)),
|
---|
244 | Qt::DirectConnection);
|
---|
245 | }
|
---|
246 |
|
---|
247 | void UIVirtualBoxEventHandlerProxy::cleanupConnections()
|
---|
248 | {
|
---|
249 | /* Nothing for now. */
|
---|
250 | }
|
---|
251 |
|
---|
252 | void UIVirtualBoxEventHandlerProxy::cleanupListener()
|
---|
253 | {
|
---|
254 | /* If event listener registered as passive one: */
|
---|
255 | if (gEDataManager->eventHandlingType() == EventHandlingType_Passive)
|
---|
256 | {
|
---|
257 | /* Unregister everything: */
|
---|
258 | m_pQtListener->getWrapped()->unregisterSources();
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* Unregister event listener for event source aggregator: */
|
---|
262 | m_comEventSource.UnregisterListener(m_comEventListener);
|
---|
263 | m_comEventSource.detach();
|
---|
264 | }
|
---|
265 |
|
---|
266 | void UIVirtualBoxEventHandlerProxy::cleanup()
|
---|
267 | {
|
---|
268 | /* Cleanup: */
|
---|
269 | cleanupConnections();
|
---|
270 | cleanupListener();
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | /*********************************************************************************************************************************
|
---|
275 | * Class UIVirtualBoxEventHandler implementation. *
|
---|
276 | *********************************************************************************************************************************/
|
---|
277 |
|
---|
278 | /* static */
|
---|
279 | UIVirtualBoxEventHandler *UIVirtualBoxEventHandler::s_pInstance = 0;
|
---|
280 |
|
---|
281 | /* static */
|
---|
282 | UIVirtualBoxEventHandler *UIVirtualBoxEventHandler::instance()
|
---|
283 | {
|
---|
284 | if (!s_pInstance)
|
---|
285 | s_pInstance = new UIVirtualBoxEventHandler;
|
---|
286 | return s_pInstance;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* static */
|
---|
290 | void UIVirtualBoxEventHandler::destroy()
|
---|
291 | {
|
---|
292 | if (s_pInstance)
|
---|
293 | {
|
---|
294 | delete s_pInstance;
|
---|
295 | s_pInstance = 0;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | UIVirtualBoxEventHandler::UIVirtualBoxEventHandler()
|
---|
300 | : m_pProxy(new UIVirtualBoxEventHandlerProxy(this))
|
---|
301 | {
|
---|
302 | /* Prepare: */
|
---|
303 | prepare();
|
---|
304 | }
|
---|
305 |
|
---|
306 | void UIVirtualBoxEventHandler::prepare()
|
---|
307 | {
|
---|
308 | /* Prepare connections: */
|
---|
309 | prepareConnections();
|
---|
310 | }
|
---|
311 |
|
---|
312 | void UIVirtualBoxEventHandler::prepareConnections()
|
---|
313 | {
|
---|
314 | /* Create queued (async) connections for signals of event proxy object.
|
---|
315 | * Keep in mind that the abstract Qt4 connection notation should be used here. */
|
---|
316 | connect(m_pProxy, SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
317 | this, SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
318 | Qt::QueuedConnection);
|
---|
319 | connect(m_pProxy, SIGNAL(sigMachineStateChange(QUuid, KMachineState)),
|
---|
320 | this, SIGNAL(sigMachineStateChange(QUuid, KMachineState)),
|
---|
321 | Qt::QueuedConnection);
|
---|
322 | connect(m_pProxy, SIGNAL(sigMachineDataChange(QUuid)),
|
---|
323 | this, SIGNAL(sigMachineDataChange(QUuid)),
|
---|
324 | Qt::QueuedConnection);
|
---|
325 | connect(m_pProxy, SIGNAL(sigMachineRegistered(QUuid, bool)),
|
---|
326 | this, SIGNAL(sigMachineRegistered(QUuid, bool)),
|
---|
327 | Qt::QueuedConnection);
|
---|
328 | connect(m_pProxy, SIGNAL(sigSessionStateChange(QUuid, KSessionState)),
|
---|
329 | this, SIGNAL(sigSessionStateChange(QUuid, KSessionState)),
|
---|
330 | Qt::QueuedConnection);
|
---|
331 | connect(m_pProxy, SIGNAL(sigSnapshotTake(QUuid, QUuid)),
|
---|
332 | this, SIGNAL(sigSnapshotTake(QUuid, QUuid)),
|
---|
333 | Qt::QueuedConnection);
|
---|
334 | connect(m_pProxy, SIGNAL(sigSnapshotDelete(QUuid, QUuid)),
|
---|
335 | this, SIGNAL(sigSnapshotDelete(QUuid, QUuid)),
|
---|
336 | Qt::QueuedConnection);
|
---|
337 | connect(m_pProxy, SIGNAL(sigSnapshotChange(QUuid, QUuid)),
|
---|
338 | this, SIGNAL(sigSnapshotChange(QUuid, QUuid)),
|
---|
339 | Qt::QueuedConnection);
|
---|
340 | connect(m_pProxy, SIGNAL(sigSnapshotRestore(QUuid, QUuid)),
|
---|
341 | this, SIGNAL(sigSnapshotRestore(QUuid, QUuid)),
|
---|
342 | Qt::QueuedConnection);
|
---|
343 | connect(m_pProxy, SIGNAL(sigStorageControllerChange(QUuid, QString)),
|
---|
344 | this, SIGNAL(sigStorageControllerChange(QUuid, QString)),
|
---|
345 | Qt::QueuedConnection);
|
---|
346 | connect(m_pProxy, SIGNAL(sigStorageDeviceChange(CMediumAttachment, bool, bool)),
|
---|
347 | this, SIGNAL(sigStorageDeviceChange(CMediumAttachment, bool, bool)),
|
---|
348 | Qt::QueuedConnection);
|
---|
349 | connect(m_pProxy, SIGNAL(sigMediumChange(CMediumAttachment)),
|
---|
350 | this, SIGNAL(sigMediumChange(CMediumAttachment)),
|
---|
351 | Qt::QueuedConnection);
|
---|
352 | connect(m_pProxy, SIGNAL(sigMediumConfigChange(CMedium)),
|
---|
353 | this, SIGNAL(sigMediumConfigChange(CMedium)),
|
---|
354 | Qt::QueuedConnection);
|
---|
355 | connect(m_pProxy, SIGNAL(sigMediumRegistered(QUuid, KDeviceType, bool)),
|
---|
356 | this, SIGNAL(sigMediumRegistered(QUuid, KDeviceType, bool)),
|
---|
357 | Qt::QueuedConnection);
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | #include "UIVirtualBoxEventHandler.moc"
|
---|
362 |
|
---|