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>
26 DbFile::DbFile(QObject *parent) : DbTableModelManager(parent)
29 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
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);
39 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
41 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
43 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
44 tmodel->setFilter(QString("id = %1").arg(plf->getId()));
46 if (tmodel->rowCount() == 1)
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();
61 bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
63 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
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();
79 int DbFile::countDataObjectRefs(int id) const
82 // return countRows("imagecontainer", "platformid", id);
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)
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);
97 if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
99 qDebug() << "Deleting data bindings failed!";
100 QSqlDatabase::database().rollback();
104 tmodel->removeRow(index->row());
106 return QSqlDatabase::database().commit();
109 QSqlQueryModel* DbFile::getData()
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"));