2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
20 #include <QSqlRelationalTableModel>
23 #include "../dataobjects/filepathobject.h"
24 #include "dbfilepath.h"
28 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
30 tableName = DbFilePath::DB_TABLE_NAME_FILEPATH;
31 dbSetup = new DbSetup(this);
34 /* Throws EmuFrontException */
35 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
37 int id = rec->value(FilePath_Id).toInt();
38 QString fpath = rec->value(FilePath_Name).toString();
39 int setupId = rec->value(FilePath_SetupId).toInt();
40 int fileType = rec->value(FilePath_FileTypeId).toInt();
41 Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId)); /* Throws EmuFrontException */
43 //int lastScanned = 0;
44 return new FilePathObject(id, fpath, fileType, sup);
47 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
49 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
52 query.prepare(QString("UPDATE filepath SET "
54 "filetypeid=:filetypeid, "
56 "lastscanned=:lastscanned "
58 query.bindValue(":name", fpo->getName());
59 query.bindValue(":filetypeid", fpo->getType());
60 query.bindValue(":lastscanned", 0); // TODO
61 query.bindValue(":id", fpo->getId());
63 if (ret) resetModel();
67 bool DbFilePath::setScanned(const EmuFrontObject *ob)
70 q.prepare("UPDATE filepath SET lastscanned=:lastscanned WHERE id=:id");
71 q.bindValue(":lastscanned", getCurrentTimeStamp());
72 q.bindValue(":id", ob->getId());
74 if (ret) resetModel();
78 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
79 int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
81 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
83 query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
84 "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
85 query.bindValue(":name", fpo->getName());
86 query.bindValue(":filetypeid", fpo->getType());
88 query.bindValue(":setupid", fpo->getSetup()->getId());
89 query.bindValue(":lastscanned", 0); // TODO
92 id = query.lastInsertId().toInt();
96 // WARNING: this will delete also all the databindings to selected media image path
97 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
102 bool DbFilePath::deleteDataObject(int id) const
104 if (countDataObjectRefs(id) > 0)
108 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
109 q.bindValue(":id", id);
113 QString DbFilePath::constructSelect(QString where) const
115 return QString("SELECT filepath.id AS FilePathId, "
116 "filepath.name AS Name, "
117 "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
118 "setup.id AS SetupId, "
119 "platform.name || ' ' || mediatype.name AS SetupName, "
120 "filepath.filetypeid "
122 "INNER JOIN setup ON filepath.setupid=setup.id "
123 "INNER JOIN platform ON setup.platformid=platform.id "
124 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
126 "ORDER BY SetupName").arg(where);
129 QString DbFilePath::constructFilterById(int id) const
131 return QString("filepath.id = %1").arg(id);
134 QString DbFilePath::constructSelectById(int id) const
136 QString where = QString("WHERE %1").arg(constructFilterById(id));
137 return constructSelect(where);
140 QSqlQueryModel* DbFilePath::getData()
142 QSqlQueryModel *model = new QSqlQueryModel(this);
143 model->setQuery(constructSelect());
144 model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
145 model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
146 model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
147 model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
148 model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
152 QString DbFilePath::getCountRefsSelect(int id) const
154 /* filepath is referenced from mediaimagecontainer */
155 return QString("SELECT count(*) FROM filepath"
156 "INNER JOIN mediaimagecontainer "
157 "ON filepath.id=mediaimagecontainer.filepathid"
158 "WHERE filepath.id=%1").arg(id);