8beec34e0b0c8e03795d8247ea7d3068e07b263f
[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 <QFile>
22 #include <QMessageBox>
23 #include "emuhelper.h"
24 #include "unziphelper.h"
25 #include "../dataobjects/mediaimagecontainer.h"
26 #include "../dataobjects/executable.h"
27 #include "../exceptions/emufrontexception.h"
28
29 EmuHelper::EmuHelper(QObject *parent) :
30     ProcessHelper(parent)
31 {
32     unzipHelper = new UnzipHelper(this);
33 }
34
35 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList,
36     QList<EmuFrontObject *> miList, int mediaCount)
37 {
38     if (miList.count() < 1) {
39         throw EmuFrontException(tr("No media images available!"));
40     }
41     if (micList.count() < 1) {
42         throw EmuFrontException(tr("No media image containers available!"));
43     }
44
45     QString tmp = "/tmp/"; // TODO: do this configurable!
46
47     // extract the media image container to tmp folder
48     // (TODO: tmp folder configuration)
49     foreach(MediaImageContainer *mic, micList) {
50         QString fp;
51         fp.append(mic->getFilePath()->getName());
52         if (!fp.endsWith('/')) fp.append("/");
53         fp.append(mic->getName());
54         int ret = unzipHelper->extractAll(fp, tmp);
55         if (ret) {
56             qDebug() << "Failed unzipping " << fp << ".";
57         }
58     }
59
60     // fill in the media image slots in the command line options ($1, $2, ...)
61     QString opts = ex->getOptions();
62     for(int i = 0; i < mediaCount && i < miList.size(); i++) {
63         QString tmpfp = " \"/tmp/";
64         tmpfp.append (miList.at(i)->getName());
65         tmpfp.append("\"");
66         opts.replace(QString("$%1").arg(i+1), tmpfp);
67     }
68
69     QString cmdWithParams;
70     cmdWithParams.append(ex->getExecutable());
71     cmdWithParams.append(" ").append(opts);
72
73     qDebug() << "Command with params " << cmdWithParams;
74     start(cmdWithParams, QIODevice::ReadOnly);
75
76     // for the moment, we'll wait for the process to be finished until we continue
77     waitForFinished(-1);
78
79     // clean the temp dir
80     foreach(EmuFrontObject *ob, miList) {
81         QString fp = tmp;
82         fp.append(ob->getName());
83         QFile f(fp);
84         if (!f.remove())
85             qDebug() << "Removing " << fp << " failed.";
86     }
87     delete ex;
88     qDeleteAll(micList);
89     //qDeleteAll(miList); these objects are already deleted along with micList
90
91 }
92
93 void EmuHelper::processError(QProcess::ProcessError)
94 {
95
96 }
97
98 void EmuHelper::processFinished(int)
99 {
100 }
101
102 void EmuHelper::connectSignals()
103 {
104     connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
105     connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
106 }