Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 26897)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineLogic.cpp	(revision 26898)
@@ -587,13 +587,29 @@
     m_pRunningOrPausedActions->setEnabled(uisession()->isRunning() || uisession()->isPaused());
 
-    /* Do we have GURU? */
-    bool fIsGuruMeditation = false;
-
     switch (state)
     {
-        case KMachineState_Stuck:
-        {
-            /* We will process GURU later: */
-            fIsGuruMeditation = true;
+        case KMachineState_Stuck: // TODO: Test it!
+        {
+            /* Prevent machine view from resizing: */
+            uisession()->setGuestResizeIgnored(true);
+
+            /* Get console: */
+            CConsole console = session().GetConsole();
+
+            /* Take the screenshot for debugging purposes and save it */
+            QString strLogFolder = console.GetMachine().GetLogFolder();
+            QString strFileName = strLogFolder + "/VBox.png";
+            CDisplay display = console.GetDisplay();
+            QImage shot = QImage(display.GetWidth(), display.GetHeight(), QImage::Format_RGB32);
+            display.TakeScreenShot(shot.bits(), shot.width(), shot.height());
+            shot.save(QFile::encodeName(strFileName), "PNG");
+
+            /* Warn the user about GURU: */
+            if (vboxProblem().remindAboutGuruMeditation(console, QDir::toNativeSeparators(strLogFolder)))
+            {
+                console.PowerDown();
+                if (!console.isOk())
+                    vboxProblem().cannotStopMachine(console);
+            }
             break;
         }
@@ -625,4 +641,18 @@
             break;
         }
+        case KMachineState_PoweredOff:
+        case KMachineState_Saved:
+        case KMachineState_Teleported:
+        case KMachineState_Aborted:
+        {
+            /* Close VM if it was turned off and closure allowed: */
+            if (!isPreventAutoClose())
+            {
+                /* VM has been powered off, saved or aborted, no matter
+                 * internally or externally. We must *safely* close VM window(s): */
+                QTimer::singleShot(0, uisession(), SLOT(sltCloseVirtualSession()));
+            }
+            break;
+        }
 #ifdef Q_WS_X11
         case KMachineState_Starting:
@@ -639,39 +669,4 @@
             break;
     }
-
-    /* Close VM if was closed someway: */
-    if (uisession()->isTurnedOff())
-    {
-        /* VM has been powered off or saved or aborted, no matter internally or externally.
-         * We must *safely* close the console window unless auto closure is disabled: */
-        QTimer::singleShot(0, uisession(), SLOT(sltCloseVirtualSession()));
-        return;
-    }
-
-#if 0 // TODO: Postprocess GURU!
-    if (fIsGuruMeditation)
-    {
-        uisession()->setGuestResizeIgnored(true);
-
-        CConsole console = session().GetConsole();
-        QString strLogFolder = console.GetMachine().GetLogFolder();
-
-        /* Take the screenshot for debugging purposes and save it */
-        QString strFileName = strLogFolder + "/VBox.png";
-        CDisplay display = console.GetDisplay();
-        QImage shot = QImage(display.GetWidth(), display.GetHeight(), QImage::Format_RGB32);
-        display.TakeScreenShot(shot.bits(), shot.width(), shot.height());
-        shot.save(QFile::encodeName(strFileName), "PNG");
-
-        /* Warn the user about GURU: */
-        if (vboxProblem().remindAboutGuruMeditation(console, QDir::toNativeSeparators(strLogFolder)))
-        {
-            qApp->processEvents();
-            console.PowerDown();
-            if (!console.isOk())
-                vboxProblem().cannotStopMachine(console);
-        }
-    }
-#endif
 }
 
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp	(revision 26897)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp	(revision 26898)
@@ -130,4 +130,10 @@
 void UIMachineWindow::closeEvent(QCloseEvent *pEvent)
 {
+    /* Always ignore close event: */
+    pEvent->ignore();
+
+    /* Should we close application? */
+    bool fCloseApplication = false;
+
     switch (uisession()->machineState())
     {
@@ -139,8 +145,8 @@
         case KMachineState_TeleportingPausedVM: // TODO: Test this!
         {
-            /* Ignoring close event initially: */
-            pEvent->ignore();
-
             bool success = true;
+
+            /* Get the ACPI status early before pausing VM: */
+            bool isACPIEnabled = session().GetConsole().GetGuestEnteredACPIMode();
 
             bool fWasPaused = uisession()->isPaused() || uisession()->machineState() == KMachineState_Stuck;
@@ -156,9 +162,9 @@
                 success = false;
 
+                /* Preventing closure right after we had paused: */
+                machineLogic()->setPreventAutoClose(true);
+
                 /* Get the machine: */
                 CMachine machine = session().GetMachine();
-
-                /* Get the ACPI status: */
-                bool isACPIEnabled = session().GetConsole().GetGuestEnteredACPIMode();
 
                 /* Prepare close dialog: */
@@ -172,5 +178,5 @@
                     dlg.mCbDiscardCurState->setText(dlg.mCbDiscardCurState->text().arg(machine.GetCurrentSnapshot().GetName()));
 
-                /* Close choice string tags: */
+                /* Choice string tags for close-dialog: */
                 QString strSave("save");
                 QString strShutdown("shutdown");
@@ -284,10 +290,4 @@
                     }
 
-                    if (success)
-                    {
-                        /* Accept the close action on success: */
-                        pEvent->accept();
-                    }
-
                     if (success || fWasShutdown)
                     {
@@ -310,70 +310,28 @@
                         AssertWrapperOk(machine);
                     }
+
+                    fCloseApplication = success;
                 }
             }
 
