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 "filepathmodel.h"
23 #include "emufrontfile.h"
24 #include "emufrontexception.h"
27 FilePathModel::FilePathModel(QObject *parent) :
28 EmuFrontQueryModel(parent)
33 void FilePathModel::refresh()
35 setQuery(constructSelect());
36 setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
37 setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
38 setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
39 setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
40 setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
43 QString FilePathModel::constructSelect(QString where) const
45 return QString("SELECT "
46 "filepath.id AS FilePathId, "
47 "filepath.name AS Name, "
48 "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
49 "setup.id AS SetupId, "
50 "platform.name || ' ' || mediatype.name AS SetupName, "
51 "filepath.filetypeid "
53 "INNER JOIN setup ON filepath.setupid=setup.id "
54 "INNER JOIN platform ON setup.platformid=platform.id "
55 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
57 "ORDER BY SetupName").arg(where);
60 Qt::ItemFlags FilePathModel::flags(const QModelIndex &index) const
62 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
63 int col = index.column();
64 if (col == FilePath_SetupId ||
65 col == FilePath_Name) {
66 flags |= Qt::ItemIsEditable;
71 bool FilePathModel::setData(const QModelIndex &index, const QVariant &value, int role)
73 int col = index.column();
74 if (col != FilePath_SetupId &&
75 col != FilePath_Name) {
79 QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), FilePath_Id);
81 int id = data(primaryKeyIndex).toInt();
85 switch(index.column()) {
86 case FilePath_SetupId:
87 ok = setSetup(id, value.toInt());
90 ok = setFilePath(id, value.toString());
94 qDebug() << "File path model, this shouldn't be happening!";
100 bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent)
102 if (parent.isValid())
103 return false; // This is a flat model
104 if (rowCount() < row)
105 row = rowCount() + 1;
108 q.exec(QString("SELECT setup.id, "
109 // The following is to get the correct order:
110 "platform.name || ' ' || mediatype.name AS SetupName "
112 "INNER JOIN platform ON setup.platformid=platform.id "
113 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
114 "ORDER BY SetupName "
117 supId = q.value(0).toInt();
118 qDebug() << "Got id " << supId << " for default setup.";
121 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
123 q.prepare(QString("INSERT INTO filepath "
124 "(id, name, filetypeid, setupid, lastscanned) "
125 "VALUES (NULL, '', :filetype, :setupid, :lastscanned )"));
126 beginInsertRows(QModelIndex(), row, row + count - 1);
127 for(int i = 0; i < count; ++i) {
128 q.bindValue(":filetype", EmuFrontFile::FileType_MediaImageContainer);
129 q.bindValue(":setupid", supId);
130 q.bindValue(":lastscanned", 0);
132 throw EmuFrontException(tr("Failed creating new filepath row: %1").
133 arg(q.lastError().text()));
141 bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent)
143 if (parent.isValid()) {
144 return false; // This is a flat model
146 if (rowCount() < row + count - 1)
150 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
151 QModelIndex primaryIndex;
153 beginRemoveRows(QModelIndex(), row, row + count - 1);
154 for(int i = 0; i < count; ++i) {
155 primaryIndex = QSqlQueryModel::index(row + i, FilePath_Id);
156 id = data(primaryIndex).toInt();
157 qDebug() << "Removing data item with id " << id;
158 q.bindValue(":id", id);
166 bool FilePathModel::setSetup(int id, int setupId)
169 q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id"));
170 q.bindValue(":setupid", setupId);
171 q.bindValue(":id", id);
175 bool FilePathModel::setFilePath(int id, QString filePath)
178 q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id"));
179 q.bindValue(":name", filePath);
180 q.bindValue(":id", id);