Inspecting the life cycle of scanned media images and media image
[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 <QSqlQuery>
22 #include <QSqlError>
23 #include "dbmediaimagecontainer.h"
24
25 DbMediaImageContainer::DbMediaImageContainer(QObject *parent)
26     : DbFile(parent) // DbQueryModelManager(parent)
27 {
28     dbMediaImage = new DbMediaImage(parent);
29     //dbFile = new DbFile(parent);
30 }
31
32 bool DbMediaImageContainer::updateDataObjectToModel(const EmuFrontObject *efo)
33 {
34     // TODO
35     return false;
36 }
37
38 int DbMediaImageContainer::insertDataObjectToModel(const EmuFrontObject *efo)
39 {
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)"*/
45
46     const MediaImageContainer *mic
47         = dynamic_cast<const MediaImageContainer *>(efo);
48
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.
51
52     int fileId = DbFile::insertDataObjectToModel(mic);
53
54     if (fileId < 0) {
55         throw new EmuFrontException(
56                 QString(tr("Inserting media image container %1 to file database failed"))
57                 .arg(mic->getName()));
58
59     }
60
61     // Insert to mediaimagecontainer table
62
63     QSqlQuery q;
64     q.prepare("INSERT INTO mediaimagecontainer "
65         "(id, fileid, filepathid, updatetime) "
66         "VALUES (NULL, :fileid, :filepathid, :updatetime");
67
68     return -1;
69 }
70
71 bool DbMediaImageContainer::deleteDataObjectFromModel(QModelIndex *i)
72 {
73     // TODO
74     return false;
75 }
76
77 int DbMediaImageContainer::countDataObjectRefs(int id) const
78 {
79     // TODO
80     return -1;
81 }
82
83 QString DbMediaImageContainer::constructSelect(QString whereClause) const
84 {
85     // TODO
86     return "";
87 }
88
89 QString DbMediaImageContainer::constructFilterById(int id) const
90 {
91     // TODO
92     return "";
93 }
94
95 QString DbMediaImageContainer::constructSelectById(int id) const
96 {
97     // TODO
98     return "";
99 }
100
101 EmuFrontObject* DbMediaImageContainer::recordToDataObject(const QSqlRecord *)
102 {
103     // TODO
104     return 0;
105 }
106
107 QSqlQueryModel* DbMediaImageContainer::getData()
108 {
109     // TODO
110     return 0;
111 }
112
113 int DbMediaImageContainer::getMediaImageContainer(QString checksum) const
114 {
115     // TODO
116     return -1;
117 }
118
119
120 /**
121 * Stores media image containers, including the media images included
122 * to database.
123 */
124 void DbMediaImageContainer::storeContainers(QList<MediaImageContainer *> lst, FilePathObject *fpo)
125 {
126     qDebug() << "Storing media image containers to database.";
127     foreach(MediaImageContainer *mic, lst)
128     {
129         qDebug() << "Media image container " << mic->getName();
130         QList<MediaImage*> images = mic->getMediaImages();
131
132         /* If media image container is already in the db, continue */
133         if (getMediaImageContainer(mic->getCheckSum()) >= 0)
134             continue;
135
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);
139
140         if (ids.count() > 0)
141         {
142             try {
143                 // mediaimagecontainer table: id, fileid, filepathid, updatetime
144
145                 // insert the media image container file to file table
146                 int micFileId = DbFile::insertDataObjectToModel(mic);
147                 if (micFileId < 0) {
148                     throw new EmuFrontException(
149                         QString(tr("Inserting media image container %1 to file database failed"))
150                             .arg(mic->getName()));
151                 }
152                 int fpId = fpo->getId();
153                 // store media image to db
154                 int micId = insertDataObjectToModel(mic);
155                 if (micId < 0){
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()));
160                 }
161
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);
167             }
168         }
169     }
170 }
171
172 void DbMediaImageContainer::linkMediaImagesWithContainer(int micId, QList<int> miIds)
173 {
174     // TODO
175 }