Added configuration of temporary directory.
[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, QString tmp)
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     if (!tmp.endsWith('/')) tmp.append("/");
46
47     // extract the media image container to tmp folder
48     foreach(MediaImageContainer *mic, micList) {
49         QString fp;
50         fp.append(mic->getFilePath()->getName());
51         if (!fp.endsWith('/')) fp.append("/");
52         fp.append(mic->getName());
53         int ret = unzipHelper->extractAll(fp, tmp);
54         if (ret) {
55             qDebug() << "Failed unzipping " << fp << ".";
56         }
57     }
58
59     // fill in the media image slots in the command line options ($1, $2, ...)
60     QString opts = ex->getOptions();
61     for(int i = 0; i < mediaCount && i < miList.size(); i++) {
62         QString tmpfp = " \"";
63         tmpfp.append(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 = " \"";
82          fp.append(tmp);
83         fp.append(ob->getName());
84         fp.append("\"");
85         if (!QFile::remove(fp))
86             qDebug() << "Removing " << fp << " failed.";
87     }
88     delete ex;
89     qDeleteAll(micList);
90     //qDeleteAll(miList); these objects are already deleted along with micList
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 }