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