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