Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIConsoleEventHandler.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIConsoleEventHandler.cpp	(revision 52894)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIConsoleEventHandler.cpp	(revision 52898)
@@ -5,5 +5,5 @@
 
 /*
- * Copyright (C) 2010-2013 Oracle Corporation
+ * Copyright (C) 2010-2014 Oracle Corporation
  *
  * This file is part of VirtualBox Open Source Edition (OSE), as
@@ -43,5 +43,8 @@
 {
     if (!m_pInstance)
+    {
         m_pInstance = new UIConsoleEventHandler(pSession);
+        m_pInstance->prepare();
+    }
     return m_pInstance;
 }
@@ -52,125 +55,7 @@
     if (m_pInstance)
     {
+        m_pInstance->cleanup();
         delete m_pInstance;
         m_pInstance = 0;
-    }
-}
-
-UIConsoleEventHandler::UIConsoleEventHandler(UISession *pSession)
-  : m_pSession(pSession)
-{
-    Assert(pSession);
-
-//    RTPrintf("Self add: %RTthrd\n", RTThreadSelf());
-    ComObjPtr<UIMainEventListenerImpl> pListener;
-    pListener.createObject();
-    pListener->init(new UIMainEventListener(), this);
-    m_mainEventListener = CEventListener(pListener);
-    QVector<KVBoxEventType> events;
-    events
-        << KVBoxEventType_OnMousePointerShapeChanged
-        << KVBoxEventType_OnMouseCapabilityChanged
-        << KVBoxEventType_OnKeyboardLedsChanged
-        << KVBoxEventType_OnStateChanged
-        << KVBoxEventType_OnAdditionsStateChanged
-        << KVBoxEventType_OnNetworkAdapterChanged
-        << KVBoxEventType_OnMediumChanged
-        << KVBoxEventType_OnVRDEServerChanged
-        << KVBoxEventType_OnVRDEServerInfoChanged
-        << KVBoxEventType_OnVideoCaptureChanged
-        << KVBoxEventType_OnUSBControllerChanged
-        << KVBoxEventType_OnUSBDeviceStateChanged
-        << KVBoxEventType_OnSharedFolderChanged
-        << KVBoxEventType_OnRuntimeError
-        << KVBoxEventType_OnCanShowWindow
-        << KVBoxEventType_OnShowWindow
-        << KVBoxEventType_OnCPUExecutionCapChanged
-        << KVBoxEventType_OnGuestMonitorChanged;
-
-    const CConsole &console = m_pSession->session().GetConsole();
-    console.GetEventSource().RegisterListener(m_mainEventListener, events, TRUE);
-    AssertWrapperOk(console);
-
-    connect(pListener->getWrapped(), SIGNAL(sigMousePointerShapeChange(bool, bool, QPoint, QSize, QVector<uint8_t>)),
-            this, SIGNAL(sigMousePointerShapeChange(bool, bool, QPoint, QSize, QVector<uint8_t>)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigMouseCapabilityChange(bool, bool, bool, bool)),
-            this, SIGNAL(sigMouseCapabilityChange(bool, bool, bool, bool)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigKeyboardLedsChangeEvent(bool, bool, bool)),
-            this, SIGNAL(sigKeyboardLedsChangeEvent(bool, bool, bool)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigStateChange(KMachineState)),
-            this, SIGNAL(sigStateChange(KMachineState)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigAdditionsChange()),
-            this, SIGNAL(sigAdditionsChange()),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigNetworkAdapterChange(CNetworkAdapter)),
-            this, SIGNAL(sigNetworkAdapterChange(CNetworkAdapter)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigMediumChange(CMediumAttachment)),
-            this, SIGNAL(sigMediumChange(CMediumAttachment)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigVRDEChange()),
-            this, SIGNAL(sigVRDEChange()),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigVideoCaptureChange()),
-            this, SIGNAL(sigVideoCaptureChange()),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigUSBControllerChange()),
-            this, SIGNAL(sigUSBControllerChange()),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigUSBDeviceStateChange(CUSBDevice, bool, CVirtualBoxErrorInfo)),
-            this, SIGNAL(sigUSBDeviceStateChange(CUSBDevice, bool, CVirtualBoxErrorInfo)),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigSharedFolderChange()),
-            this, SIGNAL(sigSharedFolderChange()),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigRuntimeError(bool, QString, QString)),
-            this, SIGNAL(sigRuntimeError(bool, QString, QString)),
-            Qt::QueuedConnection);
-
-    /* This is a vetoable event, so we have to respond to the event and have to
-     * use a direct connection therefor. */
-    connect(pListener->getWrapped(), SIGNAL(sigCanShowWindow(bool&, QString&)),
-            this, SLOT(sltCanShowWindow(bool&, QString&)),
-            Qt::DirectConnection);
-
-    /* This returns a winId, so we have to respond to the event and have to use
-     * a direct connection therefor. */
-    connect(pListener->getWrapped(), SIGNAL(sigShowWindow(LONG64&)),
-            this, SLOT(sltShowWindow(LONG64&)),
-            Qt::DirectConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigCPUExecutionCapChange()),
-            this, SIGNAL(sigCPUExecutionCapChange()),
-            Qt::QueuedConnection);
-
-    connect(pListener->getWrapped(), SIGNAL(sigGuestMonitorChange(KGuestMonitorChangedEventType, ulong, QRect)),
-            this, SIGNAL(sigGuestMonitorChange(KGuestMonitorChangedEventType, ulong, QRect)),
-            Qt::QueuedConnection);
-}
-
-UIConsoleEventHandler::~UIConsoleEventHandler()
-{
-    const CConsole &console = m_pSession->session().GetConsole();
-    if (!console.isNull())
-    {
-        CEventSource eventSource = console.GetEventSource();
-        if (!eventSource.isNull())
-            eventSource.UnregisterListener(m_mainEventListener);
     }
 }
