Fixed (QSqlRelationTableModel didn't show data, since not file
[emufront] / src / db / dbfile.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 <QDateTime>
24 #include "dbfile.h"
25
26 DbFile::DbFile(QObject *parent) : DbTableModelManager(parent)
27 { }
28
29 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
30 {
31     int id = record->value(File_Id).toInt();
32     QString name = record->value(File_Name).toString();
33     QString checksum = record->value(File_CheckSum).toString();
34     int size = record->value(File_FileSize).toInt();
35     int type = record->value(File_FileType).toInt();
36     return new EmuFrontFile(id, name, checksum, size, type);
37 }
38
39 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
40 {
41     const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
42     bool ret = false;
43     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
44     tmodel->setFilter(QString("id = %1").arg(plf->getId()));
45     tmodel->select();
46     if (tmodel->rowCount() == 1)
47     {
48         QSqlRecord record = tmodel->record(0);
49         record.setValue("name", plf->getName());
50         record.setValue("type", plf->getType());
51         record.setValue("checksum", plf->getCheckSum());
52         record.setValue("size", plf->getSize());
53         record.setValue("updatetime", QDateTime::currentDateTime().toTime_t());
54         tmodel->setRecord(0, record);
55         ret = tmodel->submitAll();
56     }
57     resetModel();
58     return ret;
59 }
60
61 bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
62 {
63     const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(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, File_Name), plf->getName());
71     tmodel->setData(sqlTableModel->index(row, File_FileType), plf->getType());
72     tmodel->setData(sqlTableModel->index(row, File_CheckSum), plf->getCheckSum());
73     tmodel->setData(sqlTableModel->index(row, File_FileSize), plf->getSize());
74     tmodel->setData(sqlTableModel->index(row, File_UpdateTime),
75         QDateTime::currentDateTime().toTime_t());
76     return tmodel->submitAll();
77 }
78
79 int DbFile::countDataObjectRefs(int id) const
80 {
81     return 0; // TODO
82     // return countRows("imagecontainer", "platformid", id);
83 }
84
85 // WARNING: this will delete also all the databindings to selected platform
86 // the delete must be confirmed in the UI
87 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
88 {
89     /*QSqlDatabase::database().transaction();*/
90     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
91     /*QSqlRecord record = tmodel->record(index->row());
92     int id = record.value(File_Id).toInt();
93     int count = countDataObjectRefs(id);
94     if (count > 0)
95     {
96         QSqlQuery query;
97         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
98         {
99             qDebug() << "Deleting data bindings failed!";
100             QSqlDatabase::database().rollback();
101             return false;
102         }
103     }*/
104     tmodel->removeRow(index->row());
105     tmodel->submitAll();
106     return QSqlDatabase::database().commit();
107 }
108
109 QSqlQueryModel* DbFile::getData()
110 {
111     QSqlTableModel *model = new QSqlTableModel(this);
112     model->setTable(DB_TABLE_NAME_FILE);
113     model->setSort(File_Name, Qt::AscendingOrder);
114     model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
115     model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
116     model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
117     model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
118     model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
119     model->select();
120     return model;
121 }