Initial implementation of setup editor. Some refactoring and bug hunting
[emufront] / src / db / dbmediatype.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 // 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.
16 //
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/>.
19
20 #include <QSqlRecord>
21 #include <QSqlQuery>
22 #include <QSqlTableModel>
23 #include <QDebug>
24 #include "dbmediatype.h"
25 #include "../dataobjects/mediatype.h"
26
27
28 DbMediaType::DbMediaType(QObject *parent) : DbTableModelManager(parent)
29 {
30     qDebug() << "Creating Media type database manager";
31     sqlTableModel = 0; //getData();
32 }
33
34 EmuFrontObject* DbMediaType::recordToDataObject(const QSqlRecord *record) const
35 {
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);
40 }
41
42 bool DbMediaType::updateDataObjectToModel(const EmuFrontObject *ob)
43 {
44     const MediaType *plf = dynamic_cast<const MediaType*>(ob);
45     bool ret = false;
46     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
47     tmodel->setFilter(QString("id = %1").arg(plf->getId()));
48     tmodel->select();
49     if (tmodel->rowCount() == 1)
50     {
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();
56     }
57     resetModel();
58     return ret;
59 }
60
61 bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
62 {
63     const MediaType *plf = dynamic_cast<const MediaType*>(ob);
64     int row = 0;
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();
73 }
74
75 int DbMediaType::countDataObjectRefs(int id) const
76 {
77     return countRows("imagecontainer", "mediatypeid", id);
78 }
79
80 // WARNING: this will delete also all the databindings to selected platform
81 bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
82 {
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);
89     if (count > 0)
90     {
91         QSqlQuery query;
92         if (!query.exec(QString("DELETE FROM imagecontainer WHERE mediatypeid = %1").arg(id)))
93         {
94             qDebug() << "Deleting data bindings failed!";
95             QSqlDatabase::database().rollback();
96             return false;
97         }
98     }
99     tmodel->removeRow(index->row());
100     tmodel->submitAll();
101     return QSqlDatabase::database().commit();
102 }
103
104 QSqlQueryModel* DbMediaType::getData()
105 {
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"));
110     model->select();
111     qDebug() << "Created a data model for media type data";
112     return model;
113 }
114