VirtualBox

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

Last change on this file was 104251, checked in by vboxsync, 6 weeks ago

FE/Qt. bugref:10622. Using new UITranslationEventListener in the manager UI classes.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use