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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
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"
26 DbMediaImage::DbMediaImage(QObject *parent)
29 type = EmuFrontFile::FileType_MediaImage;
32 /*bool DbMediaImage::updateDataObjectToModel(const EmuFrontObject *efo)
38 bool DbMediaImage::insertDataObjectToModel(const EmuFrontObject *efo)
44 bool DbMediaImage::deleteDataObjectFromModel(QModelIndex *i)
50 int DbMediaImage::countDataObjectRefs(int id) const
56 QString DbMediaImage::constructSelect(QString whereClause) const
62 QString DbMediaImage::constructSelectById(int id) const
68 EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
74 /*QSqlQueryModel* DbMediaImage::getData()
76 QSqlTableModel *model = new QSqlTableModel;
77 model->setTable(DB_TABLE_NAME_FILE);
81 /*int DbMediaImage::insertMediaImage(const MediaImage *mi)
83 return DbFile::insertDataObjectToModel(mi);
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.
90 QList<int> DbMediaImage::storeMediaImages(QList<MediaImage *> images)
92 qDebug() << "Storing media images to database.";
93 QList<int> ids = QList<int>();
94 foreach(MediaImage* mi, images)
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;
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
110 qDebug() << "This media image is not yet in the db.";
111 id = insertDataObjectToModel(mi);
114 // TODO: Build an error message of failed inserts
115 qDebug() << "Failed inserting media image" << mi->getName();
126 void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
129 // go through the list of media image ids,
130 // if the media image with curr id doesn't have a container, delete it
133 /* Fetches a list of media images inside a media image container
135 QList<MediaImage*> DbMediaImage::getMediaImages(int micId) const
137 QList<MediaImage*> list;
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);
147 QString name, checksum;
152 id = rec.value(DbMediaImage::File_Id).toInt();
153 name = rec.value(DbMediaImage::File_Name).toString();
154 checksum = rec.value(DbMediaImage::File_CheckSum).toString();
155 size = rec.value(DbMediaImage::File_FileSize).toInt();
156 list.append(new MediaImage(id, name, checksum, size));