Index: trunk/src/VBox/Frontends/VirtualBox/include/VBoxGlobal.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/include/VBoxGlobal.h	(revision 562)
+++ trunk/src/VBox/Frontends/VirtualBox/include/VBoxGlobal.h	(revision 563)
@@ -383,7 +383,6 @@
     /**
      *  Emitted at the end of the enumeration process started
-     *  by #startEnumeratingMedia().
-     *  @note #currentMediaList() will return an empty list
-     *  when this signal is emitted, use the argument instead.
+     *  by #startEnumeratingMedia(). The @a aList argument is passed for
+     *  convenience, it is exactly the same as returned by #currentMediaList().
      */
     void mediaEnumFinished (const VBoxMediaList &aList);
Index: trunk/src/VBox/Frontends/VirtualBox/src/VBoxGlobal.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/VBoxGlobal.cpp	(revision 562)
+++ trunk/src/VBox/Frontends/VirtualBox/src/VBoxGlobal.cpp	(revision 563)
@@ -1066,4 +1066,8 @@
     if (media_enum_thread)
         return;
+    
+    /* ignore the request during application termination */
+    if (vboxGlobal_cleanup)
+        return;
 
     /* composes a list of all currently known media */
@@ -1071,5 +1075,5 @@
     {
         CHardDiskEnumerator enHD = vbox.GetHardDisks().Enumerate();
-        while (enHD.HasMore() && !vboxGlobal_cleanup)
+        while (enHD.HasMore())
         {
             CHardDisk hd = enHD.GetNext();
@@ -1077,5 +1081,5 @@
         }
         CDVDImageEnumerator enCD = vbox.GetDVDImages().Enumerate();
-        while (enCD.HasMore() && !vboxGlobal_cleanup)
+        while (enCD.HasMore())
         {
             CDVDImage cd = enCD.GetNext();
@@ -1083,11 +1087,9 @@
         }
         CFloppyImageEnumerator enFD = vbox.GetFloppyImages().Enumerate();
-        while (enFD.HasMore() && !vboxGlobal_cleanup)
+        while (enFD.HasMore())
         {
             CFloppyImage fd = enFD.GetNext();
             media_list += VBoxMedia (CUnknown (fd), VBoxDefs::FD, VBoxMedia::Unknown);
         }
-        if (!vboxGlobal_cleanup)
-            emit mediaEnumStarted();
     }
 
@@ -1097,5 +1099,5 @@
     public:
 
-        Thread (VBoxMediaList &aList) : mList (aList) {}
+        Thread (const VBoxMediaList &aList) : mList (aList) {}
 
         virtual void run()
@@ -1109,10 +1111,10 @@
             /* enumerating list */
             int index = 0;
-            VBoxMediaList::Iterator it;
+            VBoxMediaList::const_iterator it;
             for (it = mList.begin();
                  it != mList.end() && !vboxGlobal_cleanup;
                  ++ it, ++ index)
             {
-                VBoxMedia &media = *it;
+                VBoxMedia media = *it;
                 switch (media.type)
                 {
@@ -1134,7 +1136,6 @@
                             }
                         }
-                        VBoxMedia newMedia (CUnknown(hd), VBoxDefs::HD, media.status);
                         QApplication::postEvent (target,
-                            new VBoxEnumerateMediaEvent (newMedia, index));
+                            new VBoxEnumerateMediaEvent (media, index));
                         break;
                     }
@@ -1146,7 +1147,6 @@
                             cd.isOk() ? VBoxMedia::Inaccessible :
                             VBoxMedia::Error;
-                        VBoxMedia newMedia (CUnknown(cd), VBoxDefs::CD, media.status);
                         QApplication::postEvent (target,
-                            new VBoxEnumerateMediaEvent (newMedia, index));
+                            new VBoxEnumerateMediaEvent (media, index));
                         break;
                     }
@@ -1158,7 +1158,6 @@
                             fd.isOk() ? VBoxMedia::Inaccessible :
                             VBoxMedia::Error;
-                        VBoxMedia newMedia (CUnknown(fd), VBoxDefs::FD, media.status);
                         QApplication::postEvent (target,
-                            new VBoxEnumerateMediaEvent (newMedia, index));
+                            new VBoxEnumerateMediaEvent (media, index));
                         break;
                     }
@@ -1181,8 +1180,14 @@
     private:
 
-        VBoxMediaList &mList;
+        const VBoxMediaList &mList;
     };
 
     media_enum_thread = new Thread (media_list);
+	AssertReturnVoid (media_enum_thread);
+
+    /* emit mediaEnumStarted() after we set media_enum_thread to != NULL
+     * to cause isMediaEnumerationStarted() to return TRUE from slots */    
+    emit mediaEnumStarted();
+
     media_enum_thread->start();
 }
