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