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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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/>.
21 #include <QSqlTableModel>
22 #include "utils/OSDaB-Zip/unzip.h"
23 #include "emulauncher.h"
24 #include "db/dbmediatype.h"
25 #include "db/dbplatform.h"
26 #include "db/dbexecutable.h"
27 #include "db/dbmediaimagecontainer.h"
28 #include "widgets/effileobjectcombobox.h"
29 #include "widgets/executablecombobox.h"
30 #include "dataobjects/executable.h"
32 EmuLauncher::EmuLauncher(QWidget *parent) :
35 dbPlatform = new DbPlatform(this);
36 dbMediaType = new DbMediaType(this);
37 dbExec = new DbExecutable(this);
44 void EmuLauncher::updateData()
46 platformSelectBox->updateDataModel();
47 mediaTypeSelectBox->updateDataModel();
48 execSelectBox->updateDataModel();
51 void EmuLauncher::initWidgets()
53 micTable = new QTableView(this);
54 micTable->setSelectionMode(QAbstractItemView::SingleSelection);
55 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
56 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
57 execSelectBox = new ExecutableComboBox(dbExec, this);
58 selectButton = new QPushButton(tr("&Update"));
59 launchButton = new QPushButton(tr("&Launch"));
62 void EmuLauncher::layout()
64 QGridLayout *grid = new QGridLayout;
65 grid->addWidget(platformSelectBox, 0, 0);
66 grid->addWidget(mediaTypeSelectBox, 1, 0);
67 grid->addWidget(selectButton, 1, 1);
68 grid->addWidget(micTable, 2, 0, 1, 2);
69 grid->addWidget(execSelectBox, 3, 0);
70 grid->addWidget(launchButton, 3, 1);
74 void EmuLauncher::connectSignals()
76 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
77 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
80 void EmuLauncher::updateMediaImageContainers()
82 qDebug() << "updateMediaImageContainers slot";
83 int mtid = mediaTypeSelectBox->getSelected()
84 ? mediaTypeSelectBox->getSelected()->getId()
86 int plfid = platformSelectBox->getSelected()
87 ? platformSelectBox->getSelected()->getId()
90 if (!dbMic) dbMic = new DbMediaImageContainer(this);
91 dbMic->filter(mtid, plfid);
92 micTable->setModel(dbMic->getDataModel());
93 micTable->resizeColumnsToContents();
94 platformSelectBox->updateDataModel();
95 mediaTypeSelectBox->updateDataModel();
98 void EmuLauncher::launchEmu()
101 if (!micTable || !micTable->model()) {
102 throw EmuFrontException(tr("No search results available!"));
104 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
105 throw EmuFrontException(tr("Emulator not selected!"));
107 // TODO: multiple media image container selection
108 QModelIndex mindex = micTable->currentIndex();
109 if (!mindex.isValid()) {
110 throw EmuFrontException(tr("Media image container not selected!"));
112 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
114 throw EmuFrontException(tr("Failed fetching selected media image container."));
116 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
118 throw EmuFrontException(tr("Failed creating media image container object!"));
120 EmuFrontObject *obExe = execSelectBox->getSelected();
122 throw EmuFrontException(tr("Failed fetching selected emulator!"));
124 Executable *exe = dynamic_cast<Executable*>(obExe);
126 throw EmuFrontException(tr("Failed creating Emulator object!"));
128 qDebug() << "Selected media image container "
129 << mic->getName() << " and emulator "
130 << obExe->getName() << ".";
131 if (mic->getMediaImages().count() > 0) {
133 // 1. Launch media image
134 // 2. If 2 or more media images in container
135 // show a diaglog for choosing the boot image
136 // 3. If 2 or more media image containers selected
137 // from a list show a dialog listing all the media
138 // images in those container for choosing the
140 // 4. If selected emulator command line containes more
141 // than one media image placeholder ($1, $2, ...)
142 // show a dialog for ordering the media images to
144 QList<MediaImage*> ls = mic->getMediaImages();
145 foreach(MediaImage *mi, ls) {
146 qDebug() << "Media image " << mi->getName();
150 } catch (EmuFrontException efe) {
151 QMessageBox::information(this, tr("Launching emulator"),
152 efe.what(), QMessageBox::Ok);
157 void EmuLauncher::launch(const Executable * ex, const MediaImageContainer * mic) const
159 // extract the media image container to tmp folder
160 // (TODO: tmp folder configuration)
164 fp.append(mic->getFilePath()->getName());
165 if (!fp.endsWith('/')) fp.append("/");
166 fp.append(mic->getName());
168 int err = unz.extractAll("/tmp/"); // TODO: this must be set dynamically
169 qDebug() << "extractAll to " << fp << " : " << err;
170 // launch the 1st media image in the media image list of ex
171 // or if emulator command options has a place for more than one
172 // media image assign the media images in the list order
173 // to emulator command line.