2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
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"
30 EmuHelper::EmuHelper(QObject *parent) :
33 unzipHelper = new UnzipHelper(this);
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)
41 if (micList.count() < 1) {
42 throw EmuFrontException(tr("No media image containers available!"));
45 MediaImageContainer *mic = micList.first();
47 fp.append(mic->getFilePath()->getName());
48 if (!fp.endsWith('/'))
50 fp.append(mic->getName());
53 QString opts = ex->getOptions();
54 opts.replace(QString("$1"), fp);
56 QString cmdWithParams;
57 cmdWithParams.append(ex->getExecutable());
58 cmdWithParams.append(" ").append(opts);
60 qDebug() << "Command with params " << cmdWithParams;
61 start(cmdWithParams, QIODevice::ReadOnly);
63 // for the moment, we'll wait for the process to be finished until we continue
70 /* Throws EmuFrontException */
71 void EmuHelper::launch(const Executable * ex, QList<MediaImageContainer *> micList,
72 QList<EmuFrontObject *> miList, int mediaCount, QString tmp)
74 if (miList.count() < 1) {
75 throw EmuFrontException(tr("No media images available!"));
77 if (micList.count() < 1) {
78 throw EmuFrontException(tr("No media image containers available!"));
81 if (!tmp.endsWith('/')) tmp.append("/");
83 // extract the media image container to tmp folder
84 foreach(MediaImageContainer *mic, micList) {
86 fp.append(mic->getFilePath()->getName());
87 if (!fp.endsWith('/')) fp.append("/");
88 fp.append(mic->getName());
89 qDebug() << "Extracting " << fp;
90 int ret = unzipHelper->extractAll(fp, tmp);
92 qDebug() << "Failed unzipping " << fp << ".";
96 // fill in the media image slots in the command line options ($1, $2, ...)
97 QString opts = ex->getOptions();
98 for(int i = 0; i < mediaCount && i < miList.size(); i++) {
99 QString tmpfp = " \"";
101 tmpfp.append (miList.at(i)->getName());
103 opts.replace(QString("$%1").arg(i+1), tmpfp);
106 QString cmdWithParams;
107 cmdWithParams.append(ex->getExecutable());
108 cmdWithParams.append(" ").append(opts);
110 qDebug() << "Command with params " << cmdWithParams;
111 start(cmdWithParams, QIODevice::ReadOnly);
113 // for the moment, we'll wait for the process to be finished until we continue
118 if (!ftmp.exists()) {
119 throw EmuFrontException(tr("Trying to remove temporary files. "
120 "Directory %s doesn't exist!").arg(tmp));
122 // clean the temp dir
123 // TODO: if selected archive with multiple items, the files are not removed!
124 foreach(EmuFrontObject *ob, miList) {
125 if (!ftmp.exists(ob->getName())) {
126 qDebug() << "File " << ob->getName() << " doesn't exist in " << tmp;
129 QString fp = ftmp.filePath(ob->getName());
132 qDebug() << "File " << fp << " doesn't exist!";
135 qDebug() << "Removing " << fp << " failed.";
138 } catch (EmuFrontException e) {
139 qDebug() << e.what();
143 //qDeleteAll(miList); these objects are already deleted along with micList
146 void EmuHelper::processError(QProcess::ProcessError)
151 void EmuHelper::processFinished(int)
155 void EmuHelper::connectSignals()
157 connect(unzipHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
158 connect(unzipHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));