Started implementing better MVC support: thanks Petro for the idea! ;)
[emufront] / src / mainwindow.cpp
index b21fbd4..eecab39 100644 (file)
 #include "mainwindow.h"
 #include "emulauncher.h"
 #include "dialogs/platformdialog.h"
+#include "dialogs/platformmaindialog.h"
 #include "dialogs/mediatypedialog.h"
 #include "dialogs/mediaimagepathmaindialog.h"
 #include "dialogs/setupmaindialog.h"
 #include "dialogs/executablemaindialog.h"
+#include "utils/datfileutil.h"
 #include "db/databasemanager.h"
+#include "db/dbcreator.h"
 #include "db/dbconfig.h"
 
-MainWindow::MainWindow()
+QString MainWindow::aboutStr = trUtf8(
+        "<h2>EmuFront</h2>"
+        "<p>&copy; 2010 Mikko Keinänen</p>"
+        "<p>mikko.keinanen@gmail.com</p>"
+        "<p>EmuFront is free software: you can redistribute it and/or modify "
+        "it under the terms of the GNU General Public License version 2 as published by "
+        "the Free Software Foundation.</p>"
+);
+
+QString MainWindow::aboutTitle = tr("About EmuFront");
+
+MainWindow::MainWindow(bool reset)
 {
+    if (!testDB(reset)) close();
+    errorMessage = new QErrorMessage(this);
     setWindowTitle("EmuFront");
     tmpDirFilePath = DbConfig::getTmpDir();
     if (tmpDirFilePath.isEmpty())
         tmpDirFilePath = QDir::homePath();
     qDebug() << "Temporary dir is " << tmpDirFilePath;
-    launcher = new EmuLauncher(this, tmpDirFilePath);
+    launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath);
     setCentralWidget(launcher);
     createActions();
     createMenus();
@@ -57,7 +73,12 @@ void MainWindow::createActions()
     configPlatformAction = new QAction(tr("&Platforms"), this);
     configPlatformAction->setStatusTip(tr("Configure platforms"));
     connect(configPlatformAction, SIGNAL(triggered()),
-           this, SLOT(configurePlatforms()));
+        this, SLOT(configurePlatforms()));
+
+    configPlatformsAction = new QAction(tr("&Set Platforms"), this);
+    configPlatformsAction->setStatusTip(tr("Add, edit and delete platforms"));
+    connect(configPlatformsAction, SIGNAL(triggered()),
+        this, SLOT(configurePlatformss()));
 
     configMediaTypeAction = new QAction(tr("&Media Types"), this);
     configMediaTypeAction->setStatusTip(tr("Configure media types"));
@@ -80,11 +101,19 @@ void MainWindow::createActions()
     configTmpDirAction->setStatusTip(tr("Configure directory for temporary files."));
     connect(configTmpDirAction, SIGNAL(triggered()), this, SLOT(configureTmpDir()));
 
+    manageDatFilesAction = new QAction(tr("&Manage dats"), this);
+    manageDatFilesAction->setStatusTip(tr("Read dat files to database."));
+    connect(manageDatFilesAction, SIGNAL(triggered()), this, SLOT(manageDatFiles()));
+
     exitAction = new QAction(tr("&Exit"), this);
     exitAction->setShortcut(tr("Ctrl+Q"));
     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()));
@@ -100,6 +129,13 @@ void MainWindow::configurePlatforms()
    activateDialog(platformDialog);
 }
 
+void MainWindow::configurePlatformss()
+{
+    plfDialog = new PlatformMainDialog(this);
+    connect(plfDialog, SIGNAL(finished(int)), this, SLOT(updateData()));
+    activateDialog(plfDialog);
+}
+
 void MainWindow::configureMediaTypes()
 {
     if (!mediaTypeDialog)
@@ -158,6 +194,29 @@ 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;
+    dfu.open();
+}
+
 void MainWindow::activateDialog(EmuFrontDialog* dia) const
 {
     dia->show();
@@ -168,15 +227,21 @@ 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"));
+    configMenu->addAction(configTmpDirAction);
+    configMenu->addSeparator();
     configMenu->addAction(configPlatformAction);
+    configMenu->addAction(configPlatformsAction);
     configMenu->addAction(configMediaTypeAction);
-    configMenu->addAction(configMediaImagePathAction);
     configMenu->addAction(configSetupAction);
+    configMenu->addAction(configMediaImagePathAction);
     configMenu->addAction(configEmulatorAction);
-    configMenu->addAction(configTmpDirAction);
+    configMenu->addSeparator();
+    configMenu->addAction(manageDatFilesAction);
 
     helpMenu = menuBar()->addMenu(tr("&Help"));
     helpMenu->addAction(aboutAction);
@@ -216,11 +281,57 @@ void MainWindow::updateData()
 
 void MainWindow::about()
 {
-    QMessageBox::about(this, tr("About EmuFront"),
-        "<h2>EmuFront</h2>"
-        "<p>&copy; 2010 Mikko Keinänen</p>"
-        "<p>EmuFront is free software: you can redistribute it and/or modify "
-        "it under the terms of the GNU General Public License version 2 as published by "
-        "the Free Software Foundation.</p>"
-        );
+    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);
+    }
+}
+