Implemented initial exception handling
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sat, 12 Jun 2010 14:20:17 +0000 (17:20 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sat, 12 Jun 2010 14:20:17 +0000 (17:20 +0300)
src/db/databasemanager.cpp
src/db/databasemanager.h
src/db/dbsetup.cpp
src/db/dbtablemodelmanager.cpp
src/dialogs/dbobjectdialog.cpp
src/dialogs/emufrontdialog.h
src/emufront.pro
src/exceptions/emufrontexception.cpp [new file with mode: 0644]
src/exceptions/emufrontexception.h [new file with mode: 0644]

index eda8961..899f28e 100644 (file)
@@ -27,6 +27,7 @@
 #include <QFile>
 #include <QDir>
 #include <QVariant>
+#include <QDebug>
 
 const QString DatabaseManager::DB_FILENAME = QString("my.db.sqlite");
 const QString DatabaseManager::DATABASE = QString("QSQLITE");
@@ -91,18 +92,24 @@ int DatabaseManager::countRows(QString tableName, QString columnName, int id) co
 
 EmuFrontObject* DatabaseManager::getDataObject(int id)
 {
+    qDebug() << "DatabaseManager::getDataObject, id " << id;
     filterById(id);
     EmuFrontObject *plf = 0;
     if (sqlTableModel->rowCount() == 1)
     {
         QSqlRecord record = sqlTableModel->record(0);
-        plf = recordToDataObject(&record);
+        if (record.isEmpty())
+        {
+            qDebug() << "No record";
+        }
+        else plf = recordToDataObject(&record);
     }
     return plf;
 }
 
 EmuFrontObject* DatabaseManager::getDataObjectFromModel(QModelIndex *index)
 {
+    qDebug() << "Data-object from row " <<  index->row() << " requested.";
     QSqlRecord record = sqlTableModel->record(index->row());
     return recordToDataObject(&record);
 }
index 858d1a9..26521b7 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <QObject>
 #include <QSqlDatabase>
+#include "../exceptions/emufrontexception.h"
 
 class QSqlError;
 class QFile;
index c8ba9c1..4550843 100644 (file)
@@ -38,14 +38,23 @@ DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
 
 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec) const
 {
+    Setup *s = 0;
+    if (!rec) return s;
     int id = rec->value(Setup_Id).toInt();
+    qDebug() << "Creating a Setup object with id " << id;
     QString extensions = rec->value(Setup_FileTypeExtensions).toString();
+    qDebug() << "Supported file types" << extensions;
     QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
     int plfId = rec->value(Setup_PlatformId).toInt();
+    qDebug() <<  "Platform id " << plfId;
     int mtId = rec->value(Setup_MediaTypeId).toInt();
+    qDebug() <<  "Media type id " << mtId;
     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
+    qDebug() << "Platform " << plf->getName();
     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
-    return new Setup(id, plf, mt, list);
+    qDebug() << "Media type " << mt->getName();
+    s = new Setup(id, plf, mt, list);
+    return s;
 }
 
 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
@@ -133,7 +142,7 @@ QString DbSetup::constructSelect(QString whereClause) const
         "setup.platformid AS PlatformId, "
         "setup.mediatypeid AS MediaTypeId, "
         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
-        "platform.name || ' ' || mediatype.name AS SetupName, "
+        "platform.name || ' ' || mediatype.name AS SetupName "
         "FROM setup %1"
         "INNER JOIN platform ON setup.platformid=platform.id "
         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
