Modified the license text comment type.
[emufront] / src / models / setupmodel.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 "setupmodel.h"
22 #include "emufrontexception.h"
23 #include <QtSql>
24
25 const QString SetupModel::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
26
27 SetupModel::SetupModel(QObject *parent) :
28     EmuFrontQueryModel(parent)
29 {
30     refresh();
31 }
32
33 void SetupModel::refresh()
34 {
35     setQuery(constructSelect());
36     setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
37     setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
38     setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
39     setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
40     setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
41 }
42
43 QString SetupModel::constructSelect(QString where) const
44 {
45     return QString(
46         "SELECT setup.id AS SetupId, "
47         "setup.platformid AS PlatformId, "
48         "setup.mediatypeid AS MediaTypeId, "
49         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
50         "platform.name || ' ' || mediatype.name AS SetupName "
51         "FROM setup "
52         "INNER JOIN platform ON setup.platformid=platform.id "
53         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
54         "ORDER BY SetupName"
55         ).arg(where);
56 }
57
58 Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const
59 {
60     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
61     int col = index.column();
62     if (col == Setup_PlatformId ||
63         col == Setup_MediaTypeId ||
64         col == Setup_FileTypeExtensions) {
65         flags |= Qt::ItemIsEditable;
66     }
67     return flags;
68 }
69
70 bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
71 {
72     int col = index.column();
73     if(col != Setup_PlatformId &&
74         col != Setup_MediaTypeId &&
75         col != Setup_FileTypeExtensions)
76         return false;
77
78     QModelIndex primaryKeyIndex
79         = QSqlQueryModel::index(index.row(), Setup_Id);
80
81     int id = data(primaryKeyIndex).toInt();
82     clear();
83
84     bool ok;
85     switch(index.column()) {
86
87     case Setup_PlatformId:
88         ok = setPlatform(id, value.toInt());
89         break;
90
91     case Setup_MediaTypeId:
92         ok = setMediaType(id, value.toInt());
93         break;
94
95     case Setup_FileTypeExtensions:
96         ok = setSupportedExtensions(id, value.toString());
97         break;
98
99     default:
100         qDebug() << "Setup model, this shouldn't be happening!";
101     };
102     refresh();
103     return ok;
104 }
105
106 bool SetupModel::setPlatform(int id, int platformId)
107 {
108     QSqlQuery query;
109     query.prepare(QString("update setup set platformid = :platformid where id = :id"));
110     query.bindValue(":platformid", platformId);
111     query.bindValue(":id", id);
112     return query.exec();
113 }
114
115 bool SetupModel::setMediaType(int id, int mediaTypeId)
116 {
117     QSqlQuery query;
118     query.prepare(QString("update setup set mediatypeid = :mediatypeid where id = :id"));
119     query.bindValue(":mediatypeid", mediaTypeId);
120     query.bindValue(":id", id);
121     return query.exec();
122 }
123
124 bool SetupModel::setSupportedExtensions(int id, QString exts)
125 {
126     QSqlQuery query;
127     query.prepare(QString("update setup set filetypeextensions = :exts where id = :id"));
128     query.bindValue(":exts", exts);
129     query.bindValue(":id", id);
130     return query.exec();
131 }
132
133 bool SetupModel::insertRows(int row, int count, const QModelIndex &parent)
134 {
135     if (parent.isValid())
136         return false; // This is a flat model
137     if (rowCount() < row)
138         row = rowCount() + 1;
139     // we need a default value for platformid and mediatypeid and if none is yet
140     // available an error message must be shown!
141     int plfId = -1;
142     int mdtId = -1;
143     QSqlQuery q;
144     q.exec(QString("SELECT id FROM platform ORDER BY name LIMIT 1"));
145     if (q.first()){
146         plfId = q.value(0).toInt();
147         qDebug() << "Got id " << plfId << " for default platform.";
148     }
149     else {
150         throw EmuFrontException(tr("No platforms yet available for setup configuration!"));
151     }
152     q.exec(QString("SELECT id FROM mediatype ORDER BY name LIMIT 1"));
153     if (q.first()) {
154         mdtId = q.value(0).toInt();
155         qDebug() << "Got id " << mdtId << " for default media type.";
156     }
157     else {
158         throw EmuFrontException(tr("No media types yet available for setup configuration!"));
159     }
160     q.prepare(QString("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions) "
161         " VALUES (NULL, :plfid, :mdtid, '') "));
162     beginInsertRows(QModelIndex(), row, row + count - 1);
163     for (int i = 0; i < count; ++i) {
164         q.bindValue(":plfid", plfId);
165         q.bindValue(":mdtid", mdtId);
166         if (!q.exec()) {
167             throw EmuFrontException(tr("Failed creating new setup: %1").
168                 arg(q.lastError().text()));
169         }
170     }
171     endInsertRows();
172     refresh();
173     return true;
174 }
175
176 bool SetupModel::removeRows(int row, int count, const QModelIndex &parent)
177 {
178     if (parent.isValid()) {
179         return false; // This is a flat model
180     }
181     if (rowCount() < row + count - 1)
182         return false;
183
184     QSqlQuery q;
185     q.prepare(QString("DELETE FROM setup WHERE id=:id"));
186     QModelIndex primaryIndex;
187     int id = -1;
188     beginRemoveRows(QModelIndex(), row, row + count - 1);
189     for(int i = 0; i < count; ++i) {
190         primaryIndex = QSqlQueryModel::index(row + i, Setup_Id);
191         id = data(primaryIndex).toInt();
192         qDebug() << "Removing data item with id " << id;
193         q.bindValue(":id", id);
194         q.exec();
195     }
196     endRemoveRows();
197     refresh();
198     return true;
199 }
200