@@ -203,2 +88,121 @@
 }
 
+UIConsoleEventHandler::UIConsoleEventHandler(UISession *pSession)
+    : m_pSession(pSession)
+{
+}
+
+void UIConsoleEventHandler::prepare()
+{
+    /* Make sure session is passed: */
+    AssertPtrReturnVoid(m_pSession);
+
+    /* Create Main-event listener instance: */
+    ComObjPtr<UIMainEventListenerImpl> pListener;
+    pListener.createObject();
+    pListener->init(new UIMainEventListener, this);
+    m_mainEventListener = CEventListener(pListener);
+
+    /* Get console: */
+    const CConsole console = m_pSession->session().GetConsole();
+    AssertReturnVoid(!console.isNull() && console.isOk());
+    /* Get event-source: */
+    CEventSource eventSource = console.GetEventSource();
+    AssertReturnVoid(!eventSource.isNull() && eventSource.isOk());
+    /* Register listener for expected event-types: */
+    QVector<KVBoxEventType> events;
+    events
+        << KVBoxEventType_OnMousePointerShapeChanged
+        << KVBoxEventType_OnMouseCapabilityChanged
+        << KVBoxEventType_OnKeyboardLedsChanged
+        << KVBoxEventType_OnStateChanged
+        << KVBoxEventType_OnAdditionsStateChanged
+        << KVBoxEventType_OnNetworkAdapterChanged
+        << KVBoxEventType_OnMediumChanged
+        << KVBoxEventType_OnVRDEServerChanged
+        << KVBoxEventType_OnVRDEServerInfoChanged
+        << KVBoxEventType_OnVideoCaptureChanged
+        << KVBoxEventType_OnUSBControllerChanged
+        << KVBoxEventType_OnUSBDeviceStateChanged
+        << KVBoxEventType_OnSharedFolderChanged
+        << KVBoxEventType_OnCPUExecutionCapChanged
+        << KVBoxEventType_OnGuestMonitorChanged
+        << KVBoxEventType_OnRuntimeError
+        << KVBoxEventType_OnCanShowWindow
+        << KVBoxEventType_OnShowWindow;
+    eventSource.RegisterListener(m_mainEventListener, events, TRUE);
+
+
+    /* Prepare connections: */
+    connect(pListener->getWrapped(), SIGNAL(sigMousePointerShapeChange(bool, bool, QPoint, QSize, QVector<uint8_t>)),
+            this, SIGNAL(sigMousePointerShapeChange(bool, bool, QPoint, QSize, QVector<uint8_t>)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigMouseCapabilityChange(bool, bool, bool, bool)),
+            this, SIGNAL(sigMouseCapabilityChange(bool, bool, bool, bool)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigKeyboardLedsChangeEvent(bool, bool, bool)),
+            this, SIGNAL(sigKeyboardLedsChangeEvent(bool, bool, bool)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigStateChange(KMachineState)),
+            this, SIGNAL(sigStateChange(KMachineState)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigAdditionsChange()),
+            this, SIGNAL(sigAdditionsChange()),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigNetworkAdapterChange(CNetworkAdapter)),
+            this, SIGNAL(sigNetworkAdapterChange(CNetworkAdapter)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigMediumChange(CMediumAttachment)),
+            this, SIGNAL(sigMediumChange(CMediumAttachment)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigVRDEChange()),
+            this, SIGNAL(sigVRDEChange()),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigVideoCaptureChange()),
+            this, SIGNAL(sigVideoCaptureChange()),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigUSBControllerChange()),
+            this, SIGNAL(sigUSBControllerChange()),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigUSBDeviceStateChange(CUSBDevice, bool, CVirtualBoxErrorInfo)),
+            this, SIGNAL(sigUSBDeviceStateChange(CUSBDevice, bool, CVirtualBoxErrorInfo)),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigSharedFolderChange()),
+            this, SIGNAL(sigSharedFolderChange()),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigCPUExecutionCapChange()),
+            this, SIGNAL(sigCPUExecutionCapChange()),
+            Qt::QueuedConnection);
+    connect(pListener->getWrapped(), SIGNAL(sigGuestMonitorChange(KGuestMonitorChangedEventType, ulong, QRect)),
+            this, SIGNAL(sigGuestMonitorChange(KGuestMonitorChangedEventType, ulong, QRect)),
+            Qt::QueuedConnection);
+
+    connect(pListener->getWrapped(), SIGNAL(sigRuntimeError(bool, QString, QString)),
+            this, SIGNAL(sigRuntimeError(bool, QString, QString)),
+            Qt::QueuedConnection);
+
+    /* This is a vetoable event, so we have to respond to the event and have to
+     * use a direct connection therefor. */
+    connect(pListener->getWrapped(), SIGNAL(sigCanShowWindow(bool&, QString&)),
+            this, SLOT(sltCanShowWindow(bool&, QString&)),
+            Qt::DirectConnection);
+    /* This returns a winId, so we have to respond to the event and have to use
+     * a direct connection therefor. */
+    connect(pListener->getWrapped(), SIGNAL(sigShowWindow(LONG64&)),
+            this, SLOT(sltShowWindow(LONG64&)),
+            Qt::DirectConnection);
+}
+
+void UIConsoleEventHandler::cleanup()
+{
+    /* Get console: */
+    const CConsole console = m_pSession->session().GetConsole();
+    if (console.isNull() || !console.isOk())
+        return;
+    /* Get event-source: */
+    CEventSource eventSource = console.GetEventSource();
+    AssertReturnVoid(!eventSource.isNull() || eventSource.isOk());
+    /* Unregister listener: */
+    eventSource.UnregisterListener(m_mainEventListener);
+}
+
Index: trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIConsoleEventHandler.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIConsoleEventHandler.h	(revision 52894)
+++ trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIConsoleEventHandler.h	(revision 52898)
@@ -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
@@ -15,11 +15,11 @@
  */
 
