New dialog for selecting from multiple emu front objects derived from
[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     QList<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         //QModelIndex mindex = micTable->currentIndex();
126         QItemSelectionModel *selModel = micTable->selectionModel();
127         QModelIndexList listMIndex =  selModel->selectedIndexes();
128         /*if (!mindex.isValid()) {
129             throw EmuFrontException(tr("Media image container not selected!"));
130         }*/
131         if (listMIndex.count() < 1) {
132             throw EmuFrontException(tr("Media image container not selected!"));
133         }
134         qDebug() << listMIndex.count() << " items selected.";
135
136         EmuFrontObject *obExe = execSelectBox->getSelected();
137         if (!obExe) {
138             throw EmuFrontException(tr("Failed fetching selected emulator!"));
139         }
140         exe = dynamic_cast<Executable*>(obExe);
141         if (!exe) {
142             throw EmuFrontException(tr("Failed creating Emulator object!"));
143         }
144
145         // TODO: multiple media image container selection
146         //          - build a list of selected media image objects
147         //          - check that the platform and media type (setup) matches
148         foreach(QModelIndex mind, listMIndex) {
149             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
150             if (!obImg) {
151                 qDebug() << "Failed creating media image container at row " << mind.row();
152                 continue;
153             }
154             MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
155             if (!mic) {
156                 qDebug() << "Failed to create media image container for " << obImg->getName();
157                 delete obImg;
158                 continue;
159             }
160             mediaImageContainers << mic;
161             QList<MediaImage*> contained = mic->getMediaImages();
162             foreach(MediaImage *mi, contained)
163                 mediaImages << mi;
164         }
165
166         if (mediaImages.count() < 1) {
167             throw new EmuFrontException("No media images available!");
168         }
169
170         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
171         QString opts = exe->getOptions();
172         QRegExp rx("(\\s\\$\\d+\\s)");
173         QStringList list;
174         int pos = 0;
175         while ((pos = rx.indexIn(opts, pos)) != -1) {
176             list << rx.cap(1);
177             pos += rx.matchedLength();
178         }
179
180         QList<MediaImage*> selectedImages;
181         if (list.count() > 1) {
182             for(int i = 0; i < list.count(); i++) {
183                 //QInputDialog::getItem();
184                 // TODO: Use input dialog here
185                 // Create a new input dialog class for emufrontobjects
186             }
187             // TODO: show dialog to set the media order (the images will be assigned to slots in the order in respect to the image list)
188         }
189         else if (mediaImages.count() > 1) {
190             // show select boot image dialog
191             bool ok;
192             EmuFrontObject *efo = EmuFrontInputDialog::getItem(
193                 this, tr("Select boot image"), tr("Select"), mediaImages, 0, false, &ok);
194             if (!ok)  {
195                 throw new EmuFrontException(tr("Boot image selection was canceled, aborting."));
196             }
197         }
198         // in the both cases the (ordered) list of media images will be passed to emuHelper
199
200         foreach(EmuFrontObject *mi, mediaImages) {
201             qDebug() << "Media image " << mi->getName();
202         }
203         emuHelper->launch(exe, mediaImageContainers, mediaImages);
204     } catch (EmuFrontException efe) {
205         delete exe;
206         qDeleteAll(mediaImageContainers);
207         qDeleteAll(mediaImages);
208         QMessageBox::information(this, tr("Launching emulator"),
209                                  efe.what(), QMessageBox::Ok);
210         return;
211     }
212 }
213
214 void EmuLauncher::processError(QProcess::ProcessError e)
215 {
216     cleanTmp();
217     QString stdErr = emuHelper->readAllStandardError();
218     QMessageBox::warning(this, tr("Emulator"),
219         tr("Launching emulator failed with: %1.\n").arg(e)
220         .append(";\n").append(proc->errorString().append(";\n")
221         .append(stdErr)), QMessageBox::Ok );
222 }
223
224 /* Slot for EmuHelper process finished, clears the temporary folder files */
225 void EmuLauncher::processFinished(int a)
226 {
227     cleanTmp();
228     QString stdErr = emuHelper->readAllStandardError();
229     QString stdMsg = emuHelper->readAllStandardOutput();
230     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
231     if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
232     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
233 }
234
235 void EmuLauncher::cleanTmp()
236 {
237     // TODO
238 }