Reset data model after succesful update.
[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 /* Throws EmuFrontException */
38 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
39 {
40     Setup *s = 0;
41     if (!rec) return s;
42     int id = rec->value(Setup_Id).toInt();
43     QString extensions = rec->value(Setup_FileTypeExtensions).toString().trimmed();
44     QStringList list;
45     if (!extensions.isEmpty())
46         list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
47     int plfId = rec->value(Setup_PlatformId).toInt();
48     int mtId = rec->value(Setup_MediaTypeId).toInt();
49     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId)); /* Throws EmuFrontException */
50     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId)); /* Throws EmuFrontException */
51     s = new Setup(id, plf, mt, list);
52     return s;
53 }
54
55 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
56 {
57     const Setup *fpo = dynamic_cast<const Setup*>(ob);
58     bool ret = false;
59     QSqlQuery query;
60     query.prepare(QString("UPDATE setup SET "
61         "platformid=:platformid, "
62         "mediatypeid=:mediatypeid, "
63         "filetypeextensions=:filetypeextensions "
64         "WHERE id = :id"));
65     if (fpo->getPlatform())
66         query.bindValue(":platformid", fpo->getPlatform()->getId());
67     if (fpo->getMediaType())
68         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
69     query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
70     query.bindValue(":id", fpo->getId());
71     ret = query.exec();
72     if (ret) resetModel();
73     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
74     return ret;
75 }
76
77 QString DbSetup::supportedExtensionsToDb(QStringList list)
78 {
79     return list.isEmpty() ? "" : list.join(FILE_TYPE_EXTENSION_SEPARATOR);
80 }
81
82 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
83 {
84     qDebug() << "Inserting setup to database...";
85     const Setup *fpo = dynamic_cast<const Setup*>(ob);
86     QSqlQuery query;
87     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
88         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
89     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
90     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
91     query.bindValue(":platformid", plfId);
92     query.bindValue(":mediatypeid", mtId);
93     query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
94     int id = -1;
95     if (query.exec())
96         id = query.lastInsertId().toInt();
97     else
98         qDebug() << query.lastError().text() << query.executedQuery();
99     return id;
100 }
101
102 QString DbSetup::constructSelect(QString where) const
103 {
104     return QString(
105         "SELECT setup.id AS SetupId, "
106         "setup.platformid AS PlatformId, "
107         "setup.mediatypeid AS MediaTypeId, "
108         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
109         "platform.name || ' ' || mediatype.name AS SetupName "
110         "FROM setup "
111         "INNER JOIN platform ON setup.platformid=platform.id "
112         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
113         "ORDER BY SetupName"
114         ).arg(where);
115 }
116
117 QString DbSetup::constructFilterById(int id) const
118 {
119      return QString("setup.id = %1").arg(id);
120 }
121
122
123 QString DbSetup::constructSelectById(int id) const
124 {
125     return constructSelect(
126         QString("WHERE %1").arg(constructFilterById(id)));
127 }
128
129 // WARNING: this will delete also all the databindings to selected media image path
130 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
131 {
132     qDebug() << "This is not currently supported";
133     return false;
134 }
135
136 QSqlQueryModel* DbSetup::getData()
137 {
138     QSqlQueryModel *model = new QSqlQueryModel(this);
139     QString select = constructSelect();
140     qDebug() << select;
141     model->setQuery(select);
142     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
143     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
144     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
145     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
146     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
147     return model;
148 }
149
150 QString DbSetup::getCountRefsSelect(int id) const
151 {
152     /* setups are referenced by executable and filepath */
153     return QString("SELECT count(*) FROM "
154             "(SELECT setup.id FROM setup "
155               "INNER JOIN executable ON setup.id=executable.setupid "
156               "WHERE setup.id=%1 "
157               "UNION ALL "
158               "SELECT setup.id FROM setup "
159               "INNER JOIN filepath ON setup.id=filepath.setupid "
160               "WHERE setup.id=%1)").arg(id);
161 }