EmuFront ... NOT Foobar :D
[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     /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
62     sqlTableModel->select();
63     if (sqlTableModel->rowCount() == 1)
64     {
65         QSqlRecord rec = sqlTableModel->record(0);
66         rec.setValue("name", fpo->getName());
67         rec.setValue("filetypeid", fpo->getFilet.bype());
68
69         Setup *sup = fpo->getSetup();
70         if (sup) rec.setValue("setupid", sup->getId());
71
72         // TODO
73         //rec.setValue("lastscanned", 0);
74     }*/
75     return ret;
76 }
77
78 bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
79 {
80     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
81     QSqlQuery query;
82     query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
83         "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
84     query.bindValue(":name", fpo->getName());
85     query.bindValue(":filetypeid", fpo->getType());
86     if (fpo->getSetup())
87         query.bindValue(":setupid", fpo->getSetup()->getId());
88     query.bindValue(":lastscanned", 0); // TODO
89     return query.exec();
90     /*int row = 0;
91
92     sqlTableModel->insertRows(row, 1);
93
94
95     Setup *sup = fpo->getSetup();
96     //Platform *pl = fpo->getPlatform();
97     //MediaType *mt = fpo->getMediaType();
98
99     //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
100     sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
101     sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
102     // not all the file path types have platform and/or media type
103     //if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
104     //if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
105     if (sup) sqlTableModel->setData(sqlTableModel->index(row, FilePath_SetupId), sup->getId());
106     // TODO:
107     sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
108     return sqlTableModel->submitAll();*/
109 }
110
111 int DbFilePath::countDataObjectRefs(int id) const
112 {
113     return 0;
114 }
115
116 // WARNING: this will delete also all the databindings to selected media image path
117 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
118 {
119     return false;
120 }
121
122 QString DbFilePath::constructSelect(QString whereClause) const
123 {
124     QString where = whereClause.isEmpty()
125         ? "" : QString("WHERE ").append(whereClause);
126
127     return QString("SELECT filepath.id AS FilePathId, "
128             "filepath.name AS Name, "
129             "filepath.lastscanned AS LastScanned, "
130             "setup.id AS SetupId, "
131             "platform.name || ' ' || mediatype.name AS SetupName "
132             "FROM filepath "
133             "INNER JOIN setup ON filepath.setupid=setup.id  "
134             "INNER JOIN platform ON setup.platformid=platform.id "
135             "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
136             "%1 "
137             "ORDER BY SetupName").arg(where);
138 }
139
140 QString DbFilePath::constructSelectById(int id) const
141 {
142     return constructSelect(QString("filepath.id = %1").arg(id));
143 }
144
145 QSqlQueryModel* DbFilePath::getData()
146 {
147     QSqlQueryModel *model = new QSqlQueryModel;
148     model->setQuery(constructSelect());
149     model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
150     model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
151     model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
152     model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
153     model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
154     return model;
155
156             //"platform.name, mediatype.name
157    /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
158    model->setTable(DB_TABLE_NAME_FILEPATH);*/
159    /*model->setRelation(FilePath_PlatformId,
160        QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
161    model->setRelation(FilePath_MediaTypeId,
162        QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"e));*/
163     /*model->setRelation(FilePath_SetupId,
164         QSqlRelation(DB_TABLE_NAME_SETUP, "id", ""));
165            model->setSort(FilePath_Name, Qt::AscendingOrder);*/
166
167    //model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
168    //model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
169    /*model->select();
170    return model;*/
171 }