Cleaning up
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Foobar 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 Foobar.  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 #include "dbplatform.h"
28 #include "dbmediatype.h"
29
30 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
31
32 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
33 {
34     dbPlatform = new DbPlatform(this);
35     dbMediaType = new DbMediaType(this);
36     sqlTableModel = 0; //getData();
37 }
38
39 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec) const
40 {
41     Setup *s = 0;
42     if (!rec) return s;
43     int id = rec->value(Setup_Id).toInt();
44     QString extensions = rec->value(Setup_FileTypeExtensions).toString();
45     QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
46     int plfId = rec->value(Setup_PlatformId).toInt();
47     int mtId = rec->value(Setup_MediaTypeId).toInt();
48     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
49     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
50     s = new Setup(id, plf, mt, list);
51     return s;
52 }
53
54 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
55 {
56     const Setup *fpo = dynamic_cast<const Setup*>(ob);
57     bool ret = false;
58     QSqlQuery query;
59     query.prepare(QString("UPDATE setup SET "
60         "platformid=:platformid, "
61         "mediatypeid=:mediatypeid, "
62         "filetypeextensions:=filetypeextensions "
63         "WHERE id = :id"));
64     if (fpo->getPlatform())
65         query.bindValue(":platformid", fpo->getPlatform()->getId());
66     if (fpo->getMediaType())
67         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
68     query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
69     query.bindValue(":id", fpo->getId());
70     query.exec();
71     return ret;
72 }
73
74 bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
75 {
76     qDebug() << "Inserting setup to database...";
77     const Setup *fpo = dynamic_cast<const Setup*>(ob);
78     QSqlQuery query;
79     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
80         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
81     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
82     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
83     QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
84     qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
85     query.bindValue(":platformid", plfId);
86     query.bindValue(":mediatypeid", mtId);
87     query.bindValue(":filetypeextensions", exts);
88     bool ret = query.exec();
89     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
90     return ret;
91 }
92
93 int DbSetup::countDataObjectRefs(int ) const
94 {
95     return 0;
96 }
97
98 QString DbSetup::constructSelect(QString whereClause) const
99 {
100     QString where = whereClause.isEmpty()
101         ? "" : QString("WHERE ").append(whereClause);
102     return QString(
103         "SELECT setup.id AS SetupId, "
104         "setup.platformid AS PlatformId, "
105         "setup.mediatypeid AS MediaTypeId, "
106         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
107         "platform.name || ' ' || mediatype.name AS SetupName "
108         "FROM setup %1"
109         "INNER JOIN platform ON setup.platformid=platform.id "
110         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
111         "ORDER BY SetupName"
112         ).arg(where);
113 }
114
115 QString DbSetup::constructSelectById(int id) const
116 {
117      return constructSelect(QString("setup.id = %1").arg(id));
118 }
119
120 // WARNING: this will delete also all the databindings to selected media image path
121 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
122 {
123     return false;
124 }
125
126 QSqlQueryModel* DbSetup::getData()
127 {
128     QSqlQueryModel *model = new QSqlQueryModel;
129     QString select = constructSelect();
130     qDebug() << select;
131     model->setQuery(select);
132     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
133     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
134     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
135     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
136     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
137     return model;
138 }