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