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 // TODO: multiple media image container selection
133 // - build a list of selected media image objects
134 // - check that the platform and media type (setup) matches
135 QModelIndex mindex = listMIndex.first();
136 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
138 throw EmuFrontException(tr("Failed fetching selected media image container."));
140 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
142 throw EmuFrontException(tr("Failed creating media image container object!"));
144 EmuFrontObject *obExe = execSelectBox->getSelected();
146 throw EmuFrontException(tr("Failed fetching selected emulator!"));
148 Executable *exe = dynamic_cast<Executable*>(obExe);
150 throw EmuFrontException(tr("Failed creating Emulator object!"));
152 qDebug() << "Selected media image container "
153 << mic->getName() << " and emulator "
154 << obExe->getName() << ".";
155 if (mic->getMediaImages().count() > 0) {
156 // 1. Launch media image
158 // 2. If 2 or more media images in container
159 // show a diaglog for choosing the boot image
160 // 3. If 2 or more media image containers selected
161 // from a list show a dialog listing all the media
162 // images in those container for choosing the
164 // 4. If selected emulator command line containes more
165 // than one media image placeholder ($1, $2, ...)
166 // show a dialog for ordering the media images to
168 QList<MediaImage*> ls = mic->getMediaImages();
169 foreach(MediaImage *mi, ls) {
170 qDebug() << "Media image " << mi->getName();
172 emuHelper->launch(exe, mic);
174 } catch (EmuFrontException efe) {
175 QMessageBox::information(this, tr("Launching emulator"),
176 efe.what(), QMessageBox::Ok);
181 void EmuLauncher::processError(QProcess::ProcessError e)
184 QString stdErr = emuHelper->readAllStandardError();
185 QMessageBox::warning(this, tr("Emulator"),
186 tr("Launching emulator failed with: %1.\n").arg(e)
187 .append(";\n").append(proc->errorString().append(";\n")
188 .append(stdErr)), QMessageBox::Ok );
191 /* Slot for EmuHelper process finished, clears the temporary folder files */
192 void EmuLauncher::processFinished(int a)
195 QString stdErr = emuHelper->readAllStandardError();
196 QString stdMsg = emuHelper->readAllStandardOutput();
197 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
198 if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
199 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
202 void EmuLauncher::cleanTmp()