Fixed selection of multiple images for multiple image slots in 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     proc = 0;
43     emuHelper = new EmuHelper(this);
44     initWidgets();
45     layout();
46     connectSignals();
47 }
48
49 EmuLauncher::~EmuLauncher()
50 {
51     if (proc) {
52         proc->kill(); // TODO: do this in a more sophisticated way
53         delete proc;
54     }
55 }
56
57 void EmuLauncher::updateData()
58 {
59     platformSelectBox->updateDataModel();
60     mediaTypeSelectBox->updateDataModel();
61     execSelectBox->updateDataModel();
62 }
63
64 void EmuLauncher::initWidgets()
65 {
66     micTable = new QTableView(this);
67     micTable->setSelectionMode(QAbstractItemView::MultiSelection);
68     mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
69     platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
70     execSelectBox = new ExecutableComboBox(dbExec, this);
71     selectButton = new QPushButton(tr("&Update"));
72     launchButton = new QPushButton(tr("&Launch"));
73 }
74
75 void EmuLauncher::layout()
76 {
77     QGridLayout *grid = new QGridLayout;
78     grid->addWidget(platformSelectBox, 0, 0);
79     grid->addWidget(mediaTypeSelectBox, 1, 0);
80     grid->addWidget(selectButton, 1, 1);
81     grid->addWidget(micTable, 2, 0, 1, 2);
82     grid->addWidget(execSelectBox, 3, 0);
83     grid->addWidget(launchButton, 3, 1);
84     setLayout(grid);
85 }
86
87 void EmuLauncher::connectSignals()
88 {
89     connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
90     connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
91     connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
92     connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
93 }
94
95 void EmuLauncher::updateMediaImageContainers()
96 {
97     qDebug() << "updateMediaImageContainers slot";
98     int mtid = mediaTypeSelectBox->getSelected()
99         ? mediaTypeSelectBox->getSelected()->getId()
100         : -1;
101     int plfid = platformSelectBox->getSelected()
102         ? platformSelectBox->getSelected()->getId()
103         : -1;
104
105     if (!dbMic) dbMic = new DbMediaImageContainer(this);
106     dbMic->filter(mtid, plfid);
107     micTable->setModel(dbMic->getDataModel());
108     micTable->resizeColumnsToContents();
109     platformSelectBox->updateDataModel();
110     mediaTypeSelectBox->updateDataModel();
111 }
112
113 void EmuLauncher::launchEmu()
114 {
115     QMap<QString, EmuFrontObject*> mediaImages;
116     QList<MediaImageContainer*> mediaImageContainers;
117     Executable *exe;
118     try {
119         if (!micTable || !micTable->model()) {
120             throw EmuFrontException(tr("No search results available!"));
121         }
122         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
123             throw EmuFrontException(tr("Emulator not selected!"));
124         }
125         QItemSelectionModel *selModel = micTable->selectionModel();
126         QModelIndexList listMIndex =  selModel->selectedIndexes();
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         exe = dynamic_cast<Executable*>(obExe);
137         if (!exe) {
138             throw EmuFrontException(tr("Failed creating Emulator object!"));
139         }
140
141         foreach(QModelIndex mind, listMIndex) {
142             if (!mind.isValid()) continue;
143             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
144             if (!obImg) {
145                 qDebug() << "Failed creating media image container at row " << mind.row();
146                 continue;
147             }
148             MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
149             if (!mic) {
150                 qDebug() << "Failed to create media image container for " << obImg->getName();
151                 delete obImg;
152                 continue;
153             }
154             mediaImageContainers << mic;
155             QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
156             mediaImages.unite(contained);
157         }
158
159         if (mediaImages.count() < 1) {
160             throw EmuFrontException("No media images available!");
161         }
162
163         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
164         QString opts = exe->getOptions();
165         QRegExp rx("(\\s\\$\\d+)");
166         QStringList list;
167         int pos = 0;
168         while ((pos = rx.indexIn(opts, pos)) != -1) {
169             list << rx.cap(1);
170             pos += rx.matchedLength();
171         }
172
173         bool ok;
174         QList<EmuFrontObject*> selectedImages;
175         if (list.count() > mediaImages.count()) {
176             throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
177         }
178         if (list.count() > 1) {
179             int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
180             for(int i = 0; i < lim; i++) {
181                 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
182                         this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
183                 if (!ok)  {
184                     throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
185                 }
186                 selectedImages << efo;
187                 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
188                 mediaImages.remove(mi->getCheckSum());
189             }
190             if (list.count() == mediaImages.count() && mediaImages.count() == 1) {
191                 // there should be only 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);
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(proc->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(proc->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 }