Set the selection mode to ExtendedSelection instead of MultiSelection.
[emufront] / src / emulauncher.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 <QtGui>
21 #include <QProcess>
22 #include <QSqlTableModel>
23 #include <QItemSelectionModel>
24 #include "emulauncher.h"
25 #include "db/dbmediatype.h"
26 #include "db/dbplatform.h"
27 #include "db/dbexecutable.h"
28 #include "db/dbmediaimagecontainer.h"
29 #include "widgets/effileobjectcombobox.h"
30 #include "widgets/executablecombobox.h"
31 #include "dataobjects/executable.h"
32 #include "utils/emuhelper.h"
33 #include "dialogs/emufrontinputdialog.h"
34
35 EmuLauncher::EmuLauncher(QWidget *parent, QString tmp) :
36     QWidget(parent), tmpDirPath(tmp)
37 {
38     dbPlatform = new DbPlatform(this);
39     dbMediaType = new DbMediaType(this);
40     dbExec = new DbExecutable(this);
41     dbMic = 0;
42     emuHelper = new EmuHelper(this);
43     initWidgets();
44     layout();
45     connectSignals();
46 }
47
48 EmuLauncher::~EmuLauncher()
49 {
50     if (emuHelper) {
51         qDebug() << "EmuLauncher destructor";
52         if (emuHelper->state() == EmuHelper::Running)
53             qDebug() << "EmuHelper process is running, killing...";
54             emuHelper->kill();
55         }
56 }
57
58 void EmuLauncher::updateData()
59 {
60     platformSelectBox->updateDataModel();
61     mediaTypeSelectBox->updateDataModel();
62     //execSelectBox->updateDataModel();
63 }
64
65 void EmuLauncher::initWidgets()
66 {
67     micTable = new QTableView(this);
68     micTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
69     mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
70     platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
71     execSelectBox = new ExecutableComboBox(dbExec, this);
72     selectButton = new QPushButton(tr("&Update"), this);
73     launchButton = new QPushButton(tr("&Launch"), this);
74 }
75
76 void EmuLauncher::layout()
77 {
78     QGridLayout *grid = new QGridLayout;
79     grid->addWidget(platformSelectBox, 0, 0);
80     grid->addWidget(mediaTypeSelectBox, 1, 0);
81     grid->addWidget(selectButton, 1, 1);
82     grid->setColumnStretch(2, 1);
83
84     grid->addWidget(micTable, 2, 0, 1, 3);
85     grid->addWidget(execSelectBox, 3, 0);
86     grid->addWidget(launchButton, 3, 1);
87     // grid will be implicitily parented to this
88     setLayout(grid);
89 }
90
91 void EmuLauncher::connectSignals()
92 {
93     connect(selectButton, SIGNAL(clicked()), this, SLOT(updateMediaImageContainers()));
94     connect(launchButton, SIGNAL(clicked()),this, SLOT(launchEmu()));
95     connect(emuHelper, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
96     connect(emuHelper, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
97 }
98
99 void EmuLauncher::updateMediaImageContainers()
100 {
101     qDebug() << "updateMediaImageContainers slot";
102     int mtid = mediaTypeSelectBox->getSelected()
103         ? mediaTypeSelectBox->getSelected()->getId()
104         : -1;
105     int plfid = platformSelectBox->getSelected()
106         ? platformSelectBox->getSelected()->getId()
107         : -1;
108
109     if (!dbMic) dbMic = new DbMediaImageContainer(this);
110     dbMic->filter(mtid, plfid);
111     micTable->setModel(dbMic->getDataModel());
112     micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
113     micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
114     micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
115     micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
116     micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
117     micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
118     micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
119     micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
120     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
121     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
122     micTable->resizeColumnsToContents();
123     platformSelectBox->updateDataModel();
124     mediaTypeSelectBox->updateDataModel();
125     execSelectBox->updateToSetup(plfid, mtid);
126 }
127
128 void EmuLauncher::launchEmu()
129 {
130     QMap<QString, EmuFrontObject*> mediaImages;
131     QList<MediaImageContainer*> mediaImageContainers;
132     Executable *exe = 0;
133     try {
134         if (!micTable || !micTable->model()) {
135             throw EmuFrontException(tr("No search results available!"));
136         }
137         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
138             throw EmuFrontException(tr("Emulator not selected!"));
139         }
140         QItemSelectionModel *selModel = micTable->selectionModel();
141         QModelIndexList listMIndex =  selModel->selectedIndexes();
142         if (listMIndex.count() < 1) {
143             throw EmuFrontException(tr("Media image container not selected!"));
144         }
145         qDebug() << listMIndex.count() << " items selected.";
146
147         EmuFrontObject *obExe = execSelectBox->getSelected();
148         if (!obExe) {
149             throw EmuFrontException(tr("Failed fetching selected emulator!"));
150         }
151         exe = dynamic_cast<Executable*>(obExe);
152         if (!exe) {
153             throw EmuFrontException(tr("Failed creating Emulator object!"));
154         }
155
156         foreach(QModelIndex mind, listMIndex) {
157             if (!mind.isValid()) continue;
158             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
159             if (!obImg) {
160                 qDebug() << "Failed creating media image container at row " << mind.row();
161                 continue;
162             }
163             MediaImageContainer *mic = dynamic_cast<MediaImageContainer*>(obImg);
164             if (!mic) {
165                 qDebug() << "Failed to create media image container for " << obImg->getName();
166                 delete obImg;
167                 continue;
168             }
169             mediaImageContainers << mic;
170             QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
171             mediaImages.unite(contained);
172         }
173
174         if (mediaImages.count() < 1) {
175             throw EmuFrontException("No media images available!");
176         }
177
178         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
179         QString opts = exe->getOptions();
180         QRegExp rx("(\\$\\d+)");
181         QStringList list;
182         int pos = 0;
183         while ((pos = rx.indexIn(opts, pos)) != -1) {
184             list << rx.cap(1);
185             pos += rx.matchedLength();
186         }
187
188         bool ok;
189         QList<EmuFrontObject*> selectedImages;
190         if (list.count() > mediaImages.count()) {
191             throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
192         }
193         if (list.count() > 1) {
194             int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
195             for(int i = 0; i < lim; i++) {
196                 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
197                         this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
198                 if (!ok)  {
199                     throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
200                 }
201                 selectedImages << efo;
202                 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
203                 QString key = mi->getCheckSum();
204                 mediaImages.remove(key);
205             }
206             if (mediaImages.count() == 1) {
207                 // there should be at least one media image left in mediaImages map
208                 selectedImages << mediaImages.values().first();
209             }
210         }
211         else if (mediaImages.count() > 1) {
212             // show select boot image dialog
213             EmuFrontObject *efo = EmuFrontInputDialog::getItem(
214                 this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok);
215             if (!ok)  {
216                 throw EmuFrontException(tr("Boot image selection was canceled, aborting."));
217             }
218             selectedImages << efo;
219         }
220         else if (mediaImages.count() == 1)
221             selectedImages << mediaImages.values().first();
222         // in the both cases the (ordered) list of media images will be passed to emuHelper
223
224         if (selectedImages.count() < 1)
225             throw EmuFrontException(tr("No media images selected"));
226
227         emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath);
228         micTable->clearSelection();
229     } catch (EmuFrontException efe) {
230         if (exe) delete exe;
231         qDeleteAll(mediaImageContainers);
232         //qDeleteAll(mediaImages); these are already deleted along with containers
233         QMessageBox::information(this, tr("Launching emulator"),
234                                  efe.what(), QMessageBox::Ok);
235         return;
236     }
237 }
238
239 void EmuLauncher::processError(QProcess::ProcessError e)
240 {
241     cleanTmp();
242     QString stdErr = emuHelper->readAllStandardError();
243     QMessageBox::warning(this, tr("Emulator"),
244         tr("Launching emulator failed with: %1.\n").arg(e)
245         .append(";\n").append(emuHelper->errorString().append(";\n")
246         .append(stdErr)), QMessageBox::Ok );
247 }
248
249 /* Slot for EmuHelper process finished, clears the temporary folder files */
250 void EmuLauncher::processFinished(int a)
251 {
252     cleanTmp();
253     QString stdErr = emuHelper->readAllStandardError();
254     QString stdMsg = emuHelper->readAllStandardOutput();
255     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
256     if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
257     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
258 }
259
260 void EmuLauncher::cleanTmp()
261 {
262     // TODO
263 }
264
265 void EmuLauncher::setTmpDirPath(QString tmp)
266 {
267     tmpDirPath = tmp;
268 }