Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 52988)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.cpp	(revision 52989)
@@ -20,5 +20,5 @@
 #else  /* !VBOX_WITH_PRECOMPILED_HEADERS */
 
-/* Local includes: */
+/* GUI includes: */
 # include "VBoxGlobal.h"
 # include "UIExtraDataManager.h"
@@ -142,13 +142,68 @@
 }
 
+UIMachineLogic* UIMachine::machineLogic() const
+{
+    if (m_pVisualState && m_pVisualState->machineLogic())
+        return m_pVisualState->machineLogic();
+    return 0;
+}
+
+QWidget* UIMachine::activeWindow() const
+{
+    if (machineLogic() &&  machineLogic()->activeMachineWindow())
+        return machineLogic()->activeMachineWindow();
+    return 0;
+}
+
+void UIMachine::asyncChangeVisualState(UIVisualStateType visualStateType)
+{
+    emit sigRequestAsyncVisualStateChange(visualStateType);
+}
+
+void UIMachine::sltChangeVisualState(UIVisualStateType newVisualStateType)
+{
+    /* Create new state: */
+    UIVisualState *pNewVisualState = new UIVisualState(this, m_pSession, newVisualStateType);
+
+    /* First we have to check if the selected mode is available at all.
+     * Only then we delete the old mode and switch to the new mode. */
+    if (pNewVisualState->prepareChange())
+    {
+        /* Delete previous state: */
+        delete m_pVisualState;
+
+        /* Set the new mode as current mode: */
+        m_pVisualState = pNewVisualState;
+        m_pVisualState->change();
+    }
+    else
+    {
+        /* Discard the temporary created new state: */
+        delete pNewVisualState;
+
+        /* If there is no state currently created => we have to exit: */
+        if (!m_pVisualState)
+            deleteLater();
+    }
+}
+
 UIMachine::UIMachine()
     : QObject(0)
-    , initialStateType(UIVisualStateType_Normal)
     , m_pSession(0)
+    , m_allowedVisualStates(UIVisualStateType_Invalid)
+    , m_initialStateType(UIVisualStateType_Normal)
     , m_pVisualState(0)
-    , m_allowedVisualStates(UIVisualStateType_Invalid)
 {
     /* Make sure VBoxGlobal is aware of VM creation: */
     vboxGlobal().setVirtualMachine(this);
+}
+
+UIMachine::~UIMachine()
+{
+    /* Make sure VBoxGlobal is aware of VM destruction: */
+    vboxGlobal().setVirtualMachine(0);
+
+    /* Cleanup machine UI: */
+    cleanup();
 }
 
@@ -192,6 +247,6 @@
     vboxGlobal().startMediumEnumeration(false /* force start */);
 
-    /* Load machine settings: */
-    loadMachineSettings();
+    /* Load settings: */
+    loadSettings();
 
     /* Prepare async visual-state change handler: */
@@ -217,80 +272,5 @@
 }
 
