3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
21 #include <QSqlRelationalTableModel>
24 #include "filepathobject.h"
25 #include "dbfilepath.h"
29 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
31 tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
32 dbSetup = new DbSetup(this);
35 /* Throws EmuFrontException */
36 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
38 int id = rec->value(FilePath_Id).toInt();
39 QString fpath = rec->value(FilePath_Name).toString();
40 int setupId = rec->value(FilePath_SetupId).toInt();
41 int fileType = rec->value(FilePath_FileTypeId).toInt();
42 Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId)); /* Throws EmuFrontException */
44 //int lastScanned = 0;
45 return new FilePathObject(id, fpath, fileType, sup);
48 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
50 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
53 query.prepare(QString("UPDATE filepath SET "
55 "filetypeid=:filetypeid, "
57 "lastscanned=:lastscanned "
59 query.bindValue(":name", fpo->getName());
60 query.bindValue(":filetypeid", fpo->getType());
61 query.bindValue(":lastscanned", 0); // TODO
62 query.bindValue(":id", fpo->getId());
64 if (ret) resetModel();
68 bool DbFilePath::setScanned(const EmuFrontObject *ob)
71 q.prepare("UPDATE filepath SET lastscanned=:lastscanned WHERE id=:id");
72 q.bindValue(":lastscanned", getCurrentTimeStamp());
73 q.bindValue(":id", ob->getId());
75 if (ret) resetModel();
79 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
80 int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
82 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
84 query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
85 "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
86 query.bindValue(":name", fpo->getName());
87 query.bindValue(":filetypeid", fpo->getType());
89 query.bindValue(":setupid", fpo->getSetup()->getId());
90 query.bindValue(":lastscanned", 0); // TODO
93 id = query.lastInsertId().toInt();
97 // WARNING: this will delete also all the databindings to selected media image path
98 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
103 bool DbFilePath::deleteDataObject(int id) const
105 if (countDataObjectRefs(id) > 0)
109 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
110 q.bindValue(":id", id);
114 QString DbFilePath::constructSelect(QString where) const
116 return QString("SELECT "
117 "filepath.id AS FilePathId, "
118 "filepath.name AS Name, "
119 "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
120 "setup.id AS SetupId, "
121 "platform.name || ' ' || mediatype.name AS SetupName, "
122 "filepath.filetypeid "
124 "INNER JOIN setup ON filepath.setupid=setup.id "
125 "INNER JOIN platform ON setup.platformid=platform.id "
126 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
128 "ORDER BY SetupName").arg(where);
131 QString DbFilePath::constructFilterById(int id) const
133 return QString("filepath.id = %1").arg(id);
136 QString DbFilePath::constructSelectById(int id) const
138 QString where = QString("WHERE %1").arg(constructFilterById(id));
139 return constructSelect(where);
142 QSqlQueryModel* DbFilePath::getData()
144 QSqlQueryModel *model = new QSqlQueryModel(this);
145 model->setQuery(constructSelect());
146 model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
147 model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
148 model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
149 model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
150 model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
154 QString DbFilePath::getCountRefsSelect(int id) const
156 /* filepath is referenced from mediaimagecontainer */
157 return QString("SELECT count(*) FROM filepath"
158 "INNER JOIN mediaimagecontainer "
159 "ON filepath.id=mediaimagecontainer.filepathid"
160 "WHERE filepath.id=%1").arg(id);