1 | /* $Id: UIWizardExportAppPageVMs.cpp 103988 2024-03-21 13:49:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIWizardExportAppPageVMs class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 <QListWidget>
|
---|
30 | #include <QVBoxLayout>
|
---|
31 |
|
---|
32 | /* GUI includes: */
|
---|
33 | #include "QIRichTextLabel.h"
|
---|
34 | #include "UICommon.h"
|
---|
35 | #include "UIGlobalSession.h"
|
---|
36 | #include "UIIconPool.h"
|
---|
37 | #include "UIMessageCenter.h"
|
---|
38 | #include "UIWizardExportApp.h"
|
---|
39 | #include "UIWizardExportAppPageVMs.h"
|
---|
40 |
|
---|
41 | /* COM includes: */
|
---|
42 | #include "CMachine.h"
|
---|
43 |
|
---|
44 | /* Namespaces: */
|
---|
45 | using namespace UIWizardExportAppVMs;
|
---|
46 |
|
---|
47 |
|
---|
48 | /** QListWidgetItem subclass for Export Appliance wizard VM list. */
|
---|
49 | class UIVMListWidgetItem : public QListWidgetItem
|
---|
50 | {
|
---|
51 | public:
|
---|
52 |
|
---|
53 | /** Constructs VM list item passing @a pixIcon, @a strText and @a pParent to the base-class.
|
---|
54 | * @param strUuid Brings the machine ID.
|
---|
55 | * @param fInSaveState Brings whether machine is in Saved state. */
|
---|
56 | UIVMListWidgetItem(QPixmap &pixIcon, QString &strText, QUuid uUuid, bool fInSaveState, QListWidget *pParent)
|
---|
57 | : QListWidgetItem(pixIcon, strText, pParent)
|
---|
58 | , m_uUuid(uUuid)
|
---|
59 | , m_fInSaveState(fInSaveState)
|
---|
60 | {}
|
---|
61 |
|
---|
62 | /** Returns whether this item is less than @a other. */
|
---|
63 | bool operator<(const QListWidgetItem &other) const RT_OVERRIDE RT_FINAL
|
---|
64 | {
|
---|
65 | return text().toLower() < other.text().toLower();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** Returns the machine ID. */
|
---|
69 | QUuid uuid() const { return m_uUuid; }
|
---|
70 | /** Returns whether machine is in Saved state. */
|
---|
71 | bool isInSaveState() const { return m_fInSaveState; }
|
---|
72 |
|
---|
73 | private:
|
---|
74 |
|
---|
75 | /** Holds the machine ID. */
|
---|
76 | QUuid m_uUuid;
|
---|
77 | /** Holds whether machine is in Saved state. */
|
---|
78 | bool m_fInSaveState;
|
---|
79 | };
|
---|
80 |
|
---|
81 |
|
---|
82 | /*********************************************************************************************************************************
|
---|
83 | * Class UIWizardExportAppVMs implementation. *
|
---|
84 | *********************************************************************************************************************************/
|
---|
85 |
|
---|
86 | void UIWizardExportAppVMs::populateVMItems(QListWidget *pVMSelector, const QStringList &selectedVMNames)
|
---|
87 | {
|
---|
88 | /* Add all VM items into VM selector: */
|
---|
89 | foreach (const CMachine &comMachine, gpGlobalSession->virtualBox().GetMachines())
|
---|
90 | {
|
---|
91 | QPixmap pixIcon;
|
---|
92 | QString strName;
|
---|
93 | QUuid uUuid;
|
---|
94 | bool fInSaveState = false;
|
---|
95 | bool fEnabled = false;
|
---|
96 | const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
|
---|
97 | if (comMachine.GetAccessible())
|
---|
98 | {
|
---|
99 | pixIcon = generalIconPool().userMachinePixmapDefault(comMachine);
|
---|
100 | if (pixIcon.isNull())
|
---|
101 | pixIcon = generalIconPool().guestOSTypePixmapDefault(comMachine.GetOSTypeId());
|
---|
102 | strName = comMachine.GetName();
|
---|
103 | uUuid = comMachine.GetId();
|
---|
104 | fEnabled = comMachine.GetSessionState() == KSessionState_Unlocked;
|
---|
105 | fInSaveState = comMachine.GetState() == KMachineState_Saved || comMachine.GetState() == KMachineState_AbortedSaved;
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | QFileInfo fi(comMachine.GetSettingsFilePath());
|
---|
110 | strName = UICommon::hasAllowedExtension(fi.completeSuffix(), VBoxFileExts) ? fi.completeBaseName() : fi.fileName();
|
---|
111 | pixIcon = QPixmap(":/os_other.png").scaled(iIconMetric, iIconMetric, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
---|
112 | }
|
---|
113 | QListWidgetItem *pItem = new UIVMListWidgetItem(pixIcon, strName, uUuid, fInSaveState, pVMSelector);
|
---|
114 | if (!fEnabled)
|
---|
115 | pItem->setFlags(Qt::ItemFlags());
|
---|
116 | pVMSelector->addItem(pItem);
|
---|
117 | }
|
---|
118 | pVMSelector->sortItems();
|
---|
119 |
|
---|
120 | /* Choose initially selected items (if passed): */
|
---|
121 | foreach (const QString &strSelectedVMName, selectedVMNames)
|
---|
122 | {
|
---|
123 | const QList<QListWidgetItem*> list = pVMSelector->findItems(strSelectedVMName, Qt::MatchExactly);
|
---|
124 | if (list.size() > 0)
|
---|
125 | {
|
---|
126 | if (pVMSelector->selectedItems().isEmpty())
|
---|
127 | pVMSelector->setCurrentItem(list.first());
|
---|
128 | else
|
---|
129 | list.first()->setSelected(true);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | void UIWizardExportAppVMs::refreshSavedMachines(QStringList &savedMachines, QListWidget *pVMSelector)
|
---|
135 | {
|
---|
136 | savedMachines.clear();
|
---|
137 | foreach (QListWidgetItem *pItem, pVMSelector->selectedItems())
|
---|
138 | if (static_cast<UIVMListWidgetItem*>(pItem)->isInSaveState())
|
---|
139 | savedMachines << pItem->text();
|
---|
140 | }
|
---|
141 |
|
---|
142 | QStringList UIWizardExportAppVMs::machineNames(QListWidget *pVMSelector)
|
---|
143 | {
|
---|
144 | /* Prepare list: */
|
---|
145 | QStringList names;
|
---|
146 | /* Iterate over all the selected items: */
|
---|
147 | foreach (QListWidgetItem *pItem, pVMSelector->selectedItems())
|
---|
148 | names << pItem->text();
|
---|
149 | /* Return result list: */
|
---|
150 | return names;
|
---|
151 | }
|
---|
152 |
|
---|
153 | QList<QUuid> UIWizardExportAppVMs::machineIDs(QListWidget *pVMSelector)
|
---|
154 | {
|
---|
155 | /* Prepare list: */
|
---|
156 | QList<QUuid> ids;
|
---|
157 | /* Iterate over all the selected items: */
|
---|
158 | foreach (QListWidgetItem *pItem, pVMSelector->selectedItems())
|
---|
159 | ids.append(static_cast<UIVMListWidgetItem*>(pItem)->uuid());
|
---|
160 | /* Return result list: */
|
---|
161 | return ids;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /*********************************************************************************************************************************
|
---|
166 | * Class UIWizardExportAppPageVMs implementation. *
|
---|
167 | *********************************************************************************************************************************/
|
---|
168 |
|
---|
169 | UIWizardExportAppPageVMs::UIWizardExportAppPageVMs(const QStringList &selectedVMNames, bool fFastTravelToNextPage)
|
---|
170 | : m_selectedVMNames(selectedVMNames)
|
---|
171 | , m_fFastTravelToNextPage(fFastTravelToNextPage)
|
---|
172 | , m_pLabelMain(0)
|
---|
173 | , m_pVMSelector(0)
|
---|
174 | {
|
---|
175 | /* Prepare main layout: */
|
---|
176 | QVBoxLayout *pLayoutMain = new QVBoxLayout(this);
|
---|
177 | if (pLayoutMain)
|
---|
178 | {
|
---|
179 | /* Prepare main label: */
|
---|
180 | m_pLabelMain = new QIRichTextLabel(this);
|
---|
181 | if (m_pLabelMain)
|
---|
182 | pLayoutMain->addWidget(m_pLabelMain);
|
---|
183 |
|
---|
184 | /* Prepare VM selector: */
|
---|
185 | m_pVMSelector = new QListWidget(this);
|
---|
186 | if (m_pVMSelector)
|
---|
187 | {
|
---|
188 | m_pVMSelector->setAlternatingRowColors(true);
|
---|
189 | m_pVMSelector->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
---|
190 | pLayoutMain->addWidget(m_pVMSelector);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* Setup connections: */
|
---|
195 | connect(m_pVMSelector, &QListWidget::itemSelectionChanged,
|
---|
196 | this, &UIWizardExportAppPageVMs::sltHandleVMItemSelectionChanged);
|
---|
197 | }
|
---|
198 |
|
---|
199 | UIWizardExportApp *UIWizardExportAppPageVMs::wizard() const
|
---|
200 | {
|
---|
201 | return qobject_cast<UIWizardExportApp*>(UINativeWizardPage::wizard());
|
---|
202 | }
|
---|
203 |
|
---|
204 | void UIWizardExportAppPageVMs::sltRetranslateUI()
|
---|
205 | {
|
---|
206 | /* Translate page: */
|
---|
207 | setTitle(UIWizardExportApp::tr("Virtual machines"));
|
---|
208 |
|
---|
209 | /* Translate widgets: */
|
---|
210 | m_pLabelMain->setText(UIWizardExportApp::tr("<p>Please select the virtual machines that should be added to the appliance. "
|
---|
211 | "You can select more than one. Please note that these machines have to be "
|
---|
212 | "turned off before they can be exported.</p>"));
|
---|
213 | }
|
---|
214 |
|
---|
215 | void UIWizardExportAppPageVMs::initializePage()
|
---|
216 | {
|
---|
217 | /* Populate VM items: */
|
---|
218 | populateVMItems(m_pVMSelector, m_selectedVMNames);
|
---|
219 | /* Translate page: */
|
---|
220 | sltRetranslateUI();
|
---|
221 |
|
---|
222 | /* Now, when we are ready, we can
|
---|
223 | * fast traver to page 2 if requested: */
|
---|
224 | if (m_fFastTravelToNextPage)
|
---|
225 | wizard()->goForward();
|
---|
226 | }
|
---|
227 |
|
---|
228 | bool UIWizardExportAppPageVMs::isComplete() const
|
---|
229 | {
|
---|
230 | /* Initial result: */
|
---|
231 | bool fResult = true;
|
---|
232 |
|
---|
233 | /* There should be at least one VM selected: */
|
---|
234 | fResult = wizard()->machineNames().size() > 0;
|
---|
235 |
|
---|
236 | /* Return result: */
|
---|
237 | return fResult;
|
---|
238 | }
|
---|
239 |
|
---|
240 | bool UIWizardExportAppPageVMs::validatePage()
|
---|
241 | {
|
---|
242 | /* Initial result: */
|
---|
243 | bool fResult = true;
|
---|
244 |
|
---|
245 | /* Ask user about machines which are in Saved state currently: */
|
---|
246 | QStringList savedMachines;
|
---|
247 | refreshSavedMachines(savedMachines, m_pVMSelector);
|
---|
248 | if (!savedMachines.isEmpty())
|
---|
249 | fResult = msgCenter().confirmExportMachinesInSaveState(savedMachines, this);
|
---|
250 |
|
---|
251 | /* Return result: */
|
---|
252 | return fResult;
|
---|
253 | }
|
---|
254 |
|
---|
255 | void UIWizardExportAppPageVMs::sltHandleVMItemSelectionChanged()
|
---|
256 | {
|
---|
257 | /* Update wizard fields: */
|
---|
258 | wizard()->setMachineNames(machineNames(m_pVMSelector));
|
---|
259 | wizard()->setMachineIDs(machineIDs(m_pVMSelector));
|
---|
260 |
|
---|
261 | /* Notify about changes: */
|
---|
262 | emit completeChanged();
|
---|
263 | }
|
---|