fa71cfcc531c52efa342ea458d7ff0e514af32d6
[emufront] / src / models / emufrontfileobjectmodel.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 <QtSql>
23 #include "emufrontfileobjectmodel.h"
24
25 EmuFrontFileObjectModel::EmuFrontFileObjectModel(QObject *parent) :
26     EmuFrontQueryModel(parent)
27 { }
28
29 Qt::ItemFlags EmuFrontFileObjectModel::flags(const QModelIndex &index) const
30 {
31     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
32     if (index.column() == EmuFrontFileObject_Name) {
33         flags |= Qt::ItemIsEditable;
34     }
35     return flags;
36 }
37
38 bool EmuFrontFileObjectModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
39 {
40     if(index.column() != EmuFrontFileObject_Name)
41         return false;
42
43     QModelIndex primaryKeyIndex
44         = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
45
46     int id = data(primaryKeyIndex).toInt();
47     clear();
48
49     bool ok;
50     if (index.column() == EmuFrontFileObject_Name) {
51         ok = setName(id, value.toString());
52     }
53
54     refresh();
55     return ok;
56 }
57
58 void EmuFrontFileObjectModel::refresh()
59  {
60      setQuery(constructSelect());
61      setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("ID"));
62      setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
63      setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("FileID"));
64      setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File Name"));
65      setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File Checksum"));
66      setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File Size"));
67      setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File Type"));
68      setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File Updated"));
69  }
70
71 QString EmuFrontFileObjectModel::constructSelect(QString where) const
72 {
73     return QString("SELECT maintbl.id AS FileObjectId, "
74             "maintbl.name AS Name, "
75             "file.id AS FileId, "
76             "file.name AS FileName, "
77             "file.type AS FileType, "
78             "file.checksum AS FileChecksum, "
79             "file.size AS FileSize, "
80             "file.updatetime AS FileUpdateTime "
81             "FROM %1 AS maintbl "
82             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
83             "%2 "
84             "ORDER BY Name").arg(tableName).arg(where);
85 }
86
87 bool EmuFrontFileObjectModel::setName(int id, const QString &name)
88 {
89     QSqlQuery query;
90     query.prepare(QString("update %1 set name = :name where id = :id").arg(tableName));
91     query.bindValue(":name", name);
92     query.bindValue(":id", id);
93     return query.exec();
94 }
95
96 bool EmuFrontFileObjectModel::insertRows(int row, int count, const QModelIndex &parent)
97 {
98     if (parent.isValid())
99         return false; // This is a flat model
100     if (rowCount() < row)
101         row = rowCount() + 1;
102     QSqlQuery q;
103     q.prepare(QString("INSERT INTO %1 (id, name, fileid) "
104         " VALUES (NULL, '', NULL) ").arg(tableName));
105     beginInsertRows(QModelIndex(), row, row + count - 1);
106     for (int i = 0; i < count; ++i) {
107         q.exec();
108     }
109     endInsertRows();
110     refresh();
111     return true;
112 }
113
114 bool EmuFrontFileObjectModel::removeRows(int row, int count, const QModelIndex &parent)
115 {
116      if (parent.isValid()) {
117         return false; // This is a flat model
118     }
119     if (rowCount() < row + count - 1)
120         return false;
121
122     QSqlQuery q;
123     q.prepare(QString("DELETE FROM %1 WHERE id=:id").arg(tableName));
124     QModelIndex primaryIndex;
125     int id = -1;
126     beginRemoveRows(QModelIndex(), row, row + count - 1);
127     for(int i = 0; i < count; ++i) {
128         primaryIndex = QSqlQueryModel::index(row + i, EmuFrontFileObject_Id);
129         id = data(primaryIndex).toInt();
130         qDebug() << "Removing data item with id " << id;
131         q.bindValue(":id", id);
132         q.exec();
133     }
134     endRemoveRows();
135     refresh();
136     return true;
137 }