Added TODO.
[emufront] / src / emulauncher.cpp
index c6403e9..2a9e40a 100644 (file)
@@ -1,43 +1,49 @@
-// EmuFront
-// Copyright 2010 Mikko Keinänen
-//
-// This file is part of EmuFront.
-//
-//
-// EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 2 as published by
-// the Free Software Foundation and appearing in the file gpl.txt included in the
-// packaging of this file.
-//
-// EmuFront is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
-
+/*
+** EmuFront
+** Copyright 2010 Mikko Keinänen
+**
+** This file is part of EmuFront.
+**
+**
+** EmuFront is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License version 2 as published by
+** the Free Software Foundation and appearing in the file gpl.txt included in the
+** packaging of this file.
+**
+** EmuFront is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+*/
 #include <QtGui>
 #include <QProcess>
 #include <QSqlTableModel>
 #include <QItemSelectionModel>
 #include "emulauncher.h"
-#include "db/dbmediatype.h"
-#include "db/dbplatform.h"
-#include "db/dbexecutable.h"
-#include "db/dbmediaimagecontainer.h"
-#include "widgets/effileobjectcombobox.h"
-#include "widgets/executablecombobox.h"
-#include "dataobjects/executable.h"
-#include "utils/emuhelper.h"
-#include "dialogs/emufrontinputdialog.h"
-
-EmuLauncher::EmuLauncher(QWidget *parent, QString tmp) :
-    QWidget(parent), tmpDirPath(tmp)
+//#include "dbmediatype.h"
+#include "mediatypemodel.h"
+//#include "dbplatform.h"
+#include "platformmodel.h"
+//#include "dbexecutable.h"
+#include "externalexecutablemodel.h"
+#include "dbmediaimagecontainer.h"
+#include "effileobjectcombobox.h"
+#include "executablecombobox.h"
+#include "executable.h"
+#include "emuhelper.h"
+#include "emufrontinputdialog.h"
+#include "mediatype.h"
+#include "platform.h"
+
+EmuLauncher::EmuLauncher(QErrorMessage *errorMessage, QWidget *parent, QString tmp) :
+    QWidget(parent), tmpDirPath(tmp), errorMessage(errorMessage)
 {
-    dbPlatform = new DbPlatform(this);
-    dbMediaType = new DbMediaType(this);
-    dbExec = new DbExecutable(this);
+    //dbPlatform = new DbPlatform(this);
+    //dbMediaType = new DbMediaType(this);
+    //dbExec = new DbExecutable(this);
     dbMic = 0;
     emuHelper = new EmuHelper(this);
     initWidgets();
@@ -57,8 +63,8 @@ EmuLauncher::~EmuLauncher()
 
 void EmuLauncher::updateData()
 {
-    platformSelectBox->updateDataModel();
-    mediaTypeSelectBox->updateDataModel();
+    //platformSelectBox->updateDataModel();
+    //mediaTypeSelectBox->updateDataModel();
     //execSelectBox->updateDataModel();
 }
 
@@ -70,9 +76,25 @@ void EmuLauncher::initWidgets()
     micTable->verticalHeader()->setVisible(false);
     //micTable->horizontalHeader()->setDisabled(true);
     micTable->horizontalHeader()->setClickable(false);
-    mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
-    platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
-    execSelectBox = new ExecutableComboBox(dbExec, this);
+    //mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
+    //platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
+    //execSelectBox = new ExecutableComboBox(dbExec, this);
+
+    MediaTypeModel *mtModel = new MediaTypeModel(this);
+    mediaTypeSelectBox = new QComboBox(this);
+    mediaTypeSelectBox->setModel(mtModel);
+    mediaTypeSelectBox->setModelColumn(MediaTypeModel::EmuFrontFileObject_Name);
+
+    PlatformModel *plfModel = new PlatformModel(this);
+    platformSelectBox = new QComboBox(this);
+    platformSelectBox->setModel(plfModel);
+    platformSelectBox->setModelColumn(PlatformModel::EmuFrontFileObject_Name);
+
+    ExternalExecutableModel *emuModel = new ExternalExecutableModel(this);
+    execSelectBox = new QComboBox(this);
+    execSelectBox->setModel(emuModel);
+    execSelectBox->setModelColumn(ExternalExecutableModel::Executable_Name);
+
     selectButton = new QPushButton(tr("&Update"), this);
     launchButton = new QPushButton(tr("&Launch"), this);
 }
@@ -88,7 +110,7 @@ void EmuLauncher::layout()
     grid->addWidget(micTable, 1, 0, 1, 4);
     grid->addWidget(execSelectBox, 2, 0);
     grid->addWidget(launchButton, 2, 1);
-    // grid will be implicitily parented to this
+    // grid will be implicitly parented to this
     setLayout(grid);
 }
 
