d607ca5d9837e717a217ede3022d036a5dea466e
[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 // 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.
16 //
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/>.
19
20 #include <QDebug>
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlTableModel>
24 #include "dbfile.h"
25
26 DbFile::DbFile(QObject *parent) : DbTableModelManager(parent)
27 {
28     type = -1;
29 }
30
31 EmuFrontObject* DbFile::recordToDataObject(const QSqlRecord *record)
32 {
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);
39 }
40
41 bool DbFile::updateDataObjectToModel(const EmuFrontObject *ob)
42 {
43     const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
44     bool ret = false;
45     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
46     tmodel->setFilter(QString("id = %1").arg(plf->getId()));
47     tmodel->select();
48     if (tmodel->rowCount() == 1)
49     {
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();
58     }
59     resetModel();
60     return ret;
61 }
62
63 bool DbFile::insertDataObjectToModel(const EmuFrontObject *ob)
64 {
65     const EmuFrontFile *plf = dynamic_cast<const EmuFrontFile*>(ob);
66     int row = 0;
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();
78 }
79
80 int DbFile::insertFile(const EmuFrontFile *mi)
81 {
82     qDebug() << "Inserting file " << mi->getName() << " to db.";
83     QSqlQuery q;
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());
92     int id = -1;
93     if (q.exec())
94         id = q.lastInsertId().toInt();
95    return id;
96
97 }
98
99 int DbFile::countDataObjectRefs(int id) const
100 {
101     return 0; // TODO
102     // return countRows("imagecontainer", "platformid", id);
103 }
104
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)
108 {
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);
114     if (count > 0)
115     {
116         QSqlQuery query;
117         if (!query.exec(QString("DELETE FROM imagecontainer WHERE platformid = %1").arg(id)))
118         {
119             qDebug() << "Deleting data bindings failed!";
120             QSqlDatabase::database().rollback();
121             return false;
122         }
123     }*/
124     tmodel->removeRow(index->row());
125     tmodel->submitAll();
126     return QSqlDatabase::database().commit();
127 }
128
129 QSqlQueryModel* DbFile::getData()
130 {
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));
140     model->select();
141     return model;
142 }
143
144 EmuFrontObject* DbFile::getFileByChecksum(QString checksum)
145 {
146     return getDataObject(QString("checksum LIKE '%1'").arg(checksum));
147 }