Index: trunk/src/VBox/Frontends/VirtualBox/src/VBoxSelectorWnd.cpp
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/src/VBoxSelectorWnd.cpp	(revision 562)
+++ trunk/src/VBox/Frontends/VirtualBox/src/VBoxSelectorWnd.cpp	(revision 563)
@@ -981,8 +981,7 @@
         if (it != list.end() && vboxProblem().remindAboutInaccessibleMedia())
         {
-            /* pass the list we got in the argument to indicate that
-             * VBoxDiskImageManagerDlg should not start the enumeration at the
-             * beginning (i.e. if being shown for the first time) */
-            VBoxDiskImageManagerDlg::showModeless (&list);
+            /* Show the VDM dialog but don't refresh once more after a
+             * just-finished refresh */
+            VBoxDiskImageManagerDlg::showModeless (false /* aRefresh */);
         }
     }
Index: trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui	(revision 562)
+++ trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui	(revision 563)
@@ -603,5 +603,5 @@
     <function returnType="QUuid">getSelectedUuid()</function>
     <function returnType="QString">getSelectedPath()</function>
-    <function>setup( int aType, bool aDoSelect, const QUuid *aTargetVMId = NULL, const VBoxMediaList *mediaList = NULL, CMachine machine = 0 )</function>
+    <function>setup( int aType, bool aDoSelect, const QUuid *aTargetVMId = NULL, bool aRefresh = true, CMachine machine = NULL )</function>
     <function access="private" returnType="QListView*">getCurrentListView()</function>
     <function access="private" returnType="QListView*">getListView( VBoxDefs::DiskType )</function>
@@ -624,5 +624,5 @@
     <function access="protected" returnType="QPushButton*">searchDefaultButton()</function>
     <function access="protected">setCurrentItem( QListView*, QListViewItem* )</function>
-    <function specifier="non virtual">static showModeless( const VBoxMediaList * = NULL )</function>
+    <function specifier="non virtual">static showModeless( bool aRefresh = true )</function>
     <function specifier="non virtual" returnType="QString">static composeHdToolTip( CHardDisk &amp;, VBoxMedia::Status = VBoxMedia::Ok )</function>
     <function specifier="non virtual" returnType="QString">static composeCdToolTip( CDVDImage &amp;, VBoxMedia::Status = VBoxMedia::Ok )</function>
Index: trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui.h
===================================================================
--- trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui.h	(revision 562)
+++ trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui.h	(revision 563)
@@ -146,5 +146,5 @@
 
 
-void VBoxDiskImageManagerDlg::showModeless (const VBoxMediaList *list /* = NULL */)
+void VBoxDiskImageManagerDlg::showModeless (bool aRefresh /* = true */)
 {
     if (!mModelessDialog)
@@ -155,5 +155,5 @@
                                          WType_TopLevel | WDestructiveClose);
         mModelessDialog->setup (VBoxDefs::HD | VBoxDefs::CD | VBoxDefs::FD,
-                                false, NULL, list);
+                                false, NULL, aRefresh);
 
         /* listen to events that may change the media status and refresh
@@ -1211,7 +1211,7 @@
 
 void VBoxDiskImageManagerDlg::setup (int aType, bool aDoSelect,
-                                     const QUuid *aTargetVMId,
-                                     const VBoxMediaList *mediaList,
-                                     CMachine machine)
+                                     const QUuid *aTargetVMId /* = NULL */,
+                                     bool aRefresh /* = true */,
+                                     CMachine machine /* = NULL */)
 {
     cmachine = machine;
@@ -1251,51 +1251,48 @@
              this, SLOT (mediaRemoved (VBoxDefs::DiskType, const QUuid &)));
 
