a816accd2e1beebb37ea9d197aa064c9fb0e07ff
[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 <QDir>
23 #include <QMessageBox>
24 #include "emuhelper.h"
25 #include "unziphelper.h"
26 #include "../dataobjects/mediaimagecontainer.h"
27 #include "../dataobjects/executable.h"
28 #include "../exceptions/emufrontexception.h"
29
30 EmuHelper::EmuHelper(QObject *parent) :
31     ProcessHelper(parent)
32 {
33     unzipHelper = new UnzipHelper(this);
34 }
35
36
37 // TODO: These two launch functions may be merged to one and/or split into some common helper functions.
38 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList)
39 {
40     if (micList.count() < 1) {
41         throw EmuFrontException(tr("No media image containers available!"));
42     }
43
44     MediaImageContainer *mic = micList.first();
45     QString fp = " \"";
46     fp.append(mic->getFilePath()->getName());
47     if (!fp.endsWith('/'))
48         fp.append("/");
49     fp.append(mic->getName());
50     fp.append("\"");
51
52     QString opts = ex->getOptions();
53     opts.replace(QString("$1"), fp);
54
55     QString cmdWithParams;
56     cmdWithParams.append(ex->getExecutable());
57     cmdWithParams.append(" ").append(opts);
58
59     qDebug() << "Command with params " << cmdWithParams;
60     start(cmdWithParams, QIODevice::ReadOnly);
61
62     // for the moment, we'll wait for the process to be finished until we continue
63     waitForFinished(-1);
64
65     delete ex;
66     qDeleteAll(micList);
67 }
68
69 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList,
70     QList<EmuFrontObject *> miList, int mediaCount, QString tmp)
71 {
72     if (miList.count() < 1) {
73         throw EmuFrontException(tr("No media images available!"));
74     }
75     if (micList.count() < 1) {
76         throw EmuFrontException(tr("No media image containers available!"));
77     }
78
79     if (!tmp.endsWith('/')) tmp.append("/");
80
81     // extract the media image container to tmp folder
82     foreach(MediaImageContainer *mic, micList) {
83         QString fp;
84         fp.append(mic->getFilePath()->getName());
85         if (!fp.endsWith('/')) fp.append("/");
86         fp.append(mic->getName());
87         qDebug() << "Extracting " << fp;
88         int ret = unzipHelper->extractAll(fp, tmp);
89         if (ret) {
90             qDebug() << "Failed unzipping " << fp << ".";
91         }
92     }
93
94     // fill in the media image slots in the command line options ($1, $2, ...)
95     QString opts = ex->getOptions();
96     for(int i = 0; i < mediaCount && i < miList.size(); i++) {
97         QString tmpfp = " \"";
98         tmpfp.append(tmp);
99         tmpfp.append (miList.at(i)->getName());
100         tmpfp.append("\"");
101         opts.replace(QString("$%1").arg(i+1), tmpfp);
102     }
103
104     QString cmdWithParams;
105     cmdWithParams.append(ex->getExecutable());
106     cmdWithParams.append(" ").append(opts);
107
108     qDebug() << "Command with params " << cmdWithParams;
109     start(cmdWithParams, QIODevice::ReadOnly);
110
111     // for the moment, we'll wait for the process to be finished until we continue
112     waitForFinished(-1);
113
114     try {
115         QDir ftmp(tmp);
116         if (!ftmp.exists()) {
117             throw EmuFrontException(tr("Trying to remove temporary files. "
118                                        "Directory %s doesn't exist!").arg(tmp));
119         }
120         // clean the temp dir
121         // TODO: if selected archive with multiple items, the files are not removed!
122         foreach(EmuFrontObject *ob, miList) {
123             if (!ftmp.exists(ob->getName())) {
124                 qDebug() << "File " << ob->getName() << " doesn't exist in " << tmp;
125                 continue;
126             }
127             QString fp = ftmp.filePath(ob->getName());
128             QFile f(fp);
129             if (!f.exists()) {
130                 qDebug() << "File " << fp << " doesn't exist!";
131             }
132             if (!f.remove()) {
133                 qDebug() << "Removing " << fp << " failed.";
134             }
135         }
136     } catch (EmuFrontException e) {
137         qDebug() << e.what();
138     }
139     delete ex;
140     qDeleteAll(micList);
141     //qDeleteAll(miList); these objects are already deleted along with micList
142 }
143
144 void EmuHelper::processError(QProcess::ProcessError)
145 {
146
147 }
148
149 void EmuHelper::processFinished(int)
150 {
151 }
152
153 void EmuHelper::connectSignals()
154 {
155     connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
156     connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
157 }