Create a list of media images inside a container.
[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 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 <QSqlTableModel>
22 #include <QSqlQuery>
23 #include <QSqlRecord>
24 #include "dbmediaimage.h"
25
26 DbMediaImage::DbMediaImage(QObject *parent)
27     : DbFile(parent)
28 {
29     type = EmuFrontFile::FileType_MediaImage;
30 }
31
32 /*bool DbMediaImage::updateDataObjectToModel(const EmuFrontObject *efo)
33 {
34     // TODO
35     return false;
36 }
37
38 bool DbMediaImage::insertDataObjectToModel(const EmuFrontObject *efo)
39 {
40     // TODO
41     return false;
42 }
43
44 bool DbMediaImage::deleteDataObjectFromModel(QModelIndex *i)
45 {
46     // TODO
47     return false;
48 }
49
50 int DbMediaImage::countDataObjectRefs(int id) const
51 {
52     // TODO
53     return -1;
54 }
55
56 QString DbMediaImage::constructSelect(QString whereClause) const
57 {
58     // TODO
59     return "";
60 }
61
62 QString DbMediaImage::constructSelectById(int id) const
63 {
64     // TODO
65     return "";
66 }
67
68 EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
69 {
70     // TODO
71     return 0;
72 }*/
73
74 /*QSqlQueryModel* DbMediaImage::getData()
75 {
76     QSqlTableModel *model = new QSqlTableModel;
77     model->setTable(DB_TABLE_NAME_FILE);
78     return model;
79 }*/
80
81 /*int DbMediaImage::insertMediaImage(const MediaImage *mi)
82 {
83     return DbFile::insertDataObjectToModel(mi);
84 }*/
85
86 /* Stores a list of media images to the database.
87    Returns a list of media image id corresponding to the given list of media
88    images inserted to the database or already in the database.
89 */
90 QList<int> DbMediaImage::storeMediaImages(QList<MediaImage *> images)
91 {
92     qDebug() << "Storing media images to database.";
93     QList<int> ids  = QList<int>();
94     foreach(MediaImage* mi, images)
95     {
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             // this media image is already in the database
105             // TODO: what if the name differs? (cannot update to database, since the same media image
106             // might be inside another container
107         }
108         else if (id < 0)
109         {
110             qDebug() << "This media image is not yet in the db.";
111             id = insertDataObjectToModel(mi);
112             if (id < 0)
113             {
114                 // TODO: Build an error message of failed inserts
115                 qDebug() << "Failed inserting media image" << mi->getName();
116             }
117         }
118         if (id > 0) {
119             ids.append(id);
120             mi->setId(id);
121         }
122     }
123     return ids;
124 }
125
126 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
127 {
128     // TODO
129     // go through the list of media image ids,
130     // if the media image with curr id doesn't have a container, delete it
131 }
132
133 /* Fetches a list of media images inside a media image container
134     with a given id */
135 QList<MediaImage*> DbMediaImage::getMediaImages(int micId) const
136 {
137     QList<MediaImage*> list;
138     QSqlQuery  q;
139     q.prepare("SELECT file.id, file.name, file.checksum, file.size "
140         "FROM file INNER JOIN mediaimagecontainer_mediaimage "
141         "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
142         "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
143     q.bindValue(":micid", micId);
144     q.exec();
145     QSqlRecord rec;
146     int id, size;
147     QString name, checksum;
148     MediaImage *mi = 0;
149     while(q.next()) {
150         rec = q.record();
151         id = rec.value(DbMediaImage::File_Id).toInt();
152         name = rec.value(DbMediaImage::File_Name).toString();
153         checksum = rec.value(DbMediaImage::File_CheckSum).toString();
154         size = rec.value(DbMediaImage::File_FileSize).toInt();
155         list.append(new MediaImage(id, name, checksum, size));
156     }
157     return list;
158 }