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, QString tmp) :
36 QWidget(parent), tmpDirPath(tmp)
38 dbPlatform = new DbPlatform(this);
39 dbMediaType = new DbMediaType(this);
40 dbExec = new DbExecutable(this);
42 emuHelper = new EmuHelper(this);
48 EmuLauncher::~EmuLauncher()
51 qDebug() << "EmuLauncher destructor";
52 if (emuHelper->state() == EmuHelper::Running)
53 qDebug() << "EmuHelper process is running, killing...";
58 void EmuLauncher::updateData()
60 platformSelectBox->updateDataModel();
61 mediaTypeSelectBox->updateDataModel();
62 //execSelectBox->updateDataModel();
65 void EmuLauncher::initWidgets()
67 micTable = new QTableView(this);
68 micTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
69 micTable->setCornerButtonEnabled(false);
70 micTable->verticalHeader()->setVisible(false);
71 //micTable->horizontalHeader()->setDisabled(true);
72 micTable->horizontalHeader()->setClickable(false);
73 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
74 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
75 execSelectBox = new ExecutableComboBox(dbExec, this);
76 selectButton = new QPushButton(tr("&Update"), this);
77 launchButton = new QPushButton(tr("&Launch"), this);
80 void EmuLauncher::layout()
82 QGridLayout *grid = new QGridLayout;
83 grid->addWidget(platformSelectBox, 0, 0);
84 grid->addWidget(mediaTypeSelectBox, 0, 1);
85 grid->addWidget(selectButton, 0, 2);
86 grid->setColumnStretch(3, 1);
88 grid->addWidget(micTable, 1, 0, 1, 4);
89 grid->addWidget(execSelectBox, 2, 0);
90 grid->addWidget(launchButton, 2, 1);
91 // grid will be implicitily parented to this
95 void EmuLauncher::connectSignals()
97 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
98 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
99 connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
100 connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
103 void EmuLauncher::updateMediaImageContainers()
105 qDebug() << "updateMediaImageContainers slot";
106 int mtid = mediaTypeSelectBox->getSelected()
107 ? mediaTypeSelectBox->getSelected()->getId()
109 int plfid = platformSelectBox->getSelected()
110 ? platformSelectBox->getSelected()->getId()
113 if (!dbMic) dbMic = new DbMediaImageContainer(this);
114 dbMic->filter(mtid, plfid);
115 micTable->setModel(dbMic->getDataModel());
116 micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
117 micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
118 micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
119 micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
120 micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
121 micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
122 micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
123 micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
124 micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
125 micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
126 micTable->resizeColumnsToContents();
127 platformSelectBox->updateDataModel();
128 mediaTypeSelectBox->updateDataModel();
129 execSelectBox->updateToSetup(plfid, mtid);
132 void EmuLauncher::launchEmu()
134 QMap<QString, EmuFrontObject*> mediaImages;
135 QList<MediaImageContainer*> mediaImageContainers;
138 if (!micTable || !micTable->model()) {
139 throw EmuFrontException(tr("No search results available!"));
141 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
142 throw EmuFrontException(tr("Emulator not selected!"));
144 QItemSelectionModel *selModel = micTable->selectionModel();
145 QModelIndexList listMIndex = selModel->selectedIndexes();
146 if (listMIndex.count() < 1) {
147 throw EmuFrontException(tr("Media image container not selected!"));
149 qDebug() << listMIndex.count() << " items selected.";
151 EmuFrontObject *obExe = execSelectBox->getSelected();
153 throw EmuFrontException(tr("Failed fetching selected emulator!"));
155 exe = dynamic_cast<Executable*>(obExe);
157 throw EmuFrontException(tr("Failed creating Emulator object!"));
160 foreach(QModelIndex mind, listMIndex) {
161 if (!mind.isValid()) continue;
162 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
164 qDebug() << "Failed creating media image container at row " << mind.row();
167 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
169 qDebug() << "Failed to create media image container for " << obImg->getName();
173 mediaImageContainers << mic;
174 QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
175 mediaImages.unite(contained);
178 if (mediaImages.count() < 1) {
179 throw EmuFrontException("No media images available!");
182 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
183 QString opts = exe->getOptions();
184 QRegExp rx("(\\$\\d+)");
187 while ((pos = rx.indexIn(opts, pos)) != -1) {
189 pos += rx.matchedLength();
193 QList<EmuFrontObject*> selectedImages;
194 if (list.count() > mediaImages.count()) {
195 throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
197 if (list.count() > 1) {
198 int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
199 for(int i = 0; i < lim; i++) {
200 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
201 this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
203 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
205 selectedImages << efo;
206 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
207 QString key = mi->getCheckSum();
208 mediaImages.remove(key);
210 if (mediaImages.count() == 1) {
211 // there should be at least one media image left in mediaImages map
212 selectedImages << mediaImages.values().first();
215 else if (mediaImages.count() > 1) {
216 // show select boot image dialog
217 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
218 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
220 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
222 selectedImages << efo;
224 else if (mediaImages.count() == 1)
225 selectedImages << mediaImages.values().first();
226 // in the both cases the (ordered) list of media images will be passed to emuHelper
228 if (selectedImages.count() < 1)
229 throw EmuFrontException(tr("No media images selected"));
231 emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
232 micTable->clearSelection();
233 } catch (EmuFrontException efe) {
235 qDeleteAll(mediaImageContainers);
236 //qDeleteAll(mediaImages); these are already deleted along with containers
237 QMessageBox::information(this, tr("Launching emulator"),
238 efe.what(), QMessageBox::Ok);
243 void EmuLauncher::processError(QProcess::ProcessError e)
246 QString stdErr = emuHelper->readAllStandardError();
247 QMessageBox::warning(this, tr("Emulator"),
248 tr("Launching emulator failed with: %1.\n").arg(e)
249 .append(";\n").append(emuHelper->errorString().append(";\n")
250 .append(stdErr)), QMessageBox::Ok );
253 /* Slot for EmuHelper process finished, clears the temporary folder files */
254 void EmuLauncher::processFinished(int a)
257 QString stdErr = emuHelper->readAllStandardError();
258 QString stdMsg = emuHelper->readAllStandardOutput();
259 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
260 if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
261 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
264 void EmuLauncher::cleanTmp()
269 void EmuLauncher::setTmpDirPath(QString tmp)