VirtualBox

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

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
1/* $Id: UIApplianceImportEditorWidget.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIApplianceImportEditorWidget class implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2019 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 , m_pAdditionalOptionsLabel(0)
70{
71 prepareWidgets();
72}
73
74void UIApplianceImportEditorWidget::prepareWidgets()
75{
76 /* Create path selector label: */
77 m_pPathSelectorLabel = new QIRichTextLabel(this);
78 if (m_pPathSelectorLabel)
79 {
80 /* Add into layout: */
81 m_pLayout->addWidget(m_pPathSelectorLabel);
82 }
83
84 /* Create path selector editor: */
85 m_pPathSelector = new UIFilePathSelector(this);
86 if (m_pPathSelector)
87 {
88 m_pPathSelector->setResetEnabled(true);
89 m_pPathSelector->setDefaultPath(vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder());
90 m_pPathSelector->setPath(vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder());
91 connect(m_pPathSelector, &UIFilePathSelector::pathChanged, this, &UIApplianceImportEditorWidget::sltHandlePathChanged);
92
93 /* Add into layout: */
94 m_pLayout->addWidget(m_pPathSelector);
95 }
96
97 /* Create options layout: */
98 m_pOptionsLayout = new QGridLayout;
99 if (m_pOptionsLayout)
100 {
101 m_pOptionsLayout->setColumnStretch(0, 0);
102 m_pOptionsLayout->setColumnStretch(1, 1);
103
104 /* Create MAC address policy label: */
105 m_pMACComboBoxLabel = new QLabel;
106 if (m_pMACComboBoxLabel)
107 {
108 m_pMACComboBoxLabel->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
109
110 /* Add into layout: */
111 m_pOptionsLayout->addWidget(m_pMACComboBoxLabel, 0, 0);
112 }
113
114 /* Create MAC address policy combo: */
115 m_pMACComboBox = new QComboBox;
116 if (m_pMACComboBox)
117 {
118 m_pMACComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
119 m_pMACComboBoxLabel->setBuddy(m_pMACComboBox);
120
121 /* Add into layout: */
122 m_pOptionsLayout->addWidget(m_pMACComboBox, 0, 1, 1, 2);
123 }
124
125 /* Create additional options label: */
126 m_pAdditionalOptionsLabel = new QLabel;
127 if (m_pAdditionalOptionsLabel)
128 {
129 m_pAdditionalOptionsLabel->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
130
131 /* Add into layout: */
132 m_pOptionsLayout->addWidget(m_pAdditionalOptionsLabel, 1, 0);
133 }
134
135 /* Create import HDs as VDIs checkbox: */
136 m_pImportHDsAsVDI = new QCheckBox;
137 {
138 m_pImportHDsAsVDI->setCheckState(Qt::Checked);
139
140 /* Add into layout: */
141 m_pOptionsLayout->addWidget(m_pImportHDsAsVDI, 1, 1);
142 }
143
144 /* Add into layout: */
145 m_pLayout->addLayout(m_pOptionsLayout);
146 }
147
148 /* Populate MAC address import combo: */
149 populateMACAddressImportPolicies();
150 /* And connect this combo' signals afterwards: */
151 connect(m_pMACComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
152 this, &UIApplianceImportEditorWidget::sltHandleMACAddressImportPolicyComboChange);
153
154 /* Apply language settings: */
155 retranslateUi();
156}
157
158bool UIApplianceImportEditorWidget::setFile(const QString& strFile)
159{
160 bool fResult = false;
161 if (!strFile.isEmpty())
162 {
163 CProgress progress;
164 CVirtualBox vbox = vboxGlobal().virtualBox();
165 /* Create a appliance object */
166 m_pAppliance = new CAppliance(vbox.CreateAppliance());
167 fResult = m_pAppliance->isOk();
168 if (fResult)
169 {
170 /* Read the appliance */
171 progress = m_pAppliance->Read(strFile);
172 fResult = m_pAppliance->isOk();
173 if (fResult)
174 {
175 /* Show some progress, so the user know whats going on */
176 msgCenter().showModalProgressDialog(progress, tr("Reading Appliance ..."), ":/progress_reading_appliance_90px.png", this);
177 if (!progress.isOk() || progress.GetResultCode() != 0)
178 fResult = false;
179 else
180 {
181 /* Now we have to interpret that stuff */
182 m_pAppliance->Interpret();
183 fResult = m_pAppliance->isOk();
184 if (fResult)
185 {
186 if (m_pModel)
187 delete m_pModel;
188
189 QVector<CVirtualSystemDescription> vsds = m_pAppliance->GetVirtualSystemDescriptions();
190
191 m_pModel = new UIApplianceModel(vsds, m_pTreeViewSettings);
192
193 ImportSortProxyModel *pProxy = new ImportSortProxyModel(this);
194 pProxy->setSourceModel(m_pModel);
195 pProxy->sort(ApplianceViewSection_Description, Qt::DescendingOrder);
196
197 UIApplianceDelegate *pDelegate = new UIApplianceDelegate(pProxy, this);
198
199 /* Set our own model */
200 m_pTreeViewSettings->setModel(pProxy);
201 /* Set our own delegate */
202 m_pTreeViewSettings->setItemDelegate(pDelegate);
203 /* For now we hide the original column. This data is displayed as tooltip
204 also. */
205 m_pTreeViewSettings->setColumnHidden(ApplianceViewSection_OriginalValue, true);
206 m_pTreeViewSettings->expandAll();
207 /* Set model root index and make it current: */
208 m_pTreeViewSettings->setRootIndex(pProxy->mapFromSource(m_pModel->root()));
209 m_pTreeViewSettings->setCurrentIndex(pProxy->mapFromSource(m_pModel->root()));
210
211 /* Check for warnings & if there are one display them. */
212 bool fWarningsEnabled = false;
213 QVector<QString> warnings = m_pAppliance->GetWarnings();
214 if (warnings.size() > 0)
215 {
216 foreach (const QString& text, warnings)
217 m_pTextEditWarning->append("- " + text);
218 fWarningsEnabled = true;
219 }
220 m_pPaneWarning->setVisible(fWarningsEnabled);
221 }
222 }
223 }
224 }
225 if (!fResult)
226 {
227 if (!m_pAppliance->isOk())
228 msgCenter().cannotImportAppliance(*m_pAppliance, this);
229 else if (!progress.isNull() && (!progress.isOk() || progress.GetResultCode() != 0))
230 msgCenter().cannotImportAppliance(progress, m_pAppliance->GetPath(), this);
231 /* Delete the appliance in a case of an error */
232 delete m_pAppliance;
233 m_pAppliance = NULL;
234 }
235 }
236 /* Make sure we initialize model items with correct base folder path: */
237 if (m_pPathSelector)
238 sltHandlePathChanged(m_pPathSelector->path());
239
240 return fResult;
241}
242
243void UIApplianceImportEditorWidget::prepareImport()
244{
245 if (m_pAppliance)
246 m_pModel->putBack();
247}
248
249bool UIApplianceImportEditorWidget::import()
250{
251 if (m_pAppliance)
252 {
253 /* Start the import asynchronously */
254 CProgress progress;
255 QVector<KImportOptions> options;
256 if (m_pMACComboBox)
257 {
258 MACAddressImportPolicy macPolicy = static_cast<MACAddressImportPolicy>(m_pMACComboBox->currentIndex());
259 switch (macPolicy)
260 {
261 case MACAddressImportPolicy_KeepAllMACs:
262 options.append(KImportOptions_KeepAllMACs);
263 break;
264 case MACAddressImportPolicy_KeepNATMACs:
265 options.append(KImportOptions_KeepNATMACs);
266 break;
267 default:
268 break;
269 }
270 }
271
272 if (m_pImportHDsAsVDI->isChecked())
273 options.append(KImportOptions_ImportToVDI);
274 progress = m_pAppliance->ImportMachines(options);
275 bool fResult = m_pAppliance->isOk();
276 if (fResult)
277 {
278 /* Show some progress, so the user know whats going on */
279 msgCenter().showModalProgressDialog(progress, tr("Importing Appliance ..."), ":/progress_import_90px.png", this);
280 if (progress.GetCanceled())
281 return false;
282 if (!progress.isOk() || progress.GetResultCode() != 0)
283 {
284 msgCenter().cannotImportAppliance(progress, m_pAppliance->GetPath(), this);
285 return false;
286 }
287 else
288 return true;
289 }
290 if (!fResult)
291 msgCenter().cannotImportAppliance(*m_pAppliance, this);
292 }
293 return false;
294}
295
296QList<QPair<QString, QString> > UIApplianceImportEditorWidget::licenseAgreements() const
297{
298 QList<QPair<QString, QString> > list;
299
300 CVirtualSystemDescriptionVector vsds = m_pAppliance->GetVirtualSystemDescriptions();
301 for (int i = 0; i < vsds.size(); ++i)
302 {
303 QVector<QString> strLicense;
304 strLicense = vsds[i].GetValuesByType(KVirtualSystemDescriptionType_License,
305 KVirtualSystemDescriptionValueType_Original);
306 if (!strLicense.isEmpty())
307 {
308 QVector<QString> strName;
309 strName = vsds[i].GetValuesByType(KVirtualSystemDescriptionType_Name,
310 KVirtualSystemDescriptionValueType_Auto);
311 list << QPair<QString, QString>(strName.first(), strLicense.first());
312 }
313 }
314
315 return list;
316}
317
318void UIApplianceImportEditorWidget::retranslateUi()
319{
320 UIApplianceEditorWidget::retranslateUi();
321 if (m_pPathSelectorLabel)
322 m_pPathSelectorLabel->setText(UIWizardImportApp::tr("You can modify the base folder which will host all the virtual machines. "
323 "Home folders can also be individually (per virtual machine) modified."));
324 if (m_pImportHDsAsVDI)
325 {
326 m_pImportHDsAsVDI->setText(tr("&Import hard drives as VDI"));
327 m_pImportHDsAsVDI->setToolTip(tr("When checked, all the hard drives that belong to this appliance will be imported in VDI format."));
328 }
329
330 /* Translate MAC address policy combo-box: */
331 m_pMACComboBoxLabel->setText(tr("MAC Address &Policy:"));
332 m_pMACComboBox->setItemText(MACAddressImportPolicy_KeepAllMACs,
333 tr("Include all network adapter MAC addresses"));
334 m_pMACComboBox->setItemText(MACAddressImportPolicy_KeepNATMACs,
335 tr("Include only NAT network adapter MAC addresses"));
336 m_pMACComboBox->setItemText(MACAddressImportPolicy_StripAllMACs,
337 tr("Generate new MAC addresses for all network adapters"));
338 m_pMACComboBox->setItemData(MACAddressImportPolicy_KeepAllMACs,
339 tr("Include all network adapter MAC addresses during cloning."), Qt::ToolTipRole);
340 m_pMACComboBox->setItemData(MACAddressImportPolicy_KeepNATMACs,
341 tr("Include only NAT network adapter MAC addresses during cloning."), Qt::ToolTipRole);
342 m_pMACComboBox->setItemData(MACAddressImportPolicy_StripAllMACs,
343 tr("Generate new MAC addresses for all network adapters during cloning."), Qt::ToolTipRole);
344
345 m_pAdditionalOptionsLabel->setText(tr("Additional Options:"));
346
347#if 0 /* this may be needed if contents became dinamical to avoid label jumping */
348 QList<QWidget*> labels;
349 labels << m_pMACComboBoxLabel;
350 labels << m_pAdditionalOptionsLabel;
351
352 int iMaxWidth = 0;
353 foreach (QWidget *pLabel, labels)
354 iMaxWidth = qMax(iMaxWidth, pLabel->minimumSizeHint().width());
355 m_pOptionsLayout->setColumnMinimumWidth(0, iMaxWidth);
356#endif /* this may be needed if contents became dinamical to avoid label jumping */
357}
358
359void UIApplianceImportEditorWidget::sltHandlePathChanged(const QString &newPath)
360{
361 setVirtualSystemBaseFolder(newPath);
362}
363
364void UIApplianceImportEditorWidget::populateMACAddressImportPolicies()
365{
366 AssertReturnVoid(m_pMACComboBox->count() == 0);
367
368 /* Apply hardcoded policies list: */
369 for (int i = 0; i < (int)MACAddressImportPolicy_MAX; ++i)
370 {
371 m_pMACComboBox->addItem(QString::number(i));
372 m_pMACComboBox->setItemData(i, i);
373 }
374
375 /* Set default: */
376 setMACAddressImportPolicy(MACAddressImportPolicy_KeepNATMACs);
377}
378
379void UIApplianceImportEditorWidget::setMACAddressImportPolicy(MACAddressImportPolicy enmMACAddressImportPolicy)
380{
381 const int iIndex = m_pMACComboBox->findData((int)enmMACAddressImportPolicy);
382 AssertMsg(iIndex != -1, ("Data not found!"));
383 m_pMACComboBox->setCurrentIndex(iIndex);
384}
385
386void UIApplianceImportEditorWidget::sltHandleMACAddressImportPolicyComboChange()
387{
388 /* Update tool-tip: */
389 updateMACAddressImportPolicyComboToolTip();
390}
391
392void UIApplianceImportEditorWidget::updateMACAddressImportPolicyComboToolTip()
393{
394 const int iCurrentIndex = m_pMACComboBox->currentIndex();
395 const QString strCurrentToolTip = m_pMACComboBox->itemData(iCurrentIndex, Qt::ToolTipRole).toString();
396 AssertMsg(!strCurrentToolTip.isEmpty(), ("Data not found!"));
397 m_pMACComboBox->setToolTip(strCurrentToolTip);
398}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use