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