DbEmuFrontFileObject is now a DbQueryModelManager instead of
[emufront] / src / db / dbfilepath.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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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 <QSqlRelationalTableModel>
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include "../dataobjects/filepathobject.h"
24 #include "dbfilepath.h"
25 #include "dbsetup.h"
26
27 DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
28 {
29     dbSetup = new DbSetup(this);
30 }
31
32 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec)
33 {
34     int id = rec->value(FilePath_Id).toInt();
35     QString fpath = rec->value(FilePath_Name).toString();
36     int setupId = rec->value(FilePath_SetupId).toInt();
37     //int fileType = rec->value(FilePath_FileTypeId).toInt();
38     Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId));
39        // TODO
40     //int lastScanned = 0;
41     return new FilePathObject(id, fpath, /* TODO */ 0, sup);
42 }
43
44 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
45 {
46     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
47     bool ret = false;
48     QSqlQuery query;
49     query.prepare(QString("UPDATE filepath SET "
50         "name=:name, "
51         "filetypeid=:filetypeid, "
52         "setupid=:setupid, "
53         "lastscanned=:lastscanned "
54         "WHERE id=:id"));
55     query.bindValue(":name", fpo->getName());
56     query.bindValue(":filetypeid", fpo->getType());
57     query.bindValue(":lastscanned", 0); // TODO
58     query.bindValue(":id", fpo->getId());
59     ret = query.exec();
60     if (ret) resetModel();
61     return ret;
62 }
63
64 int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
65 {
66     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
67     QSqlQuery query;
68     query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
69         "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
70     query.bindValue(":name", fpo->getName());
71     query.bindValue(":filetypeid", fpo->getType());
72     if (fpo->getSetup())
73         query.bindValue(":setupid", fpo->getSetup()->getId());
74     query.bindValue(":lastscanned", 0); // TODO
75     int id = -1;
76     if (query.exec())
77         id = query.lastInsertId().toInt();
78     return id;
79 }
80
81 int DbFilePath::countDataObjectRefs(int id) const
82 {
83     return 0;
84 }
85
86 // WARNING: this will delete also all the databindings to selected media image path
87 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
88 {
89     return false;
90 }
91
92 QString DbFilePath::constructSelect(QString whereClause) const
93 {
94     QString where = whereClause.isEmpty()
95         ? "" : QString("WHERE ").append(whereClause);
96
97     return QString("SELECT filepath.id AS FilePathId, "
98             "filepath.name AS Name, "
99             "filepath.lastscanned AS LastScanned, "
100             "setup.id AS SetupId, "
101             "platform.name || ' ' || mediatype.name AS SetupName "
102             "FROM filepath "
103             "INNER JOIN setup ON filepath.setupid=setup.id  "
104             "INNER JOIN platform ON setup.platformid=platform.id "
105             "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
106             "%1 "
107             "ORDER BY SetupName").arg(where);
108 }
109
110 QString DbFilePath::constructFilterById(int id) const
111 {
112     return QString("filepath.id = %1").arg(id);
113 }
114
115 QString DbFilePath::constructSelectById(int id) const
116 {
117     return constructSelect(constructFilterById(id));
118 }
119
120 QSqlQueryModel* DbFilePath::getData()
121 {
122     QSqlQueryModel *model = new QSqlQueryModel;
123     model->setQuery(constructSelect());
124     model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
125     model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
126     model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
127     model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
128     model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
129     return model;
130 }