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 mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
70 platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
71 execSelectBox = new ExecutableComboBox(dbExec, this);
72 selectButton = new QPushButton(tr("&Update"), this);
73 launchButton = new QPushButton(tr("&Launch"), this);
76 void EmuLauncher::layout()
78 QGridLayout *grid = new QGridLayout;
79 grid->addWidget(platformSelectBox, 0, 0);
80 grid->addWidget(mediaTypeSelectBox, 1, 0);
81 grid->addWidget(selectButton, 1, 1);
82 grid->setColumnStretch(2, 1);
84 grid->addWidget(micTable, 2, 0, 1, 3);
85 grid->addWidget(execSelectBox, 3, 0);
86 grid->addWidget(launchButton, 3, 1);
87 // grid will be implicitily parented to this
91 void EmuLauncher::connectSignals()
93 connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
94 connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
95 connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
96 connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
99 void EmuLauncher::updateMediaImageContainers()
101 qDebug() << "updateMediaImageContainers slot";
102 int mtid = mediaTypeSelectBox->getSelected()
103 ? mediaTypeSelectBox->getSelected()->getId()
105 int plfid = platformSelectBox->getSelected()
106 ? platformSelectBox->getSelected()->getId()
109 if (!dbMic) dbMic = new DbMediaImageContainer(this);
110 dbMic->filter(mtid, plfid);
111 micTable->setModel(dbMic->getDataModel());
112 micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
113 micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
114 micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
115 micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
116 micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
117 micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
118 micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
119 micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
120 micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
121 micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
122 micTable->resizeColumnsToContents();
123 platformSelectBox->updateDataModel();
124 mediaTypeSelectBox->updateDataModel();
125 execSelectBox->updateToSetup(plfid, mtid);
128 void EmuLauncher::launchEmu()
130 QMap<QString, EmuFrontObject*> mediaImages;
131 QList<MediaImageContainer*> mediaImageContainers;
134 if (!micTable || !micTable->model()) {
135 throw EmuFrontException(tr("No search results available!"));
137 if (!execSelectBox || execSelectBox->currentIndex() == -1) {
138 throw EmuFrontException(tr("Emulator not selected!"));
140 QItemSelectionModel *selModel = micTable->selectionModel();
141 QModelIndexList listMIndex = selModel->selectedIndexes();
142 if (listMIndex.count() < 1) {
143 throw EmuFrontException(tr("Media image container not selected!"));
145 qDebug() << listMIndex.count() << " items selected.";
147 EmuFrontObject *obExe = execSelectBox->getSelected();
149 throw EmuFrontException(tr("Failed fetching selected emulator!"));
151 exe = dynamic_cast<Executable*>(obExe);
153 throw EmuFrontException(tr("Failed creating Emulator object!"));
156 foreach(QModelIndex mind, listMIndex) {
157 if (!mind.isValid()) continue;
158 EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
160 qDebug() << "Failed creating media image container at row " << mind.row();
163 MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
165 qDebug() << "Failed to create media image container for " << obImg->getName();
169 mediaImageContainers << mic;
170 QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
171 mediaImages.unite(contained);
174 if (mediaImages.count() < 1) {
175 throw EmuFrontException("No media images available!");
178 // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
179 QString opts = exe->getOptions();
180 QRegExp rx("(\\$\\d+)");
183 while ((pos = rx.indexIn(opts, pos)) != -1) {
185 pos += rx.matchedLength();
189 QList<EmuFrontObject*> selectedImages;
190 if (list.count() > mediaImages.count()) {
191 throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
193 if (list.count() > 1) {
194 int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
195 for(int i = 0; i < lim; i++) {
196 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
197 this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
199 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
201 selectedImages << efo;
202 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
203 QString key = mi->getCheckSum();
204 mediaImages.remove(key);
206 if (mediaImages.count() == 1) {
207 // there should be at least one media image left in mediaImages map
208 selectedImages << mediaImages.values().first();
211 else if (mediaImages.count() > 1) {
212 // show select boot image dialog
213 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
214 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
216 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
218 selectedImages << efo;
220 else if (mediaImages.count() == 1)
221 selectedImages << mediaImages.values().first();
222 // in the both cases the (ordered) list of media images will be passed to emuHelper
224 if (selectedImages.count() < 1)
225 throw EmuFrontException(tr("No media images selected"));
227 emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
228 micTable->clearSelection();
229 } catch (EmuFrontException efe) {
231 qDeleteAll(mediaImageContainers);
232 //qDeleteAll(mediaImages); these are already deleted along with containers
233 QMessageBox::information(this, tr("Launching emulator"),
234 efe.what(), QMessageBox::Ok);
239 void EmuLauncher::processError(QProcess::ProcessError e)
242 QString stdErr = emuHelper->readAllStandardError();
243 QMessageBox::warning(this, tr("Emulator"),
244 tr("Launching emulator failed with: %1.\n").arg(e)
245 .append(";\n").append(emuHelper->errorString().append(";\n")
246 .append(stdErr)), QMessageBox::Ok );
249 /* Slot for EmuHelper process finished, clears the temporary folder files */
250 void EmuLauncher::processFinished(int a)
253 QString stdErr = emuHelper->readAllStandardError();
254 QString stdMsg = emuHelper->readAllStandardOutput();
255 QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
256 if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
257 QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
260 void EmuLauncher::cleanTmp()
265 void EmuLauncher::setTmpDirPath(QString tmp)