Updated the git clone command.
[emufront] / src / models / emufrontquerymodel.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 #include "emufrontquerymodel.h"
22 #include "emufrontobject.h"
23 #include "platform.h"
24 #include "mediatype.h"
25 #include <QtSql>
26
27 EmuFrontQueryModel::EmuFrontQueryModel(QObject *parent) :
28     QSqlQueryModel(parent)
29 { }
30
31 EmuFrontObject* EmuFrontQueryModel::getObject(int row)
32 {
33     if (row < 0 || row > rowCount() - 1)
34         return 0;
35     QSqlRecord rec = record(row);
36     return recordToDataObject(&rec);
37 }
38
39 EmuFrontObject* EmuFrontQueryModel::getDataObject(int id)
40 {
41     filterById(id);
42     return getFilteredDataObject();
43 }
44
45 EmuFrontObject* EmuFrontQueryModel::getDataObject(QString filter)
46 {
47     QList<QString> filters;
48     filters.append(filter);
49     filterDataObjects(filters);
50     return getFilteredDataObject();
51 }
52
53 EmuFrontObject* EmuFrontQueryModel::getFilteredDataObject()
54 {
55     EmuFrontObject *plf = 0;
56     // TODO: if record has more than one the first instance is returned
57     // ... check if this is ok in all cases!
58     if (rowCount() >= 1)
59     {
60         QSqlRecord rec = record(0);
61         if (rec.isEmpty()) {
62             return 0;
63         }
64         else plf = recordToDataObject(&rec);
65     }
66      return plf;
67 }
68
69 EmuFrontObject* EmuFrontQueryModel::getDataObject(const QModelIndex &index)
70 {
71     QSqlRecord rec = record(index.row());
72     return recordToDataObject(&rec);
73 }
74
75 int EmuFrontQueryModel::getCurrentTimeStamp() {
76     return QDateTime::currentDateTime().toTime_t();
77 }
78
79 void EmuFrontQueryModel::filterById(int id)
80 {
81     QList<QString> filters;
82     filters.append(constructFilterById(id));
83     filterDataObjects(filters);
84 }
85
86 /* filters is a list of SQL conditions e.g. setup.id=1 */
87 void EmuFrontQueryModel::filterDataObjects(QList<QString> filters)
88 {
89     QString where = constructWhereByFilters(filters);
90     QString query = constructSelect(where);
91     setQuery(query);
92 }
93
94 QString EmuFrontQueryModel::constructWhereByFilters(QList<QString>filters)
95 {
96     if (filters.count() == 0) return "";
97     QString where = " WHERE ";
98     int c = 0;
99     foreach(QString filter, filters){
100         where.append(QString(" %1 ").arg(filter));
101         if (++c < filters.count())
102             where.append(" AND ");
103     }
104     return where;
105 }
106
107 void EmuFrontQueryModel::clearFilters()
108 {
109     setQuery(constructSelect());
110 }
111