-// EmuFront
-// Copyright 2010 Mikko Keinänen
-//
-// This file is part of EmuFront.
-//
-//
-// EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License version 2 as published by
-// the Free Software Foundation and appearing in the file gpl.txt included in the
-// packaging of this file.
-//
-// EmuFront is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
-
+/*
+** EmuFront
+** Copyright 2010 Mikko Keinänen
+**
+** This file is part of EmuFront.
+**
+**
+** EmuFront is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License version 2 as published by
+** the Free Software Foundation and appearing in the file gpl.txt included in the
+** packaging of this file.
+**
+** EmuFront is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
+*/
#include <QDebug>
#include <QSqlRecord>
#include <QSqlQuery>
//#include "dbsetup.h"
#include "dbfilepath.h"
-
DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
- : DbFile(parent) // DbQueryModelManager(parent)
+ : DbFile(parent)
{
dbMediaImage = new DbMediaImage(parent);
dbFilePath = new DbFilePath(parent);
tableName = DbMediaImageContainer::DB_TABLE_MEDIAIMAGECONTAINER;
- //dbFile = new DbFile(parent);
}
bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
return false;
}
-int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
+/* Throws EmuFrontException */
+int DbMediaImageContainer::storeMediaImageContainer(EmuFrontObject *efo)
{
- const MediaImageContainer *mic
- = dynamic_cast<const MediaImageContainer *>(efo);
+ MediaImageContainer *mic
+ = dynamic_cast<MediaImageContainer *>(efo);
if (!mic->getFilePath())
- throw new EmuFrontException("Cannot install media image "
+ throw EmuFrontException("Cannot install media image "
"container to database without a file path object!");
- // check if this media image container is already in the database
-
- // Two possible solutions:
- // * multiple media image containers with matching checksum will not be stored at all
- // (only the first instance will be stored)
- // * multiple media image containers with matching checksum will be stored
- // if each instance is in a different file path
-
- EmuFrontObject *o = getFileByChecksum(mic->getCheckSum());
- int fileId = o ? o->getId() : -1;
- if (fileId >= 0) {
- // ok, we have a matching file, we could still create a new media image instance to the database
- qDebug() << "Media image container already in db with id " << fileId << ".";
- delete o;
- return fileId;
- }
+ // multiple media image containers with matching checksum will be stored
+ // if each instance is in a different file path
+ int fileId = -1;
QMap<QString, EmuFrontObject*> images = mic->getMediaImages();
QList<int> ids = dbMediaImage->storeMediaImages(images);
- qDebug() << "Stored " << ids.count() << " media images.";
+ //qDebug() << "Stored " << ids.count() << " media images.";
if (ids.count() <= 0)
return -1;
// File id is used to store the media image container instance to database,
// file id is also the media image container id
- fileId = DbFile::insertDataObjectToModel(mic);
- qDebug() << "Inserted media image container to file table with id " << fileId << ".";
+ // TODO: if this fails, the remove the media images in ids
+ fileId = insertDataObjectToModel(mic);
+
+ //qDebug() << "Inserted media image container to file table with id " << fileId << ".";
if (fileId < 0) {
// TODO: note we most surely need to catch the exception
// in the calling code block and clean
// all the media image and ...containers from
// the memory!
- throw new EmuFrontException(
+ throw EmuFrontException(
QString(tr("Inserting media image container %1 to file database failed"))
.arg(mic->getName()));
}
- // Insert to mediaimagecontainer table
+ mic->setId(fileId);
- QSqlQuery q;
- q.prepare("INSERT INTO mediaimagecontainer "
- "(fileid, filepathid, updatetime) "
- "VALUES (:fileid, :filepathid, :updatetime)");
- q.bindValue(":fileid", fileId);
- q.bindValue(":filepathid", mic->getFilePath()->getId());
- q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
- if (!q.exec()){
+ if (!linkMediaImageContainerToPath(mic)){
DbFile::deleteDataObject(fileId);
- throw new EmuFrontException("Failed inserting media image to database!");
+ throw EmuFrontException("Failed inserting media image to database!");
}
- qDebug() << "Inserted media image container " << fileId << " to mediaimagecontainer table.";
- linkMediaImagesWithContainer(fileId, ids);
- qDebug() << "Linked media image container with media images.";
+ //qDebug() << "Inserted media image container " << fileId << " to mediaimagecontainer table.";
+ linkMediaImagesWithContainer(fileId, images.values());
+ //qDebug() << "Linked media image container with media images.";
} catch (EmuFrontException e) {
dbMediaImage->removeOrphanedMediaImages(ids);
throw e;
" setup.id, "
" platform.id, platform.name, "
" mediatype.id, mediatype.name "
- "FROM mediaimagecontainer "
- "INNER JOIN file ON mediaimagecontainer.fileid = file.id "
- "INNER JOIN filepath ON mediaimagecontainer.filepathid = filepath.id "
+ "FROM file "
+ "INNER JOIN mediaimagecontainer_filepath ON mediaimagecontainer_filepath.fileid = file.id "
+ "INNER JOIN filepath ON mediaimagecontainer_filepath.filepathid = filepath.id "
"INNER JOIN setup ON filepath.setupid = setup.id "
"INNER JOIN platform ON setup.platformid = platform.id "
"INNER JOIN mediatype ON setup.mediatypeid = mediatype.id "
"%1 "
"ORDER BY file.name").arg(whereClause);
- qDebug() << select;
+ //qDebug() << select;
return select;
}
);
}
+/* Throws EmuFrontException */
EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *rec)
{
// TODO: checks!
int size = rec->value(MIC_FileSize).toInt();
int fpId = rec->value(MIC_FilePathId).toInt();
FilePathObject *fpo
- = dynamic_cast<FilePathObject*>(dbFilePath->getDataObject(fpId));
+ = dynamic_cast<FilePathObject*>(dbFilePath->getDataObject(fpId)); /* Throws EmuFrontException */
//int supId = rec->value(MIC_SetupId).toInt();
//Setup *sup = dbSetup->getDataObject(supId)
QMap<QString, EmuFrontObject*> images = dbMediaImage->getMediaImages(id);
/**
* Stores media image containers, including the media images included
* to database.
+*
+* Throws EmuFrontException
*/
void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
{
- qDebug() << "Storing media image containers to database.";
foreach(MediaImageContainer *mic, lst)
{
- qDebug() << "Media image container " << mic->getName();
- int micFileId = insertDataObjectToModel(mic);
+ //qDebug() << "Media image container " << mic->getName();
+ int micFileId = storeMediaImageContainer(mic);
}
}
-void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
+/* Throws EmuFrontException */
+void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<EmuFrontObject*> mediaImages)
{
- if (micId < 0 || miIds.count() <= 0)
+ if (micId < 0 || mediaImages.count() <= 0)
return;
- QSqlQuery q;
- q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
- "(mediaimagecontainerid, mediaimageid) "
- "VALUES (:micid, :miid) ");
- q.bindValue(":micid", micId);
-
- foreach(int miid, miIds) {
- qDebug() << "Linking media image container " << micId << " to media image " << miid << ".";
- q.bindValue(":miid", miid);
- if (!q.exec()) {
- throw new EmuFrontException(QString("Failed linking media "
- "image container %1 to a media image %2").arg(micId).arg(miid));
+ MediaImage *mi = 0;
+ foreach(EmuFrontObject *efo, mediaImages) {
+ mi = dynamic_cast<MediaImage*>(efo);
+ /*qDebug() << "Linking media image container " << micId
+ << " to media image " << mi->getId() << ", " << mi->getName() << ".";*/
+ if (!linkMediaImageToMediaImageContainer(mi, micId)) {
+ throw EmuFrontException(QString("Failed linking media "
+ "image container %1 to a media image %2").arg(micId).arg(mi->getId()));
}
}
}
void DbMediaImageContainer::filter(int mediaTypeId, int platformId)
{
- qDebug() << "Filtering media images with media type " << mediaTypeId
- << " and platform " << platformId;
+ /*qDebug() << "Filtering media images with media type " << mediaTypeId
+ << " and platform " << platformId;*/
QList<QString> filters;
if (mediaTypeId >= 0)
filters.append(QString("mediatype.id=%1").arg(mediaTypeId));
// mediaimages).
return QString("DELETE FROM file WHERE id=:id");
}
+
+/* Throws EmuFrontException */
+EmuFrontObject* DbMediaImageContainer::getMediaImageContainerByChecksum(QString checksum)
+{
+ return getDataObject(QString("file.checksum LIKE '%1'").arg(checksum));
+}
+
+bool DbMediaImageContainer::linkMediaImageContainerToPath(const MediaImageContainer *mic) const
+{
+ QSqlQuery q;
+ q.prepare("INSERT INTO mediaimagecontainer_filepath "
+ "(fileid, filepathid, updatetime) "
+ "VALUES (:fileid, :filepathid, :updatetime)");
+ q.bindValue(":fileid", mic->getId());
+ q.bindValue(":filepathid", mic->getFilePath()->getId());
+ q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
+ return q.exec();
+}
+
+bool DbMediaImageContainer::linkMediaImageToMediaImageContainer(const MediaImage *mi, int micId) const
+{
+ QSqlQuery q;
+ q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
+ "(mediaimagecontainerid, mediaimageid) "
+ "VALUES (:micid, :miid) ");
+ q.bindValue(":micid", micId);
+ q.bindValue(":miid", mi->getId());
+ return q.exec();
+}
+
+bool DbMediaImageContainer::removeFromFilePath(int filePathId) const
+{
+ QSqlQuery q;
+ q.prepare("DELETE FROM mediaimagecontainer_filepath "
+ "WHERE filepathid=:filepathid");
+ q.bindValue(":filepathid", filePathId);
+ return q.exec();
+}