1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt4 GUI ("VirtualBox"):
|
---|
4 | * VBoxExportAppliance class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "VBoxExportApplianceWzd.h"
|
---|
24 | #include "VBoxGlobal.h"
|
---|
25 | #include "QIWidgetValidator.h"
|
---|
26 | #include "VBoxProblemReporter.h"
|
---|
27 |
|
---|
28 | /* Qt includes */
|
---|
29 | #include <QDir>
|
---|
30 |
|
---|
31 | class VMListWidgetItems: public QListWidgetItem
|
---|
32 | {
|
---|
33 | public:
|
---|
34 | VMListWidgetItems (QPixmap &aIcon, QString &aText, QListWidget *aParent)
|
---|
35 | : QListWidgetItem (aIcon, aText, aParent) {}
|
---|
36 |
|
---|
37 | /* Sort like in the VM selector of the main window */
|
---|
38 | bool operator< (const QListWidgetItem &aOther) const
|
---|
39 | {
|
---|
40 | return text().toLower() < aOther.text().toLower();
|
---|
41 | }
|
---|
42 | };
|
---|
43 |
|
---|
44 | ////////////////////////////////////////////////////////////////////////////////
|
---|
45 | // VBoxExportApplianceWzd
|
---|
46 |
|
---|
47 | VBoxExportApplianceWzd::VBoxExportApplianceWzd (QWidget *aParent /* = NULL */, const QString& aSelectName /* = QString::null */)
|
---|
48 | : QIWithRetranslateUI<QIAbstractWizard> (aParent)
|
---|
49 | {
|
---|
50 | /* Apply UI decorations */
|
---|
51 | Ui::VBoxExportApplianceWzd::setupUi (this);
|
---|
52 |
|
---|
53 | /* Initialize wizard hdr */
|
---|
54 | initializeWizardHdr();
|
---|
55 |
|
---|
56 | /* Configure the VM selector widget */
|
---|
57 | mVMListWidget->setAlternatingRowColors (true);
|
---|
58 | mVMListWidget->setSelectionMode (QAbstractItemView::ExtendedSelection);
|
---|
59 |
|
---|
60 | /* Validator for the VM selector page */
|
---|
61 | mWValVMSelector = new QIWidgetValidator (mVMSelectPage, this);
|
---|
62 | connect (mWValVMSelector, SIGNAL (validityChanged (const QIWidgetValidator *)),
|
---|
63 | this, SLOT (enableNext (const QIWidgetValidator *)));
|
---|
64 | connect (mWValVMSelector, SIGNAL (isValidRequested (QIWidgetValidator *)),
|
---|
65 | this, SLOT (revalidate (QIWidgetValidator *)));
|
---|
66 | connect (mVMListWidget, SIGNAL (itemSelectionChanged()),
|
---|
67 | mWValVMSelector, SLOT (revalidate()));
|
---|
68 |
|
---|
69 | /* Fill the VM selector list */
|
---|
70 | addListViewVMItems (aSelectName);
|
---|
71 | mWValVMSelector->revalidate();
|
---|
72 |
|
---|
73 | /* Configure the file selector */
|
---|
74 | mFileSelector->setMode (VBoxFilePathSelectorWidget::Mode_File_Save);
|
---|
75 | mFileSelector->setResetEnabled (false);
|
---|
76 | mFileSelector->setFileDialogTitle (tr ("Select a file to export into"));
|
---|
77 | mFileSelector->setFileFilters (tr ("Open Virtualization Format (%1)").arg ("*.ovf"));
|
---|
78 | mFileSelector->setDefaultSaveExt ("ovf");
|
---|
79 | #ifdef Q_WS_MAC
|
---|
80 | /* Editable boxes are uncommon on the Mac */
|
---|
81 | mFileSelector->setEditable (false);
|
---|
82 | #endif /* Q_WS_MAC */
|
---|
83 |
|
---|
84 | /* Validator for the file selector page */
|
---|
85 | mWValFileSelector = new QIWidgetValidator (mFileSelectPage, this);
|
---|
86 | connect (mWValFileSelector, SIGNAL (validityChanged (const QIWidgetValidator *)),
|
---|
87 | this, SLOT (enableNext (const QIWidgetValidator *)));
|
---|
88 | connect (mWValFileSelector, SIGNAL (isValidRequested (QIWidgetValidator *)),
|
---|
89 | this, SLOT (revalidate (QIWidgetValidator *)));
|
---|
90 | connect (mFileSelector, SIGNAL (pathChanged (const QString &)),
|
---|
91 | mWValFileSelector, SLOT (revalidate()));
|
---|
92 |
|
---|
93 | mWValFileSelector->revalidate();
|
---|
94 |
|
---|
95 | /* Initialize wizard ftr */
|
---|
96 | initializeWizardFtr();
|
---|
97 |
|
---|
98 | retranslateUi();
|
---|
99 | }
|
---|
100 |
|
---|
101 | void VBoxExportApplianceWzd::retranslateUi()
|
---|
102 | {
|
---|
103 | /* Translate uic generated strings */
|
---|
104 | Ui::VBoxExportApplianceWzd::retranslateUi (this);
|
---|
105 |
|
---|
106 | mDefaultApplianceName = tr("Appliance");
|
---|
107 | }
|
---|
108 |
|
---|
109 | void VBoxExportApplianceWzd::revalidate (QIWidgetValidator *aWval)
|
---|
110 | {
|
---|
111 | /* Do individual validations for pages */
|
---|
112 | bool valid = aWval->isOtherValid();
|
---|
113 |
|
---|
114 | if (aWval == mWValVMSelector)
|
---|
115 | valid = mVMListWidget->selectedItems().size() > 0;
|
---|
116 |
|
---|
117 | if (aWval == mWValFileSelector)
|
---|
118 | valid = mFileSelector->path().toLower().endsWith (".ovf");
|
---|
119 |
|
---|
120 | aWval->setOtherValid (valid);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void VBoxExportApplianceWzd::enableNext (const QIWidgetValidator *aWval)
|
---|
124 | {
|
---|
125 | if (aWval == mWValFileSelector)
|
---|
126 | finishButton()->setEnabled (aWval->isValid());
|
---|
127 | else
|
---|
128 | nextButton (aWval->widget())->setEnabled (aWval->isValid());
|
---|
129 | }
|
---|
130 |
|
---|
131 | void VBoxExportApplianceWzd::accept()
|
---|
132 | {
|
---|
133 | /* Check if the file exists already, if yes get confirmation for
|
---|
134 | * overwriting from the user. */
|
---|
135 | if (!vboxProblem().askForOverridingFileIfExists (mFileSelector->path(), this))
|
---|
136 | return;
|
---|
137 | /* Export the VMs, on success we are finished */
|
---|
138 | if (exportVMs())
|
---|
139 | QIAbstractWizard::accept();
|
---|
140 | }
|
---|
141 |
|
---|
142 | void VBoxExportApplianceWzd::showNextPage()
|
---|
143 | {
|
---|
144 | /* We propose a filename the first time the second page is displayed */
|
---|
145 | if (sender() == mBtnNext1)
|
---|
146 | if (mFileSelector->path().isEmpty())
|
---|
147 | {
|
---|
148 | /* Set the default filename */
|
---|
149 | QString name = mDefaultApplianceName;
|
---|
150 | /* If it is one VM only, we use the VM name as file name */
|
---|
151 | if (mVMListWidget->selectedItems().size() == 1)
|
---|
152 | name = mVMListWidget->selectedItems().first()->text();
|
---|
153 |
|
---|
154 | mFileSelector->setPath (QDir::toNativeSeparators (QString ("%1/%2.ovf").arg (vboxGlobal().documentsPath())
|
---|
155 | .arg (name)));
|
---|
156 | mWValFileSelector->revalidate();
|
---|
157 | }
|
---|
158 |
|
---|
159 | QIAbstractWizard::showNextPage();
|
---|
160 | }
|
---|
161 |
|
---|
162 | void VBoxExportApplianceWzd::onPageShow()
|
---|
163 | {
|
---|
164 | /* Make sure all is properly translated & composed */
|
---|
165 | retranslateUi();
|
---|
166 |
|
---|
167 | QWidget *page = mPageStack->currentWidget();
|
---|
168 |
|
---|
169 | if (page == mFileSelectPage)
|
---|
170 | finishButton()->setDefault (true);
|
---|
171 | else
|
---|
172 | nextButton (page)->setDefault (true);
|
---|
173 | }
|
---|
174 |
|
---|
175 | void VBoxExportApplianceWzd::addListViewVMItems (const QString& aSelectName)
|
---|
176 | {
|
---|
177 | CVirtualBox vbox = vboxGlobal().virtualBox();
|
---|
178 | CMachineVector vec = vbox.GetMachines();
|
---|
179 | for (CMachineVector::ConstIterator m = vec.begin();
|
---|
180 | m != vec.end(); ++ m)
|
---|
181 | {
|
---|
182 | QPixmap icon;
|
---|
183 | QString name;
|
---|
184 | QString uuid;
|
---|
185 | bool enabled;
|
---|
186 | if (m->GetAccessible())
|
---|
187 | {
|
---|
188 | icon = vboxGlobal().vmGuestOSTypeIcon (m->GetOSTypeId()).scaled (16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
---|
189 | name = m->GetName();
|
---|
190 | uuid = m->GetId().toString();
|
---|
191 | enabled = m->GetSessionState() == KSessionState_Closed;
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | QString settingsFile = m->GetSettingsFilePath();
|
---|
196 | QFileInfo fi (settingsFile);
|
---|
197 | name = fi.completeSuffix().toLower() == "xml" ?
|
---|
198 | fi.completeBaseName() : fi.fileName();
|
---|
199 | icon = QPixmap (":/os_other.png").scaled (16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
---|
200 | enabled = false;
|
---|
201 | }
|
---|
202 | QListWidgetItem *item = new VMListWidgetItems (icon, name, mVMListWidget);
|
---|
203 | item->setData (Qt::UserRole, uuid);
|
---|
204 | if (!enabled)
|
---|
205 | item->setFlags (0);
|
---|
206 | mVMListWidget->addItem (item);
|
---|
207 | }
|
---|
208 | mVMListWidget->sortItems();
|
---|
209 |
|
---|
210 | /* Make sure aSelectName is initial selected in the list */
|
---|
211 | QList<QListWidgetItem *> list = mVMListWidget->findItems (aSelectName, Qt::MatchExactly);
|
---|
212 | if (list.size() > 0)
|
---|
213 | mVMListWidget->setCurrentItem (list.first());
|
---|
214 | }
|
---|
215 |
|
---|
216 | bool VBoxExportApplianceWzd::exportVMs()
|
---|
217 | {
|
---|
218 | CVirtualBox vbox = vboxGlobal().virtualBox();
|
---|
219 | /* Create a appliance object */
|
---|
220 | CAppliance appliance = vbox.CreateAppliance();
|
---|
221 | bool fResult = appliance.isOk();
|
---|
222 | if (fResult)
|
---|
223 | {
|
---|
224 | /* Iterate over all selected items */
|
---|
225 | QList<QListWidgetItem *> list = mVMListWidget->selectedItems();
|
---|
226 | foreach (const QListWidgetItem* item, list)
|
---|
227 | {
|
---|
228 | /* The VM uuid can be fetched by the UserRole */
|
---|
229 | QString uuid = item->data (Qt::UserRole).toString();
|
---|
230 | /* Get the machine with the uuid */
|
---|
231 | CMachine m = vbox.GetMachine (uuid);
|
---|
232 | fResult = m.isOk();
|
---|
233 | if (fResult)
|
---|
234 | {
|
---|
235 | /* Add the export description to our appliance object */
|
---|
236 | m.Export (appliance);
|
---|
237 | fResult = m.isOk();
|
---|
238 | if (!fResult)
|
---|
239 | {
|
---|
240 | vboxProblem().cannotExportAppliance (m, &appliance, this);
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | else
|
---|
245 | break;
|
---|
246 | }
|
---|
247 | /* Proceed if there where no errors */
|
---|
248 | if (fResult)
|
---|
249 | {
|
---|
250 | /* Write the appliance */
|
---|
251 | CProgress progress = appliance.Write (mFileSelector->path());
|
---|
252 | fResult = appliance.isOk();
|
---|
253 | if (fResult)
|
---|
254 | {
|
---|
255 | /* Show some progress, so the user know whats going on */
|
---|
256 | vboxProblem().showModalProgressDialog (progress, tr ("Exporting Appliance ..."), this);
|
---|
257 | if (!progress.isOk() || progress.GetResultCode() != 0)
|
---|
258 | {
|
---|
259 | vboxProblem().cannotExportAppliance (progress, &appliance, this);
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 | else
|
---|
263 | return true;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | if (!fResult)
|
---|
268 | vboxProblem().cannotExportAppliance (&appliance, this);
|
---|
269 |
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 |
|
---|