Some temporary debugging messages.
[emufront] / src / db / dbmediaimagecontainer.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QDebug>
21 #include <QSqlRecord>
22 #include <QSqlQuery>
23 #include <QSqlRelationalTableModel>
24 #include <QSqlError>
25 #include "dbmediaimagecontainer.h"
26
27 DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
28     : DbFile(parent) // DbQueryModelManager(parent)
29 {
30     dbMediaImage = new DbMediaImage(parent);
31     //dbFile = new DbFile(parent);
32 }
33
34 bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
35 {
36     // TODO
37     return false;
38 }
39
40 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
41 {
42     const MediaImageContainer *mic
43         = dynamic_cast<const MediaImageContainer *>(efo);
44
45     // check if this media image container is already in the database
46     int fileId = -1;
47     if ((fileId = getMediaImageContainer(mic->getCheckSum())) >= 0) {
48         qDebug() << "Media image container already in db with id " << fileId << ".";
49         return fileId;
50    }
51
52     if (!mic->getFilePath())
53         throw new EmuFrontException("Cannot install media image "
54             "container to database without a file path object!");
55
56     QList<MediaImage*> images = mic->getMediaImages();
57     QList<int> ids = dbMediaImage->storeMediaImages(images);
58
59     qDebug() << "Stored " << ids.count() << " media images.";
60
61     if (ids.count() <= 0)
62         return -1;
63
64     /* Contained Media images successfully stored to db,
65         storing media image container also */
66
67     try {
68
69         // Insert MediaImageContainer first as a EmuFrontFile object to file table.
70
71         // File id is used to store the media image container instance to database,
72         // file id is also the media image container id
73         fileId = DbFile::insertDataObjectToModel(mic);
74
75         qDebug() << "Inserted media image container to file table with id " << fileId << ".";
76
77         if (fileId < 0) {
78             // TODO: note we most surely need to catch the exception
79             // in the calling code block and clean
80             // all the media image and ...containers from
81             // the memory!
82             throw new EmuFrontException(
83                     QString(tr("Inserting media image container %1 to file database failed"))
84                     .arg(mic->getName()));
85         }
86
87         // Insert to mediaimagecontainer table
88
89         QSqlQuery q;
90         q.prepare("INSERT INTO mediaimagecontainer "
91                   "(fileid, filepathid, updatetime) "
92                   "VALUES (:fileid, :filepathid, :updatetime)");
93         q.bindValue(":fileid", fileId);
94         q.bindValue(":filepathid", mic->getFilePath()->getId());
95         q.bindValue(":updatetime", DatabaseManager::getCurrentTimeStamp());
96         if (!q.exec()){
97             DbFile::deleteDataObject(fileId);
98             throw new EmuFrontException("Failed inserting media image to database!");
99         }
100         qDebug() << "Inserted media image container " << fileId << " to mediaimagecontainer table.";
101         linkMediaImagesWithContainer(fileId, ids);
102         qDebug() << "Linked media image container with media images.";
103     } catch (EmuFrontException e) {
104         dbMediaImage->removeOrphanedMediaImages(ids);
105         throw e;
106     }
107
108     return fileId;
109 }
110
111 bool DbMediaImageContainer::deleteDataObjectFromModel(QModelIndex *i)
112 {
113     // TODO
114     return false;
115 }
116
117 int DbMediaImageContainer::countDataObjectRefs(int id) const
118 {
119     // TODO
120     return -1;
121 }
122
123 QString DbMediaImageContainer::constructSelect(QString whereClause) const
124 {
125     // TODO
126     return "";
127 }
128
129 QString DbMediaImageContainer::constructFilterById(int id) const
130 {
131     // TODO
132     return "";
133 }
134
135 QString DbMediaImageContainer::constructSelectById(int id) const
136 {
137     // TODO
138     return "";
139 }
140
141 EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *)
142 {
143     // TODO
144     return 0;
145 }
146
147 QSqlQueryModel* DbMediaImageContainer::getData()
148 {
149     // TODO
150     return 0;
151 }
152
153 /* Returns the id of a media image container with a given cheksum or -1 if not found */
154 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
155 {
156     // TODO
157     return -1;
158 }
159
160
161 /**
162 * Stores media image containers, including the media images included
163 * to database.
164 */
165 void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
166 {
167     qDebug() << "Storing media image containers to database.";
168     foreach(MediaImageContainer *mic, lst)
169     {
170         qDebug() << "Media image container " << mic->getName();
171         int micFileId = insertDataObjectToModel(mic);
172     }
173 }
174
175 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
176 {
177     if (micId < 0 || miIds.count() <= 0)
178         return;
179
180     QSqlQuery q;
181     q.prepare("INSERT INTO mediaimagecontainer_mediaimage "
182         "(mediaimagecontainerid, mediaimageid) "
183         "VALUES (:micid, :miid) ");
184     q.bindValue(":micid", micId);
185
186     foreach(int miid, miIds) {
187         qDebug() << "Linking media image container " << micId << " to media image " << miid  << ".";
188         q.bindValue(":miid", miid);
189         if (!q.exec()) {
190             throw new EmuFrontException(QString("Failed linking media "
191                 "image container %1 to a media image %2").arg(micId).arg(miid));
192         }
193     }
194 }