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/>.
23 #include <QSqlRelationalTableModel>
25 #include "dbmediaimagecontainer.h"
27 DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
28 : DbFile(parent) // DbQueryModelManager(parent)
30 dbMediaImage = new DbMediaImage(parent);
31 //dbFile = new DbFile(parent);
34 bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
40 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
42 const MediaImageContainer *mic
43 = dynamic_cast<const MediaImageContainer *>(efo);
45 // check if this media image container is already in the database
46 EmuFrontObject *o = getFileByChecksum(mic->getCheckSum());
47 int fileId = o ? o->getId() : -1;
48 /*int fileId = getMediaImageContainer(mic->getCheckSum());*/
50 qDebug() << "Media image container already in db with id " << fileId << ".";
54 if (!mic->getFilePath())
55 throw new EmuFrontException("Cannot install media image "
56 "container to database without a file path object!");
58 QList<MediaImage*> images = mic->getMediaImages();
59 QList<int> ids = dbMediaImage->storeMediaImages(images);
61 qDebug() << "Stored " << ids.count() << " media images.";
66 /* Contained Media images successfully stored to db,
67 storing media image container also */
71 // Insert MediaImageContainer first as a EmuFrontFile object to file table.
73 // File id is used to store the media image container instance to database,
74 // file id is also the media image container id
75 fileId = DbFile::insertDataObjectToModel(mic);
77 qDebug() << "Inserted media image container to file table with id " << fileId << ".";
80 // TODO: note we most surely need to catch the exception
81 // in the calling code block and clean
82 // all the media image and ...containers from
84 throw new EmuFrontException(
85 QString(tr("Inserting media image container %1 to file database failed"))
86 .arg(mic->getName()));
89 // Insert to mediaimagecontainer table
92 q.prepare("INSERT INTO mediaimagecontainer "
93 "(fileid, filepathid, updatetime) "
94 "VALUES (:fileid, :filepathid, :updatetime)");
95 q.bindValue(":fileid", fileId);
96 q.bindValue(":filepathid", mic->getFilePath()->getId());
97 q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
99 DbFile::deleteDataObject(fileId);
100 throw new EmuFrontException("Failed inserting media image to database!");
102 qDebug() << "Inserted media image container " << fileId << " to mediaimagecontainer table.";
103 linkMediaImagesWithContainer(fileId, ids);
104 qDebug() << "Linked media image container with media images.";
105 } catch (EmuFrontException e) {
106 dbMediaImage->removeOrphanedMediaImages(ids);
113 bool DbMediaImageContainer::deleteDataObjectFromModel(QModelIndex *i)
119 int DbMediaImageContainer::countDataObjectRefs(int id) const
125 QString DbMediaImageContainer::constructSelect(QString whereClause) const
127 return DbFile::constructSelect(whereClause);
130 QString DbMediaImageContainer::constructFilterById(int id) const
132 return DbFile::constructFilterById(id);
135 QString DbMediaImageContainer::constructSelectById(int id) const
137 return DbFile::constructSelectById(id);
140 EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *rec)
142 return DbFile::recordToDataObject(rec);
145 QSqlQueryModel* DbMediaImageContainer::getData()
147 return DbFile::getData();
150 /* Returns the id of a media image container with a given cheksum or -1 if not found */
151 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
154 q.prepare("SELECT id FROM file WHERE checksum=:checksum");
155 q.bindValue(":checksum", checksum);
158 id = q.value(0).toInt();
164 * Stores media image containers, including the media images included
167 void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
169 qDebug() << "Storing media image containers to database.";
170 foreach(MediaImageContainer *mic, lst)
172 qDebug() << "Media image container " << mic->getName();
173 int micFileId = insertDataObjectToModel(mic);
177 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
179 if (micId < 0 || miIds.count() <= 0)
183 q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
184 "(mediaimagecontainerid, mediaimageid) "
185 "VALUES (:micid, :miid) ");
186 q.bindValue(":micid", micId);
188 foreach(int miid, miIds) {
189 qDebug() << "Linking media image container " << micId << " to media image " << miid << ".";
190 q.bindValue(":miid", miid);
192 throw new EmuFrontException(QString("Failed linking media "
193 "image container %1 to a media image %2").arg(micId).arg(miid));