-            if (uisession()->machineState() == KMachineState_PoweredOff ||
-                uisession()->machineState() == KMachineState_Saved ||
-                uisession()->machineState() == KMachineState_Teleported ||
-                uisession()->machineState() == KMachineState_Aborted)
+            if (!success)
             {
-                /* The machine has been stopped while showing the Close or the Pause
-                 * failure dialog - accept the close event immediately: */
-                pEvent->accept();
-            }
-            else
-            {
-                if (!success)
-                {
-                    /* Restore the running state if needed: */
-                    if (!fWasPaused && uisession()->machineState() == KMachineState_Paused)
-                        uisession()->unpause();
-                }
+                /* Restore the running state if needed: */
+                if (!fWasPaused && uisession()->machineState() == KMachineState_Paused)
+                    uisession()->unpause();
             }
             break;
         }
 
-#if 0 // TODO: Check if all those states processing is really needed:
-        case KMachineState_PoweredOff:
-        case KMachineState_Saved:
-        case KMachineState_Teleported:
-        case KMachineState_Aborted:
-        {
-            /* The machine has been already powered off or saved or aborted - close the window immediately: */
-            pEvent->accept();
+        default:
             break;
-        }
-#endif
-
-        default:
-        {
-            /* The machine is in some temporary state like Saving or Stopping.
-             * Ignore the close event. It will be processed later if something will be failed: */
-            pEvent->ignore();
-            break;
-        }
     }
 
-    /* Postprocess event if accepted: */
-    if (pEvent->isAccepted())
+    /* Allowing closure: */
+    machineLogic()->setPreventAutoClose(false);
+
+    if (fCloseApplication)
     {
-#ifndef VBOX_GUI_SEPARATE_VM_PROCESS
-        vboxGlobal().selectorWnd().show();
-#endif
-
-#ifdef VBOX_WITH_DEBUGGER_GUI
-        /* Close & destroy the debugger GUI */
-        // TODO: Check that logic!
-        //dbgDestroy();
-#endif
-
-        /* Notify all the top-level child dialogs about closing: */
-        // TODO: Notify about closing!
-        //emit closing();
-
-        /* Reject that event finally as uimachine will close window iteself: */
-        pEvent->ignore();
-
-        /* Request uisession closing: */
+        /* VM has been powered off or saved. We must *safely* close the VM window(s): */
         QTimer::singleShot(0, uisession(), SLOT(sltCloseVirtualSession()));
     }
