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 // Foobar 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
20 #include <QSqlRelationalTableModel>
22 #include "../dataobjects/filepathobject.h"
23 #include "dbfilepath.h"
24 #include "dbplatform.h"
25 #include "dbmediatype.h"
27 DbFilePath::DbFilePath(QObject *parent) : DatabaseManager(parent)
29 dbPlatform = new DbPlatform(this);
30 dbMediaType = new DbMediaType(this);
31 sqlTableModel = getData();
34 QSqlTableModel* DbFilePath::getDataModel()
39 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec) const
41 int id = rec->value(FilePath_Id).toInt();
42 QString fpath = rec->value(FilePath_Name).toString();
43 int plfId = rec->value(FilePath_PlatformId).toInt();
44 int mtId = rec->value(FilePath_MediaTypeId).toInt();
45 int fileType = rec->value(FilePath_FileTypeId).toInt();
46 Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
47 MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
49 //int lastScanned = 0;
50 return new FilePathObject(id, fpath, fpath, fileType, plf, mt);
53 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
55 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
57 sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
58 sqlTableModel->select();
59 if (sqlTableModel->rowCount() == 1)
61 QSqlRecord rec = sqlTableModel->record(0);
62 rec.setValue("name", fpo->getName());
63 rec.setValue("filetypeid", fpo->getFiletype());
65 Platform *pl = fpo->getPlatform();
66 MediaType *mt = fpo->getMediaType();
67 if (pl) rec.setValue("platformid", pl->getId());
68 if (mt) rec.setValue("mediatypeid", mt->getId());
71 //rec.setValue("lastscanned", 0);
76 bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
78 const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
80 sqlTableModel->insertRows(row, 1);
82 Platform *pl = fpo->getPlatform();
83 MediaType *mt = fpo->getMediaType();
85 //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
86 sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
87 sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
88 // not all the file path types have platform and/or media type
89 if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
90 if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
92 sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
93 return sqlTableModel->submitAll();
96 int DbFilePath::countDataObjectRefs(int id) const
101 // WARNING: this will delete also all the databindings to selected media image path
102 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
107 QSqlTableModel* DbFilePath::getData()
109 QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
110 model->setTable(DB_TABLE_NAME_FILEPATH);
111 model->setRelation(FilePath_PlatformId,
112 QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
113 model->setRelation(FilePath_MediaTypeId,
114 QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
115 model->setSort(FilePath_Name, Qt::AscendingOrder);
116 model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
117 model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));