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::deleteDataObjectFromModel(QModelIndex *i)
46 int DbMediaImage::countDataObjectRefs(int id) const
52 QString DbMediaImage::constructSelect(QString whereClause) const
58 QString DbMediaImage::constructSelectById(int id) const
64 EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
70 /*QSqlQueryModel* DbMediaImage::getData()
72 QSqlTableModel *model = new QSqlTableModel;
73 model->setTable(DB_TABLE_NAME_FILE);
77 /*int DbMediaImage::insertMediaImage(const MediaImage *mi)
79 return DbFile::insertDataObjectToModel(mi);
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.
86 QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
88 QList<int> ids = QList<int>();
89 QMapIterator<QString, EmuFrontObject*> it(images);
94 mi = dynamic_cast<MediaImage*>(it.value());
95 int id = insertDataObjectToModel(mi);
97 // TODO: Build an error message of failed inserts
98 qDebug() << "Failed inserting media image" << mi->getName();
108 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
111 // go through the list of media image ids,
112 // if the media image with curr id doesn't have a container, delete it
115 /* Fetches a list of media images inside a media image container
117 QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
119 QMap<QString, EmuFrontObject*> list;
121 q.prepare("SELECT file.id, file.name, file.size, file.checksum "
122 "FROM file INNER JOIN mediaimagecontainer_mediaimage "
123 "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
124 "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
125 q.bindValue(":micid", micId);
129 QString name, checksum;
134 id = rec.value(DbMediaImage::File_Id).toInt();
135 name = rec.value(DbMediaImage::File_Name).toString();
136 checksum = rec.value(DbMediaImage::File_CheckSum).toString();
137 size = rec.value(DbMediaImage::File_FileSize).toInt();
138 list[checksum] = new MediaImage(id, name, checksum, size);
143 QString DbMediaImage::getCountRefsSelect(int id) const
146 /* nothing will be removed if a mediaimage file is removed
147 from the db. TODO: if all the mediaimages from
148 mediaimagecontainer are removed
149 the mediaimagecontainer should be removed! */
150 return QString("SELECT 0");
151 /*return QString("SELECT count(*) FROM file "
152 "INNER JOIN mediaimagecontainer_mediaimage "
153 "ON file.id=mediaimagecontainer_mediaimage.mediaimageid "
154 "WHERE file.id=%1").arg(id);*/