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"
25 #include "filepathobject.h"
27 #include "setupmodel.h"
30 FilePathModel::FilePathModel(QObject *parent) :
31 EmuFrontQueryModel(parent)
36 void FilePathModel::refresh()
38 setQuery(constructSelect());
39 setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
40 setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
41 setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
42 setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
43 setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
46 QString FilePathModel::constructSelect(QString where) const
48 return QString("SELECT "
49 "filepath.id AS FilePathId, "
50 "filepath.name AS Name, "
51 "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
52 "setup.id AS SetupId, "
53 "platform.name || ' ' || mediatype.name AS SetupName, "
54 "filepath.filetypeid "
56 "INNER JOIN setup ON filepath.setupid=setup.id "
57 "INNER JOIN platform ON setup.platformid=platform.id "
58 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
60 "ORDER BY SetupName").arg(where);
63 Qt::ItemFlags FilePathModel::flags(const QModelIndex &index) const
65 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
66 int col = index.column();
67 if (col == FilePath_SetupId ||
68 col == FilePath_Name) {
69 flags |= Qt::ItemIsEditable;
74 bool FilePathModel::setData(const QModelIndex &index, const QVariant &value, int role)
76 int col = index.column();
77 if (col != FilePath_SetupId &&
78 col != FilePath_Name) {
82 QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), FilePath_Id);
84 int id = data(primaryKeyIndex).toInt();
88 switch(index.column()) {
89 case FilePath_SetupId:
90 ok = setSetup(id, value.toInt());
93 ok = setFilePath(id, value.toString());
97 qDebug() << "File path model, this shouldn't be happening!";
103 bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent)
105 if (parent.isValid())
106 return false; // This is a flat model
107 if (rowCount() < row)
108 row = rowCount() + 1;
111 q.exec(QString("SELECT setup.id, "
112 // The following is to get the correct order:
113 "platform.name || ' ' || mediatype.name AS SetupName "
115 "INNER JOIN platform ON setup.platformid=platform.id "
116 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
117 "ORDER BY SetupName "
120 supId = q.value(0).toInt();
121 qDebug() << "Got id " << supId << " for default setup.";
124 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
126 q.prepare(QString("INSERT INTO filepath "
127 "(id, name, filetypeid, setupid, lastscanned) "
128 "VALUES (NULL, '', :filetype, :setupid, :lastscanned )"));
129 beginInsertRows(QModelIndex(), row, row + count - 1);
130 for(int i = 0; i < count; ++i) {
131 q.bindValue(":filetype", EmuFrontFile::FileType_MediaImageContainer);
132 q.bindValue(":setupid", supId);
133 q.bindValue(":lastscanned", 0);
135 throw EmuFrontException(tr("Failed creating new filepath row: %1").
136 arg(q.lastError().text()));
144 bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent)
146 if (parent.isValid()) {
147 return false; // This is a flat model
149 if (rowCount() < row + count - 1)
153 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
154 QModelIndex primaryIndex;
156 beginRemoveRows(QModelIndex(), row, row + count - 1);
157 for(int i = 0; i < count; ++i) {
158 primaryIndex = QSqlQueryModel::index(row + i, FilePath_Id);
159 id = data(primaryIndex).toInt();
160 qDebug() << "Removing data item with id " << id;
161 q.bindValue(":id", id);
169 bool FilePathModel::setSetup(int id, int setupId)
172 q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id"));
173 q.bindValue(":setupid", setupId);
174 q.bindValue(":id", id);
178 bool FilePathModel::setFilePath(int id, QString filePath)
181 q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id"));
182 q.bindValue(":name", filePath);
183 q.bindValue(":id", id);
187 bool FilePathModel::setScanned(int id)
190 q.prepare(QString("UPDATE filepath SET lastscanned = :timestamp WHERE id = :id"));
191 q.bindValue(":timestamp", getCurrentTimeStamp());
192 q.bindValue(":id", id);
196 FilePathObject* FilePathModel::getFilePathObject(const QModelIndex &index)
198 if (!index.isValid()) return 0;
199 EmuFrontObject *efo = getDataObject(index);
200 return dynamic_cast<FilePathObject *>(efo);
203 EmuFrontObject* FilePathModel::recordToDataObject(const QSqlRecord* rec)
205 int id = rec->value(FilePath_Id).toInt();
206 QString fpath = rec->value(FilePath_Name).toString();
207 int setupId = rec->value(FilePath_SetupId).toInt();
208 int fileType = rec->value(FilePath_FileTypeId).toInt();
210 EmuFrontObject *efo = supModel.getDataObject(setupId);
211 Setup *sup = dynamic_cast<Setup*>(efo);
212 return new FilePathObject(id, fpath, fileType, sup);
215 QString FilePathModel::constructFilterById(int id) const
217 return QString("filepath.id = %1").arg(id);