037d67cfa1c6edf5183378a2df7a2c84fa53ace9
[harmattan/cameraplus] / src / postcapturemodel.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "postcapturemodel.h"
22 #include "fileinfomodel.h"
23 #include <QDir>
24 #include <QDateTime>
25 #include <QUrl>
26
27 static QHash<QString, QString> m_mime;
28
29 PostCaptureModel::PostCaptureModel(QObject *parent) :
30   QSortFilterProxyModel(parent),
31   m_model(new FileInfoModel(this)) {
32
33   QHash<int, QByteArray> roles;
34   roles.insert(UrlRole, "url");
35   roles.insert(TitleRole, "title");
36   roles.insert(MimeTypeRole, "mimeType");
37   roles.insert(CreatedRole, "created");
38   roles.insert(FileNameRole, "fileName");
39
40   setSourceModel(m_model);
41
42   setDynamicSortFilter(true);
43   setSortRole(CreatedRole);
44
45   setRoleNames(roles);
46
47   if (m_mime.isEmpty()) {
48     m_mime.insert("jpg", "image/jpeg");
49     m_mime.insert("mp4", "video/mp4");
50   }
51 }
52
53 PostCaptureModel::~PostCaptureModel() {
54
55 }
56
57 QString PostCaptureModel::imagePath() const {
58   return m_imagePath;
59 }
60
61 void PostCaptureModel::setImagePath(const QString& path) {
62   if (m_imagePath != path) {
63     m_imagePath = path;
64     emit imagePathChanged();
65   }
66 }
67
68 QString PostCaptureModel::videoPath() const {
69   return m_videoPath;
70 }
71
72 void PostCaptureModel::setVideoPath(const QString& path) {
73   if (m_videoPath != path) {
74     m_videoPath = path;
75     emit videoPathChanged();
76   }
77 }
78
79 QStringList PostCaptureModel::loadDir(QDir& dir) {
80   QStringList files;
81   QStringList entries(dir.entryList(QDir::Files | QDir::NoDotAndDotDot));
82
83   foreach (const QString& entry, entries) {
84     files << dir.absoluteFilePath(entry);
85   }
86
87   return files;
88 }
89
90 void PostCaptureModel::reload() {
91   QStringList files;
92
93   QDir images(m_imagePath);
94   QDir videos(m_videoPath);
95
96   if (images.canonicalPath() == videos.canonicalPath()) {
97     files += loadDir(images);
98   }
99   else {
100     files += loadDir(images);
101     files += loadDir(videos);
102   }
103
104   m_data.clear();
105
106   m_model->setFiles(files);
107
108   sort(0, Qt::DescendingOrder);
109 }
110
111 void PostCaptureModel::clear() {
112   if (m_model->rowCount(QModelIndex()) == 0) {
113     return;
114   }
115
116   m_data.clear();
117
118   m_model->setFiles(QStringList());
119 }
120
121 bool PostCaptureModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
122   return info(sourceModel()->data(left, Qt::DisplayRole).toString()).created() <
123     info(sourceModel()->data(right, Qt::DisplayRole).toString()).created();
124 }
125
126 QVariant PostCaptureModel::data(const QModelIndex& index, int role) const {
127   if (!index.isValid() || index.row() < 0) {
128     return QVariant();
129   }
130
131   QFileInfo inf = info(QSortFilterProxyModel::data(index, Qt::DisplayRole).toString());
132   switch (role) {
133   case UrlRole:
134     return QUrl::fromLocalFile(inf.absoluteFilePath());
135
136   case TitleRole:
137     return inf.baseName();
138
139   case MimeTypeRole:
140     if (m_mime.contains(inf.completeSuffix())) {
141       return m_mime[inf.completeSuffix()];
142     }
143
144     return QString();
145
146   case CreatedRole:
147     return inf.created().toString();
148
149   case FileNameRole:
150     return inf.fileName();
151
152   default:
153     break;
154   }
155
156   return QVariant();
157 }
158
159 QFileInfo PostCaptureModel::info(const QString& path) const {
160   if (m_data.contains(path)) {
161     return m_data[path];
162   }
163
164   QFileInfo inf(path);
165   m_data.insert(path, inf);
166
167   return inf;
168 }
169
170 void PostCaptureModel::remove(const QUrl& file) {
171   QString path(file.toLocalFile());
172
173   m_data.remove(path);
174   m_model->removeFile(path);
175 }
176
177 #if defined(QT5)
178 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
179   return m_roles;
180 }
181
182 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {
183   m_roles = roles;
184 }
185 #endif