Added common base model for emu front objects
[emufront] / src / emulauncher.cpp
index d5eb206..100fcdb 100644 (file)
 #include "utils/emuhelper.h"
 #include "dialogs/emufrontinputdialog.h"
 
-EmuLauncher::EmuLauncher(QWidget *parent) :
-    QWidget(parent)
+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);
     dbMic = 0;
-    proc = 0;
     emuHelper = new EmuHelper(this);
     initWidgets();
     layout();
@@ -48,39 +47,48 @@ EmuLauncher::EmuLauncher(QWidget *parent) :
 
 EmuLauncher::~EmuLauncher()
 {
-    if (proc) {
-        proc->kill(); // TODO: do this in a more sophisticated way
-        delete proc;
-    }
+    if (emuHelper) {
+        qDebug() << "EmuLauncher destructor";
+        if (emuHelper->state() == EmuHelper::Running)
+            qDebug() << "EmuHelper process is running, killing...";
+            emuHelper->kill();
+        }
 }
 
 void EmuLauncher::updateData()
 {
     platformSelectBox->updateDataModel();
     mediaTypeSelectBox->updateDataModel();
-    execSelectBox->updateDataModel();
+    //execSelectBox->updateDataModel();
 }
 
 void EmuLauncher::initWidgets()
 {
     micTable = new QTableView(this);
-    micTable->setSelectionMode(QAbstractItemView::MultiSelection);
+    micTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
+    micTable->setCornerButtonEnabled(false);
+    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);
-    selectButton = new QPushButton(tr("&Update"));
-    launchButton = new QPushButton(tr("&Launch"));
+    selectButton = new QPushButton(tr("&Update"), this);
+    launchButton = new QPushButton(tr("&Launch"), this);
 }
 
 void EmuLauncher::layout()
 {
     QGridLayout *grid = new QGridLayout;
     grid->addWidget(platformSelectBox, 0, 0);
-    grid->addWidget(mediaTypeSelectBox, 1, 0);
-    grid->addWidget(selectButton, 1, 1);
-    grid->addWidget(micTable, 2, 0, 1, 2);
-    grid->addWidget(execSelectBox, 3, 0);
-    grid->addWidget(launchButton, 3, 1);
+    grid->addWidget(mediaTypeSelectBox, 0, 1);
+    grid->addWidget(selectButton, 0, 2);
+    grid->setColumnStretch(3, 1);
+
+    grid->addWidget(micTable, 1, 0, 1, 4);
+    grid->addWidget(execSelectBox, 2, 0);
+    grid->addWidget(launchButton, 2, 1);
+    // grid will be implicitly parented to this
     setLayout(grid);
 }
 
@@ -94,27 +102,46 @@ 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;
+    int mtid, plfid = -1;
+    MediaType *mt = 0;
+    Platform *plf = 0;
+    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);
     micTable->setModel(dbMic->getDataModel());
+    micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
+    micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
+    micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
+    micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
     micTable->resizeColumnsToContents();
