75734d9a93e7342dfa2838186312dcc222a67598
[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
35 /* Stores a list of media images to the database.
36    Returns a list of media image id corresponding to the given list of media
37    images inserted to the database or already in the database.
38 */
39 QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
40 {
41     QList<int> ids  = QList<int>();
42     QMapIterator<QString, EmuFrontObject*> it(images);
43     MediaImage *mi = 0;
44     while(it.hasNext())
45     {
46         it.next();
47         mi = dynamic_cast<MediaImage*>(it.value());
48         int id = insertDataObjectToModel(mi);
49         if (id < 0) {
50             // TODO: Build an error message of failed inserts
51             qDebug() << "Failed inserting media image" << mi->getName();
52         }
53         else if (id >= 0) {
54             ids.append(id);
55             mi->setId(id);
56         }
57     }
58     return ids;
59 }
60
61 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
62 {
63     // TODO
64     // go through the list of media image ids,
65     // if the media image with curr id doesn't have a container, delete it
66 }
67
68 /* Fetches a list of media images inside a media image container
69     with a given id */
70 QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
71 {
72     QMap<QString, EmuFrontObject*> list;
73     QSqlQuery  q;
74     q.prepare("SELECT file.id, file.name, file.size, file.checksum "
75         "FROM file INNER JOIN mediaimagecontainer_mediaimage "
76         "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
77         "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
78     q.bindValue(":micid", micId);
79     q.exec();
80     QSqlRecord rec;
81     int id, size;
82     QString name, checksum;
83     MediaImage *mi = 0;
84     while(q.next()) {
85         // TODO: checks?
86         rec = q.record();
87         id = rec.value(DbMediaImage::File_Id).toInt();
88         name = rec.value(DbMediaImage::File_Name).toString();
89         checksum = rec.value(DbMediaImage::File_CheckSum).toString();
90         size = rec.value(DbMediaImage::File_FileSize).toInt();
91         list[checksum] = new MediaImage(id, name, checksum, size);
92     }
93     return list;
94 }
95
96 QString DbMediaImage::getCountRefsSelect(int id) const
97 {
98
99     /* nothing will be removed if a mediaimage file is removed
100        from the db. TODO: if all the mediaimages from
101            mediaimagecontainer are removed
102            the mediaimagecontainer should be removed! */
103     return QString("SELECT 0");
104     /*return QString("SELECT count(*) FROM file "
105               "INNER JOIN mediaimagecontainer_mediaimage "
106               "ON file.id=mediaimagecontainer_mediaimage.mediaimageid "
107               "WHERE file.id=%1").arg(id);*/
108 }