Emulator selector is updated with only emulators to selected setup
[emufront] / src / emulauncher.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QtGui>
21 #include <QProcess>
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"
34
35 EmuLauncher::EmuLauncher(QWidget *parent) :
36     QWidget(parent)
37 {
38     dbPlatform = new DbPlatform(this);
39     dbMediaType = new DbMediaType(this);
40     dbExec = new DbExecutable(this);
41     dbMic = 0;
42     emuHelper = new EmuHelper(this);
43     initWidgets();
44     layout();
45     connectSignals();
46 }
47
48 EmuLauncher::~EmuLauncher()
49 {
50     if (emuHelper) {
51         qDebug() << "EmuLauncher destructor";
52         if (emuHelper->state() == EmuHelper::Running)
53             qDebug() << "EmuHelper process is running, killing...";
54             emuHelper->kill();
55         }
56 }
57
58 void EmuLauncher::updateData()
59 {
60     platformSelectBox->updateDataModel();
61     mediaTypeSelectBox->updateDataModel();
62     //execSelectBox->updateDataModel();
63 }
64
65 void EmuLauncher::initWidgets()
66 {
67     micTable = new QTableView(this);
68     micTable->setSelectionMode(QAbstractItemView::MultiSelection);
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);
74 }
75
76 void EmuLauncher::layout()
77 {
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->addWidget(micTable, 2, 0, 1, 2);
83     grid->addWidget(execSelectBox, 3, 0);
84     grid->addWidget(launchButton, 3, 1);
85     // grid will be implicitily parented to this
86     setLayout(grid);
87 }
88
89 void EmuLauncher::connectSignals()
90 {
91     connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
92     connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
93     connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
94     connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
95 }
96
97 void EmuLauncher::updateMediaImageContainers()
98 {
99     qDebug() << "updateMediaImageContainers slot";
100     int mtid = mediaTypeSelectBox->getSelected()
101         ? mediaTypeSelectBox->getSelected()->getId()
102         : -1;
103     int plfid = platformSelectBox->getSelected()
104         ? platformSelectBox->getSelected()->getId()
105         : -1;
106
107     if (!dbMic) dbMic = new DbMediaImageContainer(this);
108     dbMic->filter(mtid, plfid);
109     micTable->setModel(dbMic->getDataModel());
110     micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
111     micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
112     micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
113     micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
114     micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
115     micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
116     micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
117     micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
118     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
119     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
120     micTable->resizeColumnsToContents();
121     platformSelectBox->updateDataModel();
122     mediaTypeSelectBox->updateDataModel();
123     execSelectBox->updateToSetup(plfid, mtid);
124 }
125
126 void EmuLauncher::launchEmu()
127 {
128     QMap<QString, EmuFrontObject*> mediaImages;
129     QList<MediaImageContainer*> mediaImageContainers;
130     Executable *exe = 0;
131     try {
132         if (!micTable || !micTable->model()) {
133             throw EmuFrontException(tr("No search results available!"));
134         }
135         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
136             throw EmuFrontException(tr("Emulator not selected!"));
137         }
138         QItemSelectionModel *selModel = micTable->selectionModel();
139         QModelIndexList listMIndex =  selModel->selectedIndexes();
140         if (listMIndex.count() < 1) {
141             throw EmuFrontException(tr("Media image container not selected!"));
142         }
143         qDebug() << listMIndex.count() << " items selected.";
144
145         EmuFrontObject *obExe = execSelectBox->getSelected();
146         if (!obExe) {
147             throw EmuFrontException(tr("Failed fetching selected emulator!"));
148         }
149         exe = dynamic_cast<Executable*>(obExe);
150         if (!exe) {
151             throw EmuFrontException(tr("Failed creating Emulator object!"));
152         }
153
154         foreach(QModelIndex mind, listMIndex) {
155             if (!mind.isValid()) continue;
156             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
157             if (!obImg) {
158                 qDebug() << "Failed creating media image container at row " << mind.row();
159                 continue;
160             }
161             MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
162             if (!mic) {
163                 qDebug() << "Failed to create media image container for " << obImg->getName();
164                 delete obImg;
165                 continue;
166             }
167             mediaImageContainers << mic;
168             QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
169             mediaImages.unite(contained);
170         }
171
172         if (mediaImages.count() < 1) {
173             throw EmuFrontException("No media images available!");
174         }
175
176         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
177         QString opts = exe->getOptions();
178         QRegExp rx("(\\$\\d+)");
179         QStringList list;
180         int pos = 0;
181         while ((pos = rx.indexIn(opts, pos)) != -1) {
182             list << rx.cap(1);
183             pos += rx.matchedLength();
184         }
185
186         bool ok;
187         QList<EmuFrontObject*> selectedImages;
188         if (list.count() > mediaImages.count()) {
189             throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
190         }
191         if (list.count() > 1) {
192             int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
193             for(int i = 0; i < lim; i++) {
194                 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
195                         this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
196                 if (!ok)  {
197                     throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
198                 }
199                 selectedImages << efo;
200                 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
201                 QString key = mi->getCheckSum();
202                 mediaImages.remove(key);
203             }
204             if (mediaImages.count() == 1) {
205                 // there should be at least one media image left in mediaImages map
206                 selectedImages << mediaImages.values().first();
207             }
208         }
209         else if (mediaImages.count() > 1) {
210             // show select boot image dialog
211             EmuFrontObject *efo = EmuFrontInputDialog::getItem(
212                 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
213             if (!ok)  {
214                 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
215             }
216             selectedImages << efo;
217         }
218         else if (mediaImages.count() == 1)
219             selectedImages << mediaImages.values().first();
220         // in the both cases the (ordered) list of media images will be passed to emuHelper
221
222         if (selectedImages.count() < 1)
223             throw EmuFrontException(tr("No media images selected"));
224
225         emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
226         micTable->clearSelection();
227     } catch (EmuFrontException efe) {
228         if (exe) delete exe;
229         qDeleteAll(mediaImageContainers);
230         //qDeleteAll(mediaImages); these are already deleted along with containers
231         QMessageBox::information(this, tr("Launching emulator"),
232                                  efe.what(), QMessageBox::Ok);
233         return;
234     }
235 }
236
237 void EmuLauncher::processError(QProcess::ProcessError e)
238 {
239     cleanTmp();
240     QString stdErr = emuHelper->readAllStandardError();
241     QMessageBox::warning(this, tr("Emulator"),
242         tr("Launching emulator failed with: %1.\n").arg(e)
243         .append(";\n").append(emuHelper->errorString().append(";\n")
244         .append(stdErr)), QMessageBox::Ok );
245 }
246
247 /* Slot for EmuHelper process finished, clears the temporary folder files */
248 void EmuLauncher::processFinished(int a)
249 {
250     cleanTmp();
251     QString stdErr = emuHelper->readAllStandardError();
252     QString stdMsg = emuHelper->readAllStandardOutput();
253     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
254     if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
255     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
256 }
257
258 void EmuLauncher::cleanTmp()
259 {
260     // TODO
261 }