Some notes.
[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             // possible solution:
115             // * store media image names in different
116             //   table linked to the media image container
117         }
118         else if (id < 0)
119         {
120             qDebug() << "This media image is not yet in the db.";
121             id = insertDataObjectToModel(mi);
122             if (id < 0)
123             {
124                 // TODO: Build an error message of failed inserts
125                 qDebug() << "Failed inserting media image" << mi->getName();
126             }
127         }
128         if (id > 0) {
129             ids.append(id);
130             mi->setId(id);
131         }
132     }
133     return ids;
134 }
135
136 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
137 {
138     // TODO
139     // go through the list of media image ids,
140     // if the media image with curr id doesn't have a container, delete it
141 }
142
143 /* Fetches a list of media images inside a media image container
144     with a given id */
145 QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
146 {
147     QMap<QString, EmuFrontObject*> list;
148     QSqlQuery  q;
149     q.prepare("SELECT file.id, file.name, file.size, file.checksum "
150         "FROM file INNER JOIN mediaimagecontainer_mediaimage "
151         "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
152         "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
153     q.bindValue(":micid", micId);
154     q.exec();
155     QSqlRecord rec;
156     int id, size;
157     QString name, checksum;
158     MediaImage *mi = 0;
159     while(q.next()) {
160         // TODO: checks?
161         rec = q.record();
162         id = rec.value(DbMediaImage::File_Id).toInt();
163         name = rec.value(DbMediaImage::File_Name).toString();
164         checksum = rec.value(DbMediaImage::File_CheckSum).toString();
165         size = rec.value(DbMediaImage::File_FileSize).toInt();
166         list[checksum] = new MediaImage(id, name, checksum, size);
167     }
168     return list;
169 }
170
171 QString DbMediaImage::getCountRefsSelect(int id) const
172 {
173
174     /* nothing will be removed if a mediaimage file is removed
175        from the db. TODO: if all the mediaimages from
176            mediaimagecontainer are removed
177            the mediaimagecontainer should be removed! */
178     return QString("SELECT 0");
179     /*return QString("SELECT count(*) FROM file "
180               "INNER JOIN mediaimagecontainer_mediaimage "
181               "ON file.id=mediaimagecontainer_mediaimage.mediaimageid "
182               "WHERE file.id=%1").arg(id);*/
183 }