Expanded support for multiple media images.
[emufront] / src / utils / emuhelper.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 <QDebug>
21 #include <QMessageBox>
22 #include "emuhelper.h"
23 #include "unziphelper.h"
24 #include "../dataobjects/mediaimagecontainer.h"
25 #include "../dataobjects/executable.h"
26 #include "../exceptions/emufrontexception.h"
27
28 EmuHelper::EmuHelper(QObject *parent) :
29     ProcessHelper(parent)
30 {
31     unzipHelper = new UnzipHelper(this);
32 }
33
34 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList, QList<MediaImage *> miList)
35 {
36     // extract the media image container to tmp folder
37     // (TODO: tmp folder configuration)
38
39     // TODO: support multiple media images
40     MediaImageContainer *mic = micList.first();
41
42     QString fp;
43     fp.append(mic->getFilePath()->getName());
44     if (!fp.endsWith('/')) fp.append("/");
45     fp.append(mic->getName());
46
47     int ret = unzipHelper->extractAll(fp, "/tmp/");
48
49     if (ret) {
50         throw EmuFrontException(tr("Unzip failed with %1.").arg(ret));
51     }
52
53     // TODO: launch the 1st media image in the media image list of ex
54     // or if emulator command options has a place for more than one
55     // media image assign the media images in the list order
56     // to emulator command line.
57
58     QString opts = ex->getOptions();
59     QString tmpfp = " \"/tmp/";
60     qDebug() << "Launching file '" << mic->getMediaImages().first()->getName() << " '";
61     tmpfp.append(mic->getMediaImages().first()->getName()).append("\"");
62     opts.replace("$1", tmpfp);
63     QString cmdWithParams;
64     cmdWithParams.append(ex->getExecutable());
65     cmdWithParams.append(" ").append(opts);
66     // TODO: tmp will be set dynamically
67     // TODO: assigning multiple media images
68     qDebug() << "Command with params " << cmdWithParams;
69     // Executable and MediaImageContainer objects are no more needed:
70     delete ex;
71     delete mic;
72     start(cmdWithParams, QIODevice::ReadOnly);
73 }
74
75 void EmuHelper::processError(QProcess::ProcessError)
76 {
77
78 }
79
80 void EmuHelper::processFinished(int)
81 {
82 }
83
84 void EmuHelper::connectSignals()
85 {
86     connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
87     connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
88 }