ProcessHelper derives from QProcess. New implementation: 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 "utils/OSDaB-Zip/unzip.h"
25 #include "emulauncher.h"
26 #include "db/dbmediatype.h"
27 #include "db/dbplatform.h"
28 #include "db/dbexecutable.h"
29 #include "db/dbmediaimagecontainer.h"
30 #include "widgets/effileobjectcombobox.h"
31 #include "widgets/executablecombobox.h"
32 #include "dataobjects/executable.h"
33 #include "utils/emuhelper.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     try {
116         if (!micTable || !micTable->model()) {
117             throw EmuFrontException(tr("No search results available!"));
118         }
119         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
120             throw EmuFrontException(tr("Emulator not selected!"));
121         }
122         //QModelIndex mindex = micTable->currentIndex();
123         QItemSelectionModel *selModel = micTable->selectionModel();
124         QModelIndexList listMIndex =  selModel->selectedIndexes();
125         /*if (!mindex.isValid()) {
126             throw EmuFrontException(tr("Media image container not selected!"));
127         }*/
128         if (listMIndex.count() < 1) {
129             throw EmuFrontException(tr("Media image container not selected!"));
130         }
131         qDebug() << listMIndex.count() << " items selected.";
132
133         // TODO: multiple media image container selection
134         //          - build a list of selected media image objects
135         //          - check that the platform and media type (setup) matches
136         QModelIndex mindex = listMIndex.first();
137         EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
138         if (!obImg) {
139             throw EmuFrontException(tr("Failed fetching selected media image container."));
140         }
141         MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
142         if (!mic) {
143             throw EmuFrontException(tr("Failed creating media image container object!"));
144         }
145         EmuFrontObject *obExe = execSelectBox->getSelected();
146         if (!obExe) {
147             throw EmuFrontException(tr("Failed fetching selected emulator!"));
148         }
149         Executable *exe = dynamic_cast<Executable*>(obExe);
150         if (!exe) {
151             throw EmuFrontException(tr("Failed creating Emulator object!"));
152         }
153         qDebug() << "Selected media image container "
154                 << mic->getName() << " and emulator "
155                 << obExe->getName() << ".";
156         if (mic->getMediaImages().count() > 0) {
157             // 1. Launch media image
158             // TODO
159             // 2. If 2 or more media images in container
160             //    show a diaglog for choosing the boot image
161             // 3. If 2 or more media image containers selected
162             //    from a list show a dialog listing all the media
163             //    images in those container for choosing the
164             //    boot image
165             // 4. If selected emulator command line containes more
166             //    than one media image placeholder ($1, $2, ...)
167             //    show a dialog for ordering the media images to
168             //    right order.
169             QList<MediaImage*> ls = mic->getMediaImages();
170             foreach(MediaImage *mi, ls) {
171                 qDebug() << "Media image " << mi->getName();
172             }
173             emuHelper->launch(exe, mic);
174         }
175     } catch (EmuFrontException efe) {
176         QMessageBox::information(this, tr("Launching emulator"),
177                                  efe.what(), QMessageBox::Ok);
178         return;
179     }
180 }
181
182 void EmuLauncher::processError(QProcess::ProcessError e)
183 {
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 void EmuLauncher::processFinished(int a)
192 {
193     QString stdErr = emuHelper->readAllStandardError();
194     QString stdMsg = emuHelper->readAllStandardOutput();
195     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
196     if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
197     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
198 }