1 | /* $Id: UIVirtualMachineItemCloud.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIVirtualMachineItemCloud 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 <QPointer>
|
---|
30 | #include <QTimer>
|
---|
31 |
|
---|
32 | /* GUI includes: */
|
---|
33 | #include "UICloudNetworkingStuff.h"
|
---|
34 | #include "UIConverter.h"
|
---|
35 | #include "UIErrorString.h"
|
---|
36 | #include "UIIconPool.h"
|
---|
37 | #include "UINotificationCenter.h"
|
---|
38 | #include "UIProgressTask.h"
|
---|
39 | #include "UITranslationEventListener.h"
|
---|
40 | #include "UIThreadPool.h"
|
---|
41 | #include "UIVirtualMachineItemCloud.h"
|
---|
42 |
|
---|
43 | /* COM includes: */
|
---|
44 | #include "CProgress.h"
|
---|
45 | #include "CVirtualBoxErrorInfo.h"
|
---|
46 | #include <VBox/com/VirtualBox.h> /* For GUEST_OS_ID_STR_X86. */
|
---|
47 |
|
---|
48 |
|
---|
49 | /** UIProgressTask extension performing cloud machine refresh task.
|
---|
50 | * @todo rework this task to be a part of notification-center. */
|
---|
51 | class UIProgressTaskRefreshCloudMachine : public UIProgressTask
|
---|
52 | {
|
---|
53 | Q_OBJECT;
|
---|
54 |
|
---|
55 | public:
|
---|
56 |
|
---|
57 | /** Constructs @a comCloudMachine refresh task passing @a pParent to the base-class. */
|
---|
58 | UIProgressTaskRefreshCloudMachine(QObject *pParent, const CCloudMachine &comCloudMachine);
|
---|
59 |
|
---|
60 | protected:
|
---|
61 |
|
---|
62 | /** Creates and returns started progress-wrapper required to init UIProgressObject. */
|
---|
63 | virtual CProgress createProgress() RT_OVERRIDE;
|
---|
64 | /** Handles finished @a comProgress wrapper. */
|
---|
65 | virtual void handleProgressFinished(CProgress &comProgress) RT_OVERRIDE;
|
---|
66 |
|
---|
67 | private:
|
---|
68 |
|
---|
69 | /** Holds the cloud machine wrapper. */
|
---|
70 | CCloudMachine m_comCloudMachine;
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /*********************************************************************************************************************************
|
---|
75 | * Class UIProgressTaskRefreshCloudMachine implementation. *
|
---|
76 | *********************************************************************************************************************************/
|
---|
77 |
|
---|
78 | UIProgressTaskRefreshCloudMachine::UIProgressTaskRefreshCloudMachine(QObject *pParent, const CCloudMachine &comCloudMachine)
|
---|
79 | : UIProgressTask(pParent)
|
---|
80 | , m_comCloudMachine(comCloudMachine)
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | CProgress UIProgressTaskRefreshCloudMachine::createProgress()
|
---|
85 | {
|
---|
86 | /* Prepare resulting progress-wrapper: */
|
---|
87 | CProgress comResult;
|
---|
88 |
|
---|
89 | /* Initialize actual progress-wrapper: */
|
---|
90 | CProgress comProgress = m_comCloudMachine.Refresh();
|
---|
91 | if (!m_comCloudMachine.isOk())
|
---|
92 | UINotificationMessage::cannotRefreshCloudMachine(m_comCloudMachine);
|
---|
93 | else
|
---|
94 | comResult = comProgress;
|
---|
95 |
|
---|
96 | /* Return progress-wrapper in any case: */
|
---|
97 | return comResult;
|
---|
98 | }
|
---|
99 |
|
---|
100 | void UIProgressTaskRefreshCloudMachine::handleProgressFinished(CProgress &comProgress)
|
---|
101 | {
|
---|
102 | /* Handle progress-wrapper errors: */
|
---|
103 | if (comProgress.isNotNull() && !comProgress.GetCanceled() && (!comProgress.isOk() || comProgress.GetResultCode() != 0))
|
---|
104 | UINotificationMessage::cannotRefreshCloudMachine(comProgress);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /*********************************************************************************************************************************
|
---|
109 | * Class UIVirtualMachineItemCloud implementation. *
|
---|
110 | *********************************************************************************************************************************/
|
---|
111 |
|
---|
112 | UIVirtualMachineItemCloud::UIVirtualMachineItemCloud(UIFakeCloudVirtualMachineItemState enmState)
|
---|
113 | : UIVirtualMachineItem(UIVirtualMachineItemType_CloudFake)
|
---|
114 | , m_enmMachineState(KCloudMachineState_Invalid)
|
---|
115 | , m_enmFakeCloudItemState(enmState)
|
---|
116 | , m_fUpdateRequiredByGlobalReason(false)
|
---|
117 | , m_fUpdateRequiredByLocalReason(false)
|
---|
118 | , m_pProgressTaskRefresh(0)
|
---|
119 | {
|
---|
120 | prepare();
|
---|
121 | }
|
---|
122 |
|
---|
123 | UIVirtualMachineItemCloud::UIVirtualMachineItemCloud(const CCloudMachine &comCloudMachine)
|
---|
124 | : UIVirtualMachineItem(UIVirtualMachineItemType_CloudReal)
|
---|
125 | , m_comCloudMachine(comCloudMachine)
|
---|
126 | , m_enmMachineState(KCloudMachineState_Invalid)
|
---|
127 | , m_enmFakeCloudItemState(UIFakeCloudVirtualMachineItemState_NotApplicable)
|
---|
128 | , m_fUpdateRequiredByGlobalReason(false)
|
---|
129 | , m_fUpdateRequiredByLocalReason(false)
|
---|
130 | , m_pProgressTaskRefresh(0)
|
---|
131 | {
|
---|
132 | prepare();
|
---|
133 | }
|
---|
134 |
|
---|
135 | UIVirtualMachineItemCloud::~UIVirtualMachineItemCloud()
|
---|
136 | {
|
---|
137 | cleanup();
|
---|
138 | }
|
---|
139 |
|
---|
140 | void UIVirtualMachineItemCloud::setFakeCloudItemState(UIFakeCloudVirtualMachineItemState enmState)
|
---|
141 | {
|
---|
142 | m_enmFakeCloudItemState = enmState;
|
---|
143 | recache();
|
---|
144 | }
|
---|
145 |
|
---|
146 | void UIVirtualMachineItemCloud::setFakeCloudItemErrorMessage(const QString &strErrorMessage)
|
---|
147 | {
|
---|
148 | m_strFakeCloudItemErrorMessage = strErrorMessage;
|
---|
149 | recache();
|
---|
150 | }
|
---|
151 |
|
---|
152 | void UIVirtualMachineItemCloud::setUpdateRequiredByGlobalReason(bool fRequired)
|
---|
153 | {
|
---|
154 | m_fUpdateRequiredByGlobalReason = fRequired;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void UIVirtualMachineItemCloud::setUpdateRequiredByLocalReason(bool fRequired)
|
---|
158 | {
|
---|
159 | m_fUpdateRequiredByLocalReason = fRequired;
|
---|
160 | }
|
---|
161 |
|
---|
162 | void UIVirtualMachineItemCloud::updateInfoAsync(bool fDelayed)
|
---|
163 | {
|
---|
164 | /* Ignore refresh request if progress-task is absent: */
|
---|
165 | if (!m_pProgressTaskRefresh)
|
---|
166 | return;
|
---|
167 |
|
---|
168 | /* Schedule refresh request in a 10 or 0 seconds
|
---|
169 | * if progress-task isn't already scheduled or running: */
|
---|
170 | if ( !m_pProgressTaskRefresh->isScheduled()
|
---|
171 | && !m_pProgressTaskRefresh->isRunning())
|
---|
172 | m_pProgressTaskRefresh->schedule(fDelayed ? 10000 : 0);
|
---|
173 | }
|
---|
174 |
|
---|
175 | void UIVirtualMachineItemCloud::waitForAsyncInfoUpdateFinished()
|
---|
176 | {
|
---|
177 | /* Ignore cancel request if progress-task is absent: */
|
---|
178 | if (!m_pProgressTaskRefresh)
|
---|
179 | return;
|
---|
180 |
|
---|
181 | /* Mark update canceled in any case: */
|
---|
182 | m_fUpdateRequiredByGlobalReason = false;
|
---|
183 | m_fUpdateRequiredByLocalReason = false;
|
---|
184 |
|
---|
185 | /* Cancel refresh request
|
---|
186 | * if progress-task already running: */
|
---|
187 | if (m_pProgressTaskRefresh->isRunning())
|
---|
188 | m_pProgressTaskRefresh->cancel();
|
---|
189 | }
|
---|
190 |
|
---|
191 | void UIVirtualMachineItemCloud::recache()
|
---|
192 | {
|
---|
193 | switch (itemType())
|
---|
194 | {
|
---|
195 | case UIVirtualMachineItemType_CloudFake:
|
---|
196 | {
|
---|
197 | /* Make sure cloud VM is NOT set: */
|
---|
198 | AssertReturnVoid(m_comCloudMachine.isNull());
|
---|
199 |
|
---|
200 | /* Determine ID/name: */
|
---|
201 | m_uId = QUuid();
|
---|
202 | m_strName = QString();
|
---|
203 |
|
---|
204 | /* Determine whether VM is accessible: */
|
---|
205 | m_fAccessible = m_strFakeCloudItemErrorMessage.isNull();
|
---|
206 | m_strAccessError = m_strFakeCloudItemErrorMessage;
|
---|
207 |
|
---|
208 | /* Determine VM OS type: */
|
---|
209 | m_strOSTypeId = GUEST_OS_ID_STR_X86("Other");
|
---|
210 |
|
---|
211 | /* Determine VM states: */
|
---|
212 | m_enmMachineState = KCloudMachineState_Stopped;
|
---|
213 | switch (m_enmFakeCloudItemState)
|
---|
214 | {
|
---|
215 | case UIFakeCloudVirtualMachineItemState_Loading:
|
---|
216 | m_machineStateIcon = UIIconPool::iconSet(":/state_loading_16px.png");
|
---|
217 | break;
|
---|
218 | case UIFakeCloudVirtualMachineItemState_Done:
|
---|
219 | m_machineStateIcon = UIIconPool::iconSet(":/vm_new_16px.png");
|
---|
220 | break;
|
---|
221 | default:
|
---|
222 | break;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /* Determine configuration access level: */
|
---|
226 | m_enmConfigurationAccessLevel = ConfigurationAccessLevel_Null;
|
---|
227 |
|
---|
228 | /* Determine whether we should show this VM details: */
|
---|
229 | m_fHasDetails = true;
|
---|
230 |
|
---|
231 | break;
|
---|
232 | }
|
---|
233 | case UIVirtualMachineItemType_CloudReal:
|
---|
234 | {
|
---|
235 | /* Make sure cloud VM is set: */
|
---|
236 | AssertReturnVoid(m_comCloudMachine.isNotNull());
|
---|
237 |
|
---|
238 | /* Determine ID/name: */
|
---|
239 | m_uId = m_comCloudMachine.GetId();
|
---|
240 | m_strName = m_comCloudMachine.GetName();
|
---|
241 |
|
---|
242 | /* Determine whether VM is accessible: */
|
---|
243 | m_fAccessible = m_comCloudMachine.GetAccessible();
|
---|
244 | m_strAccessError = !m_fAccessible ? UIErrorString::formatErrorInfo(m_comCloudMachine.GetAccessError()) : QString();
|
---|
245 |
|
---|
246 | /* Determine VM OS type: */
|
---|
247 | m_strOSTypeId = m_fAccessible ? m_comCloudMachine.GetOSTypeId() : GUEST_OS_ID_STR_X86("Other");
|
---|
248 |
|
---|
249 | /* Determine VM states: */
|
---|
250 | m_enmMachineState = m_fAccessible ? m_comCloudMachine.GetState() : KCloudMachineState_Stopped;
|
---|
251 | m_machineStateIcon = gpConverter->toIcon(m_enmMachineState);
|
---|
252 |
|
---|
253 | /* Determine configuration access level: */
|
---|
254 | m_enmConfigurationAccessLevel = m_fAccessible ? ConfigurationAccessLevel_Full : ConfigurationAccessLevel_Null;
|
---|
255 |
|
---|
256 | /* Determine whether we should show this VM details: */
|
---|
257 | m_fHasDetails = true;
|
---|
258 |
|
---|
259 | break;
|
---|
260 | }
|
---|
261 | default:
|
---|
262 | {
|
---|
263 | AssertFailed();
|
---|
264 | break;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* Recache item pixmap: */
|
---|
269 | recachePixmap();
|
---|
270 |
|
---|
271 | /* Retranslate finally: */
|
---|
272 | sltRetranslateUI();
|
---|
273 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
274 | this, &UIVirtualMachineItemCloud::sltRetranslateUI);
|
---|
275 | }
|
---|
276 |
|
---|
277 | void UIVirtualMachineItemCloud::recachePixmap()
|
---|
278 | {
|
---|
279 | /* We are using icon corresponding to cached guest OS type: */
|
---|
280 | if ( itemType() == UIVirtualMachineItemType_CloudFake
|
---|
281 | && fakeCloudItemState() == UIFakeCloudVirtualMachineItemState_Loading)
|
---|
282 | m_pixmap = generalIconPool().guestOSTypePixmapDefault("Cloud", &m_logicalPixmapSize);
|
---|
283 | else
|
---|
284 | m_pixmap = generalIconPool().guestOSTypePixmapDefault(m_strOSTypeId, &m_logicalPixmapSize);
|
---|
285 | }
|
---|
286 |
|
---|
287 | bool UIVirtualMachineItemCloud::isItemEditable() const
|
---|
288 | {
|
---|
289 | return accessible()
|
---|
290 | && itemType() == UIVirtualMachineItemType_CloudReal;
|
---|
291 | }
|
---|
292 |
|
---|
293 | bool UIVirtualMachineItemCloud::isItemRemovable() const
|
---|
294 | {
|
---|
295 | return accessible()
|
---|
296 | && itemType() == UIVirtualMachineItemType_CloudReal;
|
---|
297 | }
|
---|
298 |
|
---|
299 | bool UIVirtualMachineItemCloud::isItemSaved() const
|
---|
300 | {
|
---|
301 | return accessible()
|
---|
302 | && itemType() == UIVirtualMachineItemType_CloudReal
|
---|
303 | && ( machineState() == KCloudMachineState_Stopped
|
---|
304 | || machineState() == KCloudMachineState_Running);
|
---|
305 | }
|
---|
306 |
|
---|
307 | bool UIVirtualMachineItemCloud::isItemPoweredOff() const
|
---|
308 | {
|
---|
309 | return accessible()
|
---|
310 | && ( machineState() == KCloudMachineState_Stopped
|
---|
311 | || machineState() == KCloudMachineState_Terminated);
|
---|
312 | }
|
---|
313 |
|
---|
314 | bool UIVirtualMachineItemCloud::isItemStarted() const
|
---|
315 | {
|
---|
316 | return isItemRunning()
|
---|
317 | || isItemPaused();
|
---|
318 | }
|
---|
319 |
|
---|
320 | bool UIVirtualMachineItemCloud::isItemRunning() const
|
---|
321 | {
|
---|
322 | return accessible()
|
---|
323 | && machineState() == KCloudMachineState_Running;
|
---|
324 | }
|
---|
325 |
|
---|
326 | bool UIVirtualMachineItemCloud::isItemRunningHeadless() const
|
---|
327 | {
|
---|
328 | return isItemRunning();
|
---|
329 | }
|
---|
330 |
|
---|
331 | bool UIVirtualMachineItemCloud::isItemPaused() const
|
---|
332 | {
|
---|
333 | return false;
|
---|
334 | }
|
---|
335 |
|
---|
336 | bool UIVirtualMachineItemCloud::isItemStuck() const
|
---|
337 | {
|
---|
338 | return false;
|
---|
339 | }
|
---|
340 |
|
---|
341 | bool UIVirtualMachineItemCloud::isItemCanBeSwitchedTo() const
|
---|
342 | {
|
---|
343 | return false;
|
---|
344 | }
|
---|
345 |
|
---|
346 | void UIVirtualMachineItemCloud::sltRetranslateUI()
|
---|
347 | {
|
---|
348 | /* If machine is accessible: */
|
---|
349 | if (accessible())
|
---|
350 | {
|
---|
351 | if (itemType() == UIVirtualMachineItemType_CloudFake)
|
---|
352 | {
|
---|
353 | /* Update fake machine state name: */
|
---|
354 | switch (m_enmFakeCloudItemState)
|
---|
355 | {
|
---|
356 | case UIFakeCloudVirtualMachineItemState_Loading:
|
---|
357 | m_strMachineStateName = tr("Loading ...");
|
---|
358 | break;
|
---|
359 | case UIFakeCloudVirtualMachineItemState_Done:
|
---|
360 | m_strMachineStateName = tr("Empty");
|
---|
361 | break;
|
---|
362 | default:
|
---|
363 | break;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /* Update tool-tip: */
|
---|
367 | m_strToolTipText = m_strMachineStateName;
|
---|
368 | }
|
---|
369 | else
|
---|
370 | {
|
---|
371 | /* Update real machine state name: */
|
---|
372 | m_strMachineStateName = gpConverter->toString(m_enmMachineState);
|
---|
373 |
|
---|
374 | /* Update tool-tip: */
|
---|
375 | m_strToolTipText = QString("<nobr><b>%1</b></nobr><br>"
|
---|
376 | "<nobr>%2</nobr>")
|
---|
377 | .arg(m_strName)
|
---|
378 | .arg(gpConverter->toString(m_enmMachineState));
|
---|
379 | }
|
---|
380 | }
|
---|
381 | /* Otherwise: */
|
---|
382 | else
|
---|
383 | {
|
---|
384 | /* We have our own translation for Null states: */
|
---|
385 | m_strMachineStateName = tr("Inaccessible", "VM");
|
---|
386 |
|
---|
387 | /* Update tool-tip: */
|
---|
388 | m_strToolTipText = tr("<nobr><b>%1</b></nobr><br>"
|
---|
389 | "<nobr>Inaccessible</nobr>",
|
---|
390 | "Inaccessible VM tooltip (name)")
|
---|
391 | .arg(m_strName);
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | void UIVirtualMachineItemCloud::sltHandleRefreshCloudMachineInfoDone()
|
---|
396 | {
|
---|
397 | /* Recache: */
|
---|
398 | recache();
|
---|
399 |
|
---|
400 | /* Notify listeners: */
|
---|
401 | emit sigRefreshFinished();
|
---|
402 |
|
---|
403 | /* Refresh again if required: */
|
---|
404 | if ( m_fUpdateRequiredByGlobalReason
|
---|
405 | || m_fUpdateRequiredByLocalReason)
|
---|
406 | updateInfoAsync(true /* delayed? */);
|
---|
407 | }
|
---|
408 |
|
---|
409 | void UIVirtualMachineItemCloud::prepare()
|
---|
410 | {
|
---|
411 | /* Prepare progress-task if necessary: */
|
---|
412 | if (itemType() == UIVirtualMachineItemType_CloudReal)
|
---|
413 | {
|
---|
414 | m_pProgressTaskRefresh = new UIProgressTaskRefreshCloudMachine(this, machine());
|
---|
415 | if (m_pProgressTaskRefresh)
|
---|
416 | {
|
---|
417 | connect(m_pProgressTaskRefresh, &UIProgressTaskRefreshCloudMachine::sigProgressStarted,
|
---|
418 | this, &UIVirtualMachineItemCloud::sigRefreshStarted);
|
---|
419 | connect(m_pProgressTaskRefresh, &UIProgressTaskRefreshCloudMachine::sigProgressFinished,
|
---|
420 | this, &UIVirtualMachineItemCloud::sltHandleRefreshCloudMachineInfoDone);
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | /* Recache finally: */
|
---|
425 | recache();
|
---|
426 | }
|
---|
427 |
|
---|
428 | void UIVirtualMachineItemCloud::cleanup()
|
---|
429 | {
|
---|
430 | /* Cleanup progress-task: */
|
---|
431 | delete m_pProgressTaskRefresh;
|
---|
432 | m_pProgressTaskRefresh = 0;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | #include "UIVirtualMachineItemCloud.moc"
|
---|