//
//
// EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
+// 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
#include <QDebug>
#include <QSqlTableModel>
#include <QSqlQuery>
+#include <QSqlRecord>
#include "dbmediaimage.h"
+
DbMediaImage::DbMediaImage(QObject *parent)
: DbFile(parent)
{
type = EmuFrontFile::FileType_MediaImage;
+ tableName = DbMediaImage::DB_TABLE_NAME_FILE;
}
-bool DbMediaImage::updateDataObjectToModel(const EmuFrontObject *efo)
-{
- // TODO
- return false;
-}
-
-bool DbMediaImage::insertDataObjectToModel(const EmuFrontObject *efo)
-{
- // TODO
- return false;
-}
-
-bool DbMediaImage::deleteDataObjectFromModel(QModelIndex *i)
-{
- // TODO
- return false;
-}
-
-int DbMediaImage::countDataObjectRefs(int id) const
-{
- // TODO
- return -1;
-}
-
-QString DbMediaImage::constructSelect(QString whereClause) const
-{
- // TODO
- return "";
-}
-QString DbMediaImage::constructSelectById(int id) const
+/* Stores a list of media images to the database.
+ Returns a list of media image id corresponding to the given list of media
+ images inserted to the database or already in the database.
+*/
+QList<int> DbMediaImage::storeMediaImages(QMap<QString, EmuFrontObject*> images)
{
- // TODO
- return "";
+ QList<int> ids = QList<int>();
+ QMapIterator<QString, EmuFrontObject*> it(images);
+ MediaImage *mi = 0;
+ while(it.hasNext())
+ {
+ it.next();
+ mi = dynamic_cast<MediaImage*>(it.value());
+ int id = insertDataObjectToModel(mi);
+ if (id < 0) {
+ // TODO: Build an error message of failed inserts
+ qDebug() << "Failed inserting media image" << mi->getName();
+ }
+ else if (id >= 0) {
+ ids.append(id);
+ mi->setId(id);
+ }
+ }
+ return ids;
}
-EmuFrontObject* DbMediaImage::recordToDataObject(const QSqlRecord *)
+void DbMediaImage::removeOrphanedMediaImages(QList<int> ids)
{
// TODO
- return 0;
+ // go through the list of media image ids,
+ // if the media image with curr id doesn't have a container, delete it
}
-QSqlQueryModel* DbMediaImage::getData()
+/* Fetches a list of media images inside a media image container
+ with a given id */
+QMap<QString, EmuFrontObject*> DbMediaImage::getMediaImages(int micId) const
{
- QSqlTableModel *model = new QSqlTableModel;
- model->setTable(DB_TABLE_NAME_FILE);
- return model;
+ QMap<QString, EmuFrontObject*> list;
+ QSqlQuery q;
+ q.prepare("SELECT file.id, file.name, file.size, file.checksum "
+ "FROM file INNER JOIN mediaimagecontainer_mediaimage "
+ "ON mediaimagecontainer_mediaimage.mediaimageid = file.id "
+ "WHERE mediaimagecontainer_mediaimage.mediaimagecontainerid = :micid ");
+ q.bindValue(":micid", micId);
+ q.exec();
+ QSqlRecord rec;
+ int id, size;
+ QString name, checksum;
+ MediaImage *mi = 0;
+ while(q.next()) {
+ // TODO: checks?
+ rec = q.record();
+ id = rec.value(DbMediaImage::File_Id).toInt();
+ name = rec.value(DbMediaImage::File_Name).toString();
+ checksum = rec.value(DbMediaImage::File_CheckSum).toString();
+ size = rec.value(DbMediaImage::File_FileSize).toInt();
+ list[checksum] = new MediaImage(id, name, checksum, size);
+ }
+ return list;
}
-int DbMediaImage::insertMediaImage(const MediaImage *mi)
+QString DbMediaImage::getCountRefsSelect(int id) const
{
- return DbFile::insertDataObjectToModel(mi);
-}
-QList<int> DbMediaImage::storeMediaImages(QList<MediaImage *> images)
-{
- qDebug() << "Storing media images to database.";
- QList<int> ids = QList<int>();
- foreach(MediaImage* mi, images)
- {
- QString cksum = mi->getCheckSum();
- qDebug() << "Storing media image " << mi->getName()
- << " with checksum " << cksum;
- // TODO: Crashed: Filtering never gets to dbtablemodelmanagers
- // filterDataObjects!
- EmuFrontObject *o = getFileByChecksum(cksum);
- int id = o ? o->getId() : -1;
- if (id >= 0)
- {
- qDebug() << "This media image already exists with id " << id;
- // this media image is already in the database
- // TODO: what if the name differs?
- }
- else if (id < 0)
- {
- qDebug() << "This media image is not yet in the db.";
- id = insertMediaImage(mi);
- if (id < 0)
- {
- // TODO: Build an error message of failed inserts
- qDebug() << "Failed inserting media image" << mi->getName();
- }
- }
- ids.append(id);
- }
- return ids;
+ /* nothing will be removed if a mediaimage file is removed
+ from the db. TODO: if all the mediaimages from
+ mediaimagecontainer are removed
+ the mediaimagecontainer should be removed! */
+ return QString("SELECT 0");
+ /*return QString("SELECT count(*) FROM file "
+ "INNER JOIN mediaimagecontainer_mediaimage "
+ "ON file.id=mediaimagecontainer_mediaimage.mediaimageid "
+ "WHERE file.id=%1").arg(id);*/
}