dda730e4ecdf87145a88f8770ad7835fadbd5dc4
[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(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(emuModel, this);
268         //connect(emulatorEditView, SIGNAL(finished(int)), this, SLOT(updateData()));
269     }
270     activateDialog(emulatorEditView);
271 }
272
273 void MainWindow::configureTmpDir()
274 {
275     QString fpath = QFileDialog::getExistingDirectory(this,
276         tr("Select a directory"), tmpDirFilePath,
277         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
278     QDir d(fpath);
279     if (d.exists() && d.isReadable()) {
280         tmpDirFilePath = fpath;
281         if (DbConfig::setTmpDir(tmpDirFilePath)) {
282             QMessageBox::information(
283                 this,
284                 tr("Temp dir updated"),
285                 tr("The directory for temporary files "
286                     "has been updated to '%1'.")
287                     .arg(tmpDirFilePath)
288             );
289             launcher->setTmpDirPath(tmpDirFilePath);
290         }
291     }
292 }
293
294 void MainWindow::resetDb()
295 {
296     if (QMessageBox::question(this, "Reset database?",
297         "Are you REALLY SURE you want to do this? "
298         "All the current data WILL BE LOST!",
299         QMessageBox::No,
300         QMessageBox::Yes) == QMessageBox::No) {
301         return;
302     }
303     try {
304         createDB();
305     }
306     catch (EmuFrontException e) {
307         errorMessage->showMessage(e.what());
308     }
309 }
310
311 void MainWindow::manageDatFiles()
312 {
313     DatFileUtil dfu;
314     dfu.open();
315 }
316
317 void MainWindow::activateDialog(EmuFrontDialog* dia) const
318 {
319     dia->show();
320     dia->raise();
321     dia->activateWindow();
322 }
323
324 void MainWindow::createMenus()
325 {
326     fileMenu = menuBar()->addMenu(tr("&File"));
327     fileMenu->addAction(resetDbAction);
328     fileMenu->addSeparator();
329     fileMenu->addAction(exitAction);
330
331     configMenu = menuBar()->addMenu(tr("&Config"));
332     configMenu->addAction(configTmpDirAction);
333     configMenu->addSeparator();
334     // TODO: deprecated
335     //configMenu->addAction(configPlatformAction);
336     configMenu->addAction(configPlatformsAction);
337     // TODO: deprecated
338     //configMenu->addAction(configMediaTypeAction);
339     configMenu->addAction(configMediaTypesAction);
340     // TODO: deprecated
341     //configMenu->addAction(configSetupAction);
342     configMenu->addAction(configSetupsAction);
343     // TODO: DEPRECATED
344     //configMenu->addAction(configMediaImagePathAction);
345     configMenu->addAction(configMediaImagePathsAction);
346     // TODO: DEPRECATED
347     //configMenu->addAction(configEmulatorAction);
348     configMenu->addAction(configEmulatorsAction);
349     configMenu->addSeparator();
350     configMenu->addAction(manageDatFilesAction);
351
352     helpMenu = menuBar()->addMenu(tr("&Help"));
353     helpMenu->addAction(aboutAction);
354 }
355
356 void MainWindow::createStatusBar()
357 {
358     messageLabel = new QLabel;
359     statusBar()->addWidget(messageLabel);
360 }
361
362 void MainWindow::readSettings()
363 {
364 }
365
366 void MainWindow::writeSettings()
367 {
368 }
369
370 void MainWindow::closeEvent(QCloseEvent *event)
371 {
372     if (okToContinue())
373         event->accept();
374     else event->ignore();
375 }
376
377 bool MainWindow::okToContinue()
378 {
379     return true;
380 }
381
382 void MainWindow::updateData()
383 {
384     qDebug() << "MainWindow::updateData()";
385     launcher->updateData();
386 }
387
388 void MainWindow::about()
389 {
390     QMessageBox::about(this, aboutTitle, aboutStr);
391 }
392
393 bool MainWindow::testDB(bool reset)
394 {
395     try {
396         if (DatabaseManager::openDB()) {
397             qDebug() << " Database opened succesfully!";
398         }
399         else {
400             throw EmuFrontException("Database connection failed!");
401         }
402
403         int dbVer = DbCreator::dbExists();
404         if (dbVer == 0) reset = true;
405         if (!reset && dbVer != DbCreator::DB_VERSION) {
406             QString msg("Database is not compatible "
407                         "with current version of EmuFront!"
408                         "Do you want to continue to recreate the database?"
409                         "ALL THE CURRENT DATA WILL BE LOST!!!");
410             if (QMessageBox::question(this, "Database not compatible!", msg,
411                                       QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
412                 reset = true;
413             }
414             else throw EmuFrontException("The current database is not compatible!"
415                                          " Cannot continue.");
416         }
417     
418         if (reset) {
419             createDB();
420         }
421         return true;
422     }
423     catch (EmuFrontException e) {
424         qDebug() << e.what();
425         errorMessage->showMessage(e.what());
426         return false;
427     }
428 }
429
430 void MainWindow::createDB() const
431 {
432     try
433     {
434         DbCreator dbCreator;
435         dbCreator.createDB();
436     }
437     catch (QString str) {
438         QString msg(tr("Exception while trying to create"
439                        " EmuFront database: %s").arg(str));
440         errorMessage->showMessage(msg);
441     }
442 }
443