Expanded support for multiple media images.
[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
34 EmuLauncher::EmuLauncher(QWidget *parent) :
35     QWidget(parent)
36 {
37     dbPlatform = new DbPlatform(this);
38     dbMediaType = new DbMediaType(this);
39     dbExec = new DbExecutable(this);
40     dbMic = 0;
41     proc = 0;
42     emuHelper = new EmuHelper(this);
43     initWidgets();
44     layout();
45     connectSignals();
46 }
47
48 EmuLauncher::~EmuLauncher()
49 {
50     if (proc) {
51         proc->kill(); // TODO: do this in a more sophisticated way
52         delete proc;
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     try {
115         if (!micTable || !micTable->model()) {
116             throw EmuFrontException(tr("No search results available!"));
117         }
118         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
119             throw EmuFrontException(tr("Emulator not selected!"));
120         }
121         //QModelIndex mindex = micTable->currentIndex();
122         QItemSelectionModel *selModel = micTable->selectionModel();
123         QModelIndexList listMIndex =  selModel->selectedIndexes();
124         /*if (!mindex.isValid()) {
125             throw EmuFrontException(tr("Media image container not selected!"));
126         }*/
127         if (listMIndex.count() < 1) {
128             throw EmuFrontException(tr("Media image container not selected!"));
129         }
130         qDebug() << listMIndex.count() << " items selected.";
131
132         EmuFrontObject *obExe = execSelectBox->getSelected();
133         if (!obExe) {
134             throw EmuFrontException(tr("Failed fetching selected emulator!"));
135         }
136         Executable *exe = dynamic_cast<Executable*>(obExe);
137         if (!exe) {
138             throw EmuFrontException(tr("Failed creating Emulator object!"));
139         }
140
141         // TODO: multiple media image container selection
142         //          - build a list of selected media image objects
143         //          - check that the platform and media type (setup) matches
144         QList<MediaImage*> mediaImages;
145         QList<MediaImageContainer*> mediaImageContainers;
146         foreach(QModelIndex mind, listMIndex) {
147             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
148             if (!obImg) {
149                 qDebug() << "Failed creating media image container at row " << mind.row();
150                 continue;
151             }
152             MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
153             if (!mic) {
154                 qDebug() << "Failed to create media image container for " << obImg->getName();
155                 delete obImg;
156                 continue;
157             }
158             mediaImageContainers << mic;
159             QList<MediaImage*> contained = mic->getMediaImages();
160             mediaImages << contained;
161         }
162
163         if (mediaImages.count() < 1) {
164             delete exe;
165             throw new EmuFrontException("No media images available!");
166         }
167
168         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
169         QString opts = exe->getOptions();
170         QRegExp rx("(\\s\\$\\d+\\s)");
171         QStringList list;
172         int pos = 0;
173         while ((pos = rx.indexIn(opts, pos)) != -1) {
174             list << rx.cap(1);
175             pos += rx.matchedLength();
176         }
177
178         if (list.count() > 1) {
179             // TODO: show dialog to set the media order (the images will be assigned to slots in the order in respect to the image list)
180         }
181         else {
182             // TODO: show select boot image dialog
183         }
184         // in the both cases the (ordered) list of media images will be passed to emuHelper
185
186         foreach(MediaImage *mi, mediaImages) {
187             qDebug() << "Media image " << mi->getName();
188         }
189         emuHelper->launch(exe, mediaImageContainers, mediaImages);
190     } catch (EmuFrontException efe) {
191         QMessageBox::information(this, tr("Launching emulator"),
192                                  efe.what(), QMessageBox::Ok);
193         return;
194     }
195 }
196
197 void EmuLauncher::processError(QProcess::ProcessError e)
198 {
199     cleanTmp();
200     QString stdErr = emuHelper->readAllStandardError();
201     QMessageBox::warning(this, tr("Emulator"),
202         tr("Launching emulator failed with: %1.\n").arg(e)
203         .append(";\n").append(proc->errorString().append(";\n")
204         .append(stdErr)), QMessageBox::Ok );
205 }
206
207 /* Slot for EmuHelper process finished, clears the temporary folder files */
208 void EmuLauncher::processFinished(int a)
209 {
210     cleanTmp();
211     QString stdErr = emuHelper->readAllStandardError();
212     QString stdMsg = emuHelper->readAllStandardOutput();
213     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
214     if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
215     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
216 }
217
218 void EmuLauncher::cleanTmp()
219 {
220     // TODO
221 }