-// EmuFront
-// Copyright 2010 Mikko Keinänen
-//
-// This file is part of EmuFront.
-//
-//
-// EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Foobar is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
-
-#include <QSqlRecord>
-#include <QSqlQuery>
-#include <QSqlTableModel>
-#include <QDebug>
+/*
+** EmuFront
+** Copyright 2010 Mikko Keinänen
+**
+** This file is part of EmuFront.
+**
+**
+** EmuFront is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License version 2 as published by
+** the Free Software Foundation and appearing in the file gpl.txt included in the
+** packaging of this file.
+**
+** EmuFront is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
+*/
#include "dbmediatype.h"
-#include "../dataobjects/mediatype.h"
-
-DbMediaType::DbMediaType(QObject *parent) : DbTableModelManager(parent)
+DbMediaType::DbMediaType(QObject *parent) : DbEmuFrontFileObject(parent)
{
- qDebug() << "Creating Media type database manager";
- sqlTableModel = 0; //getData();
+ tableName = DbMediaType::DB_TABLE_NAME_MEDIATYPE;
}
-EmuFrontObject* DbMediaType::recordToDataObject(const QSqlRecord *record) const
-{
- int id = record->value(MediaType_Id).toInt();
- QString name = record->value(MediaType_Name).toString();
- QString fileName = record->value(MediaType_Filename).toString();
- return new MediaType(id, name, fileName);
-}
+EmuFrontObject* DbMediaType::createEmuFrontFileObject(int id, QString name, EmuFrontFile *f)
+{ return new MediaType(id, name, f); }
-bool DbMediaType::updateDataObjectToModel(const EmuFrontObject *ob)
+QString DbMediaType::getCountRefsSelect(int id) const
{
- const MediaType *plf = dynamic_cast<const MediaType*>(ob);
- bool ret = false;
- QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
- tmodel->setFilter(QString("id = %1").arg(plf->getId()));
- tmodel->select();
- if (tmodel->rowCount() == 1)
- {
- QSqlRecord record = sqlTableModel->record(0);
- record.setValue("name", plf->getName());
- record.setValue("filename", plf->getFilename());
- tmodel->setRecord(0, record);
- ret = tmodel->submitAll();
- }
- resetModel();
- return ret;
+ return QString("SELECT count(*) FROM mediatype "
+ "INNER JOIN setup ON mediatype.id=setup.mediatypeid "
+ "WHERE mediatype.id=%1").arg(id);
}
-
-bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
-{
- const MediaType *plf = dynamic_cast<const MediaType*>(ob);
- int row = 0;
- QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
- tmodel->insertRows(row, 1);
- // the null value for index will be set implicitily
- // when we don't assign any value to cell 0 in the sql table model
- //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
- tmodel->setData(sqlTableModel->index(row, MediaType_Name), plf->getName());
- tmodel->setData(sqlTableModel->index(row, MediaType_Filename), plf->getFilename());
- return tmodel->submitAll();
-}
-
-int DbMediaType::countDataObjectRefs(int id) const
-{
- return countRows("imagecontainer", "mediatypeid", id);
-}
-
-// WARNING: this will delete also all the databindings to selected platform
-bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
-{
- QSqlDatabase::database().transaction();
- QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
- QSqlRecord record = tmodel->record(index->row());
- int id = record.value(MediaType_Id).toInt();
- qDebug() << "Deleting mediatype " << id;
- int count = countDataObjectRefs(id);
- if (count > 0)
- {
- QSqlQuery query;
- if (!query.exec(QString("DELETE FROM imagecontainer WHERE mediatypeid = %1").arg(id)))
- {
- qDebug() << "Deleting data bindings failed!";
- QSqlDatabase::database().rollback();
- return false;
- }
- }
- tmodel->removeRow(index->row());
- tmodel->submitAll();
- return QSqlDatabase::database().commit();
-}
-
-QSqlQueryModel* DbMediaType::getData()
-{
- QSqlTableModel *model = new QSqlTableModel(this);
- model->setTable(DB_TABLE_NAME_MEDIATYPE);
- model->setSort(MediaType_Name, Qt::AscendingOrder);
- model->setHeaderData(MediaType_Name, Qt::Horizontal, tr("Name"));
- model->select();
- qDebug() << "Created a data model for media type data";
- return model;
-}
-