Exception handling.
[emufront] / src / mainwindow.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include "mainwindow.h"
22 #include "emulauncher.h"
23 #include "dialogs/platformdialog.h"
24 #include "dialogs/mediatypedialog.h"
25 #include "dialogs/mediaimagepathmaindialog.h"
26 #include "dialogs/setupmaindialog.h"
27 #include "dialogs/executablemaindialog.h"
28 #include "utils/datfileutil.h"
29 #include "db/databasemanager.h"
30 #include "db/dbcreator.h"
31 #include "db/dbconfig.h"
32
33 QString MainWindow::aboutStr = trUtf8(
34         "<h2>EmuFront</h2>"
35         "<p>&copy; 2010 Mikko Keinänen</p>"
36         "<p>mikko.keinanen@gmail.com</p>"
37         "<p>EmuFront is free software: you can redistribute it and/or modify "
38         "it under the terms of the GNU General Public License version 2 as published by "
39         "the Free Software Foundation.</p>"
40 );
41
42 QString MainWindow::aboutTitle = tr("About EmuFront");
43
44 MainWindow::MainWindow(bool reset)
45 {
46     if (!testDB(reset)) close();
47     errorMessage = new QErrorMessage(this);
48     setWindowTitle("EmuFront");
49     tmpDirFilePath = DbConfig::getTmpDir();
50     if (tmpDirFilePath.isEmpty())
51         tmpDirFilePath = QDir::homePath();
52     qDebug() << "Temporary dir is " << tmpDirFilePath;
53     launcher = new EmuLauncher(this, tmpDirFilePath);
54     setCentralWidget(launcher);
55     createActions();
56     createMenus();
57     createStatusBar();
58     readSettings();
59     platformDialog = 0;
60     mediaTypeDialog = 0;
61     mediaImagePathDialog = 0;
62     setupMainDialog = 0;
63     executableMainDialog = 0;
64 }
65
66 void MainWindow::connectSignals()
67 {
68 }
69
70 void MainWindow::createActions()
71 {
72     configPlatformAction = new QAction(tr("&Platforms"), this);
73     configPlatformAction->setStatusTip(tr("Configure platforms"));
74     connect(configPlatformAction, SIGNAL(triggered()),
75             this, SLOT(configurePlatforms()));
76
77     configMediaTypeAction = new QAction(tr("&Media Types"), this);
78     configMediaTypeAction->setStatusTip(tr("Configure media types"));
79     connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
80
81     configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
82     configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
83     connect(configMediaImagePathAction, SIGNAL(triggered()),
84         this, SLOT(configureMediaImagePaths()));
85
86     configSetupAction = new QAction(tr("S&etups"), this);
87     configSetupAction->setStatusTip(tr("Configure set ups"));
88     connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
89
90     configEmulatorAction = new QAction(tr("Em&ulators"), this);
91     configEmulatorAction->setStatusTip(tr("Configure emulators"));
92     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
93
94     configTmpDirAction = new QAction(tr("&Temp dir"), this);
95     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
96     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
97
98     manageDatFilesAction = new QAction(tr("&Manage dats"), this);
99     manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
100     connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
101
102     exitAction = new QAction(tr("&Exit"), this);
103     exitAction->setShortcut(tr("Ctrl+Q"));
104     exitAction->setStatusTip(tr("Exit EmuFront"));
105     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
106
107     resetDbAction = new QAction( tr("Reset database"), this);
108     resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
109     connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
110
111     aboutAction = new QAction(tr("&About"), this);
112     aboutAction->setStatusTip(tr("About EmuFront"));
113     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
114 }
115
116 void MainWindow::configurePlatforms()
117 {
118    if (!platformDialog)
119    {
120        platformDialog = new PlatformDialog(this);
121        connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
122    }
123    activateDialog(platformDialog);
124 }
125
126 void MainWindow::configureMediaTypes()
127 {
128     if (!mediaTypeDialog)
129     {
130         mediaTypeDialog = new MediaTypeDialog(this);
131         connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
132    }
133    activateDialog(mediaTypeDialog);
134 }
135
136 void MainWindow::configureMediaImagePaths()
137 {
138     if (!mediaImagePathDialog)
139     {
140         mediaImagePathDialog = new MediaImagePathMainDialog(this);
141     }
142     activateDialog(mediaImagePathDialog);
143 }
144
145 void MainWindow::configureSetups()
146 {
147     if (!setupMainDialog)
148     {
149         qDebug() << "MainWindow: Creating a setup main dialog.";
150         setupMainDialog = new SetupMainDialog(this);
151     }
152     activateDialog(setupMainDialog);
153     setupMainDialog->refreshDataModel();
154 }
155
156 void MainWindow::configureEmulators()
157 {
158     if (!executableMainDialog) {
159         executableMainDialog = new ExecutableMainDialog(this);
160         connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
161     }
162     activateDialog(executableMainDialog);
163     executableMainDialog->refreshDataModel();
164 }
165
166 void MainWindow::configureTmpDir()
167 {
168     /*if (!tmpFolderDialog) {
169         tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
170     }
171     activateDialog(tmpFolderDialog);*/
172
173     QString fpath = QFileDialog::getExistingDirectory(this,
174         tr("Select a directory"), tmpDirFilePath,
175         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
176     QDir d(fpath);
177     if (d.exists() && d.isReadable()) {
178         tmpDirFilePath = fpath;
179         DbConfig::setTmpDir(tmpDirFilePath);
180         launcher->setTmpDirPath(tmpDirFilePath);
181     }
182 }
183
184 void MainWindow::resetDb()
185 {
186     if (QMessageBox::question(this, "Reset database?",
187         "Are you REALLY SURE you want to do this? "
188         "All the current data WILL BE LOST!",
189         QMessageBox::No,
190         QMessageBox::Yes) == QMessageBox::No) {
191         return;
192     }
193     try {
194         createDB();
195     }
196     catch (EmuFrontException e) {
197         errorMessage->showMessage(e.what());
198     }
199 }
200
201 void MainWindow::manageDatFiles()
202 {
203     DatFileUtil dfu;
204     dfu.open();
205 }
206
207 void MainWindow::activateDialog(EmuFrontDialog* dia) const
208 {
209     dia->show();
210     dia->raise();
211     dia->activateWindow();
212 }
213
214 void MainWindow::createMenus()
215 {
216     fileMenu = menuBar()->addMenu(tr("&File"));
217     fileMenu->addAction(resetDbAction);
218     fileMenu->addSeparator();
219     fileMenu->addAction(exitAction);
220
221     configMenu = menuBar()->addMenu(tr("&Config"));
222     configMenu->addAction(configTmpDirAction);
223     configMenu->addSeparator();
224     configMenu->addAction(configPlatformAction);
225     configMenu->addAction(configMediaTypeAction);
226     configMenu->addAction(configSetupAction);
227     configMenu->addAction(configMediaImagePathAction);
228     configMenu->addAction(configEmulatorAction);
229     configMenu->addSeparator();
230     configMenu->addAction(manageDatFilesAction);
231
232     helpMenu = menuBar()->addMenu(tr("&Help"));
233     helpMenu->addAction(aboutAction);
234 }
235
236 void MainWindow::createStatusBar()
237 {
238     messageLabel = new QLabel;
239     statusBar()->addWidget(messageLabel);
240 }
241
242 void MainWindow::readSettings()
243 {
244 }
245
246 void MainWindow::writeSettings()
247 {
248 }
249
250 void MainWindow::closeEvent(QCloseEvent *event)
251 {
252     if (okToContinue())
253         event->accept();
254     else event->ignore();
255 }
256
257 bool MainWindow::okToContinue()
258 {
259     return true;
260 }
261
262 void MainWindow::updateData()
263 {
264     qDebug() << "MainWindow::updateData()";
265     launcher->updateData();
266 }
267
268 void MainWindow::about()
269 {
270     QMessageBox::about(this, aboutTitle, aboutStr );
271 }
272
273 bool MainWindow::testDB(bool reset)
274 {
275     try {
276         if (DatabaseManager::openDB()) {
277             qDebug() << " Database opened succesfully!";
278         }
279         else {
280             throw EmuFrontException("Database connection failed!");
281         }
282
283         int dbVer = DbCreator::dbExists();
284         if (dbVer == 0) reset = true;
285         if (!reset && dbVer != DbCreator::DB_VERSION) {
286             QString msg("Database is not compatible "
287                         "with current version of EmuFront!"
288                         "Do you want to continue to recreate the database?"
289                         "ALL THE CURRENT DATA WILL BE LOST!!!");
290             if (QMessageBox::question(this, "Database not compatible!", msg,
291                                       QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
292                 reset = true;
293             }
294             else throw EmuFrontException("The current database is not compatible!"
295                                          " Cannot continue.");
296         }
297
298         if (reset) {
299             createDB();
300         }
301         return true;
302     }
303     catch (EmuFrontException e) {
304         qDebug() << e.what();
305         errorMessage->showMessage(e.what());
306         return false;
307     }
308 }
309
310 void MainWindow::createDB() const
311 {
312     try
313     {
314         DbCreator dbCreator;
315         dbCreator.createDB();
316     }
317     catch (QString str) {
318         QString msg(tr("Exception while trying to create"
319                        " EmuFront database: %s").arg(str));
320         errorMessage->showMessage(msg);
321     }
322 }
323