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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
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 tableName = DbSetup::DB_TABLE_NAME_SETUP;
33 dbPlatform = new DbPlatform(this);
34 dbMediaType = new DbMediaType(this);
37 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
41 int id = rec->value(Setup_Id).toInt();
42 QString extensions = rec->value(Setup_FileTypeExtensions).toString();
43 QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
44 int plfId = rec->value(Setup_PlatformId).toInt();
45 int mtId = rec->value(Setup_MediaTypeId).toInt();
46 Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
47 MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
48 s = new Setup(id, plf, mt, list);
52 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
54 const Setup *fpo = dynamic_cast<const Setup*>(ob);
57 query.prepare(QString("UPDATE setup SET "
58 "platformid=:platformid, "
59 "mediatypeid=:mediatypeid, "
60 "filetypeextensions=:filetypeextensions "
62 if (fpo->getPlatform())
63 query.bindValue(":platformid", fpo->getPlatform()->getId());
64 if (fpo->getMediaType())
65 query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
66 query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
67 query.bindValue(":id", fpo->getId());
69 if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
73 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
75 qDebug() << "Inserting setup to database...";
76 const Setup *fpo = dynamic_cast<const Setup*>(ob);
78 query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
79 "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
80 int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
81 int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
82 QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
83 qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
84 query.bindValue(":platformid", plfId);
85 query.bindValue(":mediatypeid", mtId);
86 query.bindValue(":filetypeextensions", exts);
89 id = query.lastInsertId().toInt();
91 qDebug() << query.lastError().text() << query.executedQuery();
95 QString DbSetup::constructSelect(QString where) const
98 "SELECT setup.id AS SetupId, "
99 "setup.platformid AS PlatformId, "
100 "setup.mediatypeid AS MediaTypeId, "
101 "setup.filetypeextensions AS SupportedFileTypeExtensions, "
102 "platform.name || ' ' || mediatype.name AS SetupName "
104 "INNER JOIN platform ON setup.platformid=platform.id "
105 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
110 QString DbSetup::constructFilterById(int id) const
112 return QString("setup.id = %1").arg(id);
116 QString DbSetup::constructSelectById(int id) const
118 return constructSelect(
119 QString("WHERE %1").arg(constructFilterById(id)));
122 // WARNING: this will delete also all the databindings to selected media image path
123 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
125 qDebug() << "This is not currently supported";
129 QSqlQueryModel* DbSetup::getData()
131 QSqlQueryModel *model = new QSqlQueryModel(this);
132 QString select = constructSelect();
134 model->setQuery(select);
135 model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
136 model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
137 model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
138 model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
139 model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
143 QString DbSetup::getCountRefsSelect(int id) const
145 /* setups are referenced by executable and filepath */
146 return QString("SELECT count(*) FROM "
147 "(SELECT setup.id FROM setup "
148 "INNER JOIN executable ON setup.id=executable.setupid "
151 "SELECT setup.id FROM setup "
152 "INNER JOIN filepath ON setup.id=filepath.setupid "
153 "WHERE setup.id=%1)").arg(id);