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 "emulauncher.h"
25 #include "db/dbmediatype.h"
26 #include "db/dbplatform.h"
27 #include "db/dbexecutable.h"
28 #include "db/dbmediaimagecontainer.h"
29 #include "widgets/effileobjectcombobox.h"
30 #include "widgets/executablecombobox.h"
31 #include "dataobjects/executable.h"
32 #include "utils/emuhelper.h"
34 EmuLauncher::EmuLauncher(QWidget *parent) :
37 dbPlatform = new DbPlatform(this);
38 dbMediaType = new DbMediaType(this);
39 dbExec = new DbExecutable(this);
42 emuHelper = new EmuHelper(this);
48 EmuLauncher::~EmuLauncher()
51 proc->kill(); // TODO: do this in a more sophisticated way
56 void EmuLauncher::updateData()
58 platformSelectBox->updateDataModel();
59 mediaTypeSelectBox->updateDataModel();
60 execSelectBox->updateDataModel();
63 void EmuLauncher::initWidgets()
65 micTable = new QTableView(this);
66 micTable->setSelectionMode(QAbstractItemView::MultiSelection);
67 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
68 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
69 execSelectBox = new ExecutableComboBox(dbExec, this);
70 selectButton = new QPushButton(tr("&Update"));
71 launchButton = new QPushButton(tr("&Launch"));
74 void EmuLauncher::layout()
76 QGridLayout *grid = new QGridLayout;
77 grid->addWidget(platformSelectBox, 0, 0);
78 grid->addWidget(mediaTypeSelectBox, 1, 0);
79 grid->addWidget(selectButton, 1, 1);
80 grid->addWidget(micTable, 2, 0, 1, 2);
81 grid->addWidget(execSelectBox, 3, 0);
82 grid->addWidget(launchButton, 3, 1);
86 void EmuLauncher::connectSignals()
88 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
89 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
90 connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
91 connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
94 void EmuLauncher::updateMediaImageContainers()
96 qDebug() << "updateMediaImageContainers slot";
97 int mtid = mediaTypeSelectBox->getSelected()
98 ? mediaTypeSelectBox->getSelected()->getId()
100 int plfid = platformSelectBox->getSelected()
101 ? platformSelectBox->getSelected()->getId()
104 if (!dbMic) dbMic = new DbMediaImageContainer(this);
105 dbMic->filter(mtid, plfid);
106 micTable->setModel(dbMic->getDataModel());
107 micTable->resizeColumnsToContents();
108 platformSelectBox->updateDataModel();
109 mediaTypeSelectBox->updateDataModel();
112 void EmuLauncher::launchEmu()
115 if (!micTable || !micTable->model()) {
116 throw EmuFrontException(tr("No search results available!"));
118 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
119 throw EmuFrontException(tr("Emulator not selected!"));
121 //QModelIndex mindex = micTable->currentIndex();
122 QItemSelectionModel *selModel = micTable->selectionModel();
123 QModelIndexList listMIndex = selModel->selectedIndexes();
124 /*if (!mindex.isValid()) {
125 throw EmuFrontException(tr("Media image container not selected!"));
127 if (listMIndex.count() < 1) {
128 throw EmuFrontException(tr("Media image container not selected!"));
130 qDebug() << listMIndex.count() << " items selected.";
132 EmuFrontObject *obExe = execSelectBox->getSelected();
134 throw EmuFrontException(tr("Failed fetching selected emulator!"));
136 Executable *exe = dynamic_cast<Executable*>(obExe);
138 throw EmuFrontException(tr("Failed creating Emulator object!"));
141 // TODO: multiple media image container selection
142 // - build a list of selected media image objects
143 // - check that the platform and media type (setup) matches
144 QList<MediaImage*> mediaImages;
145 QList<MediaImageContainer*> mediaImageContainers;
146 foreach(QModelIndex mind, listMIndex) {
147 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
149 qDebug() << "Failed creating media image container at row " << mind.row();
152 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
154 qDebug() << "Failed to create media image container for " << obImg->getName();
158 mediaImageContainers << mic;
159 QList<MediaImage*> contained = mic->getMediaImages();
160 mediaImages << contained;
163 if (mediaImages.count() < 1) {
165 throw new EmuFrontException("No media images available!");
168 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
169 QString opts = exe->getOptions();
170 QRegExp rx("(\\s\\$\\d+\\s)");
173 while ((pos = rx.indexIn(opts, pos)) != -1) {
175 pos += rx.matchedLength();
178 if (list.count() > 1) {
179 // TODO: show dialog to set the media order (the images will be assigned to slots in the order in respect to the image list)
182 // TODO: show select boot image dialog
184 // in the both cases the (ordered) list of media images will be passed to emuHelper
186 foreach(MediaImage *mi, mediaImages) {
187 qDebug() << "Media image " << mi->getName();
189 emuHelper->launch(exe, mediaImageContainers, mediaImages);
190 } catch (EmuFrontException efe) {
191 QMessageBox::information(this, tr("Launching emulator"),
192 efe.what(), QMessageBox::Ok);
197 void EmuLauncher::processError(QProcess::ProcessError e)
200 QString stdErr = emuHelper->readAllStandardError();
201 QMessageBox::warning(this, tr("Emulator"),
202 tr("Launching emulator failed with: %1.\n").arg(e)
203 .append(";\n").append(proc->errorString().append(";\n")
204 .append(stdErr)), QMessageBox::Ok );
207 /* Slot for EmuHelper process finished, clears the temporary folder files */
208 void EmuLauncher::processFinished(int a)
211 QString stdErr = emuHelper->readAllStandardError();
212 QString stdMsg = emuHelper->readAllStandardOutput();
213 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
214 if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
215 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
218 void EmuLauncher::cleanTmp()