Added some exception handling. Marked all the functions throwing
[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     setWindowTitle("EmuFront");
48     tmpDirFilePath = DbConfig::getTmpDir();
49     if (tmpDirFilePath.isEmpty())
50         tmpDirFilePath = QDir::homePath();
51     qDebug() << "Temporary dir is " << tmpDirFilePath;
52     launcher = new EmuLauncher(this, tmpDirFilePath);
53     setCentralWidget(launcher);
54     createActions();
55     createMenus();
56     createStatusBar();
57     readSettings();
58     platformDialog = 0;
59     mediaTypeDialog = 0;
60     mediaImagePathDialog = 0;
61     setupMainDialog = 0;
62     executableMainDialog = 0;
63 }
64
65 void MainWindow::connectSignals()
66 {
67 }
68
69 void MainWindow::createActions()
70 {
71     configPlatformAction = new QAction(tr("&Platforms"), this);
72     configPlatformAction->setStatusTip(tr("Configure platforms"));
73     connect(configPlatformAction, SIGNAL(triggered()),
74             this, SLOT(configurePlatforms()));
75
76     configMediaTypeAction = new QAction(tr("&Media Types"), this);
77     configMediaTypeAction->setStatusTip(tr("Configure media types"));
78     connect(configMediaTypeAction, SIGNAL(triggered()), this, SLOT(configureMediaTypes()));
79
80     configMediaImagePathAction = new QAction(tr("Media &Image Paths"), this);
81     configMediaImagePathAction->setStatusTip(tr("Configure media image file paths."));
82     connect(configMediaImagePathAction, SIGNAL(triggered()),
83         this, SLOT(configureMediaImagePaths()));
84
85     configSetupAction = new QAction(tr("S&etups"), this);
86     configSetupAction->setStatusTip(tr("Configure set ups"));
87     connect(configSetupAction, SIGNAL(triggered()), this, SLOT(configureSetups()));
88
89     configEmulatorAction = new QAction(tr("Em&ulators"), this);
90     configEmulatorAction->setStatusTip(tr("Configure emulators"));
91     connect(configEmulatorAction, SIGNAL(triggered()), this, SLOT(configureEmulators()));
92
93     configTmpDirAction = new QAction(tr("&Temp dir"), this);
94     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
95     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
96
97     manageDatFilesAction = new QAction(tr("&Manage dats"), this);
98     manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
99     connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
100
101     exitAction = new QAction(tr("&Exit"), this);
102     exitAction->setShortcut(tr("Ctrl+Q"));
103     exitAction->setStatusTip(tr("Exit EmuFront"));
104     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
105
106     resetDbAction = new QAction( tr("Reset database"), this);
107     resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
108     connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
109
110     aboutAction = new QAction(tr("&About"), this);
111     aboutAction->setStatusTip(tr("About EmuFront"));
112     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
113 }
114
115 void MainWindow::configurePlatforms()
116 {
117    if (!platformDialog)
118    {
119        platformDialog = new PlatformDialog(this);
120        connect(platformDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
121    }
122    activateDialog(platformDialog);
123 }
124
125 void MainWindow::configureMediaTypes()
126 {
127     if (!mediaTypeDialog)
128     {
129         mediaTypeDialog = new MediaTypeDialog(this);
130         connect(mediaTypeDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
131    }
132    activateDialog(mediaTypeDialog);
133 }
134
135 void MainWindow::configureMediaImagePaths()
136 {
137     if (!mediaImagePathDialog)
138     {
139         mediaImagePathDialog = new MediaImagePathMainDialog(this);
140     }
141     activateDialog(mediaImagePathDialog);
142 }
143
144 void MainWindow::configureSetups()
145 {
146     if (!setupMainDialog)
147     {
148         qDebug() << "MainWindow: Creating a setup main dialog.";
149         setupMainDialog = new SetupMainDialog(this);
150     }
151     activateDialog(setupMainDialog);
152     setupMainDialog->refreshDataModel();
153 }
154
155 void MainWindow::configureEmulators()
156 {
157     if (!executableMainDialog) {
158         executableMainDialog = new ExecutableMainDialog(this);
159         connect(executableMainDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
160     }
161     activateDialog(executableMainDialog);
162     executableMainDialog->refreshDataModel();
163 }
164
165 void MainWindow::configureTmpDir()
166 {
167     /*if (!tmpFolderDialog) {
168         tmpFolderDialog = new TmpFolderEditDialog(this, tmpDirFilePath);
169     }
170     activateDialog(tmpFolderDialog);*/
171
172     QString fpath = QFileDialog::getExistingDirectory(this,
173         tr("Select a directory"), tmpDirFilePath,
174         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
175     QDir d(fpath);
176     if (d.exists() && d.isReadable()) {
177         tmpDirFilePath = fpath;
178         DbConfig::setTmpDir(tmpDirFilePath);
179         launcher->setTmpDirPath(tmpDirFilePath);
180     }
181 }
182
183 void MainWindow::resetDb()
184 {
185     if (QMessageBox::question(this, "Reset database?",
186         "Are you REALLY SURE you want to do this? "
187         "All the current data WILL BE LOST!",
188         QMessageBox::No,
189         QMessageBox::Yes) == QMessageBox::No) {
190         return;
191     }
192     try {
193         createDB();
194     }
195     catch (EmuFrontException e) {
196         qDebug() << e.what();
197         QMessageBox::critical(this, "Exception", 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         QMessageBox::critical(this, "Exception", e.what());
306         return false;
307     }
308 }
309
310 /* Throws EmuFrontException */
311 void MainWindow::createDB() const
312 {
313     try
314     {
315         DbCreator dbCreator;
316         dbCreator.createDB();
317     }
318     catch (QString str) {
319         QString msg(tr("Exception while trying to create"
320                        " EmuFront database: %s").arg(str));
321         throw EmuFrontException(msg);
322     }
323 }
324