Implemented ExternalExecutableEditView utilising
[emufront] / src / mainwindow.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
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.
12 **
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.
17 **
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/>.
20 */
21 #include <QtGui>
22 #include "mainwindow.h"
23 #include "emulauncher.h"
24 // TODO: deprecated
25 #include "platformdialog.h"
26 #include "platformeditview.h"
27 #include "mediatypedialog.h"
28 // TODO: deprecated
29 #include "mediatypeeditview.h"
30 // TODO: DEPRECATED
31 #include "mediaimagepathmaindialog.h"
32 #include "filepatheditview.h"
33 // TODO: deprecated
34 #include "setupmaindialog.h"
35 #include "setupeditview.h"
36 // TODO: deprecated
37 #include "executablemaindialog.h"
38 #include "externalexecutableeditview.h"
39 #include "datfileutil.h"
40 #include "databasemanager.h"
41 #include "dbcreator.h"
42 #include "dbconfig.h"
43
44 QString MainWindow::aboutStr = trUtf8(
45         "<h2>EmuFront</h2>"
46         "<p>&copy; 2010 Mikko Keinänen</p>"
47         "<p>mikko.keinanen@gmail.com</p>"
48         "<p>EmuFront is free software: you can redistribute it and/or modify "
49         "it under the terms of the GNU General Public License version 2 as published by "
50         "the Free Software Foundation.</p>"
51 );
52
53 QString MainWindow::aboutTitle = tr("About EmuFront");
54
55 MainWindow::MainWindow(bool reset)
56 {
57     if (!testDB(reset)) close();
58     errorMessage = new QErrorMessage(this);
59     setWindowTitle("EmuFront");
60     tmpDirFilePath = DbConfig::getTmpDir();
61     if (tmpDirFilePath.isEmpty())
62         tmpDirFilePath = QDir::homePath();
63     qDebug() << "Temporary dir is " << tmpDirFilePath;
64     launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
65     setCentralWidget(launcher);
66     createActions();
67     createMenus();
68     createStatusBar();
69     readSettings();
70     // TODO: deprecated
71     platformDialog = 0;
72     plfDialog = 0;
73     // TODO: deprecated
74     mediaTypeDialog = 0;
75     mdtDialog = 0;
76     // TODO: DEPRECATED
77     mediaImagePathDialog = 0;
78     mediaImagePathView = 0;
79     // TODO: deprecated
80     setupMainDialog = 0;
81     setupMainView = 0;
82     // TODO: deprecated
83     executableMainDialog = 0;
84     emulatorEditView = 0;
85 }
86
87 void MainWindow::connectSignals()
88 {
89 }
90
91 void MainWindow::createActions()
92 {
93     // TODO: deprecated
94     configPlatformAction = new QAction(tr("&Platforms"), this);
95     configPlatformAction->setStatusTip(tr("Configure platforms"));
96     connect(configPlatformAction, SIGNAL(triggered()),
97         this, SLOT(configurePlatforms()));
98
99     configPlatformsAction = new QAction(tr("&Set Platforms"), this);
100     configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
101     connect(configPlatformsAction, SIGNAL(triggered()),
102         this, SLOT(configurePlatformss()));
103
104     // TODO: deprecated
105     configMediaTypeAction = new QAction(tr("&Media Types"), this);
106     configMediaTypeAction->setStatusTip(tr("Configure media types"));
107     connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
108
109     configMediaTypesAction = new QAction(tr("&Set Media Types"), this);
110     configMediaTypeAction->setStatusTip(tr("Add, edit and delete media types"));
111     connect(configMediaTypesAction, SIGNAL(triggered()), this, SLOT(configureMediaTypess()));
112
113     // TODO: DEPRECATED
114     configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
115     configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
116     connect(configMediaImagePathAction, SIGNAL(triggered()),
117         this, SLOT(configureMediaImagePaths()));
118
119     configMediaImagePathsAction = new QAction(tr("Set media &image paths"), this);
120     configMediaImagePathsAction->setStatusTip(tr("Add, edit and delete media image file paths."));
121     connect(configMediaImagePathsAction, SIGNAL(triggered()),
122         this, SLOT(configureMediaImagePathss()));
123
124     // TODO: deprecated
125     configSetupAction = new QAction(tr("S&etups"), this);
126     configSetupAction->setStatusTip(tr("Configure set ups"));
127     connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
128
129     configSetupsAction = new QAction(tr("Configure S&etups"), this);
130     configSetupsAction->setStatusTip(tr("Add, edit and delete setups"));
131     connect(configSetupsAction, SIGNAL(triggered()), this, SLOT(configureSetupss()));
132
133     configEmulatorAction = new QAction(tr("Em&ulators"), this);
134     configEmulatorAction->setStatusTip(tr("Configure emulators"));
135     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
136
137     configEmulatorsAction = new QAction(tr("Configure Em&ulators"), this);
138     configEmulatorsAction->setStatusTip(tr("Add, edit and delete emulator configurations"));
139     connect(configEmulatorsAction, SIGNAL(triggered()), this, SLOT(configureEmulatorss()));
140
141
142     configTmpDirAction = new QAction(tr("&Temp dir"), this);
143     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
144     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
145
146     manageDatFilesAction = new QAction(tr("&Manage dats"), this);
147     manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
148     connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
149
150     exitAction = new QAction(tr("&Exit"), this);
151     exitAction->setShortcut(tr("Ctrl+Q"));
152     exitAction->setStatusTip(tr("Exit EmuFront"));
153     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
154
155     resetDbAction = new QAction( tr("Reset database"), this);
156     resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
157     connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
158
159     aboutAction = new QAction(tr("&About"), this);
160     aboutAction->setStatusTip(tr("About EmuFront"));
161     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
162 }
163
164 // TODO: deprecated
165 void MainWindow::configurePlatforms()
166 {
167    if (!platformDialog)
168    {
169        platformDialog = new PlatformDialog(this);
170        connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
171    }
172    activateDialog(platformDialog);
173 }
174
175 void MainWindow::configurePlatformss()
176 {
177     if (!plfDialog) {
178         plfDialog = new PlatformEditView(this);
179         connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
180     }
181     activateDialog(plfDialog);
182 }
183
184 // TODO: deprecated
185 void MainWindow::configureMediaTypes()
186 {
187     if (!mediaTypeDialog)
188     {
189         mediaTypeDialog = new MediaTypeDialog(this);
190         connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
191    }
192    activateDialog(mediaTypeDialog);
193 }
194
195 void MainWindow::configureMediaTypess()
196 {
197     if (!mdtDialog)
198     {
199         mdtDialog = new MediaTypeEditView(this);
200         connect(mdtDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
201    }
202    activateDialog(mdtDialog);
203 }
204
205
206 // TODO: DEPRECATED
207 void MainWindow::configureMediaImagePaths()
208 {
209     if (!mediaImagePathDialog)
210     {
211         mediaImagePathDialog = new MediaImagePathMainDialog(this);
212     }
213     activateDialog(mediaImagePathDialog);
214 }
215
216 void MainWindow::configureMediaImagePathss()
217 {
218     if (!mediaImagePathView)
219     {
220         mediaImagePathView = new FilePathEditView(this);
221     }
222     activateDialog(mediaImagePathView);
223 }
224
225 // TODO: deprecated
226 void MainWindow::configureSetups()
227 {
228     if (!setupMainDialog)
229     {
230         setupMainDialog = new SetupMainDialog(this);
231     }
232     activateDialog(setupMainDialog);
233     setupMainDialog->refreshDataModel();
234 }
235
236 void MainWindow::configureSetupss()
237 {
238     if (!setupMainView) {
239         setupMainView = new SetupEditView(this);
240     }
241     activateDialog(setupMainView);
242 }
243
244
245 // TODO: deprecated
246 void MainWindow::configureEmulators()
247 {
248     if (!executableMainDialog) {
249         executableMainDialog = new ExecutableMainDialog(this);
250         connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
251     }
252     activateDialog(executableMainDialog);
253     executableMainDialog->refreshDataModel();
254 }
255
256 void MainWindow::configureEmulatorss()
257 {
258     if (!emulatorEditView) {
259         emulatorEditView = new ExternalExecutableEditView(this);
260         connect(emulatorEditView, SIGNAL(finished(int)), this, SLOT(updateData()));
261     }
262     activateDialog(emulatorEditView);
263 }
264
265 void MainWindow::configureTmpDir()
266 {
267     /*if (!tmpFolderDialog) {
268         tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
269     }
270     activateDialog(tmpFolderDialog);*/
271
272     QString fpath = QFileDialog::getExistingDirectory(this,
273         tr("Select a directory"), tmpDirFilePath,
274         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
275     QDir d(fpath);
276     if (d.exists() && d.isReadable()) {
277         tmpDirFilePath = fpath;
278         DbConfig::setTmpDir(tmpDirFilePath);
279         launcher->setTmpDirPath(tmpDirFilePath);
280     }
281 }
282
283 void MainWindow::resetDb()
284 {
285     if (QMessageBox::question(this, "Reset database?",
286         "Are you REALLY SURE you want to do this? "
287         "All the current data WILL BE LOST!",
288         QMessageBox::No,
289         QMessageBox::Yes) == QMessageBox::No) {
290         return;
291     }
292     try {
293         createDB();
294     }
295     catch (EmuFrontException e) {
296         errorMessage->showMessage(e.what());
297     }
298 }
299
300 void MainWindow::manageDatFiles()
301 {
302     DatFileUtil dfu;
303     dfu.open();
304 }
305
306 void MainWindow::activateDialog(EmuFrontDialog* dia) const
307 {
308     dia->show();
309     dia->raise();
310     dia->activateWindow();
311 }
312
313 void MainWindow::createMenus()
314 {
315     fileMenu = menuBar()->addMenu(tr("&File"));
316     fileMenu->addAction(resetDbAction);
317     fileMenu->addSeparator();
318     fileMenu->addAction(exitAction);
319
320     configMenu = menuBar()->addMenu(tr("&Config"));
321     configMenu->addAction(configTmpDirAction);
322     configMenu->addSeparator();
323     // TODO: deprecated
324     configMenu->addAction(configPlatformAction);
325     configMenu->addAction(configPlatformsAction);
326     // TODO: deprecated
327     configMenu->addAction(configMediaTypeAction);
328     configMenu->addAction(configMediaTypesAction);
329     // TODO: deprecated
330     configMenu->addAction(configSetupAction);
331     configMenu->addAction(configSetupsAction);
332     // TODO: DEPRECATED
333     configMenu->addAction(configMediaImagePathAction);
334     configMenu->addAction(configMediaImagePathsAction);
335     // TODO: DEPRECATED
336     configMenu->addAction(configEmulatorAction);
337     configMenu->addAction(configEmulatorsAction);
338     configMenu->addSeparator();
339     configMenu->addAction(manageDatFilesAction);
340
341     helpMenu = menuBar()->addMenu(tr("&Help"));
342     helpMenu->addAction(aboutAction);
343 }
344
345 void MainWindow::createStatusBar()
346 {
347     messageLabel = new QLabel;
348     statusBar()->addWidget(messageLabel);
349 }
350
351 void MainWindow::readSettings()
352 {
353 }
354
355 void MainWindow::writeSettings()
356 {
357 }
358
359 void MainWindow::closeEvent(QCloseEvent *event)
360 {
361     if (okToContinue())
362         event->accept();
363     else event->ignore();
364 }
365
366 bool MainWindow::okToContinue()
367 {
368     return true;
369 }
370
371 void MainWindow::updateData()
372 {
373     qDebug() << "MainWindow::updateData()";
374     launcher->updateData();
375 }
376
377 void MainWindow::about()
378 {
379     QMessageBox::about(this, aboutTitle, aboutStr);
380 }
381
382 bool MainWindow::testDB(bool reset)
383 {
384     try {
385         if (DatabaseManager::openDB()) {
386             qDebug() << " Database opened succesfully!";
387         }
388         else {
389             throw EmuFrontException("Database connection failed!");
390         }
391
392         int dbVer = DbCreator::dbExists();
393         if (dbVer == 0) reset = true;
394         if (!reset && dbVer != DbCreator::DB_VERSION) {
395             QString msg("Database is not compatible "
396                         "with current version of EmuFront!"
397                         "Do you want to continue to recreate the database?"
398                         "ALL THE CURRENT DATA WILL BE LOST!!!");
399             if (QMessageBox::question(this, "Database not compatible!", msg,
400                                       QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
401                 reset = true;
402             }
403             else throw EmuFrontException("The current database is not compatible!"
404                                          " Cannot continue.");
405         }
406
407         if (reset) {
408             createDB();
409         }
410         return true;
411     }
412     catch (EmuFrontException e) {
413         qDebug() << e.what();
414         errorMessage->showMessage(e.what());
415         return false;
416     }
417 }
418
419 void MainWindow::createDB() const
420 {
421     try
422     {
423         DbCreator dbCreator;
424         dbCreator.createDB();
425     }
426     catch (QString str) {
427         QString msg(tr("Exception while trying to create"
428                        " EmuFront database: %s").arg(str));
429         errorMessage->showMessage(msg);
430     }
431 }
432