-    platformSelectBox->updateDataModel();
-    mediaTypeSelectBox->updateDataModel();
+    execSelectBox->updateToSetup(plfid, mtid);
 }
 
 void EmuLauncher::launchEmu()
 {
+    // if selected emulator has no extensions configured, it's assumed to be a M.A.M.E. or similar and
+    // map of media images will be no be used
     QMap<QString, EmuFrontObject*> mediaImages;
     QList<MediaImageContainer*> mediaImageContainers;
-    Executable *exe;
+    Executable *exe = 0;
     try {
         if (!micTable || !micTable->model()) {
             throw EmuFrontException(tr("No search results available!"));
@@ -122,12 +149,8 @@ void EmuLauncher::launchEmu()
         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
             throw EmuFrontException(tr("Emulator not selected!"));
         }
-        //QModelIndex mindex = micTable->currentIndex();
         QItemSelectionModel *selModel = micTable->selectionModel();
         QModelIndexList listMIndex =  selModel->selectedIndexes();
-        /*if (!mindex.isValid()) {
-            throw EmuFrontException(tr("Media image container not selected!"));
-        }*/
         if (listMIndex.count() < 1) {
             throw EmuFrontException(tr("Media image container not selected!"));
         }
@@ -142,11 +165,23 @@ void EmuLauncher::launchEmu()
             throw EmuFrontException(tr("Failed creating Emulator object!"));
         }
 
-        // TODO: multiple media image container selection
-        //          - build a list of selected media image objects
-        //          - check that the platform and media type (setup) matches
+        qDebug() << "File types; " << exe->getSetup()->getSupportedFileTypeExtensions().count();
+
+        bool mame = exe->getSetup()->getSupportedFileTypeExtensions().isEmpty();
+
+        if (mame && listMIndex.count() > 1) {
+            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) {
-            EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
+            if (!mind.isValid()) continue;
+            EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind); // throws EmuFrontException
             if (!obImg) {
                 qDebug() << "Failed creating media image container at row " << mind.row();
                 continue;
@@ -160,61 +195,94 @@ void EmuLauncher::launchEmu()
             mediaImageContainers << mic;
             QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
             mediaImages.unite(contained);
-            /*QMapIterator<QString, MediaImage*> it(contained);
-            while (it.hasNext()){
-                mediaImages << it.value();
-            }*/
         }
 
-        if (mediaImages.count() < 1) {
-            throw new EmuFrontException("No media images available!");
+        if (mame) {
+            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
 
-        // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
-        QString opts = exe->getOptions();
-        QRegExp rx("(\\s\\$\\d+\\s)");
-        QStringList list;
-        int pos = 0;
-        while ((pos = rx.indexIn(opts, pos)) != -1) {
-            list << rx.cap(1);
-            pos += rx.matchedLength();
-        }
+            QList<EmuFrontObject*> selectedImages;
+            if (mediaImages.count() < 1) {
+                throw EmuFrontException("No media images available!");
+            }
 
-        QList<EmuFrontObject*> selectedImages;
-        if (list.count() > 1) {
-            for(int i = 0; i < list.count(); i++) {
-                //QInputDialog::getItem();
-                // TODO: Use input dialog here
-                // Create a new input dialog class for emufrontobjects
+            // 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();
             }
-            // TODO: show dialog to set the media order (the images will be assigned to slots in the order in respect to the image list)
-        }
-        else if (mediaImages.count() > 1) {
-            // show select boot image dialog
             bool ok;
-            EmuFrontObject *efo = EmuFrontInputDialog::getItem(
-                this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
-            if (!ok)  {
-                throw new EmuFrontException(tr("Boot image selection was canceled, aborting."));
+
+            if (list.count() > mediaImages.count()) {
+                throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
             }
-            selectedImages << efo;
-        }
-        else if (mediaImages.count() == 1)
-            selectedImages << mediaImages.values().first();
-        // in the both cases the (ordered) list of media images will be passed to emuHelper
+            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 boot image"), 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);
+            }
+            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
 
-        if (selectedImages.count() < 1)
-            throw new EmuFrontException(tr("No media images selected"));
+            // wee also keep the rest of the mediaimages in the selected containers for reference!
+            foreach(EmuFrontObject *efo, mediaImages) {
+                selectedImages << efo;
+            }
 
-        emuHelper->launch(exe, mediaImageContainers, selectedImages);
+            if (selectedImages.count() < 1)
+                throw EmuFrontException(tr("No media images selected"));
+
+            emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
+        }
     } catch (EmuFrontException efe) {
-        delete exe;
-        qDeleteAll(mediaImageContainers);
-        qDeleteAll(mediaImages);
-        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)
@@ -223,7 +291,7 @@ void EmuLauncher::processError(QProcess::ProcessError e)
     QString stdErr = emuHelper->readAllStandardError();
     QMessageBox::warning(this, tr("Emulator"),
         tr("Launching emulator failed with: %1.\n").arg(e)
-        .append(";\n").append(proc->errorString().append(";\n")
+        .append(";\n").append(emuHelper->errorString().append(";\n")
         .append(stdErr)), QMessageBox::Ok );
 }
 
@@ -234,7 +302,7 @@ void EmuLauncher::processFinished(int a)
     QString stdErr = emuHelper->readAllStandardError();
     QString stdMsg = emuHelper->readAllStandardOutput();
     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
-    if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
+    if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
 }
 
@@ -242,3 +310,8 @@ void EmuLauncher::cleanTmp()
 {
     // TODO
 }
+
+void EmuLauncher::setTmpDirPath(QString tmp)
+{
+    tmpDirPath = tmp;
+}