3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
23 #include <QSqlTableModel>
24 #include <QItemSelectionModel>
25 #include "emulauncher.h"
26 #include "setupmodel.h"
27 #include "externalexecutablemodel.h"
28 #include "dbmediaimagecontainer.h"
29 #include "effileobjectcombobox.h"
30 #include "executablecombobox.h"
31 #include "executable.h"
32 #include "emuhelper.h"
33 #include "emufrontinputdialog.h"
35 EmuLauncher::EmuLauncher(QErrorMessage *errorMessage, QWidget *parent, QString tmp) :
36 QWidget(parent), tmpDirPath(tmp), errorMessage(errorMessage)
39 emuHelper = new EmuHelper(this);
45 EmuLauncher::~EmuLauncher()
48 qDebug() << "EmuLauncher destructor";
49 if (emuHelper->state() == EmuHelper::Running)
50 qDebug() << "EmuHelper process is running, killing...";
55 void EmuLauncher::updateData()
59 void EmuLauncher::initWidgets()
61 micTable = new QTableView(this);
62 micTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
63 micTable->setCornerButtonEnabled(false);
64 micTable->verticalHeader()->setVisible(false);
65 micTable->horizontalHeader()->setClickable(false);
67 SetupModel *supModel = new SetupModel(this);
68 setupSelectBox = new QComboBox(this);
69 setupSelectBox->setModel(supModel);
70 setupSelectBox->setModelColumn(SetupModel::Setup_Name);
72 ExternalExecutableModel *emuModel = new ExternalExecutableModel(this);
73 execSelectBox = new QComboBox(this);
74 execSelectBox->setModel(emuModel);
75 execSelectBox->setModelColumn(ExternalExecutableModel::Executable_Name);
77 selectButton = new QPushButton(tr("&Update"), this);
78 launchButton = new QPushButton(tr("&Launch"), this);
81 void EmuLauncher::layout()
83 QGridLayout *grid = new QGridLayout;
84 grid->addWidget(setupSelectBox, 0, 0, 1, 2);
85 grid->addWidget(selectButton, 0, 2);
86 grid->setColumnStretch(3, 1);
88 grid->addWidget(micTable, 1, 0, 1, 4);
89 grid->addWidget(execSelectBox, 2, 0);
90 grid->addWidget(launchButton, 2, 1);
91 // grid will be implicitly parented to this
95 void EmuLauncher::connectSignals()
97 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
98 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
99 connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
100 connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
103 void EmuLauncher::updateMediaImageContainers()
105 if (setupSelectBox->currentIndex() == -1) return;
107 // 1. get selected platform and media type id
108 QAbstractItemModel *setupAbsModel = setupSelectBox->model();
109 SetupModel *supModel = qobject_cast<SetupModel *>(setupAbsModel);
110 if (!supModel) return;
112 supModel->index(setupSelectBox->currentIndex(), SetupModel::Setup_PlatformId);
113 int plfid = supModel->data(plfInd).toInt();
115 supModel->index(setupSelectBox->currentIndex(), SetupModel::Setup_MediaTypeId);
116 int mtid = supModel->data(mtInd).toInt();
118 if (mtid < 0 || plfid < 0) return;
120 // 2. fetch available media image containers
121 if (!dbMic) dbMic = new DbMediaImageContainer(this);
122 dbMic->filter(mtid, plfid);
123 micTable->setModel(dbMic->getDataModel());
124 micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
125 micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
126 micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
127 micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
128 micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
129 micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
130 micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
131 micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
132 micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
133 micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
134 micTable->resizeColumnsToContents();
136 // 3. filter available emulators
138 supModel->index(setupSelectBox->currentIndex(), SetupModel::Setup_Id);
139 int supid = supModel->data(supInd).toInt();
140 QAbstractItemModel *execAbsModel = execSelectBox->model();
141 ExternalExecutableModel *execModel = qobject_cast<ExternalExecutableModel*>(execAbsModel);
142 if (!execModel) return;
143 execModel->filterBySetup(supid);
146 void EmuLauncher::launchEmu()
148 // if selected emulator has no extensions configured, it's assumed to be a M.A.M.E. or similar and
149 // map of media images will be no be used
150 QMap<QString, EmuFrontObject*> mediaImages;
151 QList<MediaImageContainer*> mediaImageContainers;
154 if (!micTable || !micTable->model()) {
155 throw EmuFrontException(tr("No search results available!"));
157 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
158 throw EmuFrontException(tr("Emulator not selected!"));
160 QItemSelectionModel *selModel = micTable->selectionModel();
161 QModelIndexList listMIndex = selModel->selectedIndexes();
162 if (listMIndex.count() < 1) {
163 throw EmuFrontException(tr("Media image container not selected!"));
165 qDebug() << listMIndex.count() << " items selected.";
167 // TODO: write a method to ExternalExecutable to return an Executable object of a selected row.
168 // TODO2: rewrite ExecutableComboBox and reimplement getSelected?
169 /*EmuFrontObject *obExe = execSelectBox->getSelected();
171 throw EmuFrontException(tr("Failed fetching selected emulator!"));
173 exe = dynamic_cast<Executable*>(obExe);
175 throw EmuFrontException(tr("Failed creating Emulator object!"));
178 qDebug() << "File types; " << exe->getSetup()->getSupportedFileTypeExtensions().count();
180 bool mame = exe->getSetup()->getSupportedFileTypeExtensions().isEmpty();
182 if (mame && listMIndex.count() > 1) {
183 throw EmuFrontException(tr("No supported file types configured for this emulator configuration. "
184 "Assuming emulator support container files as is. "
185 "Only one container can be selected without configuring supported file types."
189 // Now we have one or more media image containers and an emulator selected,
190 // let's fetch the media image container data.
192 foreach(QModelIndex mind, listMIndex) {
193 if (!mind.isValid()) continue;
194 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind); // throws EmuFrontException
196 qDebug() << "Failed creating media image container at row " << mind.row();
199 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
201 qDebug() << "Failed to create media image container for " << obImg->getName();
205 mediaImageContainers << mic;
206 QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
207 mediaImages.unite(contained);
211 emuHelper->launch(exe, mediaImageContainers);
215 // mediaImageContainers list contains all the selected media image containers and
216 // mediaImages list contains all the media images inside all the selected containers
218 QList<EmuFrontObject*> selectedImages;
219 if (mediaImages.count() < 1) {
220 throw EmuFrontException("No media images available!");
223 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
224 QString opts = exe->getOptions();
225 QRegExp rx("(\\$\\d+)");
228 while ((pos = rx.indexIn(opts, pos)) != -1) {
230 pos += rx.matchedLength();
234 if (list.count() > mediaImages.count()) {
235 throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
237 if (list.count() > 1) {
238 // more than one placeholder for media image in the command line ($1, $2, ...)
239 int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
240 // user sets the order of media images
241 for(int i = 0; i < lim; i++) {
242 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
243 this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
245 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
247 selectedImages << efo;
248 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
249 QString key = mi->getCheckSum();
250 mediaImages.remove(key);
252 // there should be at least one media image left in mediaImages map...
253 /*if (mediaImages.count() == 1) {
254 selectedImages << mediaImages.values().first();
255 } ... this is added later-> */
257 else if (mediaImages.count() > 1) {
258 // show select boot image dialog
259 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
260 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
262 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
264 selectedImages << efo;
265 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
266 QString key = mi->getCheckSum();
267 mediaImages.remove(key);
269 else if (mediaImages.count() == 1) {
270 EmuFrontObject *efo = mediaImages.values().first();
271 selectedImages << efo;
272 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
273 QString key = mi->getCheckSum();
274 mediaImages.remove(key);
276 // in all the both cases the (ordered) list of media images will be passed to emuHelper
278 // wee also keep the rest of the mediaimages in the selected containers for reference!
279 foreach(EmuFrontObject *efo, mediaImages) {
280 selectedImages << efo;
283 if (selectedImages.count() < 1)
284 throw EmuFrontException(tr("No media images selected"));
286 emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
288 } catch (EmuFrontException efe) {
289 errorMessage->showMessage(efe.what());
292 micTable->clearSelection();
294 qDeleteAll(mediaImageContainers);
295 //qDeleteAll(mediaImages); these are already deleted along with containers
298 void EmuLauncher::processError(QProcess::ProcessError e)
301 QString stdErr = emuHelper->readAllStandardError();
302 QMessageBox::warning(this, tr("Emulator"),
303 tr("Launching emulator failed with: %1.\n").arg(e)
304 .append(";\n").append(emuHelper->errorString().append(";\n")
305 .append(stdErr)), QMessageBox::Ok );
308 /* Slot for EmuHelper process finished, clears the temporary folder files */
309 void EmuLauncher::processFinished(int a)
312 QString stdErr = emuHelper->readAllStandardError();
313 QString stdMsg = emuHelper->readAllStandardOutput();
314 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
315 if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
316 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
319 void EmuLauncher::cleanTmp()
324 void EmuLauncher::setTmpDirPath(QString tmp)