Initial implementation of setup editor. Some refactoring and bug hunting
[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 <QStringList>
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlRelationalTableModel>
24 #include "dbsetup.h"
25 #include "dbplatform.h"
26 #include "dbmediatype.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     int id = rec->value(Setup_Id).toInt();
40     QString extensions = rec->value(Setup_FileTypeExtensions).toString();
41     QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
42     int plfId = rec->value(Setup_PlatformId).toInt();
43     int mtId = rec->value(Setup_MediaTypeId).toInt();
44     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
45     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
46     return new Setup(id, plf, mt, list);
47 }
48
49 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
50 {
51     const Setup *fpo = dynamic_cast<const Setup*>(ob);
52     bool ret = false;
53     QSqlQuery query;
54     query.prepare(QString("UPDATE setup SET "
55         "platformid=:platformid, "
56         "mediatypeid=:mediatypeid, "
57         "filetypeextensions:=filetypeextensions "
58         "WHERE id = :id"));
59     if (fpo->getPlatform())
60         query.bindValue(":platformid", fpo->getPlatform()->getId());
61     if (fpo->getMediaType())
62         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
63     query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
64     query.bindValue(":id", fpo->getId());
65     query.exec();
66
67     /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
68     sqlTableModel->select();
69     if (sqlTableModel->rowCount() == 1)
70     {
71         QSqlRecord rec = sqlTableModel->record(0);
72         rec.setValue("filetypeid", fpo->getFiletype());
73
74         Platform *pl = fpo->getPlatform();
75         MediaType *mt = fpo->getMediaType();
76         if (pl) rec.setValue("platformid", pl->getId());
77         if (mt) rec.setValue("mediatypeid", mt->getId());
78
79         QStringList list = fpo->getSupportedFileTypeExtensions();
80         if (list.count() > 0)
81             rec.setValue("filetypeextensions", list.join(FILE_TYPE_EXTENSION_SEPARATOR));
82     }*/
83     return ret;
84 }
85
86 bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
87 {
88     const Setup *fpo = dynamic_cast<const Setup*>(ob);
89     QSqlQuery query;
90     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, fileextensions)"
91         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
92     if (fpo->getPlatform())
93         query.bindValue(":platformid", fpo->getPlatform()->getId());
94     if (fpo->getMediaType())
95         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
96     query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
97     return query.exec();
98
99     /*int row = 0;
100     sqlTableModel->insertRows(row, 1);
101
102     Platform *pl = fpo->getPlatform();
103     MediaType *mt = fpo->getMediaType();
104
105     //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
106     // not all the file path types have platform and/or media type
107     if (pl) sqlTableModel->setData(sqlTableModel->index(row, Setup_PlatformId), pl->getId());
108     if (mt) sqlTableModel->setData(sqlTableModel->index(row, Setup_MediaTypeId), mt->getId());
109     QStringList list = fpo->getSupportedFileTypeExtensions();
110     if (list.count() > 0)
111         sqlTableModel->setData(sqlTableModel->index(row, Setup_FileTypeExtensions), list.join(FILE_TYPE_EXTENSION_SEPARATOR));
112     return sqlTableModel->submitAll();*/
113 }
114
115 int DbSetup::countDataObjectRefs(int ) const
116 {
117     return 0;
118 }
119
120 QString DbSetup::constructSelect(QString whereClause) const
121 {
122     QString where = whereClause.isEmpty()
123         ? "" : QString("WHERE ").append(whereClause);
124     return QString(
125         "SELECT setup.id AS SetupId, "
126         "setup.platformid AS PlatformId, "
127         "setup.mediatypeid AS MediaTypeId, "
128         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
129         "platform.name || ' ' || mediatype.name AS SetupName, "
130         "FROM setup %1"
131         "INNER JOIN platform ON setup.platformid=platform.id "
132         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
133         "ORDER BY SetupName"
134         ).arg(where);
135 }
136
137 QString DbSetup::constructSelectById(int id) const
138 {
139      return constructSelect(QString("setup.id = %1").arg(id));
140 }
141
142 // WARNING: this will delete also all the databindings to selected media image path
143 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
144 {
145     return false;
146 }
147
148 QSqlQueryModel* DbSetup::getData()
149 {
150     QSqlQueryModel *model = new QSqlQueryModel;
151     model->setQuery(constructSelect());
152     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
153     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
154     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
155     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
156     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
157     return model;
158
159    /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
160    model->setTable(DB_TABLE_NAME_FILEPATH);
161    model->setRelation(FilePath_PlatformId,
162        QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
163    model->setRelation(FilePath_MediaTypeId,
164        QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
165    model->setSort(FilePath_Name, Qt::AscendingOrder);
166    model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
167    model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
168    model->select();
169    return model;*/
170 }
171
172 /*void DbMediaType::filterById(int id)
173 {
174     sqlTableModel->setQuery(constructSelectById(id));
175 }*/