-UIMachine::~UIMachine()
-{
-    /* Save machine settings: */
-    saveMachineSettings();
-
-    /* Delete visual state: */
-    delete m_pVisualState;
-    m_pVisualState = 0;
-
-    /* Delete UI session: */
-    delete m_pSession;
-    m_pSession = 0;
-
-    /* Free session finally: */
-    m_session.UnlockMachine();
-    m_session.detach();
-
-    /* Make sure VBoxGlobal is aware of VM destruction: */
-    vboxGlobal().setVirtualMachine(0);
-
-    /* Quit application: */
-    QApplication::quit();
-}
-
-QWidget* UIMachine::activeWindow() const
-{
-    if (machineLogic() &&  machineLogic()->activeMachineWindow())
-        return machineLogic()->activeMachineWindow();
-    return 0;
-}
-
-void UIMachine::asyncChangeVisualState(UIVisualStateType visualStateType)
-{
-    emit sigRequestAsyncVisualStateChange(visualStateType);
-}
-
-void UIMachine::sltChangeVisualState(UIVisualStateType newVisualStateType)
-{
-    /* Create new state: */
-    UIVisualState *pNewVisualState = new UIVisualState(this, m_pSession, newVisualStateType);
-
-    /* First we have to check if the selected mode is available at all.
-     * Only then we delete the old mode and switch to the new mode. */
-    if (pNewVisualState->prepareChange())
-    {
-        /* Delete previous state: */
-        delete m_pVisualState;
-
-        /* Set the new mode as current mode: */
-        m_pVisualState = pNewVisualState;
-        m_pVisualState->change();
-    }
-    else
-    {
-        /* Discard the temporary created new state: */
-        delete pNewVisualState;
-
-        /* If there is no state currently created => we have to exit: */
-        if (!m_pVisualState)
-            deleteLater();
-    }
-}
-
-void UIMachine::enterInitialVisualState()
-{
-    sltChangeVisualState(initialStateType);
-}
-
-UIMachineLogic* UIMachine::machineLogic() const
-{
-    if (m_pVisualState && m_pVisualState->machineLogic())
-        return m_pVisualState->machineLogic();
-    return 0;
-}
-
-void UIMachine::loadMachineSettings()
+void UIMachine::loadSettings()
 {
     /* Load 'visual state' option: */
@@ -309,6 +289,6 @@
             {
                 /* Direct transition to scale/fullscreen mode allowed: */
-                case UIVisualStateType_Scale: initialStateType = UIVisualStateType_Scale; break;
-                case UIVisualStateType_Fullscreen: initialStateType = UIVisualStateType_Fullscreen; break;
+                case UIVisualStateType_Scale: m_initialStateType = UIVisualStateType_Scale; break;
+                case UIVisualStateType_Fullscreen: m_initialStateType = UIVisualStateType_Fullscreen; break;
                 /* While to seamless is not, so we have to request transition on GA capability-change event: */
                 case UIVisualStateType_Seamless: uisession()->setRequestedVisualState(UIVisualStateType_Seamless); break;
@@ -319,5 +299,5 @@
 }
 
-void UIMachine::saveMachineSettings()
+void UIMachine::saveSettings()
 {
     /* Save 'visual state' option: */
@@ -338,4 +318,33 @@
 }
 
+void UIMachine::cleanup()
+{
+    /* Save settings: */
+    saveSettings();
+
+    /* Delete visual state: */
+    delete m_pVisualState;
+    m_pVisualState = 0;
+
+    /* Delete UI session: */
+    delete m_pSession;
+    m_pSession = 0;
+
+    /* Free session finally: */
+    m_session.UnlockMachine();
+    m_session.detach();
+
+    /* Make sure VBoxGlobal is aware of VM destruction: */
+    vboxGlobal().setVirtualMachine(0);
+
+    /* Quit application: */
+    QApplication::quit();
+}
+
+void UIMachine::enterInitialVisualState()
+{
+    sltChangeVisualState(m_initialStateType);
+}
+
 #include "UIMachine.moc"
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h	(revision 52988)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachine.h	(revision 52989)
@@ -4,5 +4,5 @@
 
 /*
- * Copyright (C) 2010-2013 Oracle Corporation
+ * Copyright (C) 2010-2014 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -52,13 +52,10 @@
     static bool startMachine(const QString &strID);
 
-    /** Constructor. */
-    UIMachine();
-    /** Destructor. */
-    ~UIMachine();
-
+    /** Returns UI session instance. */
+    UISession *uisession() const { return m_pSession; }
+    /** Returns machine-logic reference (if possible). */
+    UIMachineLogic* machineLogic() const;
     /** Returns active machine-window reference (if possible). */
     QWidget* activeWindow() const;
-    /** Returns UI session instance. */
-    UISession *uisession() const { return m_pSession; }
 
     /** Returns whether requested visual @a state allowed. */
@@ -75,28 +72,36 @@
 private:
 
+    /** Constructor. */
+    UIMachine();
+    /** Destructor. */
+    ~UIMachine();
+
     /** Prepare routine. */
     bool prepare();
+    /** Prepare routine: Loading stuff. */
+    void loadSettings();
+    /** Prepare routine: Saving stuff. */
+    void saveSettings();
+    /** Cleanup routine. */
+    void cleanup();
 
     /** Moves VM to default (normal) state. */
     void enterInitialVisualState();
 
-    /** Returns machine-logic reference (if possible). */
-    UIMachineLogic* machineLogic() const;
+    /** Holds the session instance. */
+    CSession m_session;
 
-    /** Prepare routine: Loading stuff. */
-    void loadMachineSettings();
+    /** Holds the session UI instance. */
+    UISession *m_pSession;
 
-    /** Prepare routine: Saving stuff. */
-    void saveMachineSettings();
+    /** Holds allowed visual state types. */
+    UIVisualStateType m_allowedVisualStates;
+    /** Holds initial visual state type. */
+    UIVisualStateType m_initialStateType;
+    /** Holds current visual state. */
+    UIVisualState *m_pVisualState;
 
-    /* Private variables: */
-    UIVisualStateType initialStateType;
-    CSession m_session;
-    UISession *m_pSession;
-    UIVisualState *m_pVisualState;
-    UIVisualStateType m_allowedVisualStates;
-
-    /* Friend classes: */
-    friend class UISession;
+    /* Temporary access: */
+    friend class VBoxGlobal;
 };
 
