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();
61 /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
62 sqlTableModel->select();
63 if (sqlTableModel->rowCount() == 1)
65 QSqlRecord rec = sqlTableModel->record(0);
66 rec.setValue("name", fpo->getName());
67 rec.setValue("filetypeid", fpo->getFilet.bype());
69 Setup *sup = fpo->getSetup();
70 if (sup) rec.setValue("setupid", sup->getId());
73 //rec.setValue("lastscanned", 0);
78 bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
80 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
82 query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
83 "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
84 query.bindValue(":name", fpo->getName());
85 query.bindValue(":filetypeid", fpo->getType());
87 query.bindValue(":setupid", fpo->getSetup()->getId());
88 query.bindValue(":lastscanned", 0); // TODO
92 sqlTableModel->insertRows(row, 1);
95 Setup *sup = fpo->getSetup();
96 //Platform *pl = fpo->getPlatform();
97 //MediaType *mt = fpo->getMediaType();
99 //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
100 sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
101 sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
102 // not all the file path types have platform and/or media type
103 //if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
104 //if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
105 if (sup) sqlTableModel->setData(sqlTableModel->index(row, FilePath_SetupId), sup->getId());
107 sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
108 return sqlTableModel->submitAll();*/
111 int DbFilePath::countDataObjectRefs(int id) const
116 // WARNING: this will delete also all the databindings to selected media image path
117 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
122 QString DbFilePath::constructSelect(QString whereClause) const
124 QString where = whereClause.isEmpty()
125 ? "" : QString("WHERE ").append(whereClause);
127 return QString("SELECT filepath.id AS FilePathId, "
128 "filepath.name AS Name, "
129 "filepath.lastscanned AS LastScanned, "
130 "setup.id AS SetupId, "
131 "platform.name || ' ' || mediatype.name AS SetupName "
133 "INNER JOIN setup ON filepath.setupid=setup.id "
134 "INNER JOIN platform ON setup.platformid=platform.id "
135 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
137 "ORDER BY SetupName").arg(where);
140 QString DbFilePath::constructSelectById(int id) const
142 return constructSelect(QString("filepath.id = %1").arg(id));
145 QSqlQueryModel* DbFilePath::getData()
147 QSqlQueryModel *model = new QSqlQueryModel;
148 model->setQuery(constructSelect());
149 model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
150 model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
151 model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
152 model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
153 model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
156 //"platform.name, mediatype.name
157 /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
158 model->setTable(DB_TABLE_NAME_FILEPATH);*/
159 /*model->setRelation(FilePath_PlatformId,
160 QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
161 model->setRelation(FilePath_MediaTypeId,
162 QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"e));*/
163 /*model->setRelation(FilePath_SetupId,
164 QSqlRelation(DB_TABLE_NAME_SETUP, "id", ""));
165 model->setSort(FilePath_Name, Qt::AscendingOrder);*/
167 //model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
168 //model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));