Created temporary object was not deleted.
[emufront] / src / db / dbmediaimage.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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
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 <QSqlTableModel>
22 #include <QSqlQuery>
23 #include <QSqlRecord>
24 #include "dbmediaimage.h"
25
26
27 DbMediaImage::DbMediaImage(QObject *parent)
28     : DbFile(parent)
29 {
30     type = EmuFrontFile::FileType_MediaImage;
31     tableName = DbMediaImage::DB_TABLE_NAME_FILE;
32 }
33
34 /*bool DbMediaImage::updateDataObjectToModel(const EmuFrontObject *efo)
35 {
36     // TODO
37     return false;
38 }
39
40 bool DbMediaImage::insertDataObjectToModel(const EmuFrontObject *efo)
41 {
42     // TODO
43     return false;
44 }
45
46 bool DbMediaImage::deleteDataObjectFromModel(QModelIndex *i)
47 {
48     // TODO
49     return false;
50 }
51
52 int DbMediaImage::countDataObjectRefs(int id) const
53 {
54     // TODO
55     return -1;
56 }
57
58 QString DbMediaImage::constructSelect(QString whereClause) const
59 {
60     // TODO
61     return "";
62 }
63
64 QString DbMediaImage::constructSelectById(int id) const
65 {
66     // TODO
67     return "";
68 }
69
70 EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
71 {
72     // TODO
73     return 0;
74 }*/
75
76 /*QSqlQueryModel* DbMediaImage::getData()
77 {
78     QSqlTableModel *model = new QSqlTableModel;
79     model->setTable(DB_TABLE_NAME_FILE);
80     return model;
81 }*/
82
83 /*int DbMediaImage::insertMediaImage(const MediaImage *mi)
84 {
85     return DbFile::insertDataObjectToModel(mi);
86 }*/
87
88 /* Stores a list of media images to the database.
89    Returns a list of media image id corresponding to the given list of media
90    images inserted to the database or already in the database.
91 */
92 QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
93 {
94     qDebug() << "Storing media images to database.";
95     QList<int> ids  = QList<int>();
96     QMapIterator<QString, EmuFrontObject*> it(images);
97     MediaImage *mi = 0;
98     while(it.hasNext())
99     {
100         it.next();
101         mi = dynamic_cast<MediaImage*>(it.value());
102         QString cksum = mi->getCheckSum();
103         qDebug() << "Storing media image " << mi->getName()
104             << " with checksum " << cksum;
105         EmuFrontObject *o = getFileByChecksum(cksum);
106         int id = o ? o->getId() : -1;
107         if (id >= 0)
108         {
109             qDebug() << "This media image already exists with id " << id;
110             delete o;
111             // this media image is already in the database
112             // TODO: what if the name differs? (cannot update to database, since the same media image
113             // might be inside another container
114         }
115         else if (id < 0)
116         {
117             qDebug() << "This media image is not yet in the db.";
118             id = insertDataObjectToModel(mi);
119             if (id < 0)
120             {
121                 // TODO: Build an error message of failed inserts
122                 qDebug() << "Failed inserting media image" << mi->getName();
123             }
124         }
125         if (id > 0) {
126             ids.append(id);
127             mi->setId(id);
128         }
129     }
130     return ids;
131 }
132
133 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
134 {
135     // TODO
136     // go through the list of media image ids,
137     // if the media image with curr id doesn't have a container, delete it
138 }
139
140 /* Fetches a list of media images inside a media image container
141     with a given id */
142 QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
143 {
144     QMap<QString, EmuFrontObject*> list;
145     QSqlQuery  q;
146     q.prepare("SELECT file.id, file.name, file.size, file.checksum "
147         "FROM file INNER JOIN mediaimagecontainer_mediaimage "
148         "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
149         "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
150     q.bindValue(":micid", micId);
151     q.exec();
152     QSqlRecord rec;
153     int id, size;
154     QString name, checksum;
155     MediaImage *mi = 0;
156     while(q.next()) {
157         // TODO: checks?
158         rec = q.record();
159         id = rec.value(DbMediaImage::File_Id).toInt();
160         name = rec.value(DbMediaImage::File_Name).toString();
161         checksum = rec.value(DbMediaImage::File_CheckSum).toString();
162         size = rec.value(DbMediaImage::File_FileSize).toInt();
163         list[checksum] = new MediaImage(id, name, checksum, size);
164     }
165     return list;
166 }
167
168 QString DbMediaImage::getCountRefsSelect(int id) const
169 {
170
171     /* nothing will be removed if a mediaimage file is removed
172        from the db. TODO: if all the mediaimages from
173            mediaimagecontainer are removed
174            the mediaimagecontainer should be removed! */
175     return QString("SELECT 0");
176     /*return QString("SELECT count(*) FROM file "
177               "INNER JOIN mediaimagecontainer_mediaimage "
178               "ON file.id=mediaimagecontainer_mediaimage.mediaimageid "
179               "WHERE file.id=%1").arg(id);*/
180 }