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 "utils/OSDaB-Zip/unzip.h"
25 #include "emulauncher.h"
26 #include "db/dbmediatype.h"
27 #include "db/dbplatform.h"
28 #include "db/dbexecutable.h"
29 #include "db/dbmediaimagecontainer.h"
30 #include "widgets/effileobjectcombobox.h"
31 #include "widgets/executablecombobox.h"
32 #include "dataobjects/executable.h"
34 EmuLauncher::EmuLauncher(QWidget *parent) :
37 dbPlatform = new DbPlatform(this);
38 dbMediaType = new DbMediaType(this);
39 dbExec = new DbExecutable(this);
47 EmuLauncher::~EmuLauncher()
50 proc->kill(); // TODO: do this in a more sophisticated way
55 void EmuLauncher::updateData()
57 platformSelectBox->updateDataModel();
58 mediaTypeSelectBox->updateDataModel();
59 execSelectBox->updateDataModel();
62 void EmuLauncher::initWidgets()
64 micTable = new QTableView(this);
65 micTable->setSelectionMode(QAbstractItemView::MultiSelection);
66 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
67 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
68 execSelectBox = new ExecutableComboBox(dbExec, this);
69 selectButton = new QPushButton(tr("&Update"));
70 launchButton = new QPushButton(tr("&Launch"));
73 void EmuLauncher::layout()
75 QGridLayout *grid = new QGridLayout;
76 grid->addWidget(platformSelectBox, 0, 0);
77 grid->addWidget(mediaTypeSelectBox, 1, 0);
78 grid->addWidget(selectButton, 1, 1);
79 grid->addWidget(micTable, 2, 0, 1, 2);
80 grid->addWidget(execSelectBox, 3, 0);
81 grid->addWidget(launchButton, 3, 1);
85 void EmuLauncher::connectSignals()
87 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
88 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
91 void EmuLauncher::updateMediaImageContainers()
93 qDebug() << "updateMediaImageContainers slot";
94 int mtid = mediaTypeSelectBox->getSelected()
95 ? mediaTypeSelectBox->getSelected()->getId()
97 int plfid = platformSelectBox->getSelected()
98 ? platformSelectBox->getSelected()->getId()
101 if (!dbMic) dbMic = new DbMediaImageContainer(this);
102 dbMic->filter(mtid, plfid);
103 micTable->setModel(dbMic->getDataModel());
104 micTable->resizeColumnsToContents();
105 platformSelectBox->updateDataModel();
106 mediaTypeSelectBox->updateDataModel();
109 void EmuLauncher::launchEmu()
112 if (!micTable || !micTable->model()) {
113 throw EmuFrontException(tr("No search results available!"));
115 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
116 throw EmuFrontException(tr("Emulator not selected!"));
118 //QModelIndex mindex = micTable->currentIndex();
119 QItemSelectionModel *selModel = micTable->selectionModel();
120 QModelIndexList listMIndex = selModel->selectedIndexes();
121 /*if (!mindex.isValid()) {
122 throw EmuFrontException(tr("Media image container not selected!"));
124 if (listMIndex.count() < 1) {
125 throw EmuFrontException(tr("Media image container not selected!"));
127 qDebug() << listMIndex.count() << " items selected.";
129 // TODO: multiple media image container selection
130 // - build a list of selected media image objects
131 // - check that the platform and media type (setup) matches
132 QModelIndex mindex = listMIndex.first();
133 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
135 throw EmuFrontException(tr("Failed fetching selected media image container."));
137 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
139 throw EmuFrontException(tr("Failed creating media image container object!"));
141 EmuFrontObject *obExe = execSelectBox->getSelected();
143 throw EmuFrontException(tr("Failed fetching selected emulator!"));
145 Executable *exe = dynamic_cast<Executable*>(obExe);
147 throw EmuFrontException(tr("Failed creating Emulator object!"));
149 qDebug() << "Selected media image container "
150 << mic->getName() << " and emulator "
151 << obExe->getName() << ".";
152 if (mic->getMediaImages().count() > 0) {
153 // 1. Launch media image
155 // 2. If 2 or more media images in container
156 // show a diaglog for choosing the boot image
157 // 3. If 2 or more media image containers selected
158 // from a list show a dialog listing all the media
159 // images in those container for choosing the
161 // 4. If selected emulator command line containes more
162 // than one media image placeholder ($1, $2, ...)
163 // show a dialog for ordering the media images to
165 QList<MediaImage*> ls = mic->getMediaImages();
166 foreach(MediaImage *mi, ls) {
167 qDebug() << "Media image " << mi->getName();
171 } catch (EmuFrontException efe) {
172 QMessageBox::information(this, tr("Launching emulator"),
173 efe.what(), QMessageBox::Ok);
178 void EmuLauncher::launch(const Executable * ex, const MediaImageContainer * mic)
180 // extract the media image container to tmp folder
181 // (TODO: tmp folder configuration)
185 fp.append(mic->getFilePath()->getName());
186 if (!fp.endsWith('/')) fp.append("/");
187 fp.append(mic->getName());
189 int err = unz.extractAll("/tmp/"); // TODO: this must be set dynamically
190 qDebug() << "extractAll to " << fp << " : " << err;
191 // TODO: launch the 1st media image in the media image list of ex
192 // or if emulator command options has a place for more than one
193 // media image assign the media images in the list order
194 // to emulator command line.
195 QString opts = ex->getOptions();
196 QString tmpfp = " \"/tmp/";
197 tmpfp.append(mic->getMediaImages().first()->getName()).append("\"");
198 opts.replace("$1", tmpfp);
199 QString cmdWithParams;
200 cmdWithParams.append(ex->getExecutable());
201 cmdWithParams.append(" ").append(opts);
202 // TODO: tmp will be set dynamically
203 // TODO: assigning multiple media images
204 qDebug() << "Command with params " << cmdWithParams;
205 // Executable and MediaImageContainer objects are no more needed:
209 proc = new QProcess(this); // This has to be done in the heap
210 connect(proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
211 connect(proc, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
213 proc->start(cmdWithParams, QIODevice::ReadOnly);
216 void EmuLauncher::processError(QProcess::ProcessError e)
218 QString stdErr = proc->readAllStandardError();
219 QMessageBox::warning(this, tr("Emulator"),
220 tr("Launching emulator failed with: %1.\n").arg(e)
221 .append(";\n").append(proc->errorString().append(";\n")
222 .append(stdErr)), QMessageBox::Ok );
225 void EmuLauncher::processFinished(int a)
227 QString stdErr = proc->readAllStandardError();
228 QString stdMsg = proc->readAllStandardOutput();
229 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
230 if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
231 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);