Fixed two cases of memory loss (parenting was missing).
[emufront] / src / db / dbsetup.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QDebug>
21 #include <QStringList>
22 #include <QSqlRecord>
23 #include <QSqlQuery>
24 #include <QSqlError>
25 #include <QSqlRelationalTableModel>
26 #include "dbsetup.h"
27
28 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
29
30 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
31 {
32     tableName = DbSetup::DB_TABLE_NAME_SETUP;
33     dbPlatform = new DbPlatform(this);
34     dbMediaType = new DbMediaType(this);
35 }
36
37 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
38 {
39     Setup *s = 0;
40     if (!rec) return s;
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);
49     return s;
50 }
51
52 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
53 {
54     const Setup *fpo = dynamic_cast<const Setup*>(ob);
55     bool ret = false;
56     QSqlQuery query;
57     query.prepare(QString("UPDATE setup SET "
58         "platformid=:platformid, "
59         "mediatypeid=:mediatypeid, "
60         "filetypeextensions=:filetypeextensions "
61         "WHERE id = :id"));
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());
68     ret = query.exec();
69     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
70     return ret;
71 }
72
73 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
74 {
75     qDebug() << "Inserting setup to database...";
76     const Setup *fpo = dynamic_cast<const Setup*>(ob);
77     QSqlQuery query;
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);
87     int id = -1;
88     if (query.exec())
89         id = query.lastInsertId().toInt();
90     else
91         qDebug() << query.lastError().text() << query.executedQuery();
92     return id;
93 }
94
95 QString DbSetup::constructSelect(QString where) const
96 {
97     return QString(
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 "
103         "FROM setup "
104         "INNER JOIN platform ON setup.platformid=platform.id "
105         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
106         "ORDER BY SetupName"
107         ).arg(where);
108 }
109
110 QString DbSetup::constructFilterById(int id) const
111 {
112      return QString("setup.id = %1").arg(id);
113 }
114
115
116 QString DbSetup::constructSelectById(int id) const
117 {
118     return constructSelect(
119         QString("WHERE %1").arg(constructFilterById(id)));
120 }
121
122 // WARNING: this will delete also all the databindings to selected media image path
123 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
124 {
125     qDebug() << "This is not currently supported";
126     return false;
127 }
128
129 QSqlQueryModel* DbSetup::getData()
130 {
131     QSqlQueryModel *model = new QSqlQueryModel(this);
132     QString select = constructSelect();
133     qDebug() << select;
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"));
140     return model;
141 }
142
143 QString DbSetup::getCountRefsSelect(int id) const
144 {
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 "
149               "WHERE setup.id=%1 "
150               "UNION ALL "
151               "SELECT setup.id FROM setup "
152               "INNER JOIN filepath ON setup.id=filepath.setupid "
153               "WHERE setup.id=%1)").arg(id);
154 }