2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
22 #include <QSqlTableModel>
23 #include <QItemSelectionModel>
24 //#include "utils/OSDaB-Zip/unzip.h"
25 #include "emulauncher.h"
26 #include "db/dbmediatype.h"
27 #include "db/dbplatform.h"
28 #include "db/dbexecutable.h"
29 #include "db/dbmediaimagecontainer.h"
30 #include "widgets/effileobjectcombobox.h"
31 #include "widgets/executablecombobox.h"
32 #include "dataobjects/executable.h"
33 #include "utils/emuhelper.h"
35 EmuLauncher::EmuLauncher(QWidget *parent) :
38 dbPlatform = new DbPlatform(this);
39 dbMediaType = new DbMediaType(this);
40 dbExec = new DbExecutable(this);
43 emuHelper = new EmuHelper(this);
49 EmuLauncher::~EmuLauncher()
52 proc->kill(); // TODO: do this in a more sophisticated way
57 void EmuLauncher::updateData()
59 platformSelectBox->updateDataModel();
60 mediaTypeSelectBox->updateDataModel();
61 execSelectBox->updateDataModel();
64 void EmuLauncher::initWidgets()
66 micTable = new QTableView(this);
67 micTable->setSelectionMode(QAbstractItemView::MultiSelection);
68 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
69 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
70 execSelectBox = new ExecutableComboBox(dbExec, this);
71 selectButton = new QPushButton(tr("&Update"));
72 launchButton = new QPushButton(tr("&Launch"));
75 void EmuLauncher::layout()
77 QGridLayout *grid = new QGridLayout;
78 grid->addWidget(platformSelectBox, 0, 0);
79 grid->addWidget(mediaTypeSelectBox, 1, 0);
80 grid->addWidget(selectButton, 1, 1);
81 grid->addWidget(micTable, 2, 0, 1, 2);
82 grid->addWidget(execSelectBox, 3, 0);
83 grid->addWidget(launchButton, 3, 1);
87 void EmuLauncher::connectSignals()
89 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
90 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
91 connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
92 connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
95 void EmuLauncher::updateMediaImageContainers()
97 qDebug() << "updateMediaImageContainers slot";
98 int mtid = mediaTypeSelectBox->getSelected()
99 ? mediaTypeSelectBox->getSelected()->getId()
101 int plfid = platformSelectBox->getSelected()
102 ? platformSelectBox->getSelected()->getId()
105 if (!dbMic) dbMic = new DbMediaImageContainer(this);
106 dbMic->filter(mtid, plfid);
107 micTable->setModel(dbMic->getDataModel());
108 micTable->resizeColumnsToContents();
109 platformSelectBox->updateDataModel();
110 mediaTypeSelectBox->updateDataModel();
113 void EmuLauncher::launchEmu()
116 if (!micTable || !micTable->model()) {
117 throw EmuFrontException(tr("No search results available!"));
119 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
120 throw EmuFrontException(tr("Emulator not selected!"));
122 //QModelIndex mindex = micTable->currentIndex();
123 QItemSelectionModel *selModel = micTable->selectionModel();
124 QModelIndexList listMIndex = selModel->selectedIndexes();
125 /*if (!mindex.isValid()) {
126 throw EmuFrontException(tr("Media image container not selected!"));
128 if (listMIndex.count() < 1) {
129 throw EmuFrontException(tr("Media image container not selected!"));
131 qDebug() << listMIndex.count() << " items selected.";
133 // TODO: multiple media image container selection
134 // - build a list of selected media image objects
135 // - check that the platform and media type (setup) matches
136 QModelIndex mindex = listMIndex.first();
137 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
139 throw EmuFrontException(tr("Failed fetching selected media image container."));
141 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
143 throw EmuFrontException(tr("Failed creating media image container object!"));
145 EmuFrontObject *obExe = execSelectBox->getSelected();
147 throw EmuFrontException(tr("Failed fetching selected emulator!"));
149 Executable *exe = dynamic_cast<Executable*>(obExe);
151 throw EmuFrontException(tr("Failed creating Emulator object!"));
153 qDebug() << "Selected media image container "
154 << mic->getName() << " and emulator "
155 << obExe->getName() << ".";
156 if (mic->getMediaImages().count() > 0) {
157 // 1. Launch media image
159 // 2. If 2 or more media images in container
160 // show a diaglog for choosing the boot image
161 // 3. If 2 or more media image containers selected
162 // from a list show a dialog listing all the media
163 // images in those container for choosing the
165 // 4. If selected emulator command line containes more
166 // than one media image placeholder ($1, $2, ...)
167 // show a dialog for ordering the media images to
169 QList<MediaImage*> ls = mic->getMediaImages();
170 foreach(MediaImage *mi, ls) {
171 qDebug() << "Media image " << mi->getName();
173 emuHelper->launch(exe, mic);
175 } catch (EmuFrontException efe) {
176 QMessageBox::information(this, tr("Launching emulator"),
177 efe.what(), QMessageBox::Ok);
182 void EmuLauncher::processError(QProcess::ProcessError e)
184 QString stdErr = emuHelper->readAllStandardError();
185 QMessageBox::warning(this, tr("Emulator"),
186 tr("Launching emulator failed with: %1.\n").arg(e)
187 .append(";\n").append(proc->errorString().append(";\n")
188 .append(stdErr)), QMessageBox::Ok );
191 void EmuLauncher::processFinished(int a)
193 QString stdErr = emuHelper->readAllStandardError();
194 QString stdMsg = emuHelper->readAllStandardOutput();
195 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
196 if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
197 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);