@@ -102,13 +124,43 @@ void EmuLauncher::connectSignals()
 
 void EmuLauncher::updateMediaImageContainers()
 {
-    qDebug() << "updateMediaImageContainers slot";
-    int mtid = mediaTypeSelectBox->getSelected()
-        ? mediaTypeSelectBox->getSelected()->getId()
-        : -1;
-    int plfid = platformSelectBox->getSelected()
-        ? platformSelectBox->getSelected()->getId()
-        : -1;
+    if (platformSelectBox->currentIndex() == -1 ||
+        mediaTypeSelectBox->currentIndex() == -1)
+        return;
+
+    int mtid, plfid = -1;
+
+    //MediaType *mt = 0;
+    //Platform *plf = 0;
+
+    // TODO: maybe rewrite EFFileObjectComboBox and put the following there:
+    QAbstractItemModel *plfAbsModel = platformSelectBox->model();
+    PlatformModel *plfModel = qobject_cast<PlatformModel *>(plfAbsModel);
+    if (!plfModel) return;
+    QModelIndex plfInd =
+        plfModel->index(platformSelectBox->currentIndex(), PlatformModel::EmuFrontFileObject_Id);
+    plfid = plfModel->data(plfInd).toInt();
+
+    // TODO: maybe rewrite EFFileObjectComboBox and put the following there:
+    QAbstractItemModel *mtAbsModel = mediaTypeSelectBox->model();
+    MediaTypeModel *mtModel = qobject_cast<MediaTypeModel *>(mtAbsModel);
+    if (!mtModel) return;
+    QModelIndex mtInd =
+        mtModel->index(mediaTypeSelectBox->currentIndex(), MediaTypeModel::EmuFrontFileObject_Id);
+    mtid = mtModel->data(mtInd).toInt();
+
+    /*try {
+        mt = dynamic_cast<MediaType*>(mediaTypeSelectBox->getSelected());
+        plf = dynamic_cast<Platform*>(platformSelectBox->getSelected());
+    }
+    catch(EmuFrontException &e){
+        errorMessage->showMessage(e.what());
+        return;
+    }*/
+    /*mtid = mt ? mt->getId() : -1;
+    plfid = plf ? plf->getId() : -1;
+    if (mt) delete mt;
+    if (plf) delete plf;*/
 
     if (!dbMic) dbMic = new DbMediaImageContainer(this);
     dbMic->filter(mtid, plfid);
@@ -124,9 +176,7 @@ void EmuLauncher::updateMediaImageContainers()
     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
     micTable->resizeColumnsToContents();
-    platformSelectBox->updateDataModel();
-    mediaTypeSelectBox->updateDataModel();
-    execSelectBox->updateToSetup(plfid, mtid);
+    //execSelectBox->updateToSetup(plfid, mtid);
 }
 
 void EmuLauncher::launchEmu()
@@ -150,36 +200,33 @@ void EmuLauncher::launchEmu()
         }
         qDebug() << listMIndex.count() << " items selected.";
 
-        EmuFrontObject *obExe = execSelectBox->getSelected();
+        // TODO: write a method to ExternalExecutable to return an Executable object of a selected row.
+        /*EmuFrontObject *obExe = execSelectBox->getSelected();
         if (!obExe) {
             throw EmuFrontException(tr("Failed fetching selected emulator!"));
         }
         exe = dynamic_cast<Executable*>(obExe);
         if (!exe) {
             throw EmuFrontException(tr("Failed creating Emulator object!"));
-        }
-
+        }*/
 
         qDebug() << "File types; " << exe->getSetup()->getSupportedFileTypeExtensions().count();
 
         bool mame = exe->getSetup()->getSupportedFileTypeExtensions().isEmpty();
 
