EmuLauncher extracts selected media image container to tmp.
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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 <QSqlTableModel>
22 #include "utils/OSDaB-Zip/unzip.h"
23 #include "emulauncher.h"
24 #include "db/dbmediatype.h"
25 #include "db/dbplatform.h"
26 #include "db/dbexecutable.h"
27 #include "db/dbmediaimagecontainer.h"
28 #include "widgets/effileobjectcombobox.h"
29 #include "widgets/executablecombobox.h"
30 #include "dataobjects/executable.h"
31
32 EmuLauncher::EmuLauncher(QWidget *parent) :
33     QWidget(parent)
34 {
35     dbPlatform = new DbPlatform(this);
36     dbMediaType = new DbMediaType(this);
37     dbExec = new DbExecutable(this);
38     dbMic = 0;
39     initWidgets();
40     layout();
41     connectSignals();
42 }
43
44 void EmuLauncher::updateData()
45 {
46     platformSelectBox->updateDataModel();
47     mediaTypeSelectBox->updateDataModel();
48     execSelectBox->updateDataModel();
49 }
50
51 void EmuLauncher::initWidgets()
52 {
53     micTable = new QTableView(this);
54     micTable->setSelectionMode(QAbstractItemView::SingleSelection);
55     mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
56     platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
57     execSelectBox = new ExecutableComboBox(dbExec, this);
58     selectButton = new QPushButton(tr("&Update"));
59     launchButton = new QPushButton(tr("&Launch"));
60 }
61
62 void EmuLauncher::layout()
63 {
64     QGridLayout *grid = new QGridLayout;
65     grid->addWidget(platformSelectBox, 0, 0);
66     grid->addWidget(mediaTypeSelectBox, 1, 0);
67     grid->addWidget(selectButton, 1, 1);
68     grid->addWidget(micTable, 2, 0, 1, 2);
69     grid->addWidget(execSelectBox, 3, 0);
70     grid->addWidget(launchButton, 3, 1);
71     setLayout(grid);
72 }
73
74 void EmuLauncher::connectSignals()
75 {
76     connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
77     connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
78 }
79
80 void EmuLauncher::updateMediaImageContainers()
81 {
82     qDebug() << "updateMediaImageContainers slot";
83     int mtid = mediaTypeSelectBox->getSelected()
84         ? mediaTypeSelectBox->getSelected()->getId()
85         : -1;
86     int plfid = platformSelectBox->getSelected()
87         ? platformSelectBox->getSelected()->getId()
88         : -1;
89
90     if (!dbMic) dbMic = new DbMediaImageContainer(this);
91     dbMic->filter(mtid, plfid);
92     micTable->setModel(dbMic->getDataModel());
93     micTable->resizeColumnsToContents();
94     platformSelectBox->updateDataModel();
95     mediaTypeSelectBox->updateDataModel();
96 }
97
98 void EmuLauncher::launchEmu()
99 {
100     try {
101         if (!micTable || !micTable->model()) {
102             throw EmuFrontException(tr("No search results available!"));
103         }
104         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
105             throw EmuFrontException(tr("Emulator not selected!"));
106         }
107         // TODO: multiple media image container selection
108         QModelIndex mindex = micTable->currentIndex();
109         if (!mindex.isValid()) {
110             throw EmuFrontException(tr("Media image container not selected!"));
111         }
112         EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mindex);
113         if (!obImg) {
114             throw EmuFrontException(tr("Failed fetching selected media image container."));
115         }
116         MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
117         if (!mic) {
118             throw EmuFrontException(tr("Failed creating media image container object!"));
119         }
120         EmuFrontObject *obExe = execSelectBox->getSelected();
121         if (!obExe) {
122             throw EmuFrontException(tr("Failed fetching selected emulator!"));
123         }
124         Executable *exe = dynamic_cast<Executable*>(obExe);
125         if (!exe) {
126             throw EmuFrontException(tr("Failed creating Emulator object!"));
127         }
128         qDebug() << "Selected media image container "
129                 << mic->getName() << " and emulator "
130                 << obExe->getName() << ".";
131         if (mic->getMediaImages().count() > 0) {
132             // TODO
133             // 1. Launch media image
134             // 2. If 2 or more media images in container
135             //    show a diaglog for choosing the boot image
136             // 3. If 2 or more media image containers selected
137             //    from a list show a dialog listing all the media
138             //    images in those container for choosing the
139             //    boot image
140             // 4. If selected emulator command line containes more
141             //    than one media image placeholder ($1, $2, ...)
142             //    show a dialog for ordering the media images to
143             //    right order.
144             QList<MediaImage*> ls = mic->getMediaImages();
145             foreach(MediaImage *mi, ls) {
146                 qDebug() << "Media image " << mi->getName();
147             }
148             launch(exe, mic);
149         }
150     } catch (EmuFrontException efe) {
151         QMessageBox::information(this, tr("Launching emulator"),
152                                  efe.what(), QMessageBox::Ok);
153         return;
154     }
155 }
156
157 void EmuLauncher::launch(const Executable * ex, const MediaImageContainer * mic) const
158 {
159     // extract the media image container to tmp folder
160     // (TODO: tmp folder configuration)
161     UnZip unz;
162
163     QString fp;
164     fp.append(mic->getFilePath()->getName());
165     if (!fp.endsWith('/')) fp.append("/");
166     fp.append(mic->getName());
167     unz.openArchive(fp);
168     int err = unz.extractAll("/tmp/"); // TODO: this must be set dynamically
169     qDebug() << "extractAll to " << fp << " : " << err;
170     // launch the 1st media image in the media image list of ex
171     // or if emulator command options has a place for more than one
172     // media image assign the media images in the list order
173     // to emulator command line.
174 }