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