Lots of changes in the database model. Warning: only lightly tested for
[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::deleteDataObjectFromModel(QModelIndex *i)
41 {
42     // TODO
43     return false;
44 }
45
46 int DbMediaImage::countDataObjectRefs(int id) const
47 {
48     // TODO
49     return -1;
50 }
51
52 QString DbMediaImage::constructSelect(QString whereClause) const
53 {
54     // TODO
55     return "";
56 }
57
58 QString DbMediaImage::constructSelectById(int id) const
59 {
60     // TODO
61     return "";
62 }
63
64 EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
65 {
66     // TODO
67     return 0;
68 }*/
69
70 /*QSqlQueryModel* DbMediaImage::getData()
71 {
72     QSqlTableModel *model = new QSqlTableModel;
73     model->setTable(DB_TABLE_NAME_FILE);
74     return model;
75 }*/
76
77 /*int DbMediaImage::insertMediaImage(const MediaImage *mi)
78 {
79     return DbFile::insertDataObjectToModel(mi);
80 }*/
81
82 /* Stores a list of media images to the database.
83    Returns a list of media image id corresponding to the given list of media
84    images inserted to the database or already in the database.
85 */
86 QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
87 {
88     qDebug() << "Storing media images to database.";
89     QList<int> ids  = QList<int>();
90     QMapIterator<QString, EmuFrontObject*> it(images);
91     MediaImage *mi = 0;
92     while(it.hasNext())
93     {
94         it.next();
95         mi = dynamic_cast<MediaImage*>(it.value());
96         QString cksum = mi->getCheckSum();
97         qDebug() << "Storing media image " << mi->getName()
98             << " with checksum " << cksum;
99         EmuFrontObject *o = getFileByChecksum(cksum);
100         int id = o ? o->getId() : -1;
101         if (id >= 0)
102         {
103             qDebug() << "This media image already exists with id " << id;
104
105             // this media image is already stored to db
106             // we will append the id of this media image to the list of media images
107             // and we will link this media image to the container later
108             delete o;
109         }
110         else if (id < 0)
111         {
112             qDebug() << "This media image is not yet in the db.";
113             id = insertDataObjectToModel(mi);
114             if (id < 0)
115             {
116                 // TODO: Build an error message of failed inserts
117                 qDebug() << "Failed inserting media image" << mi->getName();
118             }
119         }
120         if (id > 0) {
121             ids.append(id);
122             mi->setId(id);
123         }
124     }
125     return ids;
126 }
127
128 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
129 {
130     // TODO
131     // go through the list of media image ids,
132     // if the media image with curr id doesn't have a container, delete it
133 }
134
135 /* Fetches a list of media images inside a media image container
136     with a given id */
137 QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
138 {
139     QMap<QString, EmuFrontObject*> list;
140     QSqlQuery  q;
141     q.prepare("SELECT file.id, file.name, file.size, file.checksum "
142         "FROM file INNER JOIN mediaimagecontainer_mediaimage "
143         "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
144         "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
145     q.bindValue(":micid", micId);
146     q.exec();
147     QSqlRecord rec;
148     int id, size;
149     QString name, checksum;
150     MediaImage *mi = 0;
151     while(q.next()) {
152         // TODO: checks?
153         rec = q.record();
154         id = rec.value(DbMediaImage::File_Id).toInt();
155         name = rec.value(DbMediaImage::File_Name).toString();
156         checksum = rec.value(DbMediaImage::File_CheckSum).toString();
157         size = rec.value(DbMediaImage::File_FileSize).toInt();
158         list[checksum] = new MediaImage(id, name, checksum, size);
159     }
160     return list;
161 }
162
163 QString DbMediaImage::getCountRefsSelect(int id) const
164 {
165
166     /* nothing will be removed if a mediaimage file is removed
167        from the db. TODO: if all the mediaimages from
168            mediaimagecontainer are removed
169            the mediaimagecontainer should be removed! */
170     return QString("SELECT 0");
171     /*return QString("SELECT count(*) FROM file "
172               "INNER JOIN mediaimagecontainer_mediaimage "
173               "ON file.id=mediaimagecontainer_mediaimage.mediaimageid "
174               "WHERE file.id=%1").arg(id);*/
175 }