Cleaned up.
[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         // TODO: multiple media image container selection
133         //          - build a list of selected media image objects
134         //          - check that the platform and media type (setup) matches
135         QModelIndex mindex = listMIndex.first();
136         EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
137         if (!obImg) {
138             throw EmuFrontException(tr("Failed fetching selected media image container."));
139         }
140         MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
141         if (!mic) {
142             throw EmuFrontException(tr("Failed creating media image container object!"));
143         }
144         EmuFrontObject *obExe = execSelectBox->getSelected();
145         if (!obExe) {
146             throw EmuFrontException(tr("Failed fetching selected emulator!"));
147         }
148         Executable *exe = dynamic_cast<Executable*>(obExe);
149         if (!exe) {
150             throw EmuFrontException(tr("Failed creating Emulator object!"));
151         }
152         qDebug() << "Selected media image container "
153                 << mic->getName() << " and emulator "
154                 << obExe->getName() << ".";
155         if (mic->getMediaImages().count() > 0) {
156             // 1. Launch media image
157             // TODO
158             // 2. If 2 or more media images in container
159             //    show a diaglog for choosing the boot image
160             // 3. If 2 or more media image containers selected
161             //    from a list show a dialog listing all the media
162             //    images in those container for choosing the
163             //    boot image
164             // 4. If selected emulator command line containes more
165             //    than one media image placeholder ($1, $2, ...)
166             //    show a dialog for ordering the media images to
167             //    right order.
168             QList<MediaImage*> ls = mic->getMediaImages();
169             foreach(MediaImage *mi, ls) {
170                 qDebug() << "Media image " << mi->getName();
171             }
172             emuHelper->launch(exe, mic);
173         }
174     } catch (EmuFrontException efe) {
175         QMessageBox::information(this, tr("Launching emulator"),
176                                  efe.what(), QMessageBox::Ok);
177         return;
178     }
179 }
180
181 void EmuLauncher::processError(QProcess::ProcessError e)
182 {
183     cleanTmp();
184     QString stdErr = emuHelper->readAllStandardError();
185     QMessageBox::warning(this, tr("Emulator"),
186         tr("Launching emulator failed with: %1.\n").arg(e)
187         .append(";\n").append(proc->errorString().append(";\n")
188         .append(stdErr)), QMessageBox::Ok );
189 }
190
191 /* Slot for EmuHelper process finished, clears the temporary folder files */
192 void EmuLauncher::processFinished(int a)
193 {
194     cleanTmp();
195     QString stdErr = emuHelper->readAllStandardError();
196     QString stdMsg = emuHelper->readAllStandardOutput();
197     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
198     if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
199     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
200 }
201
202 void EmuLauncher::cleanTmp()
203 {
204     // TODO
205 }