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 "dbmediaimagecontainer.h"
25 DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
26 : DbFile(parent) // DbQueryModelManager(parent)
28 dbMediaImage = new DbMediaImage(parent);
29 //dbFile = new DbFile(parent);
32 bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
38 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
40 /* "CREATE TABLE IF NOT EXISTS mediaimagecontainer "
41 "(id INTEGER PRIMARY KEY, "
42 "fileid INTEGER REFERENCES file(id), "
43 "filepathid INTEGER REFERENCES filepath(id), "
44 "updatetime NUMERIC)"*/
46 const MediaImageContainer *mic
47 = dynamic_cast<const MediaImageContainer *>(efo);
49 // Insert MediaImageContainer first as a EmuFrontFile object to file table.
50 // File id is used to store the media image container instance to database.
52 int fileId = DbFile::insertDataObjectToModel(mic);
55 throw new EmuFrontException(
56 QString(tr("Inserting media image container %1 to file database failed"))
57 .arg(mic->getName()));
61 // Insert to mediaimagecontainer table
64 q.prepare("INSERT INTO mediaimagecontainer "
65 "(id, fileid, filepathid, updatetime) "
66 "VALUES (NULL, :fileid, :filepathid, :updatetime");
71 bool DbMediaImageContainer::deleteDataObjectFromModel(QModelIndex *i)
77 int DbMediaImageContainer::countDataObjectRefs(int id) const
83 QString DbMediaImageContainer::constructSelect(QString whereClause) const
89 QString DbMediaImageContainer::constructFilterById(int id) const
95 QString DbMediaImageContainer::constructSelectById(int id) const
101 EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *)
107 QSqlQueryModel* DbMediaImageContainer::getData()
113 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
121 * Stores media image containers, including the media images included
124 void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
126 qDebug() << "Storing media image containers to database.";
127 foreach(MediaImageContainer *mic, lst)
129 qDebug() << "Media image container " << mic->getName();
130 QList<MediaImage*> images = mic->getMediaImages();
132 /* If media image container is already in the db, continue */
133 if (getMediaImageContainer(mic->getCheckSum()) >= 0)
136 // this is a new media image container, lets build a list
137 // of media image id's for this container
138 QList<int> ids = dbMediaImage->storeMediaImages(images);
143 // mediaimagecontainer table: id, fileid, filepathid, updatetime
145 // insert the media image container file to file table
146 int micFileId = DbFile::insertDataObjectToModel(mic);
148 throw new EmuFrontException(
149 QString(tr("Inserting media image container %1 to file database failed"))
150 .arg(mic->getName()));
152 int fpId = fpo->getId();
153 // store media image to db
154 int micId = insertDataObjectToModel(mic);
156 // because the previous insert failed, the next is most likely going to fail, throw exception
157 throw new EmuFrontException(
158 QString(tr("Failed inserting media image container '%1' to database!"))
159 .arg(mic->getName()));
162 // link all the media image ids in list to media image container id
163 linkMediaImagesWithContainer(micId, ids);
164 } catch (EmuFrontException e) {
165 // need to remove the media images without media image container in list 'ids'
166 dbMediaImage->removeOrphanedMediaImages(ids);
172 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)