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 int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
69 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
71 query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
72 "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
73 query.bindValue(":name", fpo->getName());
74 query.bindValue(":filetypeid", fpo->getType());
76 query.bindValue(":setupid", fpo->getSetup()->getId());
77 query.bindValue(":lastscanned", 0); // TODO
80 id = query.lastInsertId().toInt();
84 // WARNING: this will delete also all the databindings to selected media image path
85 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
90 bool DbFilePath::deleteDataObject(int id) const
92 if (countDataObjectRefs(id) > 0)
96 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
97 q.bindValue(":id", id);
101 QString DbFilePath::constructSelect(QString where) const
103 return QString("SELECT filepath.id AS FilePathId, "
104 "filepath.name AS Name, "
105 "filepath.lastscanned AS LastScanned, "
106 "setup.id AS SetupId, "
107 "platform.name || ' ' || mediatype.name AS SetupName, "
108 "filepath.filetypeid "
110 "INNER JOIN setup ON filepath.setupid=setup.id "
111 "INNER JOIN platform ON setup.platformid=platform.id "
112 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
114 "ORDER BY SetupName").arg(where);
117 QString DbFilePath::constructFilterById(int id) const
119 return QString("filepath.id = %1").arg(id);
122 QString DbFilePath::constructSelectById(int id) const
124 QString where = QString("WHERE %1").arg(constructFilterById(id));
125 return constructSelect(where);
128 QSqlQueryModel* DbFilePath::getData()
130 QSqlQueryModel *model = new QSqlQueryModel(this);
131 model->setQuery(constructSelect());
132 model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
133 model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
134 model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
135 model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
136 model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
140 QString DbFilePath::getCountRefsSelect(int id) const
142 /* filepath is referenced from mediaimagecontainer */
143 return QString("SELECT count(*) FROM filepath"
144 "INNER JOIN mediaimagecontainer "
145 "ON filepath.id=mediaimagecontainer.filepathid"
146 "WHERE filepath.id=%1").arg(id);