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