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