2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront 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.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
20 #include "filepathmodel.h"
21 #include "emufrontfile.h"
22 #include "emufrontexception.h"
25 FilePathModel::FilePathModel(QObject *parent) :
26 EmuFrontQueryModel(parent)
31 void FilePathModel::refresh()
33 setQuery(constructSelect());
34 setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
35 setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
36 setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
37 setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
38 setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
41 QString FilePathModel::constructSelect(QString where) const
43 return QString("SELECT "
44 "filepath.id AS FilePathId, "
45 "filepath.name AS Name, "
46 "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
47 "setup.id AS SetupId, "
48 "platform.name || ' ' || mediatype.name AS SetupName, "
49 "filepath.filetypeid "
51 "INNER JOIN setup ON filepath.setupid=setup.id "
52 "INNER JOIN platform ON setup.platformid=platform.id "
53 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
55 "ORDER BY SetupName").arg(where);
58 Qt::ItemFlags FilePathModel::flags(const QModelIndex &index) const
60 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
61 int col = index.column();
62 if (col == FilePath_SetupId ||
63 col == FilePath_Name) {
64 flags |= Qt::ItemIsEditable;
69 bool FilePathModel::setData(const QModelIndex &index, const QVariant &value, int role)
71 int col = index.column();
72 if (col != FilePath_SetupId &&
73 col != FilePath_Name) {
77 QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), FilePath_Id);
79 int id = data(primaryKeyIndex).toInt();
83 switch(index.column()) {
84 case FilePath_SetupId:
85 ok = setSetup(id, value.toInt());
88 ok = setFilePath(id, value.toString());
91 qDebug() << "File path model, this shouldn't be happening!";
97 bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent)
100 return false; // This is a flat model
101 if (rowCount() < row)
102 row = rowCount() + 1;
105 q.exec(QString("SELECT setup.id, "
106 // The following is to get the correct order:
107 "platform.name || ' ' || mediatype.name AS SetupName "
109 "INNER JOIN platform ON setup.platformid=platform.id "
110 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
111 "ORDER BY SetupName "
114 supId = q.value(0).toInt();
115 qDebug() << "Got id " << supId << " for default setup.";
118 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
120 q.prepare(QString("INSERT INTO filepath "
121 "(id, name, filetypeid, setupid, lastscanned) "
122 "VALUES (NULL, '', :filetype, :setupid, :lastscanned )"));
123 beginInsertRows(QModelIndex(), row, row + count - 1);
124 for(int i = 0; i < count; ++i) {
125 q.bindValue(":filetype", EmuFrontFile::FileType_MediaImageContainer);
126 q.bindValue(":setupid", supId);
127 q.bindValue(":lastscanned", 0);
129 throw EmuFrontException(tr("Failed creating new filepath row: %1").
130 arg(q.lastError().text()));
138 bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent)
140 if (parent.isValid()) {
141 return false; // This is a flat model
143 if (rowCount() < row + count - 1)
147 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
148 QModelIndex primaryIndex;
150 beginRemoveRows(QModelIndex(), row, row + count - 1);
151 for(int i = 0; i < count; ++i) {
152 primaryIndex = QSqlQueryModel::index(row + i, FilePath_Id);
153 id = data(primaryIndex).toInt();
154 qDebug() << "Removing data item with id " << id;
155 q.bindValue(":id", id);
163 bool FilePathModel::setSetup(int id, int setupId)
166 q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id"));
167 q.bindValue(":setupid", setupId);
168 q.bindValue(":id", id);
172 bool FilePathModel::setFilePath(int id, QString filePath)
175 q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id"));
176 q.bindValue(":name", filePath);
177 q.bindValue(":id", id);