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