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);
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()
115 QMap<QString, EmuFrontObject*> mediaImages;
116 QList<MediaImageContainer*> mediaImageContainers;
119 if (!micTable || !micTable->model()) {
120 throw EmuFrontException(tr("No search results available!"));
122 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
123 throw EmuFrontException(tr("Emulator not selected!"));
125 QItemSelectionModel *selModel = micTable->selectionModel();
126 QModelIndexList listMIndex = selModel->selectedIndexes();
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 exe = dynamic_cast<Executable*>(obExe);
138 throw EmuFrontException(tr("Failed creating Emulator object!"));
141 foreach(QModelIndex mind, listMIndex) {
142 if (!mind.isValid()) continue;
143 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
145 qDebug() << "Failed creating media image container at row " << mind.row();
148 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
150 qDebug() << "Failed to create media image container for " << obImg->getName();
154 mediaImageContainers << mic;
155 QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
156 mediaImages.unite(contained);
159 if (mediaImages.count() < 1) {
160 throw EmuFrontException("No media images available!");
163 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
164 QString opts = exe->getOptions();
165 QRegExp rx("(\\s\\$\\d+)");
168 while ((pos = rx.indexIn(opts, pos)) != -1) {
170 pos += rx.matchedLength();
174 QList<EmuFrontObject*> selectedImages;
175 if (list.count() > mediaImages.count()) {
176 throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
178 if (list.count() > 1) {
179 int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
180 for(int i = 0; i < lim; i++) {
181 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
182 this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
184 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
186 selectedImages << efo;
187 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
188 QString key = mi->getCheckSum();
189 mediaImages.remove(key);
191 if (mediaImages.count() == 1) {
192 // there should be at least one media image left in mediaImages map
193 selectedImages << mediaImages.values().first();
196 else if (mediaImages.count() > 1) {
197 // show select boot image dialog
198 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
199 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
201 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
203 selectedImages << efo;
205 else if (mediaImages.count() == 1)
206 selectedImages << mediaImages.values().first();
207 // in the both cases the (ordered) list of media images will be passed to emuHelper
209 if (selectedImages.count() < 1)
210 throw EmuFrontException(tr("No media images selected"));
212 emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
213 } catch (EmuFrontException efe) {
215 qDeleteAll(mediaImageContainers);
216 //qDeleteAll(mediaImages); these are already deleted along with containers
217 QMessageBox::information(this, tr("Launching emulator"),
218 efe.what(), QMessageBox::Ok);
223 void EmuLauncher::processError(QProcess::ProcessError e)
226 QString stdErr = emuHelper->readAllStandardError();
227 QMessageBox::warning(this, tr("Emulator"),
228 tr("Launching emulator failed with: %1.\n").arg(e)
229 .append(";\n").append(proc->errorString().append(";\n")
230 .append(stdErr)), QMessageBox::Ok );
233 /* Slot for EmuHelper process finished, clears the temporary folder files */
234 void EmuLauncher::processFinished(int a)
237 QString stdErr = emuHelper->readAllStandardError();
238 QString stdMsg = emuHelper->readAllStandardOutput();
239 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
240 if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
241 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
244 void EmuLauncher::cleanTmp()