Emulators can be now configured with multiple media image slots ($1, $2,
[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,
35     QList<EmuFrontObject *> miList, int mediaCount)
36 {
37     if (miList.count() < 1) {
38         throw EmuFrontException(tr("No media images available!"));
39     }
40     if (micList.count() < 1) {
41         throw EmuFrontException(tr("No media image containers available!"));
42     }
43
44     // extract the media image container to tmp folder
45     // (TODO: tmp folder configuration)
46     foreach(MediaImageContainer *mic, micList) {
47         QString fp;
48         fp.append(mic->getFilePath()->getName());
49         if (!fp.endsWith('/')) fp.append("/");
50         fp.append(mic->getName());
51         int ret = unzipHelper->extractAll(fp, "/tmp/");
52         if (ret) {
53             qDebug() << "Failed unzipping " << fp << ".";
54         }
55     }
56
57     // fill in the media image slots in the command line options ($1, $2, ...)
58     QString opts = ex->getOptions();
59     for(int i = 0; i < mediaCount && i < miList.size(); i++) {
60         QString tmpfp = " \"/tmp/";
61         tmpfp.append (miList.at(i)->getName());
62         tmpfp.append("\"");
63         opts.replace(QString("$%1").arg(i+1), tmpfp);
64     }
65
66     QString cmdWithParams;
67     cmdWithParams.append(ex->getExecutable());
68     cmdWithParams.append(" ").append(opts);
69
70     qDebug() << "Command with params " << cmdWithParams;
71     // Executable and MediaImageContainer / MediaImage objects are no more needed:
72     delete ex;
73     qDeleteAll(micList);
74     //qDeleteAll(miList); these objects are already deleted along with micList
75     start(cmdWithParams, QIODevice::ReadOnly);
76 }
77
78 void EmuHelper::processError(QProcess::ProcessError)
79 {
80
81 }
82
83 void EmuHelper::processFinished(int)
84 {
85 }
86
87 void EmuHelper::connectSignals()
88 {
89     connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
90     connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
91 }