3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
22 #include <QStringList>
26 #include <QSqlRelationalTableModel>
29 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
31 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
33 tableName = DbSetup::DB_TABLE_NAME_SETUP;
34 dbPlatform = new DbPlatform(this);
35 dbMediaType = new DbMediaType(this);
38 /* Throws EmuFrontException */
39 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
43 int id = rec->value(Setup_Id).toInt();
44 QString extensions = rec->value(Setup_FileTypeExtensions).toString().trimmed();
46 if (!extensions.isEmpty())
47 list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
48 int plfId = rec->value(Setup_PlatformId).toInt();
49 int mtId = rec->value(Setup_MediaTypeId).toInt();
50 Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId)); /* Throws EmuFrontException */
51 MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId)); /* Throws EmuFrontException */
52 s = new Setup(id, plf, mt, list);
56 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
58 const Setup *fpo = dynamic_cast<const Setup*>(ob);
61 query.prepare(QString("UPDATE setup SET "
62 "platformid=:platformid, "
63 "mediatypeid=:mediatypeid, "
64 "filetypeextensions=:filetypeextensions "
66 if (fpo->getPlatform())
67 query.bindValue(":platformid", fpo->getPlatform()->getId());
68 if (fpo->getMediaType())
69 query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
70 query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
71 query.bindValue(":id", fpo->getId());
73 if (ret) resetModel();
74 if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
78 QString DbSetup::supportedExtensionsToDb(QStringList list)
80 return list.isEmpty() ? "" : list.join(FILE_TYPE_EXTENSION_SEPARATOR);
83 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
84 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
86 qDebug() << "Inserting setup to database...";
87 const Setup *fpo = dynamic_cast<const Setup*>(ob);
89 query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
90 "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
91 int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
92 int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
93 query.bindValue(":platformid", plfId);
94 query.bindValue(":mediatypeid", mtId);
95 query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
98 id = query.lastInsertId().toInt();
100 qDebug() << query.lastError().text() << query.executedQuery();
104 QString DbSetup::constructSelect(QString where) const
107 "SELECT setup.id AS SetupId, "
108 "setup.platformid AS PlatformId, "
109 "setup.mediatypeid AS MediaTypeId, "
110 "setup.filetypeextensions AS SupportedFileTypeExtensions, "
111 "platform.name || ' ' || mediatype.name AS SetupName "
113 "INNER JOIN platform ON setup.platformid=platform.id "
114 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
119 QString DbSetup::constructFilterById(int id) const
121 return QString("setup.id = %1").arg(id);
125 QString DbSetup::constructSelectById(int id) const
127 return constructSelect(
128 QString("WHERE %1").arg(constructFilterById(id)));
131 // WARNING: this will delete also all the databindings to selected media image path
132 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
134 qDebug() << "This is not currently supported";
138 QSqlQueryModel* DbSetup::getData()
140 QSqlQueryModel *model = new QSqlQueryModel(this);
141 QString select = constructSelect();
143 model->setQuery(select);
144 model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
145 model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
146 model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
147 model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
148 model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
152 QString DbSetup::getCountRefsSelect(int id) const
154 /* setups are referenced by executable and filepath */
155 return QString("SELECT count(*) FROM "
156 "(SELECT setup.id FROM setup "
157 "INNER JOIN executable ON setup.id=executable.setupid "
160 "SELECT setup.id FROM setup "
161 "INNER JOIN filepath ON setup.id=filepath.setupid "
162 "WHERE setup.id=%1)").arg(id);