-
-
         if (mame && listMIndex.count() > 1) {
-            throw new EmuFrontException(tr("No supported file types configured for this emulator configuration. "
+            throw EmuFrontException(tr("No supported file types configured for this emulator configuration. "
                 "Assuming emulator support container files as is. "
                 "Only one container can be selected without configuring supported file types."
             ));
         }
 
-
         // Now we have one or more media image containers and an emulator selected,
         // let's fetch the media image container data.
 
         foreach(QModelIndex mind, listMIndex) {
             if (!mind.isValid()) continue;
-            EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
+            EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind); // throws EmuFrontException
             if (!obImg) {
                 qDebug() << "Failed creating media image container at row " << mind.row();
                 continue;
@@ -199,37 +246,53 @@ void EmuLauncher::launchEmu()
             emuHelper->launch(exe, mediaImageContainers);
             return;
         }
+        else {
+            // mediaImageContainers list contains all the selected media image containers and
+            // mediaImages list contains all the media images inside all the selected containers
 
-        // mediaImageContainers list contains all the selected media image containers and
-        // mediaImages list contains all the media images inside all the selected containers
-
-
-        QList<EmuFrontObject*> selectedImages;
-        if (mediaImages.count() < 1) {
-            throw EmuFrontException("No media images available!");
-        }
+            QList<EmuFrontObject*> selectedImages;
+            if (mediaImages.count() < 1) {
+                throw EmuFrontException("No media images available!");
+            }
 
-        // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
-        QString opts = exe->getOptions();
-        QRegExp rx("(\\$\\d+)");
-        QStringList list;
-        int pos = 0;
-        while ((pos = rx.indexIn(opts, pos)) != -1) {
-            list << rx.cap(1);
-            pos += rx.matchedLength();
-        }
-        bool ok;
+            // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
+            QString opts = exe->getOptions();
+            QRegExp rx("(\\$\\d+)");
+            QStringList list;
+            int pos = 0;
+            while ((pos = rx.indexIn(opts, pos)) != -1) {
+                list << rx.cap(1);
+                pos += rx.matchedLength();
+            }
+            bool ok;
 
-        if (list.count() > mediaImages.count()) {
-            throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
-        }
-        if (list.count() > 1) {
-            // more than one placeholder for media image in the command line ($1, $2, ...)
-            int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
-            // user sets the order of media images
-            for(int i = 0; i < lim; i++) {
+            if (list.count() > mediaImages.count()) {
+                throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
+            }
+            if (list.count() > 1) {
+                // more than one placeholder for media image in the command line ($1, $2, ...)
+                int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
+                // user sets the order of media images
+                for(int i = 0; i < lim; i++) {
+                    EmuFrontObject *efo = EmuFrontInputDialog::getItem(
+                            this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
+                    if (!ok)  {
+                        throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
+                    }
+                    selectedImages << efo;
+                    MediaImage *mi = dynamic_cast<MediaImage*>(efo);
+                    QString key = mi->getCheckSum();
+                    mediaImages.remove(key);
+                }
+                // there should be at least one media image left in mediaImages map...
+                /*if (mediaImages.count() == 1) {
+                selectedImages << mediaImages.values().first();
+            } ... this is added later-> */
+            }
+            else if (mediaImages.count() > 1) {
+                // show select boot image dialog
                 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
-                        this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
+                        this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
                 if (!ok)  {
                     throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
                 }
@@ -238,50 +301,33 @@ void EmuLauncher::launchEmu()
                 QString key = mi->getCheckSum();
                 mediaImages.remove(key);
             }
-            // there should be at least one media image left in mediaImages map...
-            /*if (mediaImages.count() == 1) {
-                selectedImages << mediaImages.values().first();
-            } ... this is added later-> */
-        }
-        else if (mediaImages.count() > 1) {
-            // show select boot image dialog
-            EmuFrontObject *efo = EmuFrontInputDialog::getItem(
-                    this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
-            if (!ok)  {
-                throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
+            else if (mediaImages.count() == 1) {
+                EmuFrontObject *efo = mediaImages.values().first();
+                selectedImages << efo;
+                MediaImage *mi = dynamic_cast<MediaImage*>(efo);
+                QString key = mi->getCheckSum();
+                mediaImages.remove(key);
             }
-            selectedImages << efo;
-            MediaImage *mi = dynamic_cast<MediaImage*>(efo);
-            QString key = mi->getCheckSum();
-            mediaImages.remove(key);
-        }
-        else if (mediaImages.count() == 1) {
-            EmuFrontObject *efo = mediaImages.values().first();
-            selectedImages << efo;
-            MediaImage *mi = dynamic_cast<MediaImage*>(efo);
-            QString key = mi->getCheckSum();
-            mediaImages.remove(key);
-        }
-        // in all the both cases the (ordered) list of media images will be passed to emuHelper
+            // in all the both cases the (ordered) list of media images will be passed to emuHelper
 
-        // wee also keep the rest of the mediaimages in the selected containers for reference!
-        foreach(EmuFrontObject *efo, mediaImages) {
-            selectedImages << efo;
-        }
+            // wee also keep the rest of the mediaimages in the selected containers for reference!
+            foreach(EmuFrontObject *efo, mediaImages) {
+                selectedImages << efo;
+            }
 
-        if (selectedImages.count() < 1)
-            throw EmuFrontException(tr("No media images selected"));
+            if (selectedImages.count() < 1)
+                throw EmuFrontException(tr("No media images selected"));
 
-        emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
-        micTable->clearSelection();
+            emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
+        }
     } catch (EmuFrontException efe) {
-        if (exe) delete exe;
-        qDeleteAll(mediaImageContainers);
-        //qDeleteAll(mediaImages); these are already deleted along with containers
-        QMessageBox::information(this, tr("Launching emulator"),
-                                 efe.what(), QMessageBox::Ok);
-        return;
+        errorMessage->showMessage(efe.what());
     }
+
+    micTable->clearSelection();
+    if (exe) delete exe;
+    qDeleteAll(mediaImageContainers);
+    //qDeleteAll(mediaImages); these are already deleted along with containers
 }
 
 void EmuLauncher::processError(QProcess::ProcessError e)