Index: /trunk/src/VBox/Frontends/VirtualBox/src/wizards/exportappliance/UIWizardExportApp.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/wizards/exportappliance/UIWizardExportApp.cpp	(revision 79696)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/wizards/exportappliance/UIWizardExportApp.cpp	(revision 79697)
@@ -251,24 +251,40 @@
 bool UIWizardExportApp::exportVMs(CAppliance &comAppliance)
 {
-    /* Get the map of the password IDs: */
-    EncryptedMediumMap encryptedMedia;
-    foreach (const QString &strPasswordId, comAppliance.GetPasswordIds())
-        foreach (const QUuid &uMediumId, comAppliance.GetMediumIdsForPasswordId(strPasswordId))
-            encryptedMedia.insert(strPasswordId, uMediumId);
-
-    /* Ask for the disk encryption passwords if necessary: */
-    if (!encryptedMedia.isEmpty())
-    {
-        /* Modal dialog can be destroyed in own event-loop as a part of application
-         * termination procedure. We have to make sure that the dialog pointer is
-         * always up to date. So we are wrapping created dialog with QPointer. */
-        QPointer<UIAddDiskEncryptionPasswordDialog> pDlg =
-             new UIAddDiskEncryptionPasswordDialog(this,
-                                                   window()->windowTitle(),
-                                                   encryptedMedia);
-
-        /* Execute the dialog: */
-        if (pDlg->exec() == QDialog::Accepted)
-        {
+    /* Prepare result: */
+    bool fResult = false;
+
+    /* New Cloud VM wizard can be created
+     * in certain cases and should be cleaned up
+     * afterwards, thus this is a global variable: */
+    UISafePointerWizardNewCloudVM pNewCloudVMWizard;
+
+    /* Main API request sequence, can be interrupted after any step: */
+    do
+    {
+        /* Get the map of the password IDs: */
+        EncryptedMediumMap encryptedMedia;
+        foreach (const QString &strPasswordId, comAppliance.GetPasswordIds())
+            foreach (const QUuid &uMediumId, comAppliance.GetMediumIdsForPasswordId(strPasswordId))
+                encryptedMedia.insert(strPasswordId, uMediumId);
+
+        /* Ask for the disk encryption passwords if necessary: */
+        if (!encryptedMedia.isEmpty())
+        {
+            /* Modal dialog can be destroyed in own event-loop as a part of application
+             * termination procedure. We have to make sure that the dialog pointer is
+             * always up to date. So we are wrapping created dialog with QPointer. */
+            QPointer<UIAddDiskEncryptionPasswordDialog> pDlg =
+                new UIAddDiskEncryptionPasswordDialog(this,
+                                                      window()->windowTitle(),
+                                                      encryptedMedia);
+
+            /* Execute the dialog: */
+            if (pDlg->exec() != QDialog::Accepted)
+            {
+                /* Delete the dialog: */
+                delete pDlg;
+                break;
+            }
+
             /* Acquire the passwords provided: */
             const EncryptionPasswordMap encryptionPasswords = pDlg->encryptionPasswords();
@@ -276,7 +292,4 @@
             /* Delete the dialog: */
             delete pDlg;
-
-            /* Make sure the passwords were really provided: */
-            AssertReturn(!encryptionPasswords.isEmpty(), false);
 
             /* Provide appliance with passwords if possible: */
@@ -284,44 +297,49 @@
                                       encryptionPasswords.values().toVector());
             if (!comAppliance.isOk())
-                return msgCenter().cannotAddDiskEncryptionPassword(comAppliance);
-        }
-        else
-        {
-            /* Delete the dialog: */
-            delete pDlg;
-            return false;
-        }
-    }
-
-    /* Write the appliance: */
-    QVector<KExportOptions> options;
-    switch (field("macAddressPolicy").value<MACAddressPolicy>())
-    {
-        case MACAddressPolicy_StripAllNonNATMACs:
-            options.append(KExportOptions_StripAllNonNATMACs);
-            break;
-        case MACAddressPolicy_StripAllMACs:
-            options.append(KExportOptions_StripAllMACs);
-            break;
-        default:
-            break;
-    }
-    if (field("manifestSelected").toBool())
-        options.append(KExportOptions_CreateManifest);
-    if (field("includeISOsSelected").toBool())
-        options.append(KExportOptions_ExportDVDImages);
-    CProgress comProgress = comAppliance.Write(field("format").toString(), options, uri());
-    if (comAppliance.isOk() && comProgress.isNotNull())
-    {
+            {
+                msgCenter().cannotAddDiskEncryptionPassword(comAppliance);
+                break;
+            }
+        }
+
+        /* Prepare export options: */
+        QVector<KExportOptions> options;
+        switch (field("macAddressPolicy").value<MACAddressPolicy>())
+        {
+            case MACAddressPolicy_StripAllNonNATMACs: options.append(KExportOptions_StripAllNonNATMACs); break;
+            case MACAddressPolicy_StripAllMACs: options.append(KExportOptions_StripAllMACs); break;
+            default: break;
+        }
+        if (field("manifestSelected").toBool())
+            options.append(KExportOptions_CreateManifest);
+        if (field("includeISOsSelected").toBool())
+            options.append(KExportOptions_ExportDVDImages);
+
+        /* Prepare Export VM progress: */
+        CProgress comProgress = comAppliance.Write(field("format").toString(), options, uri());
+        if (!comAppliance.isOk())
+        {
+            msgCenter().cannotExportAppliance(comAppliance, this);
+            break;
+        }
+
+        /* Show Export VM progress: */
         msgCenter().showModalProgressDialog(comProgress, QApplication::translate("UIWizardExportApp", "Exporting Appliance ..."),
                                             ":/progress_export_90px.png", this);
         if (comProgress.GetCanceled())
-            return false;
+            break;
         if (!comProgress.isOk() || comProgress.GetResultCode() != 0)
-            return msgCenter().cannotExportAppliance(comProgress, comAppliance.GetPath(), this);
+        {
+            msgCenter().cannotExportAppliance(comProgress, comAppliance.GetPath(), this);
+            break;
+        }
 
         /* Is this VM being exported to cloud? */
         if (field("isFormatCloudOne").toBool())
         {
+            /* We can have wizard and it's result
+             * should be distinguishable: */
+            int iWizardResult = -1;
+
             switch (field("cloudExportMode").value<CloudExportMode>())
             {
@@ -333,9 +351,8 @@
                     /* Create and run short wizard mode as modal dialog: */
                     QWidget *pWizardParent = windowManager().realParentWindow(this);
-                    UISafePointerWizardNewCloudVM pWizard = new UIWizardNewCloudVM(pWizardParent, comClient, comDescription);
-                    windowManager().registerNewParent(pWizard, pWizardParent);
-                    pWizard->prepare();
-                    pWizard->exec();
-                    delete pWizard;
+                    pNewCloudVMWizard = new UIWizardNewCloudVM(pWizardParent, comClient, comDescription);
+                    windowManager().registerNewParent(pNewCloudVMWizard, pWizardParent);
+                    pNewCloudVMWizard->prepare();
+                    iWizardResult = pNewCloudVMWizard->exec();
                     break;
                 }
@@ -343,10 +360,20 @@
                     break;
             }
-        }
-    }
-    else
-        return msgCenter().cannotExportAppliance(comAppliance, this);
-
-    /* True finally: */
-    return true;
-}
+
+            /* We should stop everything only if
+             * there was wizard and it was rejected: */
+            if (iWizardResult == QDialog::Rejected)
+                break;
+        }
+
+        /* Success finally: */
+        fResult = true;
+    }
+    while (0);
+
+    /* Cleanup New Cloud VM wizard if any: */
+    delete pNewCloudVMWizard;
+
+    /* Return result: */
+    return fResult;
+}
