2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
22 #include <QSqlRelationalTableModel>
24 #include "dbemufrontfileobject.h"
26 DbEmuFrontFileObject::DbEmuFrontFileObject(QObject *parent)
27 : DbTableModelManager(parent)
29 dbFile = new DbFile(this);
32 EmuFrontObject* DbEmuFrontFileObject::recordToDataObject(const QSqlRecord *record)
34 int id = record->value(EmuFrontFileObject_Id).toInt();
35 QString name = record->value(EmuFrontFileObject_Name).toString();
36 int fileId = record->value(EmuFrontFileObject_FileId).toInt();
40 EmuFrontObject *o = dbFile->getDataObject(fileId);
41 f = dynamic_cast<EmuFrontFile*>(o);
43 EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
47 bool DbEmuFrontFileObject::updateDataObjectToModel(const EmuFrontObject *ob)
49 const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject*>(ob);
51 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
52 tmodel->setFilter(QString("id = %1").arg(plf->getId()));
54 if (tmodel->rowCount() == 1)
56 QSqlRecord record = tmodel->record(0);
57 record.setValue("name", plf->getName());
59 record.setValue("fileid", plf->getFile()->getId());
60 else record.setNull("fileid");
61 tmodel->setRecord(0, record);
62 ret = tmodel->submitAll();
68 bool DbEmuFrontFileObject::insertDataObjectToModel(const EmuFrontObject *ob)
70 const EmuFrontFileObject *plf = dynamic_cast<const EmuFrontFileObject *>(ob);
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());
80 tmodel->setData(sqlTableModel->index(row, EmuFrontFileObject_FileId), plf->getFile()->getId());
81 return tmodel->submitAll();
84 int DbEmuFrontFileObject::countDataObjectRefs(int id) const
87 // return countRows("imagecontainer", "platformid", id);
90 // WARNING: this will delete also all the databindings to selected platform
91 bool DbEmuFrontFileObject::deleteDataObjectFromModel(QModelIndex *index)
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);
102 if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
104 qDebug() << "Deleting data bindings failed!";
105 QSqlDatabase::database().rollback();
109 tmodel->removeRow(index->row());
111 return QSqlDatabase::database().commit();
114 QSqlQueryModel* DbEmuFrontFileObject::getData()
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"));