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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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"
27 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
29 dbSetup = new DbSetup(this);
32 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
34 int id = rec->value(FilePath_Id).toInt();
35 QString fpath = rec->value(FilePath_Name).toString();
36 int setupId = rec->value(FilePath_SetupId).toInt();
37 //int fileType = rec->value(FilePath_FileTypeId).toInt();
38 Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId));
40 //int lastScanned = 0;
41 return new FilePathObject(id, fpath, /* TODO */ 0, sup);
44 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
46 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
49 query.prepare(QString("UPDATE filepath SET "
51 "filetypeid=:filetypeid, "
53 "lastscanned=:lastscanned "
55 query.bindValue(":name", fpo->getName());
56 query.bindValue(":filetypeid", fpo->getType());
57 query.bindValue(":lastscanned", 0); // TODO
58 query.bindValue(":id", fpo->getId());
60 if (ret) resetModel();
64 int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
66 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
68 query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
69 "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
70 query.bindValue(":name", fpo->getName());
71 query.bindValue(":filetypeid", fpo->getType());
73 query.bindValue(":setupid", fpo->getSetup()->getId());
74 query.bindValue(":lastscanned", 0); // TODO
77 id = query.lastInsertId().toInt();
81 int DbFilePath::countDataObjectRefs(int id) const
86 // WARNING: this will delete also all the databindings to selected media image path
87 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
92 QString DbFilePath::constructSelect(QString whereClause) const
94 QString where = whereClause.isEmpty()
95 ? "" : QString("WHERE ").append(whereClause);
97 return QString("SELECT filepath.id AS FilePathId, "
98 "filepath.name AS Name, "
99 "filepath.lastscanned AS LastScanned, "
100 "setup.id AS SetupId, "
101 "platform.name || ' ' || mediatype.name AS SetupName "
103 "INNER JOIN setup ON filepath.setupid=setup.id "
104 "INNER JOIN platform ON setup.platformid=platform.id "
105 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
107 "ORDER BY SetupName").arg(where);
110 QString DbFilePath::constructFilterById(int id) const
112 return QString("filepath.id = %1").arg(id);
115 QString DbFilePath::constructSelectById(int id) const
117 return constructSelect(constructFilterById(id));
120 QSqlQueryModel* DbFilePath::getData()
122 QSqlQueryModel *model = new QSqlQueryModel;
123 model->setQuery(constructSelect());
124 model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
125 model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
126 model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
127 model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
128 model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));