VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/VBoxExportApplianceWzd.cpp@ 17455

Last change on this file since 17455 was 17455, checked in by vboxsync, 16 years ago

FE/Qt4-OVF: corrected sorting of the VM's a little bit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
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
31class VMListWidgetItems: public QListWidgetItem
32{
33public:
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
47VBoxExportApplianceWzd::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);
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#ifdef Q_WS_MAC
79 /* Editable boxes are uncommon on the Mac */
80 mFileSelector->setEditable (false);
81#endif /* Q_WS_MAC */
82
83 /* Validator for the file selector page */
84 mWValFileSelector = new QIWidgetValidator (mFileSelectPage, this);
85 connect (mWValFileSelector, SIGNAL (validityChanged (const QIWidgetValidator *)),
86 this, SLOT (enableNext (const QIWidgetValidator *)));
87 connect (mWValFileSelector, SIGNAL (isValidRequested (QIWidgetValidator *)),
88 this, SLOT (revalidate (QIWidgetValidator *)));
89 connect (mFileSelector, SIGNAL (pathChanged (const QString &)),
90 mWValFileSelector, SLOT (revalidate()));
91
92 mWValFileSelector->revalidate();
93
94 /* Initialize wizard ftr */
95 initializeWizardFtr();
96
97 retranslateUi();
98}
99
100void VBoxExportApplianceWzd::retranslateUi()
101{
102 /* Translate uic generated strings */
103 Ui::VBoxExportApplianceWzd::retranslateUi (this);
104
105 mDefaultApplianceName = tr("Appliance");
106}
107
108void VBoxExportApplianceWzd::revalidate (QIWidgetValidator *aWval)
109{
110 /* Do individual validations for pages */
111 bool valid = aWval->isOtherValid();
112
113 if (aWval == mWValVMSelector)
114 valid = mVMListWidget->selectedItems().size() > 0;
115
116 if (aWval == mWValFileSelector)
117 valid = mFileSelector->path().toLower().endsWith (".ovf");
118
119 aWval->setOtherValid (valid);
120}
121
122void VBoxExportApplianceWzd::enableNext (const QIWidgetValidator *aWval)
123{
124 if (aWval == mWValFileSelector)
125 finishButton()->setEnabled (aWval->isValid());
126 else
127 nextButton (aWval->widget())->setEnabled (aWval->isValid());
128}
129
130void VBoxExportApplianceWzd::accept()
131{
132 /* Check if the file exists already, if yes get confirmation for
133 * overwriting from the user. */
134 QFileInfo fi (mFileSelector->path());
135 if (fi.exists())
136 if (!vboxProblem().askForOverridingAppliance (mFileSelector->path(), this))
137 return;
138 /* Export the VMs, on success we are finished */
139 if (exportVMs())
140 QIAbstractWizard::accept();
141}
142
143void VBoxExportApplianceWzd::showNextPage()
144{
145 /* We propose a filename the first time the second page is displayed */
146 if (sender() == mBtnNext1)
147 if (mFileSelector->path().isEmpty())
148 {
149 /* Set the default filename */
150 QString name = mDefaultApplianceName;
151 /* If it is one VM only, we use the VM name as file name */
152 if (mVMListWidget->selectedItems().size() == 1)
153 name = mVMListWidget->selectedItems().first()->text();
154
155 mFileSelector->setPath (QDir::toNativeSeparators (QString ("%1/%2.ovf").arg (QDir::currentPath())
156 .arg (name)));
157 mWValFileSelector->revalidate();
158 }
159
160 QIAbstractWizard::showNextPage();
161}
162
163void VBoxExportApplianceWzd::onPageShow()
164{
165 /* Make sure all is properly translated & composed */
166 retranslateUi();
167
168 QWidget *page = mPageStack->currentWidget();
169
170 if (page == mFileSelectPage)
171 finishButton()->setDefault (true);
172 else
173 nextButton (page)->setDefault (true);
174}
175
176void VBoxExportApplianceWzd::addListViewVMItems (const QString& aSelectName)
177{
178 CVirtualBox vbox = vboxGlobal().virtualBox();
179 CMachineVector vec = vbox.GetMachines2();
180 for (CMachineVector::ConstIterator m = vec.begin();
181 m != vec.end(); ++ m)
182 {
183 QPixmap icon;
184 QString name;
185 QString uuid;
186 bool enabled;
187 if (m->GetAccessible())
188 {
189 icon = vboxGlobal().vmGuestOSTypeIcon (m->GetOSTypeId()).scaled (16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
190 name = m->GetName();
191 uuid = m->GetId().toString();
192 enabled = m->GetSessionState() == KSessionState_Closed;
193 }
194 else
195 {
196 QString settingsFile = m->GetSettingsFilePath();
197 QFileInfo fi (settingsFile);
198 name = fi.completeSuffix().toLower() == "xml" ?
199 fi.completeBaseName() : fi.fileName();
200 icon = QPixmap (":/os_other.png").scaled (16, 16, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
201 enabled = false;
202 }
203 QListWidgetItem *item = new VMListWidgetItems (icon, name, mVMListWidget);
204 item->setData (Qt::UserRole, uuid);
205 if (!enabled)
206 item->setFlags (Qt::NoItemFlags);
207 mVMListWidget->addItem (item);
208 }
209 mVMListWidget->sortItems();
210
211 /* Make sure aSelectName is initial selected in the list */
212 QList<QListWidgetItem *> list = mVMListWidget->findItems (aSelectName, Qt::MatchExactly);
213 if (list.size() > 0)
214 mVMListWidget->setCurrentItem (list.first(), QItemSelectionModel::SelectCurrent);
215}
216
217bool VBoxExportApplianceWzd::exportVMs()
218{
219 CVirtualBox vbox = vboxGlobal().virtualBox();
220 /* Create a appliance object */
221 CAppliance appliance = vbox.CreateAppliance();
222 bool fResult = appliance.isOk();
223 if (fResult)
224 {
225 /* Iterate over all selected items */
226 QList<QListWidgetItem *> list = mVMListWidget->selectedItems();
227 foreach (const QListWidgetItem* item, list)
228 {
229 /* The VM uuid can be fetched by the UserRole */
230 QString uuid = item->data (Qt::UserRole).toString();
231 /* Get the machine with the uuid */
232 CMachine m = vbox.GetMachine (uuid);
233 fResult = m.isOk();
234 if (fResult)
235 {
236 /* Add the export description to our appliance object */
237 m.Export (appliance);
238 fResult = m.isOk();
239 if (!fResult)
240 break;
241 }
242 else
243 break;
244 }
245 /* Proceed if there where no errors */
246 if (fResult)
247 {
248 /* Write the appliance */
249 CProgress progress = appliance.Write (mFileSelector->path());
250 fResult = appliance.isOk();
251 if (fResult)
252 {
253 /* Show some progress, so the user know whats going on */
254 vboxProblem().showModalProgressDialog (progress, tr ("Exporting Appliance ..."), this);
255 if (!progress.isOk() || progress.GetResultCode() != 0)
256 {
257 vboxProblem().cannotExportAppliance (progress, &appliance, this);
258 return false;
259 }
260 else
261 return true;
262 }
263 }
264 }
265 if (!fResult)
266 vboxProblem().cannotExportAppliance (&appliance, this);
267
268 return false;
269}
270
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette