2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlTableModel>
24 #include "dbmediaimage.h"
27 DbMediaImage::DbMediaImage(QObject *parent)
30 type = EmuFrontFile::FileType_MediaImage;
31 tableName = DbMediaImage::DB_TABLE_NAME_FILE;
34 /*bool DbMediaImage::updateDataObjectToModel(const EmuFrontObject *efo)
40 bool DbMediaImage::insertDataObjectToModel(const EmuFrontObject *efo)
46 bool DbMediaImage::deleteDataObjectFromModel(QModelIndex *i)
52 int DbMediaImage::countDataObjectRefs(int id) const
58 QString DbMediaImage::constructSelect(QString whereClause) const
64 QString DbMediaImage::constructSelectById(int id) const
70 EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
76 /*QSqlQueryModel* DbMediaImage::getData()
78 QSqlTableModel *model = new QSqlTableModel;
79 model->setTable(DB_TABLE_NAME_FILE);
83 /*int DbMediaImage::insertMediaImage(const MediaImage *mi)
85 return DbFile::insertDataObjectToModel(mi);
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.
92 QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
94 qDebug() << "Storing media images to database.";
95 QList<int> ids = QList<int>();
96 QMapIterator<QString, EmuFrontObject*> it(images);
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;
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
116 qDebug() << "This media image is not yet in the db.";
117 id = insertDataObjectToModel(mi);
120 // TODO: Build an error message of failed inserts
121 qDebug() << "Failed inserting media image" << mi->getName();
132 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
135 // go through the list of media image ids,
136 // if the media image with curr id doesn't have a container, delete it
139 /* Fetches a list of media images inside a media image container
141 QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
143 QMap<QString, EmuFrontObject*> list;
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);
153 QString name, checksum;
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);
167 QString DbMediaImage::getCountRefsSelect(int id) const
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);*/