1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * VBoxTrayIcon class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /* Local includes */
|
---|
20 | #include "VBoxTrayIcon.h"
|
---|
21 | #include "VBoxSelectorWnd.h"
|
---|
22 | #include "UIIconPool.h"
|
---|
23 | #include "UIVMItem.h"
|
---|
24 | #include "UIVMListView.h"
|
---|
25 |
|
---|
26 | /* Global includes */
|
---|
27 | #include <QMenu>
|
---|
28 | #include <QAction>
|
---|
29 |
|
---|
30 | VBoxTrayIcon::VBoxTrayIcon (VBoxSelectorWnd* aParent, UIVMItemModel* aVMModel)
|
---|
31 | {
|
---|
32 | mParent = aParent;
|
---|
33 | mVMModel = aVMModel;
|
---|
34 |
|
---|
35 | mShowSelectorAction = new QAction (this);
|
---|
36 | Assert (mShowSelectorAction);
|
---|
37 | mShowSelectorAction->setIcon(UIIconPool::iconSet(":/VirtualBox_16px.png"));
|
---|
38 |
|
---|
39 | mHideSystrayMenuAction = new QAction (this);
|
---|
40 | Assert (mHideSystrayMenuAction);
|
---|
41 | mHideSystrayMenuAction->setIcon(UIIconPool::iconSet(":/exit_16px.png"));
|
---|
42 |
|
---|
43 | /* reuse parent action data */
|
---|
44 |
|
---|
45 | mVmConfigAction = new QAction (this);
|
---|
46 | Assert (mVmConfigAction);
|
---|
47 | mVmConfigAction->setIcon (mParent->vmConfigAction()->icon());
|
---|
48 |
|
---|
49 | mVmDeleteAction = new QAction (this);
|
---|
50 | Assert (mVmDeleteAction);
|
---|
51 | mVmDeleteAction->setIcon (mParent->vmDeleteAction()->icon());
|
---|
52 |
|
---|
53 | mVmStartAction = new QAction (this);
|
---|
54 | Assert (mVmStartAction);
|
---|
55 | mVmStartAction->setIcon (mParent->vmStartAction()->icon());
|
---|
56 |
|
---|
57 | mVmDiscardAction = new QAction (this);
|
---|
58 | Assert (mVmDiscardAction);
|
---|
59 | mVmDiscardAction->setIcon (mParent->vmDiscardAction()->icon());
|
---|
60 |
|
---|
61 | mVmPauseAction = new QAction (this);
|
---|
62 | Assert (mVmPauseAction);
|
---|
63 | mVmPauseAction->setCheckable (true);
|
---|
64 | mVmPauseAction->setIcon (mParent->vmPauseAction()->icon());
|
---|
65 |
|
---|
66 | mVmRefreshAction = new QAction (this);
|
---|
67 | Assert (mVmRefreshAction);
|
---|
68 | mVmRefreshAction->setIcon (mParent->vmRefreshAction()->icon());
|
---|
69 |
|
---|
70 | mVmShowLogsAction = new QAction (this);
|
---|
71 | Assert (mVmConfigAction);
|
---|
72 | mVmShowLogsAction->setIcon (mParent->vmShowLogsAction()->icon());
|
---|
73 |
|
---|
74 | mTrayIconMenu = new QMenu (aParent);
|
---|
75 | Assert (mTrayIconMenu);
|
---|
76 |
|
---|
77 | setIcon (QIcon (":/VirtualBox_16px.png"));
|
---|
78 | setContextMenu (mTrayIconMenu);
|
---|
79 |
|
---|
80 | connect (mShowSelectorAction, SIGNAL (triggered()), mParent, SLOT (showWindow()));
|
---|
81 | connect (mHideSystrayMenuAction, SIGNAL (triggered()), this, SLOT (trayIconShow()));
|
---|
82 | }
|
---|
83 |
|
---|
84 | VBoxTrayIcon::~VBoxTrayIcon ()
|
---|
85 | {
|
---|
86 | /* Erase dialog handle in config file. */
|
---|
87 | if (mActive)
|
---|
88 | {
|
---|
89 | vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_TrayIconWinID,
|
---|
90 | QString::null);
|
---|
91 | hide();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | void VBoxTrayIcon::retranslateUi ()
|
---|
96 | {
|
---|
97 | if (!mActive)
|
---|
98 | return;
|
---|
99 |
|
---|
100 | mShowSelectorAction->setText (tr ("Show Selector Window"));
|
---|
101 | mShowSelectorAction->setStatusTip (tr (
|
---|
102 | "Show the selector window assigned to this menu"));
|
---|
103 |
|
---|
104 | mHideSystrayMenuAction->setText (tr ("Hide Tray Icon"));
|
---|
105 | mHideSystrayMenuAction->setStatusTip (tr (
|
---|
106 | "Remove this icon from the system tray"));
|
---|
107 |
|
---|
108 | /* reuse parent action data */
|
---|
109 |
|
---|
110 | mVmConfigAction->setText (mParent->vmConfigAction()->text());
|
---|
111 | mVmConfigAction->setStatusTip (mParent->vmConfigAction()->statusTip());
|
---|
112 |
|
---|
113 | mVmDeleteAction->setText (mParent->vmDeleteAction()->text());
|
---|
114 | mVmDeleteAction->setStatusTip (mParent->vmDeleteAction()->statusTip());
|
---|
115 |
|
---|
116 | mVmPauseAction->setText (mParent->vmPauseAction()->text());
|
---|
117 | mVmPauseAction->setStatusTip (mParent->vmPauseAction()->statusTip());
|
---|
118 |
|
---|
119 | mVmDiscardAction->setText (mParent->vmDiscardAction()->text());
|
---|
120 | mVmDiscardAction->setStatusTip (mParent->vmDiscardAction()->statusTip());
|
---|
121 |
|
---|
122 | mVmShowLogsAction->setText (mParent->vmShowLogsAction()->text());
|
---|
123 | mVmShowLogsAction->setStatusTip (mParent->vmShowLogsAction()->statusTip());
|
---|
124 | }
|
---|
125 |
|
---|
126 | void VBoxTrayIcon::showSubMenu ()
|
---|
127 | {
|
---|
128 | if (!mActive)
|
---|
129 | return;
|
---|
130 |
|
---|
131 | UIVMItem* pItem = NULL;
|
---|
132 | QMenu *pMenu = NULL;
|
---|
133 | QVariant vID;
|
---|
134 |
|
---|
135 | if ((pMenu = qobject_cast<QMenu*>(sender())))
|
---|
136 | {
|
---|
137 | vID = pMenu->menuAction()->data();
|
---|
138 | if (vID.canConvert<QString>() && mVMModel)
|
---|
139 | pItem = mVMModel->itemById (qvariant_cast<QString>(vID));
|
---|
140 | }
|
---|
141 |
|
---|
142 | mVmConfigAction->setData (vID);
|
---|
143 | mVmDeleteAction->setData (vID);
|
---|
144 | mVmDiscardAction->setData (vID);
|
---|
145 | mVmStartAction->setData (vID);
|
---|
146 | mVmPauseAction->setData (vID);
|
---|
147 | mVmShowLogsAction->setData (vID);
|
---|
148 |
|
---|
149 | if (pItem && pItem->accessible())
|
---|
150 | {
|
---|
151 | /* look at vmListViewCurrentChanged() */
|
---|
152 | CMachine m = pItem->machine();
|
---|
153 | KMachineState s = pItem->machineState();
|
---|
154 | bool running = pItem->sessionState() != KSessionState_Unlocked;
|
---|
155 | bool modifyEnabled = !running && s != KMachineState_Saved;
|
---|
156 |
|
---|
157 | /* Settings */
|
---|
158 | mVmConfigAction->setEnabled (modifyEnabled);
|
---|
159 |
|
---|
160 | /* Delete */
|
---|
161 | mVmDeleteAction->setEnabled (!running);
|
---|
162 |
|
---|
163 | /* Discard */
|
---|
164 | mVmDiscardAction->setEnabled (s == KMachineState_Saved && !running);
|
---|
165 |
|
---|
166 | /* Change the Start button text accordingly */
|
---|
167 | if ( s == KMachineState_PoweredOff
|
---|
168 | || s == KMachineState_Saved
|
---|
169 | || s == KMachineState_Teleported
|
---|
170 | || s == KMachineState_Aborted
|
---|
171 | )
|
---|
172 | {
|
---|
173 | mVmStartAction->setText (UIVMListView::tr ("S&tart"));
|
---|
174 | mVmStartAction->setStatusTip (
|
---|
175 | UIVMListView::tr ("Start the selected virtual machine"));
|
---|
176 | mVmStartAction->setEnabled (!running);
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | mVmStartAction->setText (UIVMListView::tr ("S&how"));
|
---|
181 | mVmStartAction->setStatusTip (
|
---|
182 | UIVMListView::tr ("Switch to the window of the selected virtual machine"));
|
---|
183 | mVmStartAction->setEnabled (pItem->canSwitchTo());
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Change the Pause/Resume button text accordingly */
|
---|
187 | mVmPauseAction->setEnabled ( s == KMachineState_Running
|
---|
188 | || s == KMachineState_Teleporting
|
---|
189 | || s == KMachineState_LiveSnapshotting
|
---|
190 | || s == KMachineState_Paused
|
---|
191 | || s == KMachineState_TeleportingPausedVM
|
---|
192 | );
|
---|
193 |
|
---|
194 | if ( s == KMachineState_Paused
|
---|
195 | || s == KMachineState_TeleportingPausedVM /*?*/
|
---|
196 | )
|
---|
197 | {
|
---|
198 | mVmPauseAction->setText (UIVMListView::tr ("R&esume"));
|
---|
199 | mVmPauseAction->setStatusTip (
|
---|
200 | UIVMListView::tr ("Resume the execution of the virtual machine"));
|
---|
201 | mVmPauseAction->blockSignals (true);
|
---|
202 | mVmPauseAction->setChecked (true);
|
---|
203 | mVmPauseAction->blockSignals (false);
|
---|
204 | }
|
---|
205 | else
|
---|
206 | {
|
---|
207 | mVmPauseAction->setText (UIVMListView::tr ("&Pause"));
|
---|
208 | mVmPauseAction->setStatusTip (
|
---|
209 | UIVMListView::tr ("Suspend the execution of the virtual machine"));
|
---|
210 | mVmPauseAction->blockSignals (true);
|
---|
211 | mVmPauseAction->setChecked (false);
|
---|
212 | mVmPauseAction->blockSignals (false);
|
---|
213 | }
|
---|
214 |
|
---|
215 | mVmShowLogsAction->setEnabled (true);
|
---|
216 |
|
---|
217 | /* Disconnect old slot which maybe was connected from another selected sub menu. */
|
---|
218 | disconnect (mVmConfigAction, SIGNAL (triggered()), this, SLOT (vmSettings()));
|
---|
219 | disconnect (mVmDeleteAction, SIGNAL (triggered()), this, SLOT (vmDelete()));
|
---|
220 | disconnect (mVmDiscardAction, SIGNAL (triggered()), this, SLOT (vmDiscard()));
|
---|
221 | disconnect (mVmStartAction, SIGNAL (triggered()), this, SLOT (vmStart()));
|
---|
222 | disconnect (mVmPauseAction, SIGNAL (toggled (bool)), this, SLOT (vmPause (bool)));
|
---|
223 | disconnect (mVmShowLogsAction, SIGNAL (triggered()), this, SLOT (vmShowLogs()));
|
---|
224 |
|
---|
225 | /* Connect new sub menu with slots. */
|
---|
226 | connect (mVmConfigAction, SIGNAL (triggered()), this, SLOT (vmSettings()));
|
---|
227 | connect (mVmDeleteAction, SIGNAL (triggered()), this, SLOT (vmDelete()));
|
---|
228 | connect (mVmDiscardAction, SIGNAL (triggered()), this, SLOT (vmDiscard()));
|
---|
229 | connect (mVmStartAction, SIGNAL (triggered()), this, SLOT (vmStart()));
|
---|
230 | connect (mVmPauseAction, SIGNAL (toggled (bool)), this, SLOT (vmPause (bool)));
|
---|
231 | connect (mVmShowLogsAction, SIGNAL (triggered()), this, SLOT (vmShowLogs()));
|
---|
232 | }
|
---|
233 | else /* Item is not accessible. */
|
---|
234 | {
|
---|
235 | mVmConfigAction->setEnabled (false);
|
---|
236 | mVmDeleteAction->setEnabled (pItem != NULL);
|
---|
237 | mVmDiscardAction->setEnabled (false);
|
---|
238 | mVmPauseAction->setEnabled (false);
|
---|
239 |
|
---|
240 | /* Set the Start button text accordingly. */
|
---|
241 | mVmStartAction->setText (UIVMListView::tr ("S&tart"));
|
---|
242 | mVmStartAction->setStatusTip (
|
---|
243 | UIVMListView::tr ("Start the selected virtual machine"));
|
---|
244 | mVmStartAction->setEnabled (false);
|
---|
245 |
|
---|
246 | /* Disable the show log item for the selected vm. */
|
---|
247 | mVmShowLogsAction->setEnabled (false);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* Build sub menu entries (add rest of sub menu entries later here). */
|
---|
251 | pMenu->addAction (mVmStartAction);
|
---|
252 | pMenu->addAction (mVmPauseAction);
|
---|
253 | }
|
---|
254 |
|
---|
255 | void VBoxTrayIcon::hideSubMenu ()
|
---|
256 | {
|
---|
257 | if (!mActive)
|
---|
258 | return;
|
---|
259 |
|
---|
260 | UIVMItem* pItem = NULL;
|
---|
261 | QVariant vID;
|
---|
262 |
|
---|
263 | if (QMenu *pMenu = qobject_cast<QMenu*>(sender()))
|
---|
264 | {
|
---|
265 | vID = pMenu->menuAction()->data();
|
---|
266 | if (vID.canConvert<QString>() && mVMModel)
|
---|
267 | pItem = mVMModel->itemById (qvariant_cast<QString>(vID));
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* Nothing to do here yet. */
|
---|
271 |
|
---|
272 | Assert (pItem);
|
---|
273 | }
|
---|
274 |
|
---|
275 | void VBoxTrayIcon::refresh ()
|
---|
276 | {
|
---|
277 | if (!mActive)
|
---|
278 | return;
|
---|
279 |
|
---|
280 | AssertReturnVoid (mVMModel);
|
---|
281 | AssertReturnVoid (mTrayIconMenu);
|
---|
282 |
|
---|
283 | mTrayIconMenu->clear();
|
---|
284 |
|
---|
285 | UIVMItem* pItem = NULL;
|
---|
286 | QMenu* pCurMenu = mTrayIconMenu;
|
---|
287 | QMenu* pSubMenu = NULL;
|
---|
288 |
|
---|
289 | int iCurItemCount = 0;
|
---|
290 |
|
---|
291 | mTrayIconMenu->addAction (mShowSelectorAction);
|
---|
292 | mTrayIconMenu->setDefaultAction (mShowSelectorAction);
|
---|
293 |
|
---|
294 | if (mVMModel->rowCount() > 0)
|
---|
295 | mTrayIconMenu->addSeparator();
|
---|
296 |
|
---|
297 | for (int i = 0; i < mVMModel->rowCount(); i++, iCurItemCount++)
|
---|
298 | {
|
---|
299 | pItem = mVMModel->itemByRow(i);
|
---|
300 | Assert(pItem);
|
---|
301 |
|
---|
302 | if (iCurItemCount > 10) /* 10 machines per sub menu. */
|
---|
303 | {
|
---|
304 | pSubMenu = new QMenu (tr ("&Other Machines...", "tray menu"));
|
---|
305 | Assert (pSubMenu);
|
---|
306 | pCurMenu->addMenu (pSubMenu);
|
---|
307 | pCurMenu = pSubMenu;
|
---|
308 | iCurItemCount = 0;
|
---|
309 | }
|
---|
310 |
|
---|
311 | pSubMenu = new QMenu (QString ("&%1. %2")
|
---|
312 | .arg ((iCurItemCount + 1) % 100).arg (pItem->name()));
|
---|
313 | Assert (pSubMenu);
|
---|
314 | pSubMenu->setIcon (pItem->machineStateIcon());
|
---|
315 |
|
---|
316 | QAction *pAction = NULL;
|
---|
317 | QVariant vID;
|
---|
318 | vID.setValue (pItem->id());
|
---|
319 |
|
---|
320 | pSubMenu->menuAction()->setData (vID);
|
---|
321 | connect (pSubMenu, SIGNAL (aboutToShow()), this, SLOT (showSubMenu()));
|
---|
322 | connect (pSubMenu, SIGNAL (aboutToHide()), this, SLOT (hideSubMenu()));
|
---|
323 | pCurMenu->addMenu (pSubMenu);
|
---|
324 | }
|
---|
325 |
|
---|
326 | if (mVMModel->rowCount() > 0)
|
---|
327 | mTrayIconMenu->addSeparator();
|
---|
328 |
|
---|
329 | mTrayIconMenu->addAction (mHideSystrayMenuAction);
|
---|
330 |
|
---|
331 | /* We're done constructing the menu, show it */
|
---|
332 | setVisible (true);
|
---|
333 | }
|
---|
334 |
|
---|
335 | UIVMItem* VBoxTrayIcon::GetItem (QObject* aObject)
|
---|
336 | {
|
---|
337 | UIVMItem* pItem = NULL;
|
---|
338 | if (QAction *pAction = qobject_cast<QAction*>(sender()))
|
---|
339 | {
|
---|
340 | QVariant v = pAction->data();
|
---|
341 | if (v.canConvert<QString>() && mVMModel)
|
---|
342 | pItem = mVMModel->itemById (qvariant_cast<QString>(v));
|
---|
343 | }
|
---|
344 |
|
---|
345 | Assert (pItem);
|
---|
346 | return pItem;
|
---|
347 | }
|
---|
348 |
|
---|
349 | void VBoxTrayIcon::trayIconShow (bool aShow)
|
---|
350 | {
|
---|
351 | if (!vboxGlobal().isTrayMenu())
|
---|
352 | return;
|
---|
353 |
|
---|
354 | mActive = aShow;
|
---|
355 | if (mActive)
|
---|
356 | {
|
---|
357 | refresh();
|
---|
358 | retranslateUi();
|
---|
359 | }
|
---|
360 | setVisible (mActive);
|
---|
361 |
|
---|
362 | if (!mActive)
|
---|
363 | mParent->fileExit();
|
---|
364 | }
|
---|
365 |
|
---|
366 | void VBoxTrayIcon::vmSettings()
|
---|
367 | {
|
---|
368 | UIVMItem* pItem = GetItem (sender());
|
---|
369 | mParent->vmSettings (NULL, NULL, pItem->id());
|
---|
370 | }
|
---|
371 |
|
---|
372 | void VBoxTrayIcon::vmDelete()
|
---|
373 | {
|
---|
374 | UIVMItem* pItem = GetItem (sender());
|
---|
375 | mParent->vmDelete (pItem->id());
|
---|
376 | }
|
---|
377 |
|
---|
378 | void VBoxTrayIcon::vmStart()
|
---|
379 | {
|
---|
380 | UIVMItem* pItem = GetItem (sender());
|
---|
381 | mParent->vmStart (pItem->id());
|
---|
382 | }
|
---|
383 |
|
---|
384 | void VBoxTrayIcon::vmDiscard()
|
---|
385 | {
|
---|
386 | UIVMItem* pItem = GetItem (sender());
|
---|
387 | mParent->vmDiscard (pItem->id());
|
---|
388 | }
|
---|
389 |
|
---|
390 | void VBoxTrayIcon::vmPause(bool aPause)
|
---|
391 | {
|
---|
392 | UIVMItem* pItem = GetItem (sender());
|
---|
393 | mParent->vmPause (aPause, pItem->id());
|
---|
394 | }
|
---|
395 |
|
---|
396 | void VBoxTrayIcon::vmRefresh()
|
---|
397 | {
|
---|
398 | UIVMItem* pItem = GetItem (sender());
|
---|
399 | mParent->vmRefresh (pItem->id());
|
---|
400 | }
|
---|
401 |
|
---|
402 | void VBoxTrayIcon::vmShowLogs()
|
---|
403 | {
|
---|
404 | UIVMItem* pItem = GetItem (sender());
|
---|
405 | mParent->vmShowLogs (pItem->id());
|
---|
406 | }
|
---|
407 |
|
---|