Clear the selection after launching emulator.
[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"));
73     launchButton = new QPushButton(tr("&Launch"));
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     setLayout(grid);
86 }
87
88 void EmuLauncher::connectSignals()
89 {
90     connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
91     connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
92     connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
93     connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
94 }
95
96 void EmuLauncher::updateMediaImageContainers()
97 {
98     qDebug() << "updateMediaImageContainers slot";
99     int mtid = mediaTypeSelectBox->getSelected()
100         ? mediaTypeSelectBox->getSelected()->getId()
101         : -1;
102     int plfid = platformSelectBox->getSelected()
103         ? platformSelectBox->getSelected()->getId()
104         : -1;
105
106     if (!dbMic) dbMic = new DbMediaImageContainer(this);
107     dbMic->filter(mtid, plfid);
108     micTable->setModel(dbMic->getDataModel());
109     micTable->resizeColumnsToContents();
110     platformSelectBox->updateDataModel();
111     mediaTypeSelectBox->updateDataModel();
112 }
113
114 void EmuLauncher::launchEmu()
115 {
116     QMap<QString, EmuFrontObject*> mediaImages;
117     QList<MediaImageContainer*> mediaImageContainers;
118     Executable *exe;
119     try {
120         if (!micTable || !micTable->model()) {
121             throw EmuFrontException(tr("No search results available!"));
122         }
123         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
124             throw EmuFrontException(tr("Emulator not selected!"));
125         }
126         QItemSelectionModel *selModel = micTable->selectionModel();
127         QModelIndexList listMIndex =  selModel->selectedIndexes();
128         if (listMIndex.count() < 1) {
129             throw EmuFrontException(tr("Media image container not selected!"));
130         }
131         qDebug() << listMIndex.count() << " items selected.";
132
133         EmuFrontObject *obExe = execSelectBox->getSelected();
134         if (!obExe) {
135             throw EmuFrontException(tr("Failed fetching selected emulator!"));
136         }
137         exe = dynamic_cast<Executable*>(obExe);
138         if (!exe) {
139             throw EmuFrontException(tr("Failed creating Emulator object!"));
140         }
141
142         foreach(QModelIndex mind, listMIndex) {
143             if (!mind.isValid()) continue;
144             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
145             if (!obImg) {
146                 qDebug() << "Failed creating media image container at row " << mind.row();
147                 continue;
148             }
149             MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
150             if (!mic) {
151                 qDebug() << "Failed to create media image container for " << obImg->getName();
152                 delete obImg;
153                 continue;
154             }
155             mediaImageContainers << mic;
156             QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
157             mediaImages.unite(contained);
158         }
159
160         if (mediaImages.count() < 1) {
161             throw EmuFrontException("No media images available!");
162         }
163
164         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
165         QString opts = exe->getOptions();
166         QRegExp rx("(\\s\\$\\d+)");
167         QStringList list;
168         int pos = 0;
169         while ((pos = rx.indexIn(opts, pos)) != -1) {
170             list << rx.cap(1);
171             pos += rx.matchedLength();
172         }
173
174         bool ok;
175         QList<EmuFrontObject*> selectedImages;
176         if (list.count() > mediaImages.count()) {
177             throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
178         }
179         if (list.count() > 1) {
180             int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
181             for(int i = 0; i < lim; i++) {
182                 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
183                         this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
184                 if (!ok)  {
185                     throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
186                 }
187                 selectedImages << efo;
188                 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
189                 QString key = mi->getCheckSum();
190                 mediaImages.remove(key);
191             }
192             if (mediaImages.count() == 1) {
193                 // there should be at least one media image left in mediaImages map
194                 selectedImages << mediaImages.values().first();
195             }
196         }
197         else if (mediaImages.count() > 1) {
198             // show select boot image dialog
199             EmuFrontObject *efo = EmuFrontInputDialog::getItem(
200                 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
201             if (!ok)  {
202                 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
203             }
204             selectedImages << efo;
205         }
206         else if (mediaImages.count() == 1)
207             selectedImages << mediaImages.values().first();
208         // in the both cases the (ordered) list of media images will be passed to emuHelper
209
210         if (selectedImages.count() < 1)
211             throw EmuFrontException(tr("No media images selected"));
212
213         emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
214         micTable->clearSelection();
215     } catch (EmuFrontException efe) {
216         delete exe;
217         qDeleteAll(mediaImageContainers);
218         //qDeleteAll(mediaImages); these are already deleted along with containers
219         QMessageBox::information(this, tr("Launching emulator"),
220                                  efe.what(), QMessageBox::Ok);
221         return;
222     }
223 }
224
225 void EmuLauncher::processError(QProcess::ProcessError e)
226 {
227     cleanTmp();
228     QString stdErr = emuHelper->readAllStandardError();
229     QMessageBox::warning(this, tr("Emulator"),
230         tr("Launching emulator failed with: %1.\n").arg(e)
231         .append(";\n").append(emuHelper->errorString().append(";\n")
232         .append(stdErr)), QMessageBox::Ok );
233 }
234
235 /* Slot for EmuHelper process finished, clears the temporary folder files */
236 void EmuLauncher::processFinished(int a)
237 {
238     cleanTmp();
239     QString stdErr = emuHelper->readAllStandardError();
240     QString stdMsg = emuHelper->readAllStandardOutput();
241     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
242     if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
243     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
244 }
245
246 void EmuLauncher::cleanTmp()
247 {
248     // TODO
249 }