Exception handling.
[emufront] / src / mainwindow.cpp
index d5d5583..91dd252 100644 (file)
@@ -27,6 +27,7 @@
 #include "dialogs/executablemaindialog.h"
 #include "utils/datfileutil.h"
 #include "db/databasemanager.h"
+#include "db/dbcreator.h"
 #include "db/dbconfig.h"
 
 QString MainWindow::aboutStr = trUtf8(
@@ -40,8 +41,10 @@ QString MainWindow::aboutStr = trUtf8(
 
 QString MainWindow::aboutTitle = tr("About EmuFront");
 
-MainWindow::MainWindow()
+MainWindow::MainWindow(bool reset)
 {
+    if (!testDB(reset)) close();
+    errorMessage = new QErrorMessage(this);
     setWindowTitle("EmuFront");
     tmpDirFilePath = DbConfig::getTmpDir();
     if (tmpDirFilePath.isEmpty())
@@ -101,6 +104,10 @@ void MainWindow::createActions()
     exitAction->setStatusTip(tr("Exit EmuFront"));
     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
 
+    resetDbAction = new QAction( tr("Reset database"), this);
+    resetDbAction->setStatusTip(tr("Deletes all the current data and create a new database."));
+    connect(resetDbAction, SIGNAL(triggered()), this, SLOT(resetDb()));
+
     aboutAction = new QAction(tr("&About"), this);
     aboutAction->setStatusTip(tr("About EmuFront"));
     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
@@ -174,6 +181,23 @@ void MainWindow::configureTmpDir()
     }
 }
 
+void MainWindow::resetDb()
+{
+    if (QMessageBox::question(this, "Reset database?",
+        "Are you REALLY SURE you want to do this? "
+        "All the current data WILL BE LOST!",
+        QMessageBox::No,
+        QMessageBox::Yes) == QMessageBox::No) {
+        return;
+    }
+    try {
+        createDB();
+    }
+    catch (EmuFrontException e) {
+        errorMessage->showMessage(e.what());
+    }
+}
+
 void MainWindow::manageDatFiles()
 {
     DatFileUtil dfu;
@@ -190,6 +214,8 @@ void MainWindow::activateDialog(EmuFrontDialog* dia) const
 void MainWindow::createMenus()
 {
     fileMenu = menuBar()->addMenu(tr("&File"));
+    fileMenu->addAction(resetDbAction);
+    fileMenu->addSeparator();
     fileMenu->addAction(exitAction);
 
     configMenu = menuBar()->addMenu(tr("&Config"));
@@ -243,3 +269,55 @@ void MainWindow::about()
 {
     QMessageBox::about(this, aboutTitle, aboutStr );
 }
+
+bool MainWindow::testDB(bool reset)
+{
+    try {
+        if (DatabaseManager::openDB()) {
+            qDebug() << " Database opened succesfully!";
+        }
+        else {
+            throw EmuFrontException("Database connection failed!");
+        }
+
+        int dbVer = DbCreator::dbExists();
+        if (dbVer == 0) reset = true;
+        if (!reset && dbVer != DbCreator::DB_VERSION) {
+            QString msg("Database is not compatible "
+                        "with current version of EmuFront!"
+                        "Do you want to continue to recreate the database?"
+                        "ALL THE CURRENT DATA WILL BE LOST!!!");
+            if (QMessageBox::question(this, "Database not compatible!", msg,
+                                      QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
+                reset = true;
+            }
+            else throw EmuFrontException("The current database is not compatible!"
+                                         " Cannot continue.");
+        }
+
+        if (reset) {
+            createDB();
+        }
+        return true;
+    }
+    catch (EmuFrontException e) {
+        qDebug() << e.what();
+        errorMessage->showMessage(e.what());
+        return false;
+    }
+}
+
+void MainWindow::createDB() const
+{
+    try
+    {
+        DbCreator dbCreator;
+        dbCreator.createDB();
+    }
+    catch (QString str) {
+        QString msg(tr("Exception while trying to create"
+                       " EmuFront database: %s").arg(str));
+        errorMessage->showMessage(msg);
+    }
+}
+