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