b77d48b095abbd5dce0584a8b1cd74659e340e6d
[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 bool DbFilePath::setScanned(const EmuFrontObject *ob)
68 {
69     QSqlQuery q;
70     q.prepare("UPDATE filepath SET lastscanned=:lastscanned WHERE id=:id");
71     q.bindValue(":lastscanned", getCurrentTimeStamp());
72     q.bindValue(":id", ob->getId());
73     bool ret = q.exec();
74     if (ret) resetModel();
75     return ret;
76 }
77
78 /* Returns id of inserted data item after succesful insert, -1 if insert failed */
79 int DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
80 {
81     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
82     QSqlQuery query;
83     query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
84         "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
85     query.bindValue(":name", fpo->getName());
86     query.bindValue(":filetypeid", fpo->getType());
87     if (fpo->getSetup())
88         query.bindValue(":setupid", fpo->getSetup()->getId());
89     query.bindValue(":lastscanned", 0); // TODO
90     int id = -1;
91     if (query.exec())
92         id = query.lastInsertId().toInt();
93     return id;
94 }
95
96 // WARNING: this will delete also all the databindings to selected media image path
97 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
98 {
99     return false;
100 }
101
102 bool DbFilePath::deleteDataObject(int id) const
103 {
104     if (countDataObjectRefs(id) > 0)
105         // TODO
106         return false;
107     QSqlQuery q;
108     q.prepare(QString("DELETE FROM filepath WHERE id=:id"));
109     q.bindValue(":id", id);
110     return q.exec();
111 }
112
113 QString DbFilePath::constructSelect(QString where) const
114 {
115     return QString("SELECT filepath.id AS FilePathId, "
116             "filepath.name AS Name, "
117             "datetime(filepath.lastscanned, 'unixepoch') AS LastScanned, "
118             "setup.id AS SetupId, "
119             "platform.name || ' ' || mediatype.name AS SetupName, "
120             "filepath.filetypeid "
121             "FROM filepath "
122             "INNER JOIN setup ON filepath.setupid=setup.id  "
123             "INNER JOIN platform ON setup.platformid=platform.id "
124             "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
125             "%1 "
126             "ORDER BY SetupName").arg(where);
127 }
128
129 QString DbFilePath::constructFilterById(int id) const
130 {
131     return QString("filepath.id = %1").arg(id);
132 }
133
134 QString DbFilePath::constructSelectById(int id) const
135 {
136     QString where = QString("WHERE %1").arg(constructFilterById(id));
137     return constructSelect(where);
138 }
139
140 QSqlQueryModel* DbFilePath::getData()
141 {
142     QSqlQueryModel *model = new QSqlQueryModel(this);
143     model->setQuery(constructSelect());
144     model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
145     model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
146     model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
147     model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
148     model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
149     return model;
150 }
151
152 QString DbFilePath::getCountRefsSelect(int id) const
153 {
154     /* filepath is referenced from mediaimagecontainer */
155     return QString("SELECT count(*) FROM filepath"
156               "INNER JOIN mediaimagecontainer "
157               "ON filepath.id=mediaimagecontainer.filepathid"
158               "WHERE filepath.id=%1").arg(id);
159 }