09e0df3c85e78546bb4635f89dc377017bb6958d
[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
28 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
29
30 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
31 {
32     dbPlatform = new DbPlatform(this);
33     dbMediaType = new DbMediaType(this);
34     sqlTableModel = 0; //getData();
35 }
36
37 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec) const
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     query.exec();
69     return ret;
70 }
71
72 bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
73 {
74     qDebug() << "Inserting setup to database...";
75     const Setup *fpo = dynamic_cast<const Setup*>(ob);
76     QSqlQuery query;
77     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
78         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
79     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
80     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
81     QString exts = fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR);
82     qDebug() << "Going to insert setup with platform " << plfId << ", media type " << mtId << " and extensions " << exts;
83     query.bindValue(":platformid", plfId);
84     query.bindValue(":mediatypeid", mtId);
85     query.bindValue(":filetypeextensions", exts);
86     bool ret = query.exec();
87     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
88     return ret;
89 }
90
91 int DbSetup::countDataObjectRefs(int ) const
92 {
93     return 0;
94 }
95
96 QString DbSetup::constructSelect(QString whereClause) const
97 {
98     QString where = whereClause.isEmpty()
99         ? "" : QString("WHERE ").append(whereClause);
100     return QString(
101         "SELECT setup.id AS SetupId, "
102         "setup.platformid AS PlatformId, "
103         "setup.mediatypeid AS MediaTypeId, "
104         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
105         "platform.name || ' ' || mediatype.name AS SetupName "
106         "FROM setup %1"
107         "INNER JOIN platform ON setup.platformid=platform.id "
108         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
109         "ORDER BY SetupName"
110         ).arg(where);
111 }
112
113 QString DbSetup::constructSelectById(int id) const
114 {
115      return constructSelect(QString("setup.id = %1").arg(id));
116 }
117
118 // WARNING: this will delete also all the databindings to selected media image path
119 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
120 {
121     return false;
122 }
123
124 QSqlQueryModel* DbSetup::getData()
125 {
126     QSqlQueryModel *model = new QSqlQueryModel;
127     QString select = constructSelect();
128     qDebug() << select;
129     model->setQuery(select);
130     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
131     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
132     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
133     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
134     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
135     return model;
136 }