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"
33 #include "dialogs/emufrontinputdialog.h"
35 EmuLauncher::EmuLauncher(QWidget *parent) :
38 dbPlatform = new DbPlatform(this);
39 dbMediaType = new DbMediaType(this);
40 dbExec = new DbExecutable(this);
42 emuHelper = new EmuHelper(this);
48 EmuLauncher::~EmuLauncher()
51 qDebug() << "EmuLauncher destructor";
52 if (emuHelper->state() == EmuHelper::Running)
53 qDebug() << "EmuHelper process is running, killing...";
58 void EmuLauncher::updateData()
60 platformSelectBox->updateDataModel();
61 mediaTypeSelectBox->updateDataModel();
62 execSelectBox->updateDataModel();
65 void EmuLauncher::initWidgets()
67 micTable = new QTableView(this);
68 micTable->setSelectionMode(QAbstractItemView::MultiSelection);
69 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
70 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
71 execSelectBox = new ExecutableComboBox(dbExec, this);
72 selectButton = new QPushButton(tr("&Update"), this);
73 launchButton = new QPushButton(tr("&Launch"), this);
76 void EmuLauncher::layout()
78 QGridLayout *grid = new QGridLayout;
79 grid->addWidget(platformSelectBox, 0, 0);
80 grid->addWidget(mediaTypeSelectBox, 1, 0);
81 grid->addWidget(selectButton, 1, 1);
82 grid->addWidget(micTable, 2, 0, 1, 2);
83 grid->addWidget(execSelectBox, 3, 0);
84 grid->addWidget(launchButton, 3, 1);
85 // grid will be implicitily parented to this
89 void EmuLauncher::connectSignals()
91 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
92 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
93 connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
94 connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
97 void EmuLauncher::updateMediaImageContainers()
99 qDebug() << "updateMediaImageContainers slot";
100 int mtid = mediaTypeSelectBox->getSelected()
101 ? mediaTypeSelectBox->getSelected()->getId()
103 int plfid = platformSelectBox->getSelected()
104 ? platformSelectBox->getSelected()->getId()
107 if (!dbMic) dbMic = new DbMediaImageContainer(this);
108 dbMic->filter(mtid, plfid);
109 micTable->setModel(dbMic->getDataModel());
110 micTable->resizeColumnsToContents();
111 platformSelectBox->updateDataModel();
112 mediaTypeSelectBox->updateDataModel();
115 void EmuLauncher::launchEmu()
117 QMap<QString, EmuFrontObject*> mediaImages;
118 QList<MediaImageContainer*> mediaImageContainers;
121 if (!micTable || !micTable->model()) {
122 throw EmuFrontException(tr("No search results available!"));
124 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
125 throw EmuFrontException(tr("Emulator not selected!"));
127 QItemSelectionModel *selModel = micTable->selectionModel();
128 QModelIndexList listMIndex = selModel->selectedIndexes();
129 if (listMIndex.count() < 1) {
130 throw EmuFrontException(tr("Media image container not selected!"));
132 qDebug() << listMIndex.count() << " items selected.";
134 EmuFrontObject *obExe = execSelectBox->getSelected();
136 throw EmuFrontException(tr("Failed fetching selected emulator!"));
138 exe = dynamic_cast<Executable*>(obExe);
140 throw EmuFrontException(tr("Failed creating Emulator object!"));
143 foreach(QModelIndex mind, listMIndex) {
144 if (!mind.isValid()) continue;
145 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
147 qDebug() << "Failed creating media image container at row " << mind.row();
150 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
152 qDebug() << "Failed to create media image container for " << obImg->getName();
156 mediaImageContainers << mic;
157 QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
158 mediaImages.unite(contained);
161 if (mediaImages.count() < 1) {
162 throw EmuFrontException("No media images available!");
165 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
166 QString opts = exe->getOptions();
167 QRegExp rx("(\\$\\d+)");
170 while ((pos = rx.indexIn(opts, pos)) != -1) {
172 pos += rx.matchedLength();
176 QList<EmuFrontObject*> selectedImages;
177 if (list.count() > mediaImages.count()) {
178 throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
180 if (list.count() > 1) {
181 int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
182 for(int i = 0; i < lim; i++) {
183 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
184 this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
186 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
188 selectedImages << efo;
189 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
190 QString key = mi->getCheckSum();
191 mediaImages.remove(key);
193 if (mediaImages.count() == 1) {
194 // there should be at least one media image left in mediaImages map
195 selectedImages << mediaImages.values().first();
198 else if (mediaImages.count() > 1) {
199 // show select boot image dialog
200 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
201 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
203 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
205 selectedImages << efo;
207 else if (mediaImages.count() == 1)
208 selectedImages << mediaImages.values().first();
209 // in the both cases the (ordered) list of media images will be passed to emuHelper
211 if (selectedImages.count() < 1)
212 throw EmuFrontException(tr("No media images selected"));
214 emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
215 micTable->clearSelection();
216 } catch (EmuFrontException efe) {
218 qDeleteAll(mediaImageContainers);
219 //qDeleteAll(mediaImages); these are already deleted along with containers
220 QMessageBox::information(this, tr("Launching emulator"),
221 efe.what(), QMessageBox::Ok);
226 void EmuLauncher::processError(QProcess::ProcessError e)
229 QString stdErr = emuHelper->readAllStandardError();
230 QMessageBox::warning(this, tr("Emulator"),
231 tr("Launching emulator failed with: %1.\n").arg(e)
232 .append(";\n").append(emuHelper->errorString().append(";\n")
233 .append(stdErr)), QMessageBox::Ok );
236 /* Slot for EmuHelper process finished, clears the temporary folder files */
237 void EmuLauncher::processFinished(int a)
240 QString stdErr = emuHelper->readAllStandardError();
241 QString stdMsg = emuHelper->readAllStandardOutput();
242 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
243 if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
244 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
247 void EmuLauncher::cleanTmp()