1 | /* $Id: UIVirtualBoxClientEventHandler.cpp 103771 2024-03-11 15:16:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIVirtualBoxClientEventHandler class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-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 | /* GUI includes: */
|
---|
29 | #include "UIExtraDataManager.h"
|
---|
30 | #include "UIGlobalSession.h"
|
---|
31 | #include "UIMainEventListener.h"
|
---|
32 | #include "UIVirtualBoxClientEventHandler.h"
|
---|
33 |
|
---|
34 | /* COM includes: */
|
---|
35 | #include "CEventListener.h"
|
---|
36 | #include "CEventSource.h"
|
---|
37 | #include "CVirtualBoxClient.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /** Private QObject extension providing UIVirtualBoxClientEventHandler with CVirtualBoxClient event-source. */
|
---|
41 | class UIVirtualBoxClientEventHandlerProxy : public QObject
|
---|
42 | {
|
---|
43 | Q_OBJECT;
|
---|
44 |
|
---|
45 | signals:
|
---|
46 |
|
---|
47 | /** Notifies about the VBoxSVC become @a fAvailable. */
|
---|
48 | void sigVBoxSVCAvailabilityChange(bool fAvailable);
|
---|
49 |
|
---|
50 | public:
|
---|
51 |
|
---|
52 | /** Constructs event proxy object on the basis of passed @a pParent. */
|
---|
53 | UIVirtualBoxClientEventHandlerProxy(QObject *pParent);
|
---|
54 | /** Destructs event proxy object. */
|
---|
55 | ~UIVirtualBoxClientEventHandlerProxy();
|
---|
56 |
|
---|
57 | protected:
|
---|
58 |
|
---|
59 | /** @name Prepare/Cleanup cascade.
|
---|
60 | * @{ */
|
---|
61 | /** Prepares all. */
|
---|
62 | void prepare();
|
---|
63 | /** Prepares listener. */
|
---|
64 | void prepareListener();
|
---|
65 | /** Prepares connections. */
|
---|
66 | void prepareConnections();
|
---|
67 |
|
---|
68 | /** Cleanups connections. */
|
---|
69 | void cleanupConnections();
|
---|
70 | /** Cleanups listener. */
|
---|
71 | void cleanupListener();
|
---|
72 | /** Cleanups all. */
|
---|
73 | void cleanup();
|
---|
74 | /** @} */
|
---|
75 |
|
---|
76 | private:
|
---|
77 |
|
---|
78 | /** Holds the COM event source instance. */
|
---|
79 | CEventSource m_comEventSource;
|
---|
80 |
|
---|
81 | /** Holds the Qt event listener instance. */
|
---|
82 | ComObjPtr<UIMainEventListenerImpl> m_pQtListener;
|
---|
83 | /** Holds the COM event listener instance. */
|
---|
84 | CEventListener m_comEventListener;
|
---|
85 | };
|
---|
86 |
|
---|
87 |
|
---|
88 | /*********************************************************************************************************************************
|
---|
89 | * Class UIVirtualBoxClientEventHandlerProxy implementation. *
|
---|
90 | *********************************************************************************************************************************/
|
---|
91 |
|
---|
92 | UIVirtualBoxClientEventHandlerProxy::UIVirtualBoxClientEventHandlerProxy(QObject *pParent)
|
---|
93 | : QObject(pParent)
|
---|
94 | {
|
---|
95 | /* Prepare: */
|
---|
96 | prepare();
|
---|
97 | }
|
---|
98 |
|
---|
99 | UIVirtualBoxClientEventHandlerProxy::~UIVirtualBoxClientEventHandlerProxy()
|
---|
100 | {
|
---|
101 | /* Cleanup: */
|
---|
102 | cleanup();
|
---|
103 | }
|
---|
104 |
|
---|
105 | void UIVirtualBoxClientEventHandlerProxy::prepare()
|
---|
106 | {
|
---|
107 | /* Prepare: */
|
---|
108 | prepareListener();
|
---|
109 | prepareConnections();
|
---|
110 | }
|
---|
111 |
|
---|
112 | void UIVirtualBoxClientEventHandlerProxy::prepareListener()
|
---|
113 | {
|
---|
114 | /* Create Main event listener instance: */
|
---|
115 | m_pQtListener.createObject();
|
---|
116 | m_pQtListener->init(new UIMainEventListener, this);
|
---|
117 | m_comEventListener = CEventListener(m_pQtListener);
|
---|
118 |
|
---|
119 | /* Get VirtualBoxClient: */
|
---|
120 | const CVirtualBoxClient comVBoxClient = gpGlobalSession->virtualBoxClient();
|
---|
121 | /* Get VirtualBoxClient event source: */
|
---|
122 | m_comEventSource = comVBoxClient.GetEventSource();
|
---|
123 | Assert(comVBoxClient.isOk());
|
---|
124 |
|
---|
125 | /* Enumerate all the required event-types: */
|
---|
126 | QVector<KVBoxEventType> eventTypes;
|
---|
127 | eventTypes
|
---|
128 | << KVBoxEventType_OnVBoxSVCAvailabilityChanged;
|
---|
129 |
|
---|
130 | /* Register event listener for event source aggregator: */
|
---|
131 | m_comEventSource.RegisterListener(m_comEventListener, eventTypes, FALSE /* active? */);
|
---|
132 | Assert(m_comEventSource.isOk());
|
---|
133 |
|
---|
134 | /* Register event sources in their listeners as well: */
|
---|
135 | m_pQtListener->getWrapped()->registerSource(m_comEventSource, m_comEventListener);
|
---|
136 | }
|
---|
137 |
|
---|
138 | void UIVirtualBoxClientEventHandlerProxy::prepareConnections()
|
---|
139 | {
|
---|
140 | /* Create direct (sync) connections for signals of main event listener.
|
---|
141 | * Keep in mind that the abstract Qt4 connection notation should be used here. */
|
---|
142 | connect(m_pQtListener->getWrapped(), SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
143 | this, SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
144 | Qt::DirectConnection);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void UIVirtualBoxClientEventHandlerProxy::cleanupConnections()
|
---|
148 | {
|
---|
149 | /* Nothing for now. */
|
---|
150 | }
|
---|
151 |
|
---|
152 | void UIVirtualBoxClientEventHandlerProxy::cleanupListener()
|
---|
153 | {
|
---|
154 | /* Unregister everything: */
|
---|
155 | m_pQtListener->getWrapped()->unregisterSources();
|
---|
156 |
|
---|
157 | /* Unregister event listener for event source aggregator: */
|
---|
158 | m_comEventSource.UnregisterListener(m_comEventListener);
|
---|
159 | m_comEventSource.detach();
|
---|
160 | }
|
---|
161 |
|
---|
162 | void UIVirtualBoxClientEventHandlerProxy::cleanup()
|
---|
163 | {
|
---|
164 | /* Cleanup: */
|
---|
165 | cleanupConnections();
|
---|
166 | cleanupListener();
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /*********************************************************************************************************************************
|
---|
171 | * Class UIVirtualBoxClientEventHandler implementation. *
|
---|
172 | *********************************************************************************************************************************/
|
---|
173 |
|
---|
174 | /* static */
|
---|
175 | UIVirtualBoxClientEventHandler *UIVirtualBoxClientEventHandler::s_pInstance = 0;
|
---|
176 |
|
---|
177 | /* static */
|
---|
178 | UIVirtualBoxClientEventHandler *UIVirtualBoxClientEventHandler::instance()
|
---|
179 | {
|
---|
180 | if (!s_pInstance)
|
---|
181 | s_pInstance = new UIVirtualBoxClientEventHandler;
|
---|
182 | return s_pInstance;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* static */
|
---|
186 | void UIVirtualBoxClientEventHandler::destroy()
|
---|
187 | {
|
---|
188 | if (s_pInstance)
|
---|
189 | {
|
---|
190 | delete s_pInstance;
|
---|
191 | s_pInstance = 0;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | UIVirtualBoxClientEventHandler::UIVirtualBoxClientEventHandler()
|
---|
196 | : m_pProxy(new UIVirtualBoxClientEventHandlerProxy(this))
|
---|
197 | {
|
---|
198 | /* Prepare: */
|
---|
199 | prepare();
|
---|
200 | }
|
---|
201 |
|
---|
202 | void UIVirtualBoxClientEventHandler::prepare()
|
---|
203 | {
|
---|
204 | /* Prepare connections: */
|
---|
205 | prepareConnections();
|
---|
206 | }
|
---|
207 |
|
---|
208 | void UIVirtualBoxClientEventHandler::prepareConnections()
|
---|
209 | {
|
---|
210 | /* Create queued (async) connections for signals of event proxy object.
|
---|
211 | * Keep in mind that the abstract Qt4 connection notation should be used here. */
|
---|
212 | connect(m_pProxy, SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
213 | this, SIGNAL(sigVBoxSVCAvailabilityChange(bool)),
|
---|
214 | Qt::QueuedConnection);
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | #include "UIVirtualBoxClientEventHandler.moc"
|
---|
219 |
|
---|