Updated the git clone command.
[emufront] / src / db / dbsetup.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QDebug>
22 #include <QStringList>
23 #include <QSqlRecord>
24 #include <QSqlQuery>
25 #include <QSqlError>
26 #include <QSqlRelationalTableModel>
27 #include "dbsetup.h"
28
29 const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
30
31 DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
32 {
33     tableName = DbSetup::DB_TABLE_NAME_SETUP;
34     dbPlatform = new DbPlatform(this);
35     dbMediaType = new DbMediaType(this);
36 }
37
38 /* Throws EmuFrontException */
39 EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec)
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().trimmed();
45     QStringList list;
46     if (!extensions.isEmpty())
47         list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
48     int plfId = rec->value(Setup_PlatformId).toInt();
49     int mtId = rec->value(Setup_MediaTypeId).toInt();
50     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId)); /* Throws EmuFrontException */
51     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId)); /* Throws EmuFrontException */
52     s = new Setup(id, plf, mt, list);
53     return s;
54 }
55
56 bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
57 {
58     const Setup *fpo = dynamic_cast<const Setup*>(ob);
59     bool ret = false;
60     QSqlQuery query;
61     query.prepare(QString("UPDATE setup SET "
62         "platformid=:platformid, "
63         "mediatypeid=:mediatypeid, "
64         "filetypeextensions=:filetypeextensions "
65         "WHERE id = :id"));
66     if (fpo->getPlatform())
67         query.bindValue(":platformid", fpo->getPlatform()->getId());
68     if (fpo->getMediaType())
69         query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
70     query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
71     query.bindValue(":id", fpo->getId());
72     ret = query.exec();
73     if (ret) resetModel();
74     if (!ret) qDebug() << query.lastError().text() << query.executedQuery();
75     return ret;
76 }
77
78 QString DbSetup::supportedExtensionsToDb(QStringList list)
79 {
80     return list.isEmpty() ? "" : list.join(FILE_TYPE_EXTENSION_SEPARATOR);
81 }
82
83 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
84 int DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
85 {
86     qDebug() << "Inserting setup to database...";
87     const Setup *fpo = dynamic_cast<const Setup*>(ob);
88     QSqlQuery query;
89     query.prepare("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions)"
90         "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
91     int plfId = fpo->getPlatform() ? fpo->getPlatform()->getId() : -1;
92     int mtId = fpo->getMediaType() ? fpo->getMediaType()->getId() : -1;
93     query.bindValue(":platformid", plfId);
94     query.bindValue(":mediatypeid", mtId);
95     query.bindValue(":filetypeextensions", supportedExtensionsToDb(fpo->getSupportedFileTypeExtensions()));
96     int id = -1;
97     if (query.exec())
98         id = query.lastInsertId().toInt();
99     else
100         qDebug() << query.lastError().text() << query.executedQuery();
101     return id;
102 }
103
104 QString DbSetup::constructSelect(QString where) const
105 {
106     return QString(
107         "SELECT setup.id AS SetupId, "
108         "setup.platformid AS PlatformId, "
109         "setup.mediatypeid AS MediaTypeId, "
110         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
111         "platform.name || ' ' || mediatype.name AS SetupName "
112         "FROM setup "
113         "INNER JOIN platform ON setup.platformid=platform.id "
114         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
115         "ORDER BY SetupName"
116         ).arg(where);
117 }
118
119 QString DbSetup::constructFilterById(int id) const
120 {
121      return QString("setup.id = %1").arg(id);
122 }
123
124
125 QString DbSetup::constructSelectById(int id) const
126 {
127     return constructSelect(
128         QString("WHERE %1").arg(constructFilterById(id)));
129 }
130
131 // WARNING: this will delete also all the databindings to selected media image path
132 bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
133 {
134     qDebug() << "This is not currently supported";
135     return false;
136 }
137
138 QSqlQueryModel* DbSetup::getData()
139 {
140     QSqlQueryModel *model = new QSqlQueryModel(this);
141     QString select = constructSelect();
142     qDebug() << select;
143     model->setQuery(select);
144     model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
145     model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
146     model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
147     model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
148     model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
149     return model;
150 }
151
152 QString DbSetup::getCountRefsSelect(int id) const
153 {
154     /* setups are referenced by executable and filepath */
155     return QString("SELECT count(*) FROM "
156             "(SELECT setup.id FROM setup "
157               "INNER JOIN executable ON setup.id=executable.setupid "
158               "WHERE setup.id=%1 "
159               "UNION ALL "
160               "SELECT setup.id FROM setup "
161               "INNER JOIN filepath ON setup.id=filepath.setupid "
162               "WHERE setup.id=%1)").arg(id);
163 }