cdb9083ed382bad8b70b77a45334acc4c6d11d1f
[emufront] / src / utils / emuhelper.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QDebug>
22 #include <QFile>
23 #include <QDir>
24 #include <QMessageBox>
25 #include "emuhelper.h"
26 #include "unziphelper.h"
27 #include "mediaimagecontainer.h"
28 #include "executable.h"
29 #include "emufrontexception.h"
30
31 EmuHelper::EmuHelper(QObject *parent) :
32     ProcessHelper(parent)
33 {
34     unzipHelper = new UnzipHelper(this);
35 }
36
37
38 // TODO: These two launch functions may be merged to one and/or split into some common helper functions.
39 /* Throws EmuFrontException */
40 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList)
41 {
42     if (micList.count() < 1) {
43         throw EmuFrontException(tr("No media image containers available!"));
44     }
45
46     MediaImageContainer *mic = micList.first();
47     QString fp = " \"";
48     fp.append(mic->getFilePath()->getName());
49     if (!fp.endsWith('/'))
50         fp.append("/");
51     fp.append(mic->getName());
52     fp.append("\"");
53
54     QString opts = ex->getOptions();
55     opts.replace(QString("$1"), fp);
56
57     QString cmdWithParams;
58     cmdWithParams.append(ex->getExecutable());
59     cmdWithParams.append(" ").append(opts);
60
61     qDebug() << "Command with params " << cmdWithParams;
62     start(cmdWithParams, QIODevice::ReadOnly);
63
64     // for the moment, we'll wait for the process to be finished until we continue
65     waitForFinished(-1);
66
67     // these will be deleted in calling function:
68     //delete ex;
69     //qDeleteAll(micList);
70 }
71
72 /* Throws EmuFrontException */
73 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList,
74     QList<EmuFrontObject *> miList, int mediaCount, QString tmp)
75 {
76     if (miList.count() < 1) {
77         throw EmuFrontException(tr("No media images available!"));
78     }
79     if (micList.count() < 1) {
80         throw EmuFrontException(tr("No media image containers available!"));
81     }
82
83     if (!tmp.endsWith('/')) tmp.append("/");
84
85     // extract the media image container to tmp folder
86     foreach(MediaImageContainer *mic, micList) {
87         QString fp;
88         fp.append(mic->getFilePath()->getName());
89         if (!fp.endsWith('/')) fp.append("/");
90         fp.append(mic->getName());
91         qDebug() << "Extracting " << fp;
92         int ret = unzipHelper->extractAll(fp, tmp); // throws EmuFrontException
93         if (ret) {
94             qDebug() << "Failed unzipping " << fp << ".";
95         }
96     }
97
98     // fill in the media image slots in the command line options ($1, $2, ...)
99     QString opts = ex->getOptions();
100     for(int i = 0; i < mediaCount && i < miList.size(); i++) {
101         QString tmpfp = " \"";
102         tmpfp.append(tmp);
103         tmpfp.append (miList.at(i)->getName());
104         tmpfp.append("\"");
105         opts.replace(QString("$%1").arg(i+1), tmpfp);
106     }
107
108     QString cmdWithParams;
109     cmdWithParams.append(ex->getExecutable());
110     cmdWithParams.append(" ").append(opts);
111
112     qDebug() << "Command with params " << cmdWithParams;
113     start(cmdWithParams, QIODevice::ReadOnly);
114
115     // for the moment, we'll wait for the process to be finished until we continue
116     waitForFinished(-1);
117
118     try {
119         QDir ftmp(tmp);
120         if (!ftmp.exists()) {
121             throw EmuFrontException(tr("Trying to remove temporary files. "
122                                        "Directory %s doesn't exist!").arg(tmp));
123         }
124         // clean the temp dir
125         // TODO: if selected archive with multiple items, the files are not removed!
126         foreach(EmuFrontObject *ob, miList) {
127             if (!ftmp.exists(ob->getName())) {
128                 qDebug() << "File " << ob->getName() << " doesn't exist in " << tmp;
129                 continue;
130             }
131             QString fp = ftmp.filePath(ob->getName());
132             QFile f(fp);
133             if (!f.exists()) {
134                 qDebug() << "File " << fp << " doesn't exist!";
135             }
136             if (!f.remove()) {
137                 qDebug() << "Removing " << fp << " failed.";
138             }
139         }
140     } catch (EmuFrontException e) {
141         qDebug() << e.what();
142         throw e;
143     }
144     // these will be deleted in calling function:
145     //delete ex;
146     //qDeleteAll(micList);delete ex;
147     //qDeleteAll(miList); these objects are already deleted along with micList
148 }
149
150 void EmuHelper::processError(QProcess::ProcessError)
151 {
152
153 }
154
155 void EmuHelper::processFinished(int)
156 {
157 }
158
159 void EmuHelper::connectSignals()
160 {
161     connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
162     connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
163 }