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