3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
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.
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.
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/>.
22 #include "externalexecutablemodel.h"
23 #include "executable.h"
25 #include "setupmodel.h"
26 #include "emufrontexception.h"
29 ExternalExecutableModel::ExternalExecutableModel(QObject *parent) :
30 EmuFrontQueryModel(parent)
32 editableColumns << Executable_Name;
33 editableColumns << Executable_Options;
34 editableColumns << Executable_Executable;
35 editableColumns << Executable_SetupId;
39 void ExternalExecutableModel::refresh()
41 setQuery(constructSelect());
42 setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
43 setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
44 setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
45 setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
46 setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
47 setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
48 setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
51 QString ExternalExecutableModel::constructSelect(QString where) const
53 return QString("SELECT "
54 "executable.id AS ExecutableId, "
55 "executable.name AS ExecutableName, "
56 "executable.executable AS Executable, "
57 "executable.options AS ExecutableOptions, "
58 "executable.type AS ExecutableType, "
59 "setup.id As ExecutableSetupId, "
60 "platform.name || ' ' || mediatype.name AS SetupName "
62 "INNER JOIN setup ON executable.setupid = setup.id "
63 "INNER JOIN platform ON setup.platformid=platform.id "
64 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
66 "ORDER BY executable.name").arg(where);
69 Qt::ItemFlags ExternalExecutableModel::flags(const QModelIndex &index) const
71 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
72 int col = index.column();
73 if (editableColumns.contains(index.column())) {
74 flags |= Qt::ItemIsEditable;
79 bool ExternalExecutableModel::setData(const QModelIndex &index, const QVariant &value, int role)
81 if (!editableColumns.contains(index.column()))
84 QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), Executable_Id);
85 int id = data(primaryKeyIndex).toInt();
88 switch(index.column())
91 ok = setExecutableName(id, value.toString());
93 case Executable_Executable:
94 ok = setExecutable(id, value.toString());
96 case Executable_Options:
97 ok = setOptions(id, value.toString());
99 case Executable_SetupId:
100 ok = setSetup(id, value.toInt());
109 bool ExternalExecutableModel::insertRows(int row, int count, const QModelIndex &parent)
111 if (parent.isValid())
112 return false; // This is a flat model
113 if (rowCount() < row)
114 row = rowCount() + 1;
116 // Fetch a setup for an initial selection
119 q.exec(QString("SELECT setup.id, "
120 // The following is to get the correct order:
121 "platform.name || ' ' || mediatype.name AS SetupName "
123 "INNER JOIN platform ON setup.platformid=platform.id "
124 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
125 "ORDER BY SetupName "
128 supId = q.value(0).toInt();
129 qDebug() << "Got id " << supId << " for default setup.";
132 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
134 q.prepare(QString("INSERT INTO executable "
135 "(id, name, executable, options, type, setupid) "
136 "VALUES (NULL, '', '', '', :type, :supid)"
138 beginInsertRows(QModelIndex(), row, row + count - 1);
139 for(int i = 0; i < count; ++i) {
140 q.bindValue(":supid", supId);
141 q.bindValue(":type", Executable::ExecutableType_Emulator);
143 throw EmuFrontException(tr("Failed creating new external executable row: %1").
144 arg(q.lastError().text()));
152 bool ExternalExecutableModel::removeRows(int row, int count, const QModelIndex &parent)
154 if (parent.isValid()) {
155 return false; // This is a flat model
157 if (rowCount() < row + count - 1)
161 q.prepare(QString("DELETE FROM executable WHERE id=:id"));
162 QModelIndex primaryIndex;
164 beginRemoveRows(QModelIndex(), row, row + count - 1);
165 for(int i = 0; i < count; ++i) {
166 primaryIndex = QSqlQueryModel::index(row + i, Executable_Id);
167 id = data(primaryIndex).toInt();
168 qDebug() << "Removing data item with id " << id;
169 q.bindValue(":id", id);
177 bool ExternalExecutableModel::setSetup(int id, int setupId)
180 q.prepare(QString("UPDATE executable SET setupid = :supid WHERE id = :id"));
181 q.bindValue(":supid", setupId);
182 q.bindValue(":id", id);
186 bool ExternalExecutableModel::setExecutable(int id, QString name)
189 q.prepare(QString("UPDATE executable SET executable = :exec WHERE id = :id"));
190 q.bindValue(":exec", name);
191 q.bindValue(":id", id);
195 bool ExternalExecutableModel::setOptions(int id, QString options)
198 q.prepare(QString("UPDATE executable SET options = :opts WHERE id = :id"));
199 q.bindValue(":opts", options);
200 q.bindValue(":id", id);
204 bool ExternalExecutableModel::setExecutableName(int id, QString name)
207 q.prepare(QString("UPDATE executable SET name = :name WHERE id = :id"));
208 q.bindValue(":name", name);
209 q.bindValue(":id", id);
213 void ExternalExecutableModel::filterBySetup(int setupid)
215 QList<QString> filters;
216 filters.append(QString("executable.setupid=%1").arg(setupid));
217 filterDataObjects(filters);
220 // Implemented for EmuFrontQueryModel:
221 EmuFrontObject* ExternalExecutableModel::recordToDataObject(const QSqlRecord* rec)
225 int id = rec->value(Executable_Id).toInt();
226 int supid = rec->value(Executable_SetupId).toInt();
228 EmuFrontObject *ob = supModel.getDataObject(supid);
229 Setup *sup = dynamic_cast<Setup*>(ob);
230 QString name = rec->value(Executable_Name).toString();
231 QString exec = rec->value(Executable_Executable).toString();
232 QString opts = rec->value(Executable_Options).toString();
233 int type = rec->value(Executable_TypeId).toInt();
234 ex = new Executable(id, name, exec, opts, sup, type);
238 QString ExternalExecutableModel::constructFilterById(int id) const
240 return QString("executable.id=%1").arg(id);