Initial support for multiple selection.
[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
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     initWidgets();
43     layout();
44     connectSignals();
45 }
46
47 EmuLauncher::~EmuLauncher()
48 {
49     if (proc) {
50         proc->kill(); // TODO: do this in a more sophisticated way
51         delete proc;
52     }
53 }
54
55 void EmuLauncher::updateData()
56 {
57     platformSelectBox->updateDataModel();
58     mediaTypeSelectBox->updateDataModel();
59     execSelectBox->updateDataModel();
60 }
61
62 void EmuLauncher::initWidgets()
63 {
64     micTable = new QTableView(this);
65     micTable->setSelectionMode(QAbstractItemView::MultiSelection);
66     mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
67     platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
68     execSelectBox = new ExecutableComboBox(dbExec, this);
69     selectButton = new QPushButton(tr("&Update"));
70     launchButton = new QPushButton(tr("&Launch"));
71 }
72
73 void EmuLauncher::layout()
74 {
75     QGridLayout *grid = new QGridLayout;
76     grid->addWidget(platformSelectBox, 0, 0);
77     grid->addWidget(mediaTypeSelectBox, 1, 0);
78     grid->addWidget(selectButton, 1, 1);
79     grid->addWidget(micTable, 2, 0, 1, 2);
80     grid->addWidget(execSelectBox, 3, 0);
81     grid->addWidget(launchButton, 3, 1);
82     setLayout(grid);
83 }
84
85 void EmuLauncher::connectSignals()
86 {
87     connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
88     connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
89 }
90
91 void EmuLauncher::updateMediaImageContainers()
92 {
93     qDebug() << "updateMediaImageContainers slot";
94     int mtid = mediaTypeSelectBox->getSelected()
95         ? mediaTypeSelectBox->getSelected()->getId()
96         : -1;
97     int plfid = platformSelectBox->getSelected()
98         ? platformSelectBox->getSelected()->getId()
99         : -1;
100
101     if (!dbMic) dbMic = new DbMediaImageContainer(this);
102     dbMic->filter(mtid, plfid);
103     micTable->setModel(dbMic->getDataModel());
104     micTable->resizeColumnsToContents();
105     platformSelectBox->updateDataModel();
106     mediaTypeSelectBox->updateDataModel();
107 }
108
109 void EmuLauncher::launchEmu()
110 {
111     try {
112         if (!micTable || !micTable->model()) {
113             throw EmuFrontException(tr("No search results available!"));
114         }
115         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
116             throw EmuFrontException(tr("Emulator not selected!"));
117         }
118         //QModelIndex mindex = micTable->currentIndex();
119         QItemSelectionModel *selModel = micTable->selectionModel();
120         QModelIndexList listMIndex =  selModel->selectedIndexes();
121         /*if (!mindex.isValid()) {
122             throw EmuFrontException(tr("Media image container not selected!"));
123         }*/
124         if (listMIndex.count() < 1) {
125             throw EmuFrontException(tr("Media image container not selected!"));
126         }
127         qDebug() << listMIndex.count() << " items selected.";
128         // TODO: multiple media image container selection
129         QModelIndex mindex = listMIndex.first();
130         EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
131         if (!obImg) {
132             throw EmuFrontException(tr("Failed fetching selected media image container."));
133         }
134         MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
135         if (!mic) {
136             throw EmuFrontException(tr("Failed creating media image container object!"));
137         }
138         EmuFrontObject *obExe = execSelectBox->getSelected();
139         if (!obExe) {
140             throw EmuFrontException(tr("Failed fetching selected emulator!"));
141         }
142         Executable *exe = dynamic_cast<Executable*>(obExe);
143         if (!exe) {
144             throw EmuFrontException(tr("Failed creating Emulator object!"));
145         }
146         qDebug() << "Selected media image container "
147                 << mic->getName() << " and emulator "
148                 << obExe->getName() << ".";
149         if (mic->getMediaImages().count() > 0) {
150             // 1. Launch media image
151             // TODO
152             // 2. If 2 or more media images in container
153             //    show a diaglog for choosing the boot image
154             // 3. If 2 or more media image containers selected
155             //    from a list show a dialog listing all the media
156             //    images in those container for choosing the
157             //    boot image
158             // 4. If selected emulator command line containes more
159             //    than one media image placeholder ($1, $2, ...)
160             //    show a dialog for ordering the media images to
161             //    right order.
162             QList<MediaImage*> ls = mic->getMediaImages();
163             foreach(MediaImage *mi, ls) {
164                 qDebug() << "Media image " << mi->getName();
165             }
166             launch(exe, mic);
167         }
168     } catch (EmuFrontException efe) {
169         QMessageBox::information(this, tr("Launching emulator"),
170                                  efe.what(), QMessageBox::Ok);
171         return;
172     }
173 }
174
175 void EmuLauncher::launch(const Executable * ex, const MediaImageContainer * mic)
176 {
177     // extract the media image container to tmp folder
178     // (TODO: tmp folder configuration)
179     UnZip unz;
180
181     QString fp;
182     fp.append(mic->getFilePath()->getName());
183     if (!fp.endsWith('/')) fp.append("/");
184     fp.append(mic->getName());
185     unz.openArchive(fp);
186     int err = unz.extractAll("/tmp/"); // TODO: this must be set dynamically
187     qDebug() << "extractAll to " << fp << " : " << err;
188     // TODO: launch the 1st media image in the media image list of ex
189     // or if emulator command options has a place for more than one
190     // media image assign the media images in the list order
191     // to emulator command line.
192     QString opts = ex->getOptions();
193     QString tmpfp = " \"/tmp/";
194     tmpfp.append(mic->getMediaImages().first()->getName()).append("\"");
195     opts.replace("$1", tmpfp);
196     QString cmdWithParams;
197     cmdWithParams.append(ex->getExecutable());
198     cmdWithParams.append(" ").append(opts);
199     // TODO: tmp will be set dynamically
200     // TODO: assigning multiple media images
201     qDebug() << "Command with params " << cmdWithParams;
202     // Executable and MediaImageContainer objects are no more needed:
203     delete ex;
204     delete mic;
205     if (!proc) {
206         proc = new QProcess(this); // This has to be done in the heap
207         connect(proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
208         connect(proc, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
209     }
210     proc->start(cmdWithParams, QIODevice::ReadOnly);
211 }
212
213 void EmuLauncher::processError(QProcess::ProcessError e)
214 {
215     QString stdErr = proc->readAllStandardError();
216     QMessageBox::warning(this, tr("Emulator"),
217         tr("Launching emulator failed with: %1.\n").arg(e)
218         .append(";\n").append(proc->errorString().append(";\n")
219         .append(stdErr)), QMessageBox::Ok );
220 }
221
222 void EmuLauncher::processFinished(int a)
223 {
224     QString stdErr = proc->readAllStandardError();
225     QString stdMsg = proc->readAllStandardOutput();
226     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
227     if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
228     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
229 }