3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
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.
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.
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/>.
24 #include <QMessageBox>
25 #include "emuhelper.h"
26 #include "unziphelper.h"
27 #include "mediaimagecontainer.h"
28 #include "executable.h"
29 #include "emufrontexception.h"
31 EmuHelper::EmuHelper(QObject *parent) :
34 unzipHelper = new UnzipHelper(this);
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)
42 if (micList.count() < 1) {
43 throw EmuFrontException(tr("No media image containers available!"));
46 MediaImageContainer *mic = micList.first();
48 fp.append(mic->getFilePath()->getName());
49 if (!fp.endsWith('/'))
51 fp.append(mic->getName());
54 QString opts = ex->getOptions();
55 opts.replace(QString("$1"), fp);
57 QString cmdWithParams;
58 cmdWithParams.append(ex->getExecutable());
59 cmdWithParams.append(" ").append(opts);
61 qDebug() << "Command with params " << cmdWithParams;
62 start(cmdWithParams, QIODevice::ReadOnly);
64 // for the moment, we'll wait for the process to be finished until we continue
67 // these will be deleted in calling function:
69 //qDeleteAll(micList);
72 /* Throws EmuFrontException */
73 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList,
74 QList<EmuFrontObject *> miList, int mediaCount, QString tmp)
76 if (miList.count() < 1) {
77 throw EmuFrontException(tr("No media images available!"));
79 if (micList.count() < 1) {
80 throw EmuFrontException(tr("No media image containers available!"));
83 if (!tmp.endsWith('/')) tmp.append("/");
85 // extract the media image container to tmp folder
86 foreach(MediaImageContainer *mic, micList) {
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
94 qDebug() << "Failed unzipping " << fp << ".";
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 = " \"";
103 tmpfp.append (miList.at(i)->getName());
105 opts.replace(QString("$%1").arg(i+1), tmpfp);
108 QString cmdWithParams;
109 cmdWithParams.append(ex->getExecutable());
110 cmdWithParams.append(" ").append(opts);
112 qDebug() << "Command with params " << cmdWithParams;
113 start(cmdWithParams, QIODevice::ReadOnly);
115 // for the moment, we'll wait for the process to be finished until we continue
120 if (!ftmp.exists()) {
121 throw EmuFrontException(tr("Trying to remove temporary files. "
122 "Directory %s doesn't exist!").arg(tmp));
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;
131 QString fp = ftmp.filePath(ob->getName());
134 qDebug() << "File " << fp << " doesn't exist!";
137 qDebug() << "Removing " << fp << " failed.";
140 } catch (EmuFrontException e) {
141 qDebug() << e.what();
144 // these will be deleted in calling function:
146 //qDeleteAll(micList);delete ex;
147 //qDeleteAll(miList); these objects are already deleted along with micList
150 void EmuHelper::processError(QProcess::ProcessError)
155 void EmuHelper::processFinished(int)
159 void EmuHelper::connectSignals()
161 connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
162 connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));