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/>.
21 #include <QStringList>
25 #include <QSqlRelationalTableModel>
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);
36 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
40 int id = rec->value(Setup_Id).toInt();
41 QString extensions = rec->value(Setup_FileTypeExtensions).toString();
42 QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
43 int plfId = rec->value(Setup_PlatformId).toInt();
44 int mtId = rec->value(Setup_MediaTypeId).toInt();
45 Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
46 MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
47 s = new Setup(id, plf, mt, list);
51 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
53 const Setup *fpo = dynamic_cast<const Setup*>(ob);
56 query.prepare(QString("UPDATE setup SET "
57 "platformid=:platformid, "
58 "mediatypeid=:mediatypeid, "
59 "filetypeextensions:=filetypeextensions "
61 if (fpo->getPlatform())
62 query.bindValue(":platformid", fpo->getPlatform()->getId());
63 if (fpo->getMediaType())
64 query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
65 query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
66 query.bindValue(":id", fpo->getId());
71 bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
73 qDebug() << "Inserting setup to database...";
74 const Setup *fpo = dynamic_cast<const Setup*>(ob);
76 query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
77 "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
78 int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
79 int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
80 QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
81 qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
82 query.bindValue(":platformid", plfId);
83 query.bindValue(":mediatypeid", mtId);
84 query.bindValue(":filetypeextensions", exts);
85 bool ret = query.exec();
86 if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
90 int DbSetup::countDataObjectRefs(int ) const
95 QString DbSetup::constructSelect(QString whereClause) const
97 QString where = whereClause.isEmpty()
98 ? "" : QString("WHERE ").append(whereClause);
100 "SELECT setup.id AS SetupId, "
101 "setup.platformid AS PlatformId, "
102 "setup.mediatypeid AS MediaTypeId, "
103 "setup.filetypeextensions AS SupportedFileTypeExtensions, "
104 "platform.name || ' ' || mediatype.name AS SetupName "
106 "INNER JOIN platform ON setup.platformid=platform.id "
107 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
112 QString DbSetup::constructSelectById(int id) const
114 return constructSelect(QString("setup.id = %1").arg(id));
117 // WARNING: this will delete also all the databindings to selected media image path
118 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
123 QSqlQueryModel* DbSetup::getData()
125 QSqlQueryModel *model = new QSqlQueryModel;
126 QString select = constructSelect();
128 model->setQuery(select);
129 model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
130 model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
131 model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
132 model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
133 model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));