@@ -155,25 +164,15 @@ bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
 QSqlQueryModel* DbSetup::getData()
 {
     QSqlQueryModel *model = new QSqlQueryModel;
-    model->setQuery(constructSelect());
+    QString select = constructSelect();
+    qDebug() << select;
+    model->setQuery(select);
     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
     return model;
-
-   /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
-   model->setTable(DB_TABLE_NAME_FILEPATH);
-   model->setRelation(FilePath_PlatformId,
-       QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
-   model->setRelation(FilePath_MediaTypeId,
-       QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
-   model->setSort(FilePath_Name, Qt::AscendingOrder);
-   model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
-   model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
-   model->select();
-   return model;*/
 }
 
 /*void DbMediaType::filterById(int id)
index 4245459..ed67e53 100644 (file)
@@ -27,6 +27,7 @@ DbTableModelManager::DbTableModelManager(QObject *parent)
 
 void DbTableModelManager::filterById(int id)
 {
+    if (!sqlTableModel) throw EmuFrontException("Data model not available!");
     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
     tmodel->setFilter(QString("id = %1").arg(id));
     tmodel->select();
index 7a6e021..72b2e08 100644 (file)
@@ -104,23 +104,39 @@ bool DbObjectDialog::deleteItem()
     QModelIndex index = objectList->currentIndex();
     if (!index.isValid()) return false;
 
-    EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
-    if (!ob) return false;
+    qDebug() << "DbObjectDialog going to delete item at row " << index.row();
 
-    int numBindings = dbManager->countDataObjectRefs(ob->getId());
-    if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
+    try
     {
+        EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
+
+        if (!ob) return false;
+        qDebug() << "DbObjectDialog going to delete item" << ob->getName();
+
+        int numBindings = dbManager->countDataObjectRefs(ob->getId());
+
+        qDebug() << "Got " << numBindings << " bindings.";
+
+        if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
+        { return false; }
+        deleteCurrentObject();
+
+        qDebug() << "Deleted object from memory, going to delete from db.";
+
+        bool delOk = dbManager->deleteDataObjectFromModel(&index);
+        if (!delOk)
+        {
+            qDebug() << "delete failed";
             return false;
+        }
+        qDebug() << "Object deleted from database";
+        updateList();
+        objectList->setFocus();
     }
-    deleteCurrentObject();
-    bool delOk = dbManager->deleteDataObjectFromModel(&index);
-    if (!delOk)
+    catch(EmuFrontException e)
     {
-        qDebug() << "delete failed";
-        return false;
+        errorMessage->showMessage(e.what());
     }
-    updateList();
-    objectList->setFocus();
     return false;
 }
 
index dc34d3a..9622285 100644 (file)
@@ -20,6 +20,7 @@
 #ifndef EMUFRONTDIALOG_H
 #define EMUFRONTDIALOG_H
 #include <QDialog>
+#include "../exceptions/emufrontexception.h"
 
 class QErrorMessage;
 
index b98ff4e..790506e 100644 (file)
@@ -42,7 +42,8 @@ HEADERS += mainwindow.h \
     db/dbquerymodelmanager.h \
     dialogs/setupmaindialog.h \
     dialogs/setupeditdialog.h \
-    widgets/stringlistwidget.h
+    widgets/stringlistwidget.h \
+    exceptions/emufrontexception.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -74,6 +75,6 @@ SOURCES += main.cpp \
     db/dbquerymodelmanager.cpp \
     dialogs/setupmaindialog.cpp \
     dialogs/setupeditdialog.cpp \
-    widgets/stringlistwidget.cpp
-
-OTHER_FILES +=
+    widgets/stringlistwidget.cpp \
+    exceptions/emufrontexception.cpp
+OTHER_FILES += 
diff --git a/src/exceptions/emufrontexception.cpp b/src/exceptions/emufrontexception.cpp
new file mode 100644 (file)
index 0000000..15180be
--- /dev/null
@@ -0,0 +1,29 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "emufrontexception.h"
+
+EmuFrontException::EmuFrontException(QString msg) : msg(msg)
+{ }
+
+const QString EmuFrontException::what() const
+{ return msg; }
+
+void EmuFrontException::addInfo(QString str)
+{ msg.append(str); }
diff --git a/src/exceptions/emufrontexception.h b/src/exceptions/emufrontexception.h
new file mode 100644 (file)
index 0000000..71aec25
--- /dev/null
@@ -0,0 +1,36 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef EMUFRONTEXCEPTION_H
+#define EMUFRONTEXCEPTION_H
+
+#include <QString>
+
+class EmuFrontException
+{
+public:
+    EmuFrontException(QString str);
+    const QString what() const;
+    void addInfo(QString);
+
+private:
+    QString msg;
+};
+
+#endif // EMUFRONTEXCEPTION_H