d26a8f88a48e04b95ddadfc37994cea8f9235f3c
[emufront] / src / models / mediaimagecontainermodel.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "mediaimagecontainer.h"
23 #include "mediaimagecontainermodel.h"
24 #include "filepathmodel.h"
25 #include "mediaimagemodel.h"
26 #include <QtSql>
27
28 MediaImageContainerModel::MediaImageContainerModel(QObject *parent) :
29     EmuFrontQueryModel(parent)
30 {
31 }
32
33 QString MediaImageContainerModel::constructSelect(QString where) const
34 {
35     // TODO, for a usual search need a "light" version of this select
36     // and MediaImageContainer (only id, name)
37     return QString(
38         "SELECT file.id, file.name, file.checksum, file.size, "
39         "        filepath.id, filepath.name, "
40         "        setup.id, "
41         "        platform.id, platform.name, "
42         "        mediatype.id, mediatype.name "
43         "FROM file "
44         "INNER JOIN mediaimagecontainer_filepath ON mediaimagecontainer_filepath.fileid = file.id "
45         "INNER JOIN filepath ON mediaimagecontainer_filepath.filepathid = filepath.id "
46         "INNER JOIN setup ON filepath.setupid = setup.id "
47         "INNER JOIN platform ON setup.platformid = platform.id "
48         "INNER JOIN mediatype ON setup.mediatypeid = mediatype.id "
49         "%1 "
50         "ORDER BY file.name"
51     ).arg(where);
52 }
53
54 QString MediaImageContainerModel::constructFilterById(int id) const
55 {
56     return QString("file.id = %1").arg(id);
57 }
58
59 void MediaImageContainerModel::refresh()
60 {
61     //setQuery(constructSelect());
62     setHeaderData(MIC_FileId, Qt::Horizontal, tr("File id"));
63     setHeaderData(MIC_FileName, Qt::Horizontal, tr("File Name"));
64     setHeaderData(MIC_FileCheckSum, Qt::Horizontal, tr("File checksum"));
65     setHeaderData(MIC_FileSize, Qt::Horizontal, tr("File Size"));
66     setHeaderData(MIC_FilePathId, Qt::Horizontal, tr("File path id"));
67     setHeaderData(MIC_FilePathName, Qt::Horizontal, tr("File path name"));
68     setHeaderData(MIC_SetupId, Qt::Horizontal, tr("Setup id"));
69     setHeaderData(MIC_PlatformId, Qt::Horizontal, tr("Platform id"));
70     setHeaderData(MIC_PlatformName, Qt::Horizontal, tr("Platform name"));
71     setHeaderData(MIC_MediaTypeId, Qt::Horizontal, tr("Media type id"));
72     setHeaderData(MIC_MediaTypeName, Qt::Horizontal, tr("Media type name"));
73 }
74
75 EmuFrontObject* MediaImageContainerModel::recordToDataObject(const QSqlRecord *rec)
76 {
77     // TODO: checks!
78     MediaImageContainer *mic = 0;
79     if (!rec) return mic;
80     int id = rec->value(MIC_FileId).toInt();
81     QString name = rec->value(MIC_FileName).toString();
82     QString checksum = rec->value(MIC_FileCheckSum).toString();
83     int size = rec->value(MIC_FileSize).toInt();
84     int fpId = rec->value(MIC_FilePathId).toInt();
85     FilePathModel fpModel;
86     EmuFrontObject *efo = fpModel.getDataObject(fpId);
87     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efo);
88     if (!fpo) return 0;
89     //int supId = rec->value(MIC_SetupId).toInt();
90     //Setup *sup = dbSetup->getDataObject(supId)
91     MediaImageModel miModel;
92     QMap<QString, EmuFrontObject*> images = miModel.getMediaImages(id);
93     mic = new MediaImageContainer(
94        id, name, checksum, size, images, fpo
95     );
96     return mic;
97 }
98
99 void MediaImageContainerModel::filterBySetup(int setupId)
100 {
101     QList<QString> filters;
102     filters.append(QString("setup.id = %1").arg(setupId));
103     filterDataObjects(filters);
104 }