
Copyright (c) 2009  Robert Millan

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Index: src/VBox/Frontends/VirtualBox/include/VBoxConsoleWnd.h
===================================================================
--- src/VBox/Frontends/VirtualBox/include/VBoxConsoleWnd.h	(revision 18709)
+++ src/VBox/Frontends/VirtualBox/include/VBoxConsoleWnd.h	(working copy)
@@ -208,6 +208,7 @@ private slots:
     void devicesSwitchVrdp (bool);
     void devicesOpenSFDialog();
     void devicesInstallGuestAdditions();
+    void devicesInstallWineD3D();
 
     void prepareFloppyMenu();
     void prepareDVDMenu();
@@ -282,6 +283,7 @@ private:
     QAction *mDevicesSwitchVrdpAction;
     QAction *mDevicesSFDialogAction;
     QAction *mDevicesInstallGuestToolsAction;
+    QAction *mDevicesInstallWineD3DAction;
 
 #ifdef VBOX_WITH_DEBUGGER_GUI
     /* Debugger actions */
Index: src/VBox/Frontends/VirtualBox/src/VBoxConsoleWnd.cpp
===================================================================
--- src/VBox/Frontends/VirtualBox/src/VBoxConsoleWnd.cpp	(revision 18709)
+++ src/VBox/Frontends/VirtualBox/src/VBoxConsoleWnd.cpp	(working copy)
@@ -379,6 +379,10 @@ VBoxConsoleWnd (VBoxConsoleWnd **aSelf, 
         VBoxGlobal::iconSet (":/guesttools_16px.png",
                              ":/guesttools_disabled_16px.png"));
 
+    mDevicesInstallWineD3DAction = new QAction (mRunningActions);
+    mDevicesInstallWineD3DAction->setIcon (VBoxGlobal::iconSet (":/guesttools_16px.png",
+                                                                     ":/guesttools_disabled_16px.png"));
+
 #ifdef VBOX_WITH_DEBUGGER_GUI
     if (vboxGlobal().isDebuggerEnabled())
     {
@@ -476,6 +480,7 @@ VBoxConsoleWnd (VBoxConsoleWnd **aSelf, 
     mDevicesVRDPMenuSeparator = mDevicesMenu->addSeparator();
 
     mDevicesMenu->addAction (mDevicesInstallGuestToolsAction);
+    mDevicesMenu->addAction (mDevicesInstallWineD3DAction);
 
     /* reset the "context menu" flag */
     mDevicesMountFloppyMenu->menuAction()->setData (false);
@@ -635,6 +640,7 @@ VBoxConsoleWnd (VBoxConsoleWnd **aSelf, 
     connect (mDevicesSwitchVrdpAction, SIGNAL(toggled (bool)), this, SLOT(devicesSwitchVrdp (bool)));
     connect (mDevicesSFDialogAction, SIGNAL(triggered()), this, SLOT(devicesOpenSFDialog()));
     connect (mDevicesInstallGuestToolsAction, SIGNAL(triggered()), this, SLOT(devicesInstallGuestAdditions()));
+    connect (mDevicesInstallWineD3DAction, SIGNAL(triggered()), this, SLOT(devicesInstallWineD3D()));
 
 
     connect (mDevicesMountFloppyMenu, SIGNAL(aboutToShow()), this, SLOT(prepareFloppyMenu()));
@@ -1710,6 +1716,10 @@ void VBoxConsoleWnd::retranslateUi()
     mDevicesInstallGuestToolsAction->setStatusTip (
         tr ("Mount the Guest Additions installation image"));
 
+    mDevicesInstallWineD3DAction->setText (tr ("Install &WineD3D (for Direct3D)..."));
+    mDevicesInstallWineD3DAction->setStatusTip (
+        tr ("Mount the WineD3D installation image. WineD3D provides Direct3D functionality on Windows guests."));
+
 #ifdef VBOX_WITH_DEBUGGER_GUI
     /* Debug actions */
 
@@ -2883,6 +2893,55 @@ void VBoxConsoleWnd::devicesInstallGuest
     }
 }
 
+/* Based on devicesInstallGuestAdditions() */
+void VBoxConsoleWnd::devicesInstallWineD3D()
+{
+#if defined (DEBUG_dmik) /* subscribe yourself here if you care for this behavior. */
+    QString src1 = qApp->applicationDirPath() + "/../../release/bin/wined3d.iso";
+    QString src2 = qApp->applicationDirPath() + "/../../release/bin/additions/wined3d.iso";
+#else
+    char szAppPrivPath [RTPATH_MAX];
+    int rc = RTPathAppPrivateNoArch (szAppPrivPath, sizeof (szAppPrivPath));
+    AssertRC (rc);
+
+    QString src1 = QString (szAppPrivPath) + "/wined3d.iso";
+    QString src2 = qApp->applicationDirPath() + "/additions/wined3d.iso";
+#endif
+
+    /* Check the standard image locations */
+    if (QFile::exists (src1))
+        return installGuestAdditionsFrom (src1);
+    else if (QFile::exists (src2))
+        return installGuestAdditionsFrom (src2);
+
+    /* Check for the already registered image */
+    CVirtualBox vbox = vboxGlobal().virtualBox();
+    QString name = QString ("wined3d.iso");
+
+    CDVDImage2Vector vec = vbox.GetDVDImages();
+    for (CDVDImage2Vector::ConstIterator it = vec.begin();
+         it != vec.end(); ++ it)
+    {
+        QString path = it->GetLocation();
+        /* Compare the name part ignoring the file case */
+        QString fn = QFileInfo (path).fileName();
+        if (RTPathCompare (name.toUtf8().constData(), fn.toUtf8().constData()) == 0)
+            return installGuestAdditionsFrom (path);
+    }
+
+    /* Download the required image */
+    {
+        QString source = QString ("http://download.savannah.nongnu.org/releases/wined3d/latest/wined3d.iso");
+        QString target = QDir (vboxGlobal().virtualBox().GetHomeFolder())
+                               .absoluteFilePath (name);
+
+        VBoxAdditionsDownloader *dl =
+            new VBoxAdditionsDownloader (source, target, mDevicesInstallWineD3DAction);
+        statusBar()->addWidget (dl, 0);
+        dl->start();
+    }
+}
+
 void VBoxConsoleWnd::installGuestAdditionsFrom (const QString &aSource)
 {
     CVirtualBox vbox = vboxGlobal().virtualBox();
