bd5972907db0d3a8b47ad139741a693fd8ad0f36
[emufront] / src / models / filepathmodel.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
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.
12 **
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.
17 **
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/>.
20 */
21
22 #include "filepathmodel.h"
23 #include "emufrontfile.h"
24 #include "emufrontexception.h"
25 #include "filepathobject.h"
26 #include "setup.h"
27 #include "setupmodel.h"
28 #include <QtSql>
29
30 FilePathModel::FilePathModel(QObject *parent) :
31     EmuFrontQueryModel(parent)
32 {
33     refresh();
34 }
35
36 void FilePathModel::refresh()
37 {
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"));
44 }
45
46 QString FilePathModel::constructSelect(QString where) const
47 {
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 "
55                    "FROM filepath "
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 "
59                    "%1 "
60                    "ORDER BY SetupName").arg(where);
61 }
62
63 Qt::ItemFlags FilePathModel::flags(const QModelIndex &index) const
64 {
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;
70     }
71     return flags;
72 }
73
74 bool FilePathModel::setData(const QModelIndex &index, const QVariant &value, int role)
75 {
76     int col = index.column();
77     if (col != FilePath_SetupId &&
78         col != FilePath_Name) {
79         return false;
80     }
81
82     QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), FilePath_Id);
83
84     int id = data(primaryKeyIndex).toInt();
85     clear();
86
87     bool ok;
88     switch(index.column()) {
89     case FilePath_SetupId:
90         ok = setSetup(id, value.toInt());
91         break;
92     case FilePath_Name:
93         ok = setFilePath(id, value.toString());
94         break;
95     default:
96         ok = false;
97         qDebug() << "File path model, this shouldn't be happening!";
98     }
99     refresh();
100     return ok;
101 }
102
103 bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent)
104 {
105     if (parent.isValid())
106         return false; // This is a flat model
107     if (rowCount() < row)
108         row = rowCount() + 1;
109     int supId = -1;
110     QSqlQuery q;
111     q.exec(QString("SELECT setup.id, "
112            // The following is to get the correct order:
113            "platform.name || ' ' || mediatype.name AS SetupName "
114            "FROM setup "
115            "INNER JOIN platform ON setup.platformid=platform.id "
116            "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
117            "ORDER BY SetupName "
118            "LIMIT 1"));
119     if (q.first()) {
120         supId = q.value(0).toInt();
121         qDebug() << "Got id " << supId << " for default setup.";
122     }
123     else {
124         throw EmuFrontException(tr("No setups yet available for file path configuration!"));
125     }
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);
134         if (!q.exec()) {
135             throw EmuFrontException(tr("Failed creating new filepath row: %1").
136                                     arg(q.lastError().text()));
137         }
138     }
139     endInsertRows();
140     refresh();
141     return true;
142 }
143
144 bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent)
145 {
146     if (parent.isValid()) {
147         return false; // This is a flat model
148     }
149     if (rowCount() < row + count - 1)
150         return false;
151
152     QSqlQuery q;
153     q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
154     QModelIndex primaryIndex;
155     int id = -1;
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);
162         q.exec();
163     }
164     endRemoveRows();
165     refresh();
166     return true;
167 }
168
169 bool FilePathModel::setSetup(int id, int setupId)
170 {
171     QSqlQuery q;
172     q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id"));
173     q.bindValue(":setupid", setupId);
174     q.bindValue(":id", id);
175     return q.exec();
176 }
177
178 bool FilePathModel::setFilePath(int id, QString filePath)
179 {
180     QSqlQuery q;
181     q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id"));
182     q.bindValue(":name", filePath);
183     q.bindValue(":id", id);
184     return q.exec();
185 }
186
187 bool FilePathModel::setScanned(int id)
188 {
189     QSqlQuery q;
190     q.prepare(QString("UPDATE filepath SET lastscanned = :timestamp WHERE id = :id"));
191     q.bindValue(":timestamp", getCurrentTimeStamp());
192     q.bindValue(":id", id);
193     return q.exec();
194 }
195
196 FilePathObject* FilePathModel::getFilePathObject(const QModelIndex &index)
197 {
198     if (!index.isValid()) return 0;
199     EmuFrontObject *efo = getDataObject(index);
200     return dynamic_cast<FilePathObject *>(efo);
201 }
202
203 EmuFrontObject* FilePathModel::recordToDataObject(const QSqlRecord* rec)
204 {
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();
209     SetupModel supModel;
210     EmuFrontObject *efo = supModel.getDataObject(setupId);
211     Setup *sup = dynamic_cast<Setup*>(efo);
212     return new FilePathObject(id, fpath, fileType, sup);
213 }
214
215 QString FilePathModel::constructFilterById(int id) const
216 {
217     return QString("filepath.id = %1").arg(id);
218 }