VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIApplianceImportEditorWidget.cpp@ 74611

Last change on this file since 74611 was 74611, checked in by vboxsync, 6 years ago

FE/Qt: bugref:9260. Build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1/* $Id: UIApplianceImportEditorWidget.cpp 74611 2018-10-04 12:15:23Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIApplianceImportEditorWidget class implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifdef VBOX_WITH_PRECOMPILED_HEADERS
19# include <precomp.h>
20#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
21
22/* Qt includes: */
23# include <QCheckBox>
24# include <QGridLayout>
25# include <QLabel>
26# include <QTextEdit>
27# include <QVBoxLayout>
28
29/* GUI includes: */
30# include "QIRichTextLabel.h"
31# include "QITreeView.h"
32# include "UIApplianceImportEditorWidget.h"
33# include "UIFilePathSelector.h"
34# include "UIMessageCenter.h"
35# include "UIWizardImportApp.h"
36# include "VBoxGlobal.h"
37
38/* COM includes: */
39# include "CAppliance.h"
40# include "CSystemProperties.h"
41
42#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
43
44
45////////////////////////////////////////////////////////////////////////////////
46// ImportSortProxyModel
47
48class ImportSortProxyModel: public UIApplianceSortProxyModel
49{
50public:
51 ImportSortProxyModel(QObject *pParent = NULL)
52 : UIApplianceSortProxyModel(pParent)
53 {
54 m_aFilteredList << KVirtualSystemDescriptionType_License;
55 }
56};
57
58////////////////////////////////////////////////////////////////////////////////
59// UIApplianceImportEditorWidget
60
61UIApplianceImportEditorWidget::UIApplianceImportEditorWidget(QWidget *pParent)
62 : UIApplianceEditorWidget(pParent)
63 , m_pPathSelectorLabel(0)
64 , m_pPathSelector(0)
65 , m_pImportHDsAsVDI(0)
66 , m_pMACComboBoxLabel(0)
67 , m_pMACComboBox(0)
68 , m_pOptionsLayout(0)
69{
70 prepareWidgets();
71}
72
73void UIApplianceImportEditorWidget::prepareWidgets()
74{
75 m_pPathSelectorLabel = new QIRichTextLabel(this);
76 if (m_pPathSelectorLabel)
77 {
78 m_pLayout->addWidget(m_pPathSelectorLabel);
79 }
80 m_pPathSelector = new UIFilePathSelector(this);
81 if (m_pPathSelector)
82 {
83 m_pPathSelector->setResetEnabled(true);
84 m_pPathSelector->setDefaultPath(vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder());
85 m_pPathSelector->setPath(vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder());
86 connect(m_pPathSelector, &UIFilePathSelector::pathChanged, this, &UIApplianceImportEditorWidget::sltHandlePathChanged);
87 m_pLayout->addWidget(m_pPathSelector);
88 }
89
90 m_pOptionsLayout = new QGridLayout;
91
92 m_pMACComboBox = new QComboBox;
93 if (m_pMACComboBox)
94 {
95 m_pMACComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
96 /* Add into layout: */
97 m_pOptionsLayout->addWidget(m_pMACComboBox, 0, 1);
98 }
99
100 /* Create format combo-box label: */
101 m_pMACComboBoxLabel = new QLabel;
102 if (m_pMACComboBoxLabel)
103 {
104 m_pMACComboBoxLabel->setAlignment(Qt::AlignLeft | Qt::AlignTrailing | Qt::AlignVCenter);
105 /* Add into layout: */
106 m_pOptionsLayout->addWidget(m_pMACComboBoxLabel, 0, 0);
107 }
108
109 if (m_pMACComboBoxLabel)
110 m_pMACComboBoxLabel->setBuddy(m_pMACComboBox);
111
112 m_pImportHDsAsVDI = new QCheckBox;
113 {
114 m_pOptionsLayout->addWidget(m_pImportHDsAsVDI, 2, 1);
115 m_pImportHDsAsVDI->setCheckState(Qt::Checked);
116 }
117
118
119 m_pLayout->addLayout(m_pOptionsLayout);
120 populateMACAddressImportPolicies();
121 connect(m_pMACComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
122 this, &UIApplianceImportEditorWidget::sltHandleMACAddressImportPolicyComboChange);
123 retranslateUi();
124}
125
126bool UIApplianceImportEditorWidget::setFile(const QString& strFile)
127{
128 bool fResult = false;
129 if (!strFile.isEmpty())
130 {
131 CProgress progress;
132 CVirtualBox vbox = vboxGlobal().virtualBox();
133 /* Create a appliance object */
134 m_pAppliance = new CAppliance(vbox.CreateAppliance());
135 fResult = m_pAppliance->isOk();
136 if (fResult)
137 {
138 /* Read the appliance */
139 progress = m_pAppliance->Read(strFile);
140 fResult = m_pAppliance->isOk();
141 if (fResult)
142 {
143 /* Show some progress, so the user know whats going on */
144 msgCenter().showModalProgressDialog(progress, tr("Reading Appliance ..."), ":/progress_reading_appliance_90px.png", this);
145 if (!progress.isOk() || progress.GetResultCode() != 0)
146 fResult = false;
147 else
148 {
149 /* Now we have to interpret that stuff */
150 m_pAppliance->Interpret();
151 fResult = m_pAppliance->isOk();
152 if (fResult)
153 {
154 if (m_pModel)
155 delete m_pModel;
156
157 QVector<CVirtualSystemDescription> vsds = m_pAppliance->GetVirtualSystemDescriptions();
158
159 m_pModel = new UIApplianceModel(vsds, m_pTreeViewSettings);
160
161 ImportSortProxyModel *pProxy = new ImportSortProxyModel(this);
162 pProxy->setSourceModel(m_pModel);
163 pProxy->sort(ApplianceViewSection_Description, Qt::DescendingOrder);
164
165 UIApplianceDelegate *pDelegate = new UIApplianceDelegate(pProxy, this);
166
167 /* Set our own model */
168 m_pTreeViewSettings->setModel(pProxy);
169 /* Set our own delegate */
170 m_pTreeViewSettings->setItemDelegate(pDelegate);
171 /* For now we hide the original column. This data is displayed as tooltip
172 also. */
173 m_pTreeViewSettings->setColumnHidden(ApplianceViewSection_OriginalValue, true);
174 m_pTreeViewSettings->expandAll();
175 /* Set model root index and make it current: */
176 m_pTreeViewSettings->setRootIndex(pProxy->mapFromSource(m_pModel->root()));
177 m_pTreeViewSettings->setCurrentIndex(pProxy->mapFromSource(m_pModel->root()));
178
179 /* Check for warnings & if there are one display them. */
180 bool fWarningsEnabled = false;
181 QVector<QString> warnings = m_pAppliance->GetWarnings();
182 if (warnings.size() > 0)
183 {
184 foreach (const QString& text, warnings)
185 m_pTextEditWarning->append("- " + text);
186 fWarningsEnabled = true;
187 }
188 m_pPaneWarning->setVisible(fWarningsEnabled);
189 }
190 }
191 }
192 }
193 if (!fResult)
194 {
195 if (!m_pAppliance->isOk())
196 msgCenter().cannotImportAppliance(*m_pAppliance, this);
197 else if (!progress.isNull() && (!progress.isOk() || progress.GetResultCode() != 0))
198 msgCenter().cannotImportAppliance(progress, m_pAppliance->GetPath(), this);
199 /* Delete the appliance in a case of an error */
200 delete m_pAppliance;
201 m_pAppliance = NULL;
202 }
203 }
204 /* Make sure we initialize model items with correct base folder path: */
205 if (m_pPathSelector)
206 sltHandlePathChanged(m_pPathSelector->path());
207
208 return fResult;
209}
210
211void UIApplianceImportEditorWidget::prepareImport()
212{
213 if (m_pAppliance)
214 m_pModel->putBack();
215}
216
217bool UIApplianceImportEditorWidget::import()
218{
219 if (m_pAppliance)
220 {
221 /* Start the import asynchronously */
222 CProgress progress;
223 QVector<KImportOptions> options;
224 if (m_pMACComboBox)
225 {
226 MACAddressImportPolicy macPolicy = static_cast<MACAddressImportPolicy>(m_pMACComboBox->currentIndex());
227 switch (macPolicy)
228 {
229 case MACAddressImportPolicy_KeepAllMACs:
230 options.append(KImportOptions_KeepAllMACs);
231 break;
232 case MACAddressImportPolicy_KeepNATMACs:
233 options.append(KImportOptions_KeepNATMACs);
234 break;
235 default:
236 break;
237 }
238 }
239
240 if (m_pImportHDsAsVDI->isChecked())
241 options.append(KImportOptions_ImportToVDI);
242 progress = m_pAppliance->ImportMachines(options);
243 bool fResult = m_pAppliance->isOk();
244 if (fResult)
245 {
246 /* Show some progress, so the user know whats going on */
247 msgCenter().showModalProgressDialog(progress, tr("Importing Appliance ..."), ":/progress_import_90px.png", this);
248 if (progress.GetCanceled())
249 return false;
250 if (!progress.isOk() || progress.GetResultCode() != 0)
251 {
252 msgCenter().cannotImportAppliance(progress, m_pAppliance->GetPath(), this);
253 return false;
254 }
255 else
256 return true;
257 }
258 if (!fResult)
259 msgCenter().cannotImportAppliance(*m_pAppliance, this);
260 }
261 return false;
262}
263
264QList<QPair<QString, QString> > UIApplianceImportEditorWidget::licenseAgreements() const
265{
266 QList<QPair<QString, QString> > list;
267
268 CVirtualSystemDescriptionVector vsds = m_pAppliance->GetVirtualSystemDescriptions();
269 for (int i = 0; i < vsds.size(); ++i)
270 {
271 QVector<QString> strLicense;
272 strLicense = vsds[i].GetValuesByType(KVirtualSystemDescriptionType_License,
273 KVirtualSystemDescriptionValueType_Original);
274 if (!strLicense.isEmpty())
275 {
276 QVector<QString> strName;
277 strName = vsds[i].GetValuesByType(KVirtualSystemDescriptionType_Name,
278 KVirtualSystemDescriptionValueType_Auto);
279 list << QPair<QString, QString>(strName.first(), strLicense.first());
280 }
281 }
282
283 return list;
284}
285
286void UIApplianceImportEditorWidget::retranslateUi()
287{
288 UIApplianceEditorWidget::retranslateUi();
289 if (m_pPathSelectorLabel)
290 m_pPathSelectorLabel->setText(UIWizardImportApp::tr("You can modify the base folder which will host all the virtual machines.\n"
291 "Home folders can also be individually (per virtual machine) modified."));
292 if (m_pImportHDsAsVDI)
293 {
294 m_pImportHDsAsVDI->setText(tr("&Import hard drives as VDI"));
295 m_pImportHDsAsVDI->setToolTip(tr("When checked a all the hard drives that belong to this appliance will be imported in VDI format"));
296 }
297
298 /* Translate MAC address policy combo-box: */
299 m_pMACComboBoxLabel->setText(tr("MAC Address &Policy:"));
300 m_pMACComboBox->setItemText(MACAddressImportPolicy_KeepAllMACs,
301 tr("Include all network adapter MAC addresses"));
302 m_pMACComboBox->setItemText(MACAddressImportPolicy_KeepNATMACs,
303 tr("Include only NAT network adapter MAC addresses"));
304 m_pMACComboBox->setItemText(MACAddressImportPolicy_StripAllMACs,
305 tr("Generate new MAC addresses for all network adapters"));
306 m_pMACComboBox->setItemData(MACAddressImportPolicy_KeepAllMACs,
307 tr("Include all network adapter MAC addresses in exported "
308 "during cloning."), Qt::ToolTipRole);
309 m_pMACComboBox->setItemData(MACAddressImportPolicy_KeepNATMACs,
310 tr("Include only NAT network adapter MAC addresses "
311 "during cloning."), Qt::ToolTipRole);
312 m_pMACComboBox->setItemData(MACAddressImportPolicy_StripAllMACs,
313 tr("Generate new MAC addresses for all network adapters "
314 "during cloning."), Qt::ToolTipRole);
315}
316
317void UIApplianceImportEditorWidget::sltHandlePathChanged(const QString &newPath)
318{
319 setVirtualSystemBaseFolder(newPath);
320}
321
322void UIApplianceImportEditorWidget::populateMACAddressImportPolicies()
323{
324 AssertReturnVoid(m_pMACComboBox->count() == 0);
325
326 /* Apply hardcoded policies list: */
327 for (int i = 0; i < (int)MACAddressImportPolicy_MAX; ++i)
328 {
329 m_pMACComboBox->addItem(QString::number(i));
330 m_pMACComboBox->setItemData(i, i);
331 }
332
333 /* Set default: */
334 setMACAddressImportPolicy(MACAddressImportPolicy_KeepNATMACs);
335}
336
337void UIApplianceImportEditorWidget::setMACAddressImportPolicy(MACAddressImportPolicy enmMACAddressImportPolicy)
338{
339 const int iIndex = m_pMACComboBox->findData((int)enmMACAddressImportPolicy);
340 AssertMsg(iIndex != -1, ("Data not found!"));
341 m_pMACComboBox->setCurrentIndex(iIndex);
342}
343
344void UIApplianceImportEditorWidget::sltHandleMACAddressImportPolicyComboChange()
345{
346 /* Update tool-tip: */
347 updateMACAddressImportPolicyComboToolTip();
348}
349
350void UIApplianceImportEditorWidget::updateMACAddressImportPolicyComboToolTip()
351{
352 const int iCurrentIndex = m_pMACComboBox->currentIndex();
353 const QString strCurrentToolTip = m_pMACComboBox->itemData(iCurrentIndex, Qt::ToolTipRole).toString();
354 AssertMsg(!strCurrentToolTip.isEmpty(), ("Data not found!"));
355 m_pMACComboBox->setToolTip(strCurrentToolTip);
356}
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