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 // 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.
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/>.
22 #include <QSqlTableModel>
24 #include "dbmediatype.h"
25 #include "../dataobjects/mediatype.h"
28 DbMediaType::DbMediaType(QObject *parent) : DbTableModelManager(parent)
30 qDebug() << "Creating Media type database manager";
31 sqlTableModel = 0; //getData();
34 EmuFrontObject* DbMediaType::recordToDataObject(const QSqlRecord *record) const
36 int id = record->value(MediaType_Id).toInt();
37 QString name = record->value(MediaType_Name).toString();
38 QString fileName = record->value(MediaType_Filename).toString();
39 return new MediaType(id, name, fileName);
42 bool DbMediaType::updateDataObjectToModel(const EmuFrontObject *ob)
44 const MediaType *plf = dynamic_cast<const MediaType*>(ob);
46 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
47 tmodel->setFilter(QString("id = %1").arg(plf->getId()));
49 if (tmodel->rowCount() == 1)
51 QSqlRecord record = sqlTableModel->record(0);
52 record.setValue("name", plf->getName());
53 record.setValue("filename", plf->getFilename());
54 tmodel->setRecord(0, record);
55 ret = tmodel->submitAll();
61 bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
63 const MediaType *plf = dynamic_cast<const MediaType*>(ob);
65 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
66 tmodel->insertRows(row, 1);
67 // the null value for index will be set implicitily
68 // when we don't assign any value to cell 0 in the sql table model
69 //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
70 tmodel->setData(sqlTableModel->index(row, MediaType_Name), plf->getName());
71 tmodel->setData(sqlTableModel->index(row, MediaType_Filename), plf->getFilename());
72 return tmodel->submitAll();
75 int DbMediaType::countDataObjectRefs(int id) const
77 return countRows("imagecontainer", "mediatypeid", id);
80 // WARNING: this will delete also all the databindings to selected platform
81 bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
83 QSqlDatabase::database().transaction();
84 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
85 QSqlRecord record = tmodel->record(index->row());
86 int id = record.value(MediaType_Id).toInt();
87 qDebug() << "Deleting mediatype " << id;
88 int count = countDataObjectRefs(id);
92 if (!query.exec(QString("DELETE FROM imagecontainer WHERE mediatypeid = %1").arg(id)))
94 qDebug() << "Deleting data bindings failed!";
95 QSqlDatabase::database().rollback();
99 tmodel->removeRow(index->row());
101 return QSqlDatabase::database().commit();
104 QSqlQueryModel* DbMediaType::getData()
106 QSqlTableModel *model = new QSqlTableModel(this);
107 model->setTable(DB_TABLE_NAME_MEDIATYPE);
108 model->setSort(MediaType_Name, Qt::AscendingOrder);
109 model->setHeaderData(MediaType_Name, Qt::Horizontal, tr("Name"));
111 qDebug() << "Created a data model for media type data";