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 <QStringList>
23 #include <QSqlRelationalTableModel>
25 #include "dbplatform.h"
26 #include "dbmediatype.h"
28 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
30 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
32 dbPlatform = new DbPlatform(this);
33 dbMediaType = new DbMediaType(this);
34 sqlTableModel = 0; //getData();
37 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec) const
39 int id = rec->value(Setup_Id).toInt();
40 QString extensions = rec->value(Setup_FileTypeExtensions).toString();
41 QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
42 int plfId = rec->value(Setup_PlatformId).toInt();
43 int mtId = rec->value(Setup_MediaTypeId).toInt();
44 Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
45 MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
46 return new Setup(id, plf, mt, list);
49 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
51 const Setup *fpo = dynamic_cast<const Setup*>(ob);
54 query.prepare(QString("UPDATE setup SET "
55 "platformid=:platformid, "
56 "mediatypeid=:mediatypeid, "
57 "filetypeextensions:=filetypeextensions "
59 if (fpo->getPlatform())
60 query.bindValue(":platformid", fpo->getPlatform()->getId());
61 if (fpo->getMediaType())
62 query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
63 query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
64 query.bindValue(":id", fpo->getId());
67 /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
68 sqlTableModel->select();
69 if (sqlTableModel->rowCount() == 1)
71 QSqlRecord rec = sqlTableModel->record(0);
72 rec.setValue("filetypeid", fpo->getFiletype());
74 Platform *pl = fpo->getPlatform();
75 MediaType *mt = fpo->getMediaType();
76 if (pl) rec.setValue("platformid", pl->getId());
77 if (mt) rec.setValue("mediatypeid", mt->getId());
79 QStringList list = fpo->getSupportedFileTypeExtensions();
81 rec.setValue("filetypeextensions", list.join(FILE_TYPE_EXTENSION_SEPARATOR));
86 bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
88 const Setup *fpo = dynamic_cast<const Setup*>(ob);
90 query.prepare("INSERT INTO setup (id, platformid, mediatypeid, fileextensions)"
91 "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
92 if (fpo->getPlatform())
93 query.bindValue(":platformid", fpo->getPlatform()->getId());
94 if (fpo->getMediaType())
95 query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
96 query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
100 sqlTableModel->insertRows(row, 1);
102 Platform *pl = fpo->getPlatform();
103 MediaType *mt = fpo->getMediaType();
105 //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
106 // not all the file path types have platform and/or media type
107 if (pl) sqlTableModel->setData(sqlTableModel->index(row, Setup_PlatformId), pl->getId());
108 if (mt) sqlTableModel->setData(sqlTableModel->index(row, Setup_MediaTypeId), mt->getId());
109 QStringList list = fpo->getSupportedFileTypeExtensions();
110 if (list.count() > 0)
111 sqlTableModel->setData(sqlTableModel->index(row, Setup_FileTypeExtensions), list.join(FILE_TYPE_EXTENSION_SEPARATOR));
112 return sqlTableModel->submitAll();*/
115 int DbSetup::countDataObjectRefs(int ) const
120 QString DbSetup::constructSelect(QString whereClause) const
122 QString where = whereClause.isEmpty()
123 ? "" : QString("WHERE ").append(whereClause);
125 "SELECT setup.id AS SetupId, "
126 "setup.platformid AS PlatformId, "
127 "setup.mediatypeid AS MediaTypeId, "
128 "setup.filetypeextensions AS SupportedFileTypeExtensions, "
129 "platform.name || ' ' || mediatype.name AS SetupName, "
131 "INNER JOIN platform ON setup.platformid=platform.id "
132 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
137 QString DbSetup::constructSelectById(int id) const
139 return constructSelect(QString("setup.id = %1").arg(id));
142 // WARNING: this will delete also all the databindings to selected media image path
143 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
148 QSqlQueryModel* DbSetup::getData()
150 QSqlQueryModel *model = new QSqlQueryModel;
151 model->setQuery(constructSelect());
152 model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
153 model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
154 model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
155 model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
156 model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
159 /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
160 model->setTable(DB_TABLE_NAME_FILEPATH);
161 model->setRelation(FilePath_PlatformId,
162 QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
163 model->setRelation(FilePath_MediaTypeId,
164 QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
165 model->setSort(FilePath_Name, Qt::AscendingOrder);
166 model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
167 model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
172 /*void DbMediaType::filterById(int id)
174 sqlTableModel->setQuery(constructSelectById(id));