Modified the license text comment type.
[emufront] / src / db / dbfilepath.cpp
index 7fc52a1..eee69d6 100644 (file)
@@ -1,26 +1,27 @@
-// 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/>.
-
+/*
+** 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"
 
@@ -31,16 +32,17 @@ DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
     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)
@@ -63,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);
@@ -99,11 +113,13 @@ bool DbFilePath::deleteDataObject(int id) const
 
 QString DbFilePath::constructSelect(QString where) const
 {
-    return QString("SELECT filepath.id AS FilePathId, "
+    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 "
@@ -125,7 +141,7 @@ QString DbFilePath::constructSelectById(int id) const
 
 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"));