1 | /* $Id: UIExtraDataEventHandler.cpp 41689 2012-06-13 17:13:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * UIExtraDataEventHandler class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Qt includes: */
|
---|
21 | #include <QMutex>
|
---|
22 |
|
---|
23 | /* GUI includes: */
|
---|
24 | #include "UIExtraDataEventHandler.h"
|
---|
25 | #include "UIMainEventListener.h"
|
---|
26 | #include "VBoxGlobal.h"
|
---|
27 | #include "VBoxGlobalSettings.h"
|
---|
28 |
|
---|
29 | /* COM includes: */
|
---|
30 | #include "COMEnums.h"
|
---|
31 | #include "CEventSource.h"
|
---|
32 |
|
---|
33 | class UIExtraDataEventHandlerPrivate: public QObject
|
---|
34 | {
|
---|
35 | Q_OBJECT;
|
---|
36 |
|
---|
37 | public:
|
---|
38 | UIExtraDataEventHandlerPrivate(QObject *pParent = 0)
|
---|
39 | : QObject(pParent)
|
---|
40 | , m_fIsRegDlgOwner(false)
|
---|
41 | , m_fIsUpdDlgOwner(false)
|
---|
42 | #ifdef VBOX_GUI_WITH_SYSTRAY
|
---|
43 | , m_fIsTrayIconOwner(false)
|
---|
44 | #endif /* VBOX_GUI_WITH_SYSTRAY */
|
---|
45 | {}
|
---|
46 |
|
---|
47 | public slots:
|
---|
48 |
|
---|
49 | void sltExtraDataCanChange(QString strId, QString strKey, QString strValue, bool &fVeto, QString &strVetoReason)
|
---|
50 | {
|
---|
51 | if (strId.isEmpty())
|
---|
52 | {
|
---|
53 | /* it's a global extra data key someone wants to change */
|
---|
54 | if (strKey.startsWith("GUI/"))
|
---|
55 | {
|
---|
56 | if (strKey == GUI_RegistrationDlgWinID)
|
---|
57 | {
|
---|
58 | if (m_fIsRegDlgOwner)
|
---|
59 | {
|
---|
60 | if (!(strValue.isEmpty() ||
|
---|
61 | strValue == QString("%1")
|
---|
62 | .arg((qulonglong)vboxGlobal().mainWindow()->winId())))
|
---|
63 | fVeto = true;
|
---|
64 | }
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | #ifdef VBOX_GUI_WITH_SYSTRAY
|
---|
69 | if (strKey == GUI_TrayIconWinID)
|
---|
70 | {
|
---|
71 | if (m_fIsTrayIconOwner)
|
---|
72 | {
|
---|
73 | if (!(strValue.isEmpty() ||
|
---|
74 | strValue == QString("%1")
|
---|
75 | .arg((qulonglong)vboxGlobal().mainWindow()->winId())))
|
---|
76 | fVeto = true;
|
---|
77 | }
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 | /* Try to set the global setting to check its syntax */
|
---|
82 | VBoxGlobalSettings gs(false /* non-null */);
|
---|
83 | if (gs.setPublicProperty (strKey, strValue))
|
---|
84 | {
|
---|
85 | /* this is a known GUI property key */
|
---|
86 | if (!gs)
|
---|
87 | {
|
---|
88 | strVetoReason = gs.lastError();
|
---|
89 | /* disallow the change when there is an error*/
|
---|
90 | fVeto = true;
|
---|
91 | }
|
---|
92 | return;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | void sltExtraDataChange(QString strId, QString strKey, QString strValue)
|
---|
99 | {
|
---|
100 | if (strId.isEmpty())
|
---|
101 | {
|
---|
102 | if (strKey.startsWith ("GUI/"))
|
---|
103 | {
|
---|
104 | if (strKey == GUI_RegistrationDlgWinID)
|
---|
105 | {
|
---|
106 | if (strValue.isEmpty())
|
---|
107 | {
|
---|
108 | m_fIsRegDlgOwner = false;
|
---|
109 | emit sigCanShowRegistrationDlg(true);
|
---|
110 | }
|
---|
111 | else if (strValue == QString("%1")
|
---|
112 | .arg((qulonglong)vboxGlobal().mainWindow()->winId()))
|
---|
113 | {
|
---|
114 | m_fIsRegDlgOwner = true;
|
---|
115 | emit sigCanShowRegistrationDlg(true);
|
---|
116 | }
|
---|
117 | else
|
---|
118 | emit sigCanShowRegistrationDlg(false);
|
---|
119 | }
|
---|
120 | if (strKey == GUI_LanguageId)
|
---|
121 | emit sigGUILanguageChange(strValue);
|
---|
122 | #ifdef VBOX_GUI_WITH_SYSTRAY
|
---|
123 | if (strKey == GUI_MainWindowCount)
|
---|
124 | emit sigMainWindowCountChange(strValue.toInt());
|
---|
125 | if (strKey == GUI_TrayIconWinID)
|
---|
126 | {
|
---|
127 | if (strValue.isEmpty())
|
---|
128 | {
|
---|
129 | m_fIsTrayIconOwner = false;
|
---|
130 | emit sigCanShowTrayIcon(true);
|
---|
131 | }
|
---|
132 | else if (strValue == QString("%1")
|
---|
133 | .arg((qulonglong)vboxGlobal().mainWindow()->winId()))
|
---|
134 | {
|
---|
135 | m_fIsTrayIconOwner = true;
|
---|
136 | emit sigCanShowTrayIcon(true);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | emit sigCanShowTrayIcon(false);
|
---|
140 | }
|
---|
141 | if (strKey == GUI_TrayIconEnabled)
|
---|
142 | emit sigTrayIconChange((strValue.toLower() == "true") ? true : false);
|
---|
143 | #endif /* VBOX_GUI_WITH_SYSTRAY */
|
---|
144 | #ifdef Q_WS_MAC
|
---|
145 | if (strKey == GUI_PresentationModeEnabled)
|
---|
146 | {
|
---|
147 | /* Default to true if it is an empty value */
|
---|
148 | QString testStr = strValue.toLower();
|
---|
149 | bool f = (testStr.isEmpty() || testStr == "false");
|
---|
150 | emit sigPresentationModeChange(f);
|
---|
151 | }
|
---|
152 | #endif /* Q_WS_MAC */
|
---|
153 |
|
---|
154 | m_mutex.lock();
|
---|
155 | vboxGlobal().settings().setPublicProperty(strKey, strValue);
|
---|
156 | m_mutex.unlock();
|
---|
157 | Assert(!!vboxGlobal().settings());
|
---|
158 | }
|
---|
159 | }
|
---|
160 | #ifdef Q_WS_MAC
|
---|
161 | else if (vboxGlobal().isVMConsoleProcess())
|
---|
162 | {
|
---|
163 | /* Check for the currently running machine */
|
---|
164 | if (strId == vboxGlobal().managedVMUuid())
|
---|
165 | {
|
---|
166 | if ( strKey == GUI_RealtimeDockIconUpdateEnabled
|
---|
167 | || strKey == GUI_RealtimeDockIconUpdateMonitor)
|
---|
168 | {
|
---|
169 | bool f = strValue.toLower() == "false" ? false : true;
|
---|
170 | emit sigDockIconAppearanceChange(f);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | #endif /* Q_WS_MAC */
|
---|
175 | }
|
---|
176 |
|
---|
177 | signals:
|
---|
178 | void sigCanShowRegistrationDlg(bool fEnabled);
|
---|
179 | void sigGUILanguageChange(QString strLang);
|
---|
180 | #ifdef VBOX_GUI_WITH_SYSTRAY
|
---|
181 | void sigMainWindowCountChange(int count);
|
---|
182 | void sigCanShowTrayIcon(bool fEnabled);
|
---|
183 | void sigTrayIconChange(bool fEnabled);
|
---|
184 | #endif /* VBOX_GUI_WITH_SYSTRAY */
|
---|
185 | #ifdef RT_OS_DARWIN
|
---|
186 | void sigPresentationModeChange(bool fEnabled);
|
---|
187 | void sigDockIconAppearanceChange(bool fEnabled);
|
---|
188 | #endif /* RT_OS_DARWIN */
|
---|
189 |
|
---|
190 | private:
|
---|
191 | /** protects #OnExtraDataChange() */
|
---|
192 | QMutex m_mutex;
|
---|
193 |
|
---|
194 | /* Private member vars */
|
---|
195 | bool m_fIsRegDlgOwner;
|
---|
196 | bool m_fIsUpdDlgOwner;
|
---|
197 | #ifdef VBOX_GUI_WITH_SYSTRAY
|
---|
198 | bool m_fIsTrayIconOwner;
|
---|
199 | #endif /* VBOX_GUI_WITH_SYSTRAY */
|
---|
200 | };
|
---|
201 |
|
---|
202 | /* static */
|
---|
203 | UIExtraDataEventHandler *UIExtraDataEventHandler::m_pInstance = 0;
|
---|
204 |
|
---|
205 | /* static */
|
---|
206 | UIExtraDataEventHandler* UIExtraDataEventHandler::instance()
|
---|
207 | {
|
---|
208 | if (!m_pInstance)
|
---|
209 | m_pInstance = new UIExtraDataEventHandler();
|
---|
210 | return m_pInstance;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* static */
|
---|
214 | void UIExtraDataEventHandler::destroy()
|
---|
215 | {
|
---|
216 | if (m_pInstance)
|
---|
217 | {
|
---|
218 | delete m_pInstance;
|
---|
219 | m_pInstance = 0;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | UIExtraDataEventHandler::UIExtraDataEventHandler()
|
---|
224 | : m_pHandler(new UIExtraDataEventHandlerPrivate(this))
|
---|
225 | {
|
---|
226 | // RTPrintf("Self add: %RTthrd\n", RTThreadSelf());
|
---|
227 | const CVirtualBox &vbox = vboxGlobal().virtualBox();
|
---|
228 | ComObjPtr<UIMainEventListenerImpl> pListener;
|
---|
229 | pListener.createObject();
|
---|
230 | pListener->init(new UIMainEventListener(), this);
|
---|
231 | m_mainEventListener = CEventListener(pListener);
|
---|
232 | QVector<KVBoxEventType> events;
|
---|
233 | events
|
---|
234 | << KVBoxEventType_OnExtraDataCanChange
|
---|
235 | << KVBoxEventType_OnExtraDataChanged;
|
---|
236 |
|
---|
237 | vbox.GetEventSource().RegisterListener(m_mainEventListener, events, TRUE);
|
---|
238 | AssertWrapperOk(vbox);
|
---|
239 |
|
---|
240 | /* This is a vetoable event, so we have to respond to the event and have to
|
---|
241 | * use a direct connection therefor. */
|
---|
242 | connect(pListener->getWrapped(), SIGNAL(sigExtraDataCanChange(QString, QString, QString, bool&, QString&)),
|
---|
243 | m_pHandler, SLOT(sltExtraDataCanChange(QString, QString, QString, bool&, QString&)),
|
---|
244 | Qt::DirectConnection);
|
---|
245 |
|
---|
246 | /* Use a direct connection to the helper class. */
|
---|
247 | connect(pListener->getWrapped(), SIGNAL(sigExtraDataChange(QString, QString, QString)),
|
---|
248 | m_pHandler, SLOT(sltExtraDataChange(QString, QString, QString)),
|
---|
249 | Qt::DirectConnection);
|
---|
250 |
|
---|
251 | /* UI signals */
|
---|
252 | connect(m_pHandler, SIGNAL(sigCanShowRegistrationDlg(bool)),
|
---|
253 | this, SIGNAL(sigCanShowRegistrationDlg(bool)),
|
---|
254 | Qt::QueuedConnection);
|
---|
255 |
|
---|
256 | connect(m_pHandler, SIGNAL(sigGUILanguageChange(QString)),
|
---|
257 | this, SIGNAL(sigGUILanguageChange(QString)),
|
---|
258 | Qt::QueuedConnection);
|
---|
259 |
|
---|
260 | #ifdef VBOX_GUI_WITH_SYSTRAY
|
---|
261 | connect(m_pHandler, SIGNAL(sigMainWindowCountChange(int)),
|
---|
262 | this, SIGNAL(sigMainWindowCountChange(int)),
|
---|
263 | Qt::QueuedConnection);
|
---|
264 |
|
---|
265 | connect(m_pHandler, SIGNAL(sigCanShowTrayIcon(bool)),
|
---|
266 | this, SIGNAL(sigCanShowTrayIcon(bool)),
|
---|
267 | Qt::QueuedConnection);
|
---|
268 |
|
---|
269 | connect(m_pHandler, SIGNAL(sigTrayIconChange(bool)),
|
---|
270 | this, SIGNAL(sigTrayIconChange(bool)),
|
---|
271 | Qt::QueuedConnection);
|
---|
272 | #endif /* VBOX_GUI_WITH_SYSTRAY */
|
---|
273 |
|
---|
274 | #ifdef Q_WS_MAC
|
---|
275 | connect(m_pHandler, SIGNAL(sigPresentationModeChange(bool)),
|
---|
276 | this, SIGNAL(sigPresentationModeChange(bool)),
|
---|
277 | Qt::QueuedConnection);
|
---|
278 |
|
---|
279 | connect(m_pHandler, SIGNAL(sigDockIconAppearanceChange(bool)),
|
---|
280 | this, SIGNAL(sigDockIconAppearanceChange(bool)),
|
---|
281 | Qt::QueuedConnection);
|
---|
282 | #endif /* Q_WS_MAC */
|
---|
283 | }
|
---|
284 |
|
---|
285 | UIExtraDataEventHandler::~UIExtraDataEventHandler()
|
---|
286 | {
|
---|
287 | const CVirtualBox &vbox = vboxGlobal().virtualBox();
|
---|
288 | vbox.GetEventSource().UnregisterListener(m_mainEventListener);
|
---|
289 | }
|
---|
290 |
|
---|
291 | #include "UIExtraDataEventHandler.moc"
|
---|