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 // EmuFront 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 EmuFront. If not, see <http://www.gnu.org/licenses/>.
23 #include <QSqlTableModel>
26 DbFile::DbFile(QObject *parent) : DbTableModelManager(parent)
31 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
33 int id = record->value(File_Id).toInt();
34 QString name = record->value(File_Name).toString();
35 QString checksum = record->value(File_CheckSum).toString();
36 int size = record->value(File_FileSize).toInt();
37 int type = record->value(File_FileType).toInt();
38 return new EmuFrontFile(id, name, checksum, size, type);
41 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
43 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
45 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
46 tmodel->setFilter(QString("id = %1").arg(plf->getId()));
48 if (tmodel->rowCount() == 1)
50 QSqlRecord record = tmodel->record(0);
51 record.setValue("name", plf->getName());
52 record.setValue("type", plf->getType());
53 record.setValue("checksum", plf->getCheckSum());
54 record.setValue("size", plf->getSize());
55 record.setValue("updatetime", getCurrentTimeStamp());
56 tmodel->setRecord(0, record);
57 ret = tmodel->submitAll();
63 bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
65 const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
67 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
68 tmodel->insertRows(row, 1);
69 // the null value for index will be set implicitily
70 // when we don't assign any value to cell 0 in the sql table model
71 //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
72 tmodel->setData(sqlTableModel->index(row, File_Name), plf->getName());
73 tmodel->setData(sqlTableModel->index(row, File_FileType), plf->getType());
74 tmodel->setData(sqlTableModel->index(row, File_CheckSum), plf->getCheckSum());
75 tmodel->setData(sqlTableModel->index(row, File_FileSize), plf->getSize());
76 tmodel->setData(sqlTableModel->index(row, File_UpdateTime), getCurrentTimeStamp());
77 return tmodel->submitAll();
80 int DbFile::insertFile(const EmuFrontFile *mi)
82 qDebug() << "Inserting file " << mi->getName() << " to db.";
84 q.prepare("INSERT INTO file "
85 "(id, name, type, checksum, size, updatetime) "
86 "VALUES (NULL, :name, :type, :checksum, :size, :updatetime)");
87 q.bindValue(":name", mi->getName());
88 q.bindValue(":type", mi->getType());
89 q.bindValue(":checksum", mi->getCheckSum());
90 q.bindValue(":size", mi->getSize());
91 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
94 id = q.lastInsertId().toInt();
99 int DbFile::countDataObjectRefs(int id) const
102 // return countRows("imagecontainer", "platformid", id);
105 // WARNING: this will delete also all the databindings to selected platform
106 // the delete must be confirmed in the UI
107 bool DbFile::deleteDataObjectFromModel(QModelIndex *index)
109 /*QSqlDatabase::database().transaction();*/
110 QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
111 /*QSqlRecord record = tmodel->record(index->row());
112 int id = record.value(File_Id).toInt();
113 int count = countDataObjectRefs(id);
117 if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
119 qDebug() << "Deleting data bindings failed!";
120 QSqlDatabase::database().rollback();
124 tmodel->removeRow(index->row());
126 return QSqlDatabase::database().commit();
129 QSqlQueryModel* DbFile::getData()
131 QSqlTableModel *model = new QSqlTableModel(this);
132 model->setTable(DB_TABLE_NAME_FILE);
133 model->setSort(File_Name, Qt::AscendingOrder);
134 model->setHeaderData(File_Name, Qt::Horizontal, tr("Name"));
135 model->setHeaderData(File_FileType, Qt::Horizontal, tr("Type"));
136 model->setHeaderData(File_CheckSum, Qt::Horizontal, tr("Checksum"));
137 model->setHeaderData(File_FileSize, Qt::Horizontal, tr("Size"));
138 model->setHeaderData(File_UpdateTime, Qt::Horizontal, tr("Updated"));
139 if (type >= 0) model->setFilter(QString("type=%1").arg(type));
144 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
146 return getDataObject(QString("checksum LIKE '%1'").arg(checksum));