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!";
99 if (ok) emit dataChanged();
104 bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent)
106 if (parent.isValid())
107 return false; // This is a flat model
108 if (rowCount() < row)
109 row = rowCount() + 1;
112 q.exec(QString("SELECT setup.id, "
113 // The following is to get the correct order:
114 "platform.name || ' ' || mediatype.name AS SetupName "
116 "INNER JOIN platform ON setup.platformid=platform.id "
117 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
118 "ORDER BY SetupName "
121 supId = q.value(0).toInt();
122 qDebug() << "Got id " << supId << " for default setup.";
125 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
127 q.prepare(QString("INSERT INTO filepath "
128 "(id, name, filetypeid, setupid, lastscanned) "
129 "VALUES (NULL, '', :filetype, :setupid, :lastscanned )"));
130 beginInsertRows(QModelIndex(), row, row + count - 1);
131 for(int i = 0; i < count; ++i) {
132 q.bindValue(":filetype", EmuFrontFile::FileType_MediaImageContainer);
133 q.bindValue(":setupid", supId);
134 q.bindValue(":lastscanned", 0);
136 throw EmuFrontException(tr("Failed creating new filepath row: %1").
137 arg(q.lastError().text()));
145 bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent)
147 if (parent.isValid()) {
148 return false; // This is a flat model
150 if (rowCount() < row + count - 1)
154 q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
155 QModelIndex primaryIndex;
157 beginRemoveRows(QModelIndex(), row, row + count - 1);
158 for(int i = 0; i < count; ++i) {
159 primaryIndex = QSqlQueryModel::index(row + i, FilePath_Id);
160 id = data(primaryIndex).toInt();
161 qDebug() << "Removing data item with id " << id;
162 q.bindValue(":id", id);
170 bool FilePathModel::setSetup(int id, int setupId)
173 q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id"));
174 q.bindValue(":setupid", setupId);
175 q.bindValue(":id", id);
179 bool FilePathModel::setFilePath(int id, QString filePath)
182 q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id"));
183 q.bindValue(":name", filePath);
184 q.bindValue(":id", id);
188 bool FilePathModel::setScanned(int id)
191 q.prepare(QString("UPDATE filepath SET lastscanned = :timestamp WHERE id = :id"));
192 q.bindValue(":timestamp", getCurrentTimeStamp());
193 q.bindValue(":id", id);
197 FilePathObject* FilePathModel::getFilePathObject(const QModelIndex &index)
199 if (!index.isValid()) return 0;
200 EmuFrontObject *efo = getDataObject(index);
201 return dynamic_cast<FilePathObject *>(efo);
204 EmuFrontObject* FilePathModel::recordToDataObject(const QSqlRecord* rec)
206 int id = rec->value(FilePath_Id).toInt();
207 QString fpath = rec->value(FilePath_Name).toString();
208 int setupId = rec->value(FilePath_SetupId).toInt();
209 int fileType = rec->value(FilePath_FileTypeId).toInt();
211 EmuFrontObject *efo = supModel.getDataObject(setupId);
212 Setup *sup = dynamic_cast<Setup*>(efo);
213 return new FilePathObject(id, fpath, fileType, sup);
216 QString FilePathModel::constructFilterById(int id) const
218 return QString("filepath.id = %1").arg(id);