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.";
128 // TODO: multiple media image container selection
129 QModelIndex mindex = listMIndex.first();
130 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
132 throw EmuFrontException(tr("Failed fetching selected media image container."));
134 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
136 throw EmuFrontException(tr("Failed creating media image container object!"));
138 EmuFrontObject *obExe = execSelectBox->getSelected();
140 throw EmuFrontException(tr("Failed fetching selected emulator!"));
142 Executable *exe = dynamic_cast<Executable*>(obExe);
144 throw EmuFrontException(tr("Failed creating Emulator object!"));
146 qDebug() << "Selected media image container "
147 << mic->getName() << " and emulator "
148 << obExe->getName() << ".";
149 if (mic->getMediaImages().count() > 0) {
150 // 1. Launch media image
152 // 2. If 2 or more media images in container
153 // show a diaglog for choosing the boot image
154 // 3. If 2 or more media image containers selected
155 // from a list show a dialog listing all the media
156 // images in those container for choosing the
158 // 4. If selected emulator command line containes more
159 // than one media image placeholder ($1, $2, ...)
160 // show a dialog for ordering the media images to
162 QList<MediaImage*> ls = mic->getMediaImages();
163 foreach(MediaImage *mi, ls) {
164 qDebug() << "Media image " << mi->getName();
168 } catch (EmuFrontException efe) {
169 QMessageBox::information(this, tr("Launching emulator"),
170 efe.what(), QMessageBox::Ok);
175 void EmuLauncher::launch(const Executable * ex, const MediaImageContainer * mic)
177 // extract the media image container to tmp folder
178 // (TODO: tmp folder configuration)
182 fp.append(mic->getFilePath()->getName());
183 if (!fp.endsWith('/')) fp.append("/");
184 fp.append(mic->getName());
186 int err = unz.extractAll("/tmp/"); // TODO: this must be set dynamically
187 qDebug() << "extractAll to " << fp << " : " << err;
188 // TODO: launch the 1st media image in the media image list of ex
189 // or if emulator command options has a place for more than one
190 // media image assign the media images in the list order
191 // to emulator command line.
192 QString opts = ex->getOptions();
193 QString tmpfp = " \"/tmp/";
194 tmpfp.append(mic->getMediaImages().first()->getName()).append("\"");
195 opts.replace("$1", tmpfp);
196 QString cmdWithParams;
197 cmdWithParams.append(ex->getExecutable());
198 cmdWithParams.append(" ").append(opts);
199 // TODO: tmp will be set dynamically
200 // TODO: assigning multiple media images
201 qDebug() << "Command with params " << cmdWithParams;
202 // Executable and MediaImageContainer objects are no more needed:
206 proc = new QProcess(this); // This has to be done in the heap
207 connect(proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
208 connect(proc, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
210 proc->start(cmdWithParams, QIODevice::ReadOnly);
213 void EmuLauncher::processError(QProcess::ProcessError e)
215 QString stdErr = proc->readAllStandardError();
216 QMessageBox::warning(this, tr("Emulator"),
217 tr("Launching emulator failed with: %1.\n").arg(e)
218 .append(";\n").append(proc->errorString().append(";\n")
219 .append(stdErr)), QMessageBox::Ok );
222 void EmuLauncher::processFinished(int a)
224 QString stdErr = proc->readAllStandardError();
225 QString stdMsg = proc->readAllStandardOutput();
226 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
227 if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
228 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);