-#ifndef __UIConsoleEventHandler_h__
-#define __UIConsoleEventHandler_h__
+#ifndef ___UIConsoleEventHandler_h___
+#define ___UIConsoleEventHandler_h___
 
 /* COM includes: */
 #include "COMEnums.h"
+#include "CEventListener.h"
 #include "CVirtualBoxErrorInfo.h"
-#include "CEventListener.h"
 #include "CMediumAttachment.h"
 #include "CNetworkAdapter.h"
@@ -29,42 +29,77 @@
 class UISession;
 
+/** Console event handler. */
 class UIConsoleEventHandler: public QObject
 {
     Q_OBJECT;
 
+signals:
+
+    /** Notifies about mouse pointer shape change. */
+    void sigMousePointerShapeChange(bool fVisible, bool fAlpha, QPoint hotCorner, QSize size, QVector<uint8_t> shape);
+    /** Notifies about mouse capability change. */
+    void sigMouseCapabilityChange(bool fSupportsAbsolute, bool fSupportsRelative, bool fSupportsMultiTouch, bool fNeedsHostCursor);
+    /** Notifies about keyboard LEDs change. */
+    void sigKeyboardLedsChangeEvent(bool fNumLock, bool fCapsLock, bool fScrollLock);
+    /** Notifies about machine state change. */
+    void sigStateChange(KMachineState state);
+    /** Notifies about guest additions state change. */
+    void sigAdditionsChange();
+    /** Notifies about network adapter state change. */
+    void sigNetworkAdapterChange(CNetworkAdapter adapter);
+    /** Notifies about storage medium state change. */
+    void sigMediumChange(CMediumAttachment attachment);
+    /** Notifies about VRDE device state change. */
+    void sigVRDEChange();
+    /** Notifies about Video Capture device state change. */
+    void sigVideoCaptureChange();
+    /** Notifies about USB controller state change. */
+    void sigUSBControllerChange();
+    /** Notifies about USB device state change. */
+    void sigUSBDeviceStateChange(CUSBDevice device, bool fAttached, CVirtualBoxErrorInfo error);
+    /** Notifies about shared folder state change. */
+    void sigSharedFolderChange();
+    /** Notifies about CPU execution-cap change. */
+    void sigCPUExecutionCapChange();
+    /** Notifies about guest-screen configuration change. */
+    void sigGuestMonitorChange(KGuestMonitorChangedEventType changeType, ulong uScreenId, QRect screenGeo);
+    /** Notifies about Runtime error. */
+    void sigRuntimeError(bool fFatal, QString strId, QString strMessage);
+#ifdef RT_OS_DARWIN
+    /** Notifies about VM window should be shown. */
+    void sigShowWindow();
+#endif /* RT_OS_DARWIN */
+
 public:
+
+    /** Static instance factory. */
     static UIConsoleEventHandler* instance(UISession *pSession = 0);
+    /** Static instance destructor. */
     static void destroy();
 
-signals:
-    void sigMousePointerShapeChange(bool fVisible, bool fAlpha, QPoint hotCorner, QSize size, QVector<uint8_t> shape);
-    void sigMouseCapabilityChange(bool fSupportsAbsolute, bool fSupportsRelative, bool fSupportsMultiTouch, bool fNeedsHostCursor);
-    void sigKeyboardLedsChangeEvent(bool fNumLock, bool fCapsLock, bool fScrollLock);
-    void sigStateChange(KMachineState state);
-    void sigAdditionsChange();
-    void sigNetworkAdapterChange(CNetworkAdapter adapter);
-    void sigMediumChange(CMediumAttachment attachment);
-    void sigVRDEChange();
-    void sigVideoCaptureChange();
-    void sigUSBControllerChange();
-    void sigUSBDeviceStateChange(CUSBDevice device, bool fAttached, CVirtualBoxErrorInfo error);
-    void sigSharedFolderChange();
-    void sigRuntimeError(bool fFatal, QString strId, QString strMessage);
-#ifdef RT_OS_DARWIN
-    void sigShowWindow();
-#endif /* RT_OS_DARWIN */
-    void sigCPUExecutionCapChange();
-    void sigGuestMonitorChange(KGuestMonitorChangedEventType changeType, ulong uScreenId, QRect screenGeo);
+private slots:
 
-private slots:
+    /** Returns whether VM window can be shown. */
     void sltCanShowWindow(bool &fVeto, QString &strReason);
+    /** Shows VM window if possible. */
     void sltShowWindow(LONG64 &winId);
 
 private:
+
+    /** Constructor: */
     UIConsoleEventHandler(UISession *pSession);
-    ~UIConsoleEventHandler();
 
+    /** Prepare routine. */
+    void prepare();
+    /** Cleanup routine. */
+    void cleanup();
+
+    /** Holds the static instance. */
     static UIConsoleEventHandler *m_pInstance;
+
+    /** Holds the UI session reference. */
     UISession *m_pSession;
+
+    /** Holds the main event listener instance. */
     CEventListener m_mainEventListener;
 };
@@ -72,4 +107,3 @@
 #define gConsoleEvents UIConsoleEventHandler::instance()
 
-#endif /* !__UIConsoleEventHandler_h__ */
-
+#endif /* !___UIConsoleEventHandler_h___ */