-    /* insert already enumerated media */
-    VBoxMediaList list = mediaList ?
-        *mediaList : vboxGlobal().currentMediaList();
+    if (aRefresh && !vboxGlobal().isMediaEnumerationStarted())
+    {
+        vboxGlobal().startEnumeratingMedia();
+    }
+    else
+    {    
+        /* insert already enumerated media */
+        const VBoxMediaList &list = vboxGlobal().currentMediaList();
+        prepareToRefresh (list.size());
+        VBoxMediaList::const_iterator it;
+        int index = 0;
+        for (it = list.begin(); it != list.end(); ++ it)
+        {
+            mediaAdded (*it);
+            if ((*it).status != VBoxMedia::Unknown)
+                mProgressBar->setProgress (++ index);
+        }
+
+        /* select first child */
+        setCurrentItem (hdsView, hdsView->firstChild());
+        setCurrentItem (cdsView, cdsView->firstChild());
+        setCurrentItem (fdsView, fdsView->firstChild());
+        
+        /* emulate the finished signal to reuse the code */
+        if (!vboxGlobal().isMediaEnumerationStarted())
+            mediaEnumFinished (list);
+    }
+}
+
+
+void VBoxDiskImageManagerDlg::mediaEnumStarted()
+{
+    /* load current media list */
+    const VBoxMediaList &list = vboxGlobal().currentMediaList();
     prepareToRefresh (list.size());
-    VBoxMediaList::const_iterator it;
-    int index = 0;
-    for (it = list.begin(); it != list.end(); ++ it)
-    {
-        mediaAdded (*it);
-        if ((*it).status != VBoxMedia::Unknown)
-            mProgressBar->setProgress (index ++);
-    }
-    /* select first child */
-    setCurrentItem (hdsView, hdsView->firstChild());
-    setCurrentItem (cdsView, cdsView->firstChild());
-    setCurrentItem (fdsView, fdsView->firstChild());
-
-    if (mediaList)
-    {
-        mediaEnumFinished (list);
-    }
-    else
-    {
-        /* only start enumerating media if we haven't been supplied a list
-         * (it's ok if the enumeration has been already started, nothing will
-         * happen) */
-        vboxGlobal().startEnumeratingMedia();
-    }
-}
-
-
-void VBoxDiskImageManagerDlg::mediaEnumStarted()
-{
-    VBoxMediaList list = vboxGlobal().currentMediaList();
-    prepareToRefresh (list.size());
-    /* clearing lists */
-    hdsView->clear(), cdsView->clear(), fdsView->clear();
-    /* load current media list */
     VBoxMediaList::const_iterator it;
     for (it = list.begin(); it != list.end(); ++ it)
         mediaAdded (*it);
     /* select first child */
-    if (!hdsView->isSelected (hdsView->currentItem()))
+    if (!hdsView->currentItem())
         setCurrentItem (hdsView, hdsView->firstChild());
-    if (!cdsView->isSelected (cdsView->currentItem()))
+    if (!cdsView->currentItem())
         setCurrentItem (cdsView, cdsView->firstChild());
-    if (!fdsView->isSelected (fdsView->currentItem()))
+    if (!fdsView->currentItem())
         setCurrentItem (fdsView, fdsView->firstChild());
 
@@ -1307,6 +1304,7 @@
 {
     mediaUpdated (aMedia);
+    Assert (aMedia.status != VBoxMedia::Unknown);
     if (aMedia.status != VBoxMedia::Unknown)
-        mProgressBar->setProgress (aIndex);
+        mProgressBar->setProgress (aIndex + 1);
 }
 
@@ -1318,4 +1316,18 @@
     imRefreshAction->setEnabled (true);
     unsetCursor();
+
+    /* adjust columns (it is strange to repeat but it works) */
+
+    hdsView->adjustColumn (1);
+    hdsView->adjustColumn (2);
+    hdsView->adjustColumn (1);
+
+    cdsView->adjustColumn (1);
+    cdsView->adjustColumn (2);
+    cdsView->adjustColumn (1);
+    
+    fdsView->adjustColumn (1);
+    fdsView->adjustColumn (2);
+    fdsView->adjustColumn (1);
 
     processCurrentChanged();
@@ -1450,4 +1462,26 @@
     imRefreshAction->setEnabled (false);
     setCursor (QCursor (BusyCursor));
+
+    /* store the current list selections */
+    
+    QListViewItem *item;
+    DiskImageItem *di;
+
+    item = hdsView->currentItem();
+    di = (item && item->rtti() == 1001) ? static_cast <DiskImageItem *> (item) : 0;
+    hdSelectedId = di ? di->getUuid() : QUuid();
+ 
+    item = cdsView->currentItem();
+    di = (item && item->rtti() == 1001) ? static_cast <DiskImageItem *> (item) : 0;
+    cdSelectedId = di ? di->getUuid() : QUuid(); 
+
+    item = fdsView->currentItem();
+    di = (item && item->rtti() == 1001) ? static_cast <DiskImageItem *> (item) : 0;
+    fdSelectedId = di ? di->getUuid() : QUuid(); 
+
+    /* finally, clear all lists */
+    hdsView->clear();
+    cdsView->clear();
+    fdsView->clear();
 }
 
@@ -1528,8 +1562,4 @@
     aListView->setCurrentItem (aItem);
     aListView->setSelected (aListView->currentItem(), true);
-    /* it is strange repeat but it works */
-    aListView->adjustColumn (1);
-    aListView->adjustColumn (2);
-    aListView->adjustColumn (1);
 }
 
@@ -1608,5 +1638,4 @@
             hdsPane4->setText (item->getInformation (item->getUsage()));
             hdsPane5->setText (item->getInformation (item->getSnapshotName()));
-            hdSelectedId = item->getUuid();
         }
         else if (item->listView() == cdsView)
@@ -1614,5 +1643,4 @@
             cdsPane1->setText (item->getInformation (item->getPath(), true, "end"));
             cdsPane2->setText (item->getInformation (item->getUsage()));
-            cdSelectedId = item->getUuid();
         }
         else if (item->listView() == fdsView)
@@ -1620,5 +1648,4 @@
             fdsPane1->setText (item->getInformation (item->getPath(), true, "end"));
             fdsPane2->setText (item->getInformation (item->getUsage()));
-            fdSelectedId = item->getUuid();
         }
     }
