Modified the license text comment type.
[emufront] / src / db / dbfilepath.cpp
index a86da93..eee69d6 100644 (file)
@@ -1,44 +1,48 @@
-// 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.
-//
-// 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/>.
-
+/*
+** 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 <QSqlQuery>
-#include "../dataobjects/filepathobject.h"
+#include "filepathobject.h"
 #include "dbfilepath.h"
 #include "dbsetup.h"
 
+
 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
 {
+    tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
     dbSetup = new DbSetup(this);
 }
 
+/* Throws EmuFrontException */
 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
 {
     int id = rec->value(FilePath_Id).toInt();
     QString fpath = rec->value(FilePath_Name).toString();
     int setupId = rec->value(FilePath_SetupId).toInt();
-    //int fileType = rec->value(FilePath_FileTypeId).toInt();
-    Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId));
+    int fileType = rec->value(FilePath_FileTypeId).toInt();
+    Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId)); /* Throws EmuFrontException */
        // TODO
     //int lastScanned = 0;
-    return new FilePathObject(id, fpath, /* TODO */ 0, sup);
+    return new FilePathObject(id, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
@@ -61,6 +65,18 @@ bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
     return ret;
 }
 
+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;
+}
+
+/* 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);
@@ -78,27 +94,32 @@ int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
     return id;
 }
 
-int DbFilePath::countDataObjectRefs(int id) const
-{
-    return 0;
-}
-
 // WARNING: this will delete also all the databindings to selected media image path
 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
 {
     return false;
 }
 
-QString DbFilePath::constructSelect(QString whereClause) const
+bool DbFilePath::deleteDataObject(int id) const
 {
-    QString where = whereClause.isEmpty()
-        ? "" : QString("WHERE ").append(whereClause);
+    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();
+}
 
-    return QString("SELECT filepath.id AS FilePathId, "
+QString DbFilePath::constructSelect(QString where) const
+{
+    return QString("SELECT "
+            "filepath.id AS FilePathId, "
             "filepath.name AS Name, "
-            "filepath.lastscanned AS LastScanned, "
+            "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
             "setup.id AS SetupId, "
-            "platform.name || ' ' || mediatype.name AS SetupName "
+            "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 "
@@ -114,12 +135,13 @@ QString DbFilePath::constructFilterById(int id) const
 
 QString DbFilePath::constructSelectById(int id) const
 {
-    return constructSelect(constructFilterById(id));
+    QString where = QString("WHERE %1").arg(constructFilterById(id));
+    return constructSelect(where);
 }
 
 QSqlQueryModel* DbFilePath::getData()
 {
-    QSqlQueryModel *model = new QSqlQueryModel;
+    QSqlQueryModel *model = new QSqlQueryModel(this);
     model->setQuery(constructSelect());
     model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
     model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
@@ -128,3 +150,12 @@ QSqlQueryModel* DbFilePath::getData()
     model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
     return model;
 }
+
+QString DbFilePath::getCountRefsSelect(int id) const
+{
+    /* 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);
+}