DbFilePath (insert and update functionality). getDataObjectFromModel and
[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 "../dataobjects/filepathobject.h"
23 #include "dbfilepath.h"
24 #include "dbplatform.h"
25 #include "dbmediatype.h"
26
27 DbFilePath::DbFilePath(QObject *parent) : DatabaseManager(parent)
28 {
29     dbPlatform = new DbPlatform(this);
30     dbMediaType = new DbMediaType(this);
31     sqlTableModel = getData();
32 }
33
34 QSqlTableModel* DbFilePath::getDataModel()
35 {
36     return sqlTableModel;
37 }
38
39 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec) const
40 {
41     int id = rec->value(FilePath_Id).toInt();
42     QString fpath = rec->value(FilePath_Name).toString();
43     int plfId = rec->value(FilePath_PlatformId).toInt();
44     int mtId = rec->value(FilePath_MediaTypeId).toInt();
45     int fileType = rec->value(FilePath_FileTypeId).toInt();
46     Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
47     MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
48        // TODO
49     //int lastScanned = 0;
50     return new FilePathObject(id, fpath, fpath, fileType, plf, mt);
51 }
52
53 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
54 {
55     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
56     bool ret = false;
57     sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
58     sqlTableModel->select();
59     if (sqlTableModel->rowCount() == 1)
60     {
61         QSqlRecord rec = sqlTableModel->record(0);
62         rec.setValue("name", fpo->getName());
63         rec.setValue("filetypeid", fpo->getFiletype());
64
65         Platform *pl = fpo->getPlatform();
66         MediaType *mt = fpo->getMediaType();
67         if (pl) rec.setValue("platformid", pl->getId());
68         if (mt) rec.setValue("mediatypeid", mt->getId());
69
70         // TODO
71         //rec.setValue("lastscanned", 0);
72     }
73     return ret;
74 }
75
76 bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
77 {
78     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
79     int row = 0;
80     sqlTableModel->insertRows(row, 1);
81
82     Platform *pl = fpo->getPlatform();
83     MediaType *mt = fpo->getMediaType();
84
85     //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
86     sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
87     sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
88     // not all the file path types have platform and/or media type
89     if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
90     if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
91     // TODO:
92     sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
93     return sqlTableModel->submitAll();
94 }
95
96 int DbFilePath::countDataObjectRefs(int id) const
97 {
98     return 0;
99 }
100
101 // WARNING: this will delete also all the databindings to selected media image path
102 bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
103 {
104     return false;
105 }
106
107 QSqlTableModel* DbFilePath::getData()
108 {
109    QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
110    model->setTable(DB_TABLE_NAME_FILEPATH);
111    model->setRelation(FilePath_PlatformId,
112        QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
113    model->setRelation(FilePath_MediaTypeId,
114        QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
115    model->setSort(FilePath_Name, Qt::AscendingOrder);
116    model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
117    model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
118    model->select();
119    return model;
120 }