Filetype to MediaImageContainer instead of MediaImage.
[emufront] / src / models / filepathmodel.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include "filepathmodel.h"
21 #include "emufrontfile.h"
22 #include "emufrontexception.h"
23 #include <QtSql>
24
25 FilePathModel::FilePathModel(QObject *parent) :
26     EmuFrontQueryModel(parent)
27 {
28     refresh();
29 }
30
31 void FilePathModel::refresh()
32 {
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"));
39 }
40
41 QString FilePathModel::constructSelect(QString where) const
42 {
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 "
50                    "FROM filepath "
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 "
54                    "%1 "
55                    "ORDER BY SetupName").arg(where);
56 }
57
58 Qt::ItemFlags FilePathModel::flags(const QModelIndex &index) const
59 {
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;
65     }
66     return flags;
67 }
68
69 bool FilePathModel::setData(const QModelIndex &index, const QVariant &value, int role)
70 {
71     int col = index.column();
72     if (col != FilePath_SetupId &&
73         col != FilePath_Name) {
74         return false;
75     }
76
77     QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), FilePath_Id);
78
79     int id = data(primaryKeyIndex).toInt();
80     clear();
81
82     bool ok;
83     switch(index.column()) {
84     case FilePath_SetupId:
85         ok = setSetup(id, value.toInt());
86         break;
87     case FilePath_Name:
88         ok = setFilePath(id, value.toString());
89         break;
90     default:
91         qDebug() << "File path model, this shouldn't be happening!";
92     }
93     refresh();
94     return ok;
95 }
96
97 bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent)
98 {
99     if (parent.isValid())
100         return false; // This is a flat model
101     if (rowCount() < row)
102         row = rowCount() + 1;
103     int supId = -1;
104     QSqlQuery q;
105     q.exec(QString("SELECT setup.id, "
106            // The following is to get the correct order:
107            "platform.name || ' ' || mediatype.name AS SetupName "
108            "FROM setup "
109            "INNER JOIN platform ON setup.platformid=platform.id "
110            "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
111            "ORDER BY SetupName "
112            "LIMIT 1"));
113     if (q.first()) {
114         supId = q.value(0).toInt();
115         qDebug() << "Got id " << supId << " for default setup.";
116     }
117     else {
118         throw EmuFrontException(tr("No setups yet available for file path configuration!"));
119     }
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);
128         if (!q.exec()) {
129             throw EmuFrontException(tr("Failed creating new filepath row: %1").
130                                     arg(q.lastError().text()));
131         }
132     }
133     endInsertRows();
134     refresh();
135     return true;
136 }
137
138 bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent)
139 {
140     if (parent.isValid()) {
141         return false; // This is a flat model
142     }
143     if (rowCount() < row + count - 1)
144         return false;
145
146     QSqlQuery q;
147     q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
148     QModelIndex primaryIndex;
149     int id = -1;
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);
156         q.exec();
157     }
158     endRemoveRows();
159     refresh();
160     return true;
161 }
162
163 bool FilePathModel::setSetup(int id, int setupId)
164 {
165     QSqlQuery q;
166     q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id"));
167     q.bindValue(":setupid", setupId);
168     q.bindValue(":id", id);
169     return q.exec();
170 }
171
172 bool FilePathModel::setFilePath(int id, QString filePath)
173 {
174     QSqlQuery q;
175     q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id"));
176     q.bindValue(":name", filePath);
177     q.bindValue(":id", id);
178     return q.exec();
179 }