EmuFront ... NOT Foobar :D
[emufront] / src / db / dbemufrontfileobject.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 <QSqlRecord>
21 #include <QSqlQuery>
22 #include <QSqlRelationalTableModel>
23 #include <QDebug>
24 #include "dbemufrontfileobject.h"
25
26 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
27     : DbTableModelManager(parent)
28 {
29     dbFile = new DbFile(this);
30 }
31
32 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
33 {
34     int id = record->value(EmuFrontFileObject_Id).toInt();
35     QString name = record->value(EmuFrontFileObject_Name).toString();
36     int fileId = record->value(EmuFrontFileObject_FileId).toInt();
37     EmuFrontFile *f = 0;
38     if (fileId > 0)
39     {
40         EmuFrontObject *o = dbFile->getDataObject(fileId);
41         f = dynamic_cast<EmuFrontFile*>(o);
42     }
43     EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
44     return efo;
45 }
46
47 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
48 {
49     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
50     bool ret = false;
51     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
52     tmodel->setFilter(QString("id = %1").arg(plf->getId()));
53     tmodel->select();
54     if (tmodel->rowCount() == 1)
55     {
56         QSqlRecord record = tmodel->record(0);
57         record.setValue("name", plf->getName());
58         if (plf->getFile())
59             record.setValue("fileid", plf->getFile()->getId());
60         else record.setNull("fileid");
61         tmodel->setRecord(0, record);
62         ret = tmodel->submitAll();
63     }
64     resetModel();
65     return ret;
66 }
67
68 bool DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
69 {
70     const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
71     int row = 0;
72     if (!sqlTableModel) sqlTableModel = getDataModel();
73     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
74     tmodel->insertRows(row, 1);
75     // the null value for index will be set implicitily
76     // when we don't assign any value to cell 0 in the sql table model
77     //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
78     tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_Name), plf->getName());
79     if (plf->getFile())
80         tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_FileId), plf->getFile()->getId());
81     return tmodel->submitAll();
82 }
83
84 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
85 {
86     return 0; // TODO
87     // return countRows("imagecontainer", "platformid", id);
88 }
89
90 // WARNING: this will delete also all the databindings to selected platform
91 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
92 {
93     //QSqlDatabase::database().transaction();
94     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
95     /*QSqlRecord record = tmodel->record(index->row());
96     int id = record.value(EmuFrontFileObject_Id).toInt();
97     qDebug() << "Deleting platform " << id;
98     int count = countDataObjectRefs(id);
99     if (count > 0)
100     {
101         QSqlQuery query;
102         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
103         {
104             qDebug() << "Deleting data bindings failed!";
105             QSqlDatabase::database().rollback();
106             return false;
107         }
108     }*/
109     tmodel->removeRow(index->row());
110     tmodel->submitAll();
111     return QSqlDatabase::database().commit();
112 }
113
114 QSqlQueryModel* DbEmuFrontFileObject::getData()
115 {
116     QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
117     model->setTable(tableName);
118     // TODO: table realtion model seems not to be suitable for this
119     // since not always does data object have a file relation:
120     //model->setRelation(EmuFrontFileObject_FileId, QSqlRelation("file", "id", "name"));
121     model->setSort(EmuFrontFileObject_Name, Qt::AscendingOrder);
122     model->setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
123     model->setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("Icon"));
124     model->select();
125     return model;
126 }