DbMediaImagePath replaced by more generic DbFilePath.
[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 const QString DbMediaType::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
28
29
30 DbMediaType::DbMediaType(QObject *parent) : DatabaseManager(parent)
31 {
32     sqlTableModel = getData();
33 }
34
35 QSqlTableModel* DbMediaType::getDataModel()
36 {
37     return sqlTableModel;
38 }
39
40 EmuFrontObject* DbMediaType::getDataObjectFromModel(QModelIndex *index)
41 {
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);
48 }
49
50 bool DbMediaType::updateDataObjectToModel(const EmuFrontObject *ob)
51 {
52     const MediaType *plf = dynamic_cast<const MediaType*>(ob);
53     bool ret = false;
54     sqlTableModel->setFilter(QString("id = %1").arg(plf->getId()));
55     sqlTableModel->select();
56     if (sqlTableModel->rowCount() == 1)
57     {
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();
63     }
64     resetModel();
65     return ret;
66 }
67
68 bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
69 {
70     const MediaType *plf = dynamic_cast<const MediaType*>(ob);
71     int row = 0;
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();
79 }
80
81 int DbMediaType::countDataObjectRefs(int id) const
82 {
83     return countRows("imagecontainer", "mediatypeid", id);
84 }
85
86 // WARNING: this will delete also all the databindings to selected platform
87 bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
88 {
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);
94     if (count > 0)
95     {
96         QSqlQuery query;
97         if (!query.exec(QString("DELETE FROM imagecontainer WHERE mediatypeid = %1").arg(id)))
98         {
99             qDebug() << "Deleting data bindings failed!";
100             QSqlDatabase::database().rollback();
101             return false;
102         }
103     }
104     sqlTableModel->removeRow(index->row());
105     sqlTableModel->submitAll();
106     return QSqlDatabase::database().commit();
107 }
108
109 QSqlTableModel* DbMediaType::getData()
110 {
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"));
115     model->select();
116     return model;
117 }