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