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