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 QAbstractItemModel *absModel = execSelectBox->model();
168 ExternalExecutableModel *extModel = qobject_cast<ExternalExecutableModel*>(absModel);
169 exe = extModel->getExecutable(execSelectBox->currentIndex());
171 errorMessage->showMessage(tr("Failed creating an executable object from selection."));
175 bool mame = exe->getSetup()->getSupportedFileTypeExtensions().isEmpty();
177 if (mame && listMIndex.count() > 1) {
178 throw EmuFrontException(tr("No supported file types configured for this emulator configuration. "
179 "Assuming emulator support container files as is. "
180 "Only one container can be selected without configuring supported file types."
184 // Now we have one or more media image containers and an emulator selected,
185 // let's fetch the media image container data.
187 foreach(QModelIndex mind, listMIndex) {
188 if (!mind.isValid()) continue;
189 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind); // throws EmuFrontException
191 qDebug() << "Failed creating media image container at row " << mind.row();
194 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
196 qDebug() << "Failed to create media image container for " << obImg->getName();
200 mediaImageContainers << mic;
201 QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
202 mediaImages.unite(contained);
206 emuHelper->launch(exe, mediaImageContainers);
210 // mediaImageContainers list contains all the selected media image containers and
211 // mediaImages list contains all the media images inside all the selected containers
213 QList<EmuFrontObject*> selectedImages;
214 if (mediaImages.count() < 1) {
215 throw EmuFrontException("No media images available!");
218 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
219 QString opts = exe->getOptions();
220 QRegExp rx("(\\$\\d+)");
223 while ((pos = rx.indexIn(opts, pos)) != -1) {
225 pos += rx.matchedLength();
229 if (list.count() > mediaImages.count()) {
230 throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
232 if (list.count() > 1) {
233 // more than one placeholder for media image in the command line ($1, $2, ...)
234 int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
235 // user sets the order of media images
236 for(int i = 0; i < lim; i++) {
237 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
238 this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
240 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
242 selectedImages << efo;
243 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
244 QString key = mi->getCheckSum();
245 mediaImages.remove(key);
247 // there should be at least one media image left in mediaImages map...
248 /*if (mediaImages.count() == 1) {
249 selectedImages << mediaImages.values().first();
250 } ... this is added later-> */
252 else if (mediaImages.count() > 1) {
253 // show select boot image dialog
254 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
255 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
257 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
259 selectedImages << efo;
260 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
261 QString key = mi->getCheckSum();
262 mediaImages.remove(key);
264 else if (mediaImages.count() == 1) {
265 EmuFrontObject *efo = mediaImages.values().first();
266 selectedImages << efo;
267 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
268 QString key = mi->getCheckSum();
269 mediaImages.remove(key);
271 // in all the both cases the (ordered) list of media images will be passed to emuHelper
273 // wee also keep the rest of the mediaimages in the selected containers for reference!
274 foreach(EmuFrontObject *efo, mediaImages) {
275 selectedImages << efo;
278 if (selectedImages.count() < 1)
279 throw EmuFrontException(tr("No media images selected"));
281 emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
283 } catch (EmuFrontException efe) {
284 errorMessage->showMessage(efe.what());
287 micTable->clearSelection();
289 qDeleteAll(mediaImageContainers);
290 //qDeleteAll(mediaImages); these are already deleted along with containers
293 void EmuLauncher::processError(QProcess::ProcessError e)
296 QString stdErr = emuHelper->readAllStandardError();
297 QMessageBox::warning(this, tr("Emulator"),
298 tr("Launching emulator failed with: %1.\n").arg(e)
299 .append(";\n").append(emuHelper->errorString().append(";\n")
300 .append(stdErr)), QMessageBox::Ok );
303 /* Slot for EmuHelper process finished, clears the temporary folder files */
304 void EmuLauncher::processFinished(int a)
307 QString stdErr = emuHelper->readAllStandardError();
308 QString stdMsg = emuHelper->readAllStandardOutput();
309 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
310 if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
311 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
314 void EmuLauncher::cleanTmp()
319 void EmuLauncher::setTmpDirPath(QString tmp)