#include <QProcess>
#include <QSqlTableModel>
#include <QItemSelectionModel>
-//#include "utils/OSDaB-Zip/unzip.h"
#include "emulauncher.h"
#include "db/dbmediatype.h"
#include "db/dbplatform.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) :
QWidget(parent)
dbMediaType = new DbMediaType(this);
dbExec = new DbExecutable(this);
dbMic = 0;
- proc = 0;
+ emuHelper = new EmuHelper(this);
initWidgets();
layout();
connectSignals();
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()
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()
grid->addWidget(micTable, 2, 0, 1, 2);
grid->addWidget(execSelectBox, 3, 0);
grid->addWidget(launchButton, 3, 1);
+ // grid will be implicitily parented to this
setLayout(grid);
}
{
connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
+ connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
+ connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
}
void EmuLauncher::updateMediaImageContainers()
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();
void EmuLauncher::launchEmu()
{
+ QMap<QString, EmuFrontObject*> mediaImages;
+ QList<MediaImageContainer*> mediaImageContainers;
+ Executable *exe;
try {
if (!micTable || !micTable->model()) {
throw EmuFrontException(tr("No search results available!"));
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!"));
}
qDebug() << listMIndex.count() << " items selected.";
- // TODO: multiple media image container selection
- // - build a list of selected media image objects
- // - check that the platform and media type (setup) matches
- QModelIndex mindex = listMIndex.first();
- EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
- if (!obImg) {
- throw EmuFrontException(tr("Failed fetching selected media image container."));
- }
- MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
- if (!mic) {
- throw EmuFrontException(tr("Failed creating media image container object!"));
- }
EmuFrontObject *obExe = execSelectBox->getSelected();
if (!obExe) {
throw EmuFrontException(tr("Failed fetching selected emulator!"));
}
- Executable *exe = dynamic_cast<Executable*>(obExe);
+ exe = dynamic_cast<Executable*>(obExe);
if (!exe) {
throw EmuFrontException(tr("Failed creating Emulator object!"));
}
- qDebug() << "Selected media image container "
- << mic->getName() << " and emulator "
- << obExe->getName() << ".";
- if (mic->getMediaImages().count() > 0) {
- // 1. Launch media image
- // TODO
- // 2. If 2 or more media images in container
- // show a diaglog for choosing the boot image
- // 3. If 2 or more media image containers selected
- // from a list show a dialog listing all the media
- // images in those container for choosing the
- // boot image
- // 4. If selected emulator command line containes more
- // than one media image placeholder ($1, $2, ...)
- // show a dialog for ordering the media images to
- // right order.
- QList<MediaImage*> ls = mic->getMediaImages();
- foreach(MediaImage *mi, ls) {
- qDebug() << "Media image " << mi->getName();
+
+ foreach(QModelIndex mind, listMIndex) {
+ if (!mind.isValid()) continue;
+ EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
+ if (!obImg) {
+ qDebug() << "Failed creating media image container at row " << mind.row();
+ continue;
+ }
+ MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
+ if (!mic) {
+ qDebug() << "Failed to create media image container for " << obImg->getName();
+ delete obImg;
+ continue;
}
- launch(exe, mic);
+ mediaImageContainers << mic;
+ QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
+ mediaImages.unite(contained);
}
+
+ 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;
+ QList<EmuFrontObject*> selectedImages;
+ if (list.count() > mediaImages.count()) {
+ throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
+ }
+ if (list.count() > 1) {
+ int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
+ 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);
+ }
+ if (mediaImages.count() == 1) {
+ // there should be at least one media image left in mediaImages map
+ selectedImages << mediaImages.values().first();
+ }
+ }
+ 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;
+ }
+ 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 (selectedImages.count() < 1)
+ throw EmuFrontException(tr("No media images selected"));
+
+ emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
+ micTable->clearSelection();
} catch (EmuFrontException efe) {
+ delete exe;
+ qDeleteAll(mediaImageContainers);
+ //qDeleteAll(mediaImages); these are already deleted along with containers
QMessageBox::information(this, tr("Launching emulator"),
efe.what(), QMessageBox::Ok);
return;
}
}
-void EmuLauncher::launch(const Executable * ex, const MediaImageContainer * mic)
-{
- // extract the media image container to tmp folder
- // (TODO: tmp folder configuration)
- //UnZip unz;
-
- QString fp;
- fp.append(mic->getFilePath()->getName());
- if (!fp.endsWith('/')) fp.append("/");
- fp.append(mic->getName());
-
- //unz.openArchive(fp);
- //int err = unz.extractAll("/tmp/"); // TODO: this must be set dynamically
- //qDebug() << "extractAll to " << fp << " : " << err;
-
- // TODO: launch the 1st media image in the media image list of ex
- // or if emulator command options has a place for more than one
- // media image assign the media images in the list order
- // to emulator command line.
-
- QString opts = ex->getOptions();
- QString tmpfp = " \"/tmp/";
- qDebug() << "Launching file '" << mic->getMediaImages().first()->getName() << " '";
- tmpfp.append(mic->getMediaImages().first()->getName()).append("\"");
- opts.replace("$1", tmpfp);
- QString cmdWithParams;
- cmdWithParams.append(ex->getExecutable());
- cmdWithParams.append(" ").append(opts);
- // TODO: tmp will be set dynamically
- // TODO: assigning multiple media images
- qDebug() << "Command with params " << cmdWithParams;
- // Executable and MediaImageContainer objects are no more needed:
- delete ex;
- delete mic;
- if (!proc) {
- proc = new QProcess(this); // This has to be done in the heap
- connect(proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
- connect(proc, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
- }
- proc->start(cmdWithParams, QIODevice::ReadOnly);
-}
-
void EmuLauncher::processError(QProcess::ProcessError e)
{
- QString stdErr = proc->readAllStandardError();
+ cleanTmp();
+ 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 );
}
+/* Slot for EmuHelper process finished, clears the temporary folder files */
void EmuLauncher::processFinished(int a)
{
- QString stdErr = proc->readAllStandardError();
- QString stdMsg = proc->readAllStandardOutput();
+ cleanTmp();
+ 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);
}
+
+void EmuLauncher::cleanTmp()
+{
+ // TODO
+}