VirtualBox

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

Last change on this file since 90564 was 90564, checked in by vboxsync, 4 years ago

FE/Qt: bugref:10067: UINotificationCenter: Rework a way of public access through pointer, since center can be NULL.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1/* $Id: UIApplianceImportEditorWidget.cpp 90564 2021-08-07 11:12:13Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIApplianceImportEditorWidget class implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2020 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/* Qt includes: */
19#include <QCheckBox>
20#include <QGridLayout>
21#include <QLabel>
22#include <QTextEdit>
23#include <QVBoxLayout>
24
25/* GUI includes: */
26#include "QITreeView.h"
27#include "UIApplianceImportEditorWidget.h"
28#include "UICommon.h"
29#include "UIFilePathSelector.h"
30#include "UIMessageCenter.h"
31#include "UINotificationCenter.h"
32#include "UIWizardImportApp.h"
33
34/* COM includes: */
35#include "CAppliance.h"
36#include "CSystemProperties.h"
37
38
39////////////////////////////////////////////////////////////////////////////////
40// ImportSortProxyModel
41
42class ImportSortProxyModel: public UIApplianceSortProxyModel
43{
44public:
45 ImportSortProxyModel(QObject *pParent = NULL)
46 : UIApplianceSortProxyModel(pParent)
47 {
48 m_aFilteredList << KVirtualSystemDescriptionType_License;
49 }
50};
51
52////////////////////////////////////////////////////////////////////////////////
53// UIApplianceImportEditorWidget
54
55UIApplianceImportEditorWidget::UIApplianceImportEditorWidget(QWidget *pParent)
56 : UIApplianceEditorWidget(pParent)
57 , m_pPathSelectorLabel(0)
58 , m_pPathSelector(0)
59 , m_pImportHDsAsVDI(0)
60 , m_pMACComboBoxLabel(0)
61 , m_pMACComboBox(0)
62 , m_pOptionsLayout(0)
63 , m_pAdditionalOptionsLabel(0)
64{
65 prepareWidgets();
66}
67
68void UIApplianceImportEditorWidget::prepareWidgets()
69{
70 /* Create options layout: */
71 m_pOptionsLayout = new QGridLayout;
72 if (m_pOptionsLayout)
73 {
74 m_pOptionsLayout->setColumnStretch(0, 0);
75 m_pOptionsLayout->setColumnStretch(1, 1);
76
77 /* Create path selector label: */
78 m_pPathSelectorLabel = new QLabel;
79 if (m_pPathSelectorLabel)
80 {
81 m_pPathSelectorLabel->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
82
83 /* Add into layout: */
84 m_pOptionsLayout->addWidget(m_pPathSelectorLabel, 0, 0);
85 }
86
87 /* Create path selector editor: */
88 m_pPathSelector = new UIFilePathSelector;
89 if (m_pPathSelector)
90 {
91 m_pPathSelector->setResetEnabled(true);
92 m_pPathSelector->setDefaultPath(uiCommon().virtualBox().GetSystemProperties().GetDefaultMachineFolder());
93 m_pPathSelector->setPath(uiCommon().virtualBox().GetSystemProperties().GetDefaultMachineFolder());
94 m_pPathSelectorLabel->setBuddy(m_pPathSelector);
95
96 /* Add into layout: */
97 m_pOptionsLayout->addWidget(m_pPathSelector, 0, 1, 1, 2);
98 }
99
100 /* Create MAC address policy label: */
101 m_pMACComboBoxLabel = new QLabel;
102 if (m_pMACComboBoxLabel)
103 {
104 m_pMACComboBoxLabel->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
105
106 /* Add into layout: */
107 m_pOptionsLayout->addWidget(m_pMACComboBoxLabel, 1, 0);
108 }
109
110 /* Create MAC address policy combo: */
111 m_pMACComboBox = new QComboBox;
112 if (m_pMACComboBox)
113 {
114 m_pMACComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
115 m_pMACComboBoxLabel->setBuddy(m_pMACComboBox);
116
117 /* Add into layout: */
118 m_pOptionsLayout->addWidget(m_pMACComboBox, 1, 1, 1, 2);
119 }
120
121 /* Create additional options label: */
122 m_pAdditionalOptionsLabel = new QLabel;
123 if (m_pAdditionalOptionsLabel)
124 {
125 m_pAdditionalOptionsLabel->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
126
127 /* Add into layout: */
128 m_pOptionsLayout->addWidget(m_pAdditionalOptionsLabel, 2, 0);
129 }
130
131 /* Create import HDs as VDIs checkbox: */
132 m_pImportHDsAsVDI = new QCheckBox;
133 {
134 m_pImportHDsAsVDI->setCheckState(Qt::Checked);
135
136 /* Add into layout: */
137 m_pOptionsLayout->addWidget(m_pImportHDsAsVDI, 2, 1);
138 }
139
140 /* Add into layout: */
141 m_pLayout->addLayout(m_pOptionsLayout);
142 }
143
144 /* Populate MAC address import combo: */
145 populateMACAddressImportPolicies();
146 /* And connect signals afterwards: */
147 connect(m_pPathSelector, &UIFilePathSelector::pathChanged,
148 this, &UIApplianceImportEditorWidget::sltHandlePathChanged);
149 connect(m_pMACComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
150 this, &UIApplianceImportEditorWidget::sltHandleMACAddressImportPolicyComboChange);
151
152 /* Apply language settings: */
153 retranslateUi();
154}
155
156bool UIApplianceImportEditorWidget::setFile(const QString& strFile)
157{
158 bool fResult = false;
159 if (!strFile.isEmpty())
160 {
161 CProgress progress;
162 CVirtualBox vbox = uiCommon().virtualBox();
163 /* Create a appliance object */
164 m_pAppliance = new CAppliance(vbox.CreateAppliance());
165 fResult = m_pAppliance->isOk();
166 if (fResult)
167 {
168 /* Read the appliance */
169 progress = m_pAppliance->Read(strFile);
170 fResult = m_pAppliance->isOk();
171 if (fResult)
172 {
173 /* Show some progress, so the user know whats going on */
174 msgCenter().showModalProgressDialog(progress, tr("Reading Appliance ..."), ":/progress_reading_appliance_90px.png", this);
175 if (!progress.isOk() || progress.GetResultCode() != 0)
176 fResult = false;
177 else
178 {
179 /* Now we have to interpret that stuff */
180 m_pAppliance->Interpret();
181 fResult = m_pAppliance->isOk();
182 if (fResult)
183 {
184 if (m_pModel)
185 delete m_pModel;
186
187 QVector<CVirtualSystemDescription> vsds = m_pAppliance->GetVirtualSystemDescriptions();
188
189 m_pModel = new UIApplianceModel(vsds, m_pTreeViewSettings);
190
191 ImportSortProxyModel *pProxy = new ImportSortProxyModel(this);
192 pProxy->setSourceModel(m_pModel);
193 pProxy->sort(ApplianceViewSection_Description, Qt::DescendingOrder);
194
195 UIApplianceDelegate *pDelegate = new UIApplianceDelegate(pProxy, this);
196
197 /* Set our own model */
198 m_pTreeViewSettings->setModel(pProxy);
199 /* Set our own delegate */
200 m_pTreeViewSettings->setItemDelegate(pDelegate);
201 /* For now we hide the original column. This data is displayed as tooltip
202 also. */
203 m_pTreeViewSettings->setColumnHidden(ApplianceViewSection_OriginalValue, true);
204 m_pTreeViewSettings->expandAll();
205 /* Set model root index and make it current: */
206 m_pTreeViewSettings->setRootIndex(pProxy->mapFromSource(m_pModel->root()));
207 m_pTreeViewSettings->setCurrentIndex(pProxy->mapFromSource(m_pModel->root()));
208
209 /* Check for warnings & if there are one display them. */
210 bool fWarningsEnabled = false;
211 QVector<QString> warnings = m_pAppliance->GetWarnings();
212 if (warnings.size() > 0)
213 {
214 foreach (const QString& text, warnings)
215 m_pTextEditWarning->append("- " + text);
216 fWarningsEnabled = true;
217 }
218 m_pPaneWarning->setVisible(fWarningsEnabled);
219 }
220 }
221 }
222 }
223 if (!fResult)
224 {
225 if (!m_pAppliance->isOk())
226 msgCenter().cannotImportAppliance(*m_pAppliance, this);
227 else if (!progress.isNull() && (!progress.isOk() || progress.GetResultCode() != 0))
228 msgCenter().cannotImportAppliance(progress, m_pAppliance->GetPath(), this);
229 /* Delete the appliance in a case of an error */
230 delete m_pAppliance;
231 m_pAppliance = NULL;
232 }
233 }
234 /* Make sure we initialize model items with correct base folder path: */
235 if (m_pPathSelector)
236 sltHandlePathChanged(m_pPathSelector->path());
237
238 return fResult;
239}
240
241void UIApplianceImportEditorWidget::prepareImport()
242{
243 if (m_pAppliance)
244 m_pModel->putBack();
245}
246
247bool UIApplianceImportEditorWidget::import()
248{
249 if (m_pAppliance)
250 {
251 /* Configure appliance importing: */
252 QVector<KImportOptions> options;
253 if (m_pMACComboBox)
254 {
255 const MACAddressImportPolicy enmPolicy = m_pMACComboBox->currentData().value<MACAddressImportPolicy>();
256 switch (enmPolicy)
257 {
258 case MACAddressImportPolicy_KeepAllMACs:
259 options.append(KImportOptions_KeepAllMACs);
260 break;
261 case MACAddressImportPolicy_KeepNATMACs:
262 options.append(KImportOptions_KeepNATMACs);
263 break;
264 default:
265 break;
266 }
267 }
268 if (m_pImportHDsAsVDI->isChecked())
269 options.append(KImportOptions_ImportToVDI);
270
271 /* Import appliance: */
272 UINotificationProgressApplianceImport *pNotification = new UINotificationProgressApplianceImport(*m_pAppliance,
273 options);
274 gpNotificationCenter->append(pNotification);
275
276 /* Positive: */
277 return true;
278 }
279 return false;
280}
281
282QList<QPair<QString, QString> > UIApplianceImportEditorWidget::licenseAgreements() const
283{
284 QList<QPair<QString, QString> > list;
285
286 CVirtualSystemDescriptionVector vsds = m_pAppliance->GetVirtualSystemDescriptions();
287 for (int i = 0; i < vsds.size(); ++i)
288 {
289 QVector<QString> strLicense;
290 strLicense = vsds[i].GetValuesByType(KVirtualSystemDescriptionType_License,
291 KVirtualSystemDescriptionValueType_Original);
292 if (!strLicense.isEmpty())
293 {
294 QVector<QString> strName;
295 strName = vsds[i].GetValuesByType(KVirtualSystemDescriptionType_Name,
296 KVirtualSystemDescriptionValueType_Auto);
297 list << QPair<QString, QString>(strName.first(), strLicense.first());
298 }
299 }
300
301 return list;
302}
303
304void UIApplianceImportEditorWidget::retranslateUi()
305{
306 UIApplianceEditorWidget::retranslateUi();
307 if (m_pPathSelectorLabel)
308 m_pPathSelectorLabel->setText(tr("&Machine Base Folder:"));
309 if (m_pImportHDsAsVDI)
310 {
311 m_pImportHDsAsVDI->setText(tr("&Import hard drives as VDI"));
312 m_pImportHDsAsVDI->setToolTip(tr("When checked, all the hard drives that belong to this appliance will be imported in VDI format."));
313 }
314
315 /* Translate MAC address policy combo-box: */
316 m_pMACComboBoxLabel->setText(tr("MAC Address &Policy:"));
317 for (int i = 0; i < m_pMACComboBox->count(); ++i)
318 {
319 const MACAddressImportPolicy enmPolicy = m_pMACComboBox->itemData(i).value<MACAddressImportPolicy>();
320 switch (enmPolicy)
321 {
322 case MACAddressImportPolicy_KeepAllMACs:
323 {
324 m_pMACComboBox->setItemText(i, tr("Include all network adapter MAC addresses"));
325 m_pMACComboBox->setItemData(i, tr("Include all network adapter MAC addresses during importing."), Qt::ToolTipRole);
326 break;
327 }
328 case MACAddressImportPolicy_KeepNATMACs:
329 {
330 m_pMACComboBox->setItemText(i, tr("Include only NAT network adapter MAC addresses"));
331 m_pMACComboBox->setItemData(i, tr("Include only NAT network adapter MAC addresses during importing."), Qt::ToolTipRole);
332 break;
333 }
334 case MACAddressImportPolicy_StripAllMACs:
335 {
336 m_pMACComboBox->setItemText(i, tr("Generate new MAC addresses for all network adapters"));
337 m_pMACComboBox->setItemData(i, tr("Generate new MAC addresses for all network adapters during importing."), Qt::ToolTipRole);
338 break;
339 }
340 default:
341 break;
342 }
343 }
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 /* Map known import options to known MAC address import policies: */
369 QMap<KImportOptions, MACAddressImportPolicy> knownOptions;
370 knownOptions[KImportOptions_KeepAllMACs] = MACAddressImportPolicy_KeepAllMACs;
371 knownOptions[KImportOptions_KeepNATMACs] = MACAddressImportPolicy_KeepNATMACs;
372
373 /* Load currently supported import options: */
374 CSystemProperties comProperties = uiCommon().virtualBox().GetSystemProperties();
375 const QVector<KImportOptions> supportedOptions = comProperties.GetSupportedImportOptions();
376
377 /* Check which of supported options/policies are known: */
378 QList<MACAddressImportPolicy> supportedPolicies;
379 foreach (const KImportOptions &enmOption, supportedOptions)
380 if (knownOptions.contains(enmOption))
381 supportedPolicies << knownOptions.value(enmOption);
382
383 /* Add supported policies first: */
384 foreach (const MACAddressImportPolicy &enmPolicy, supportedPolicies)
385 m_pMACComboBox->addItem(QString(), QVariant::fromValue(enmPolicy));
386
387 /* Add hardcoded policy finally: */
388 m_pMACComboBox->addItem(QString(), QVariant::fromValue(MACAddressImportPolicy_StripAllMACs));
389
390 /* Set default: */
391 if (supportedPolicies.contains(MACAddressImportPolicy_KeepNATMACs))
392 setMACAddressImportPolicy(MACAddressImportPolicy_KeepNATMACs);
393 else
394 setMACAddressImportPolicy(MACAddressImportPolicy_StripAllMACs);
395}
396
397void UIApplianceImportEditorWidget::setMACAddressImportPolicy(MACAddressImportPolicy enmMACAddressImportPolicy)
398{
399 const int iIndex = m_pMACComboBox->findData(enmMACAddressImportPolicy);
400 AssertMsg(iIndex != -1, ("Data not found!"));
401 m_pMACComboBox->setCurrentIndex(iIndex);
402}
403
404void UIApplianceImportEditorWidget::sltHandleMACAddressImportPolicyComboChange()
405{
406 /* Update tool-tip: */
407 updateMACAddressImportPolicyComboToolTip();
408}
409
410void UIApplianceImportEditorWidget::updateMACAddressImportPolicyComboToolTip()
411{
412 const QString strCurrentToolTip = m_pMACComboBox->currentData(Qt::ToolTipRole).toString();
413 AssertMsg(!strCurrentToolTip.isEmpty(), ("Data not found!"));
414 m_pMACComboBox->setToolTip(strCurrentToolTip);
415}
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