VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/manager/UIVirtualMachineItemLocal.cpp

Last change on this file was 106061, checked in by vboxsync, 3 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: UIVirtualMachineItemLocal.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIVirtualMachineItem class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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/* Qt includes: */
29#include <QFileInfo>
30#include <QIcon>
31
32/* GUI includes: */
33#include "UICommon.h"
34#include "UIConverter.h"
35#include "UIErrorString.h"
36#include "UIExtraDataManager.h"
37#include "UIIconPool.h"
38#include "UILocalMachineStuff.h"
39#include "UITranslationEventListener.h"
40#include "UIVirtualMachineItemLocal.h"
41#ifdef VBOX_WS_MAC
42# include <ApplicationServices/ApplicationServices.h>
43#endif /* VBOX_WS_MAC */
44
45/* COM includes: */
46#include "CSnapshot.h"
47#include "CVirtualBoxErrorInfo.h"
48
49
50/*********************************************************************************************************************************
51* Class UIVirtualMachineItemLocal implementation. *
52*********************************************************************************************************************************/
53
54UIVirtualMachineItemLocal::UIVirtualMachineItemLocal(const CMachine &comMachine)
55 : UIVirtualMachineItem(UIVirtualMachineItemType_Local)
56 , m_comMachine(comMachine)
57 , m_cSnaphot(0)
58 , m_enmMachineState(KMachineState_Null)
59 , m_enmSessionState(KSessionState_Null)
60{
61 recache();
62}
63
64UIVirtualMachineItemLocal::~UIVirtualMachineItemLocal()
65{
66}
67
68void UIVirtualMachineItemLocal::recache()
69{
70 /* Determine attributes which are always available: */
71 m_uId = m_comMachine.GetId();
72 m_strSettingsFile = m_comMachine.GetSettingsFilePath();
73
74 /* Now determine whether VM is accessible: */
75 m_fAccessible = m_comMachine.GetAccessible();
76 if (m_fAccessible)
77 {
78 /* Reset last access error information: */
79 m_strAccessError.clear();
80
81 /* Determine own VM attributes: */
82 m_strName = m_comMachine.GetName();
83 m_strOSTypeId = m_comMachine.GetOSTypeId();
84 m_groups = m_comMachine.GetGroups().toList();
85
86 /* Determine snapshot attributes: */
87 CSnapshot comSnapshot = m_comMachine.GetCurrentSnapshot();
88 m_strSnapshotName = comSnapshot.isNull() ? QString() : comSnapshot.GetName();
89 m_lastStateChange.setSecsSinceEpoch(m_comMachine.GetLastStateChange() / 1000);
90 m_cSnaphot = m_comMachine.GetSnapshotCount();
91
92 /* Determine VM states: */
93 m_enmMachineState = m_comMachine.GetState();
94 m_machineStateIcon = gpConverter->toIcon(m_enmMachineState);
95 m_enmSessionState = m_comMachine.GetSessionState();
96
97 /* Determine configuration access level: */
98 m_enmConfigurationAccessLevel = ::configurationAccessLevel(m_enmSessionState, m_enmMachineState);
99 /* Also take restrictions into account: */
100 if ( m_enmConfigurationAccessLevel != ConfigurationAccessLevel_Null
101 && !gEDataManager->machineReconfigurationEnabled(m_uId))
102 m_enmConfigurationAccessLevel = ConfigurationAccessLevel_Null;
103
104 /* Determine PID finally: */
105 if ( m_enmMachineState == KMachineState_PoweredOff
106 || m_enmMachineState == KMachineState_Saved
107 || m_enmMachineState == KMachineState_Teleported
108 || m_enmMachineState == KMachineState_Aborted
109 || m_enmMachineState == KMachineState_AbortedSaved
110 )
111 {
112 m_pid = (ULONG) ~0;
113 }
114 else
115 {
116 m_pid = m_comMachine.GetSessionPID();
117 }
118
119 /* Determine whether we should show this VM details: */
120 m_fHasDetails = gEDataManager->showMachineInVirtualBoxManagerDetails(m_uId);
121 }
122 else
123 {
124 /* Update last access error information: */
125 m_strAccessError = UIErrorString::formatErrorInfo(m_comMachine.GetAccessError());
126
127 /* Determine machine name on the basis of settings file only: */
128 QFileInfo fi(m_strSettingsFile);
129 m_strName = UICommon::hasAllowedExtension(fi.completeSuffix(), VBoxFileExts)
130 ? fi.completeBaseName()
131 : fi.fileName();
132 /* Reset other VM attributes: */
133 m_strOSTypeId = QString();
134 m_groups.clear();
135
136 /* Reset snapshot attributes: */
137 m_strSnapshotName = QString();
138 m_lastStateChange = QDateTime::currentDateTime();
139 m_cSnaphot = 0;
140
141 /* Reset VM states: */
142 m_enmMachineState = KMachineState_Null;
143 m_machineStateIcon = gpConverter->toIcon(KMachineState_Aborted);
144 m_enmSessionState = KSessionState_Null;
145
146 /* Reset configuration access level: */
147 m_enmConfigurationAccessLevel = ConfigurationAccessLevel_Null;
148
149 /* Reset PID finally: */
150 m_pid = (ULONG) ~0;
151
152 /* Reset whether we should show this VM details: */
153 m_fHasDetails = true;
154 }
155
156 /* Recache item pixmap: */
157 recachePixmap();
158
159 /* Retranslate finally: */
160 sltRetranslateUI();
161 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
162 this, &UIVirtualMachineItemLocal::sltRetranslateUI);
163}
164
165void UIVirtualMachineItemLocal::recachePixmap()
166{
167 /* If machine is accessible: */
168 if (m_fAccessible)
169 {
170 /* First, we are trying to acquire personal machine guest OS type icon: */
171 m_pixmap = generalIconPool().userMachinePixmapDefault(m_comMachine, &m_logicalPixmapSize);
172 /* If there is nothing, we are using icon corresponding to cached guest OS type: */
173 if (m_pixmap.isNull())
174 m_pixmap = generalIconPool().guestOSTypePixmapDefault(m_strOSTypeId, &m_logicalPixmapSize);
175 }
176 /* Otherwise: */
177 else
178 {
179 /* We are using "Other" guest OS type icon: */
180 m_pixmap = generalIconPool().guestOSTypePixmapDefault("Other", &m_logicalPixmapSize);
181 }
182}
183
184bool UIVirtualMachineItemLocal::isItemEditable() const
185{
186 return accessible()
187 && sessionState() == KSessionState_Unlocked;
188}
189
190bool UIVirtualMachineItemLocal::isItemRemovable() const
191{
192 return !accessible()
193 || sessionState() == KSessionState_Unlocked;
194}
195
196bool UIVirtualMachineItemLocal::isItemSaved() const
197{
198 return accessible()
199 && ( machineState() == KMachineState_Saved
200 || machineState() == KMachineState_AbortedSaved);
201}
202
203bool UIVirtualMachineItemLocal::isItemPoweredOff() const
204{
205 return accessible()
206 && ( machineState() == KMachineState_PoweredOff
207 || machineState() == KMachineState_Saved
208 || machineState() == KMachineState_Teleported
209 || machineState() == KMachineState_Aborted
210 || machineState() == KMachineState_AbortedSaved);
211}
212
213bool UIVirtualMachineItemLocal::isItemStarted() const
214{
215 return isItemRunning()
216 || isItemPaused();
217}
218
219bool UIVirtualMachineItemLocal::isItemRunning() const
220{
221 return accessible()
222 && ( machineState() == KMachineState_Running
223 || machineState() == KMachineState_Teleporting
224 || machineState() == KMachineState_LiveSnapshotting);
225}
226
227bool UIVirtualMachineItemLocal::isItemRunningHeadless() const
228{
229 if (isItemRunning())
230 {
231 /* Open session to determine which frontend VM is started with: */
232 CSession comSession = openExistingSession(id());
233 if (!comSession.isNull())
234 {
235 /* Acquire the session name: */
236 const QString strSessionName = comSession.GetMachine().GetSessionName();
237 /* Close the session early: */
238 comSession.UnlockMachine();
239 /* Check whether we are in 'headless' session: */
240 return strSessionName == "headless";
241 }
242 }
243 return false;
244}
245
246bool UIVirtualMachineItemLocal::isItemPaused() const
247{
248 return accessible()
249 && ( machineState() == KMachineState_Paused
250 || machineState() == KMachineState_TeleportingPausedVM);
251}
252
253bool UIVirtualMachineItemLocal::isItemStuck() const
254{
255 return accessible()
256 && machineState() == KMachineState_Stuck;
257}
258
259bool UIVirtualMachineItemLocal::isItemCanBeSwitchedTo() const
260{
261 return const_cast<CMachine&>(m_comMachine).CanShowConsoleWindow()
262 || isItemRunningHeadless();
263}
264
265void UIVirtualMachineItemLocal::sltRetranslateUI()
266{
267 /* This is used in tool-tip generation: */
268 const QString strDateTime = m_lastStateChange.date() == QDate::currentDate()
269 ? QLocale::system().toString(m_lastStateChange.time(), QLocale::ShortFormat)
270 : QLocale::system().toString(m_lastStateChange, QLocale::ShortFormat);
271
272 /* If machine is accessible: */
273 if (m_fAccessible)
274 {
275 /* Just use the usual translation for valid states: */
276 m_strMachineStateName = gpConverter->toString(m_enmMachineState);
277 m_strSessionStateName = gpConverter->toString(m_enmSessionState);
278
279 /* Update tool-tip: */
280 m_strToolTipText = QString("<b>%1</b>").arg(m_strName);
281 if (!m_strSnapshotName.isNull())
282 m_strToolTipText += QString(" (%1)").arg(m_strSnapshotName);
283 m_strToolTipText = tr("<nobr>%1<br></nobr>"
284 "<nobr>%2 since %3</nobr><br>"
285 "<nobr>Session %4</nobr>",
286 "VM tooltip (name, last state change, session state)")
287 .arg(m_strToolTipText)
288 .arg(gpConverter->toString(m_enmMachineState))
289 .arg(strDateTime)
290 .arg(gpConverter->toString(m_enmSessionState).toLower());
291 }
292 /* Otherwise: */
293 else
294 {
295 /* We have our own translation for Null states: */
296 m_strMachineStateName = tr("Inaccessible");
297 m_strSessionStateName = tr("Inaccessible");
298
299 /* Update tool-tip: */
300 m_strToolTipText = tr("<nobr><b>%1</b><br></nobr>"
301 "<nobr>Inaccessible since %2</nobr>",
302 "Inaccessible VM tooltip (name, last state change)")
303 .arg(m_strSettingsFile)
304 .arg(strDateTime);
305 }
306}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette