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"
27 const QString DbMediaType::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
30 DbMediaType::DbMediaType(QObject *parent) : DatabaseManager(parent)
32 sqlTableModel = getData();
35 QSqlTableModel* DbMediaType::getDataModel()
40 EmuFrontObject* DbMediaType::getDataObjectFromModel(QModelIndex *index)
42 QSqlRecord record = sqlTableModel->record(index->row());
43 int id = record.value(MediaType_Id).toInt();
44 QString name = record.value(MediaType_Name).toString();
45 QString fileName = record.value(MediaType_Filename).toString();
46 //qDebug() << "Got platform Name " << name << " id " << id;
47 return new MediaType(id, name, fileName);
50 bool DbMediaType::updateDataObjectToModel(const EmuFrontObject *ob)
52 const MediaType *plf = dynamic_cast<const MediaType*>(ob);
54 sqlTableModel->setFilter(QString("id = %1").arg(plf->getId()));
55 sqlTableModel->select();
56 if (sqlTableModel->rowCount() == 1)
58 QSqlRecord record = sqlTableModel->record(0);
59 record.setValue("name", plf->getName());
60 record.setValue("filename", plf->getFilename());
61 sqlTableModel->setRecord(0, record);
62 ret = sqlTableModel->submitAll();
68 bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
70 const MediaType *plf = dynamic_cast<const MediaType*>(ob);
72 sqlTableModel->insertRows(row, 1);
73 // the null value for index will be set implicitily
74 // when we don't assign any value to cell 0 in the sql table model
75 //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
76 sqlTableModel->setData(sqlTableModel->index(row, 1), plf->getName());
77 sqlTableModel->setData(sqlTableModel->index(row, 2), plf->getFilename());
78 return sqlTableModel->submitAll();
81 int DbMediaType::countDataObjectRefs(int id) const
83 return countRows("imagecontainer", "mediatypeid", id);
86 // WARNING: this will delete also all the databindings to selected platform
87 bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
89 QSqlDatabase::database().transaction();
90 QSqlRecord record = sqlTableModel->record(index->row());
91 int id = record.value(MediaType_Id).toInt();
92 qDebug() << "Deleting mediatype " << id;
93 int count = countDataObjectRefs(id);
97 if (!query.exec(QString("DELETE FROM imagecontainer WHERE mediatypeid = %1").arg(id)))
99 qDebug() << "Deleting data bindings failed!";
100 QSqlDatabase::database().rollback();
104 sqlTableModel->removeRow(index->row());
105 sqlTableModel->submitAll();
106 return QSqlDatabase::database().commit();
109 QSqlTableModel* DbMediaType::getData()
111 QSqlTableModel *model = new QSqlTableModel(this);
112 model->setTable(DB_TABLE_NAME_MEDIATYPE);
113 model->setSort(MediaType_Name, Qt::AscendingOrder);
114 model->setHeaderData(MediaType_Name, Qt::Horizontal, tr("Name"));