Modified the license text comment type.
[emufront] / src / db / dbfilepath.cpp
index cfd89f0..eee69d6 100644 (file)
-// 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/>.
-
+/*
+** 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 version 2 as published by
+** the Free Software Foundation and appearing in the file gpl.txt included in the
+** packaging of this file.
+**
+** EmuFront 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+*/
 #include <QSqlRelationalTableModel>
 #include <QSqlRecord>
-#include "../dataobjects/filepathobject.h"
+#include <QSqlQuery>
+#include "filepathobject.h"
 #include "dbfilepath.h"
-#include "dbplatform.h"
-#include "dbmediatype.h"
+#include "dbsetup.h"
 
-DbFilePath::DbFilePath(QObject *parent) : DatabaseManager(parent)
-{
-    dbPlatform = new DbPlatform(this);
-    dbMediaType = new DbMediaType(this);
-    sqlTableModel = getData();
-}
 
-QSqlTableModel* DbFilePath::getDataModel()
+DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
 {
-    return sqlTableModel;
+    tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
+    dbSetup = new DbSetup(this);
 }
 
-EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec) const
+/* Throws EmuFrontException */
+EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
 {
     int id = rec->value(FilePath_Id).toInt();
     QString fpath = rec->value(FilePath_Name).toString();
-    int plfId = rec->value(FilePath_PlatformId).toInt();
-    int mtId = rec->value(FilePath_MediaTypeId).toInt();
+    int setupId = rec->value(FilePath_SetupId).toInt();
     int fileType = rec->value(FilePath_FileTypeId).toInt();
-    Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
-    MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
+    Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId)); /* Throws EmuFrontException */
        // TODO
     //int lastScanned = 0;
-    return new FilePathObject(id, fpath, fpath, fileType, plf, mt);
+    return new FilePathObject(id, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
 {
     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
     bool ret = false;
-    sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
-    sqlTableModel->select();
-    if (sqlTableModel->rowCount() == 1)
-    {
-        QSqlRecord rec = sqlTableModel->record(0);
-        rec.setValue("name", fpo->getName());
-        rec.setValue("filetypeid", fpo->getFiletype());
-
-        Platform *pl = fpo->getPlatform();
-        MediaType *mt = fpo->getMediaType();
-        if (pl) rec.setValue("platformid", pl->getId());
-        if (mt) rec.setValue("mediatypeid", mt->getId());
+    QSqlQuery query;
+    query.prepare(QString("UPDATE filepath SET "
+        "name=:name, "
+        "filetypeid=:filetypeid, "
+        "setupid=:setupid, "
+        "lastscanned=:lastscanned "
+        "WHERE id=:id"));
+    query.bindValue(":name", fpo->getName());
+    query.bindValue(":filetypeid", fpo->getType());
+    query.bindValue(":lastscanned", 0); // TODO
+    query.bindValue(":id", fpo->getId());
+    ret = query.exec();
+    if (ret) resetModel();
+    return ret;
+}
 
-        // TODO
-        //rec.setValue("lastscanned", 0);
-    }
+bool DbFilePath::setScanned(const EmuFrontObject *ob)
+{
+    QSqlQuery q;
+    q.prepare("UPDATE filepath SET lastscanned=:lastscanned WHERE id=:id");
+    q.bindValue(":lastscanned", getCurrentTimeStamp());
+    q.bindValue(":id", ob->getId());
+    bool ret = q.exec();
+    if (ret) resetModel();
     return ret;
 }
 
-bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
+/* Returns id of inserted data item after succesful insert, -1 if insert failed */
+int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
-    int row = 0;
-    sqlTableModel->insertRows(row, 1);
+    QSqlQuery query;
+    query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
+        "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
+    query.bindValue(":name", fpo->getName());
+    query.bindValue(":filetypeid", fpo->getType());
+    if (fpo->getSetup())
+        query.bindValue(":setupid", fpo->getSetup()->getId());
+    query.bindValue(":lastscanned", 0); // TODO
+    int id = -1;
+    if (query.exec())
+        id = query.lastInsertId().toInt();
+    return id;
+}
 
-    Platform *pl = fpo->getPlatform();
-    MediaType *mt = fpo->getMediaType();
+// WARNING: this will delete also all the databindings to selected media image path
+bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
+{
+    return false;
+}
 
-    //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
-    sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
-    sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
-    // not all the file path types have platform and/or media type
-    if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
-    if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
-    // TODO:
-    sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
-    return sqlTableModel->submitAll();
+bool DbFilePath::deleteDataObject(int id) const
+{
+    if (countDataObjectRefs(id) > 0)
+        // TODO
+        return false;
+    QSqlQuery q;
+    q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
+    q.bindValue(":id", id);
+    return q.exec();
 }
 
-int DbFilePath::countDataObjectRefs(int id) const
+QString DbFilePath::constructSelect(QString where) const
 {
-    return 0;
+    return QString("SELECT "
+            "filepath.id AS FilePathId, "
+            "filepath.name AS Name, "
+            "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
+            "setup.id AS SetupId, "
+            "platform.name || ' ' || mediatype.name AS SetupName, "
+            "filepath.filetypeid "
+            "FROM filepath "
+            "INNER JOIN setup ON filepath.setupid=setup.id  "
+            "INNER JOIN platform ON setup.platformid=platform.id "
+            "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
+            "%1 "
+            "ORDER BY SetupName").arg(where);
 }
 
-// WARNING: this will delete also all the databindings to selected media image path
-bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
+QString DbFilePath::constructFilterById(int id) const
 {
-    return false;
+    return QString("filepath.id = %1").arg(id);
+}
+
+QString DbFilePath::constructSelectById(int id) const
+{
+    QString where = QString("WHERE %1").arg(constructFilterById(id));
+    return constructSelect(where);
+}
+
+QSqlQueryModel* DbFilePath::getData()
+{
+    QSqlQueryModel *model = new QSqlQueryModel(this);
+    model->setQuery(constructSelect());
+    model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
+    model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
+    model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
+    model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
+    model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
+    return model;
 }
 
-QSqlTableModel* DbFilePath::getData()
+QString DbFilePath::getCountRefsSelect(int id) const
 {
-   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;
+    /* filepath is referenced from mediaimagecontainer */
+    return QString("SELECT count(*) FROM filepath"
+              "INNER JOIN mediaimagecontainer "
+              "ON filepath.id=mediaimagecontainer.filepathid"
+              "WHERE filepath.id=%1").arg(id);
 }