1f71a588cb76bc747ad28762edd9ff427726e984
[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 bool PostCaptureModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
112   return info(sourceModel()->data(left, Qt::DisplayRole).toString()).created() <
113     info(sourceModel()->data(right, Qt::DisplayRole).toString()).created();
114 }
115
116 QVariant PostCaptureModel::data(const QModelIndex& index, int role) const {
117   if (!index.isValid() || index.row() < 0) {
118     return QVariant();
119   }
120
121   QFileInfo inf = info(QSortFilterProxyModel::data(index, Qt::DisplayRole).toString());
122   switch (role) {
123   case UrlRole:
124     return QUrl::fromLocalFile(inf.absoluteFilePath());
125
126   case TitleRole:
127     return inf.baseName();
128
129   case MimeTypeRole:
130     if (m_mime.contains(inf.completeSuffix())) {
131       return m_mime[inf.completeSuffix()];
132     }
133
134     return QString();
135
136   case CreatedRole:
137     return inf.created().toString();
138
139   case FileNameRole:
140     return inf.fileName();
141
142   default:
143     break;
144   }
145
146   return QVariant();
147 }
148
149 QFileInfo PostCaptureModel::info(const QString& path) const {
150   if (m_data.contains(path)) {
151     return m_data[path];
152   }
153
154   QFileInfo inf(path);
155   m_data.insert(path, inf);
156
157   return inf;
158 }
159
160 void PostCaptureModel::remove(const QUrl& file) {
161   QString path(file.toLocalFile());
162
163   m_data.remove(path);
164   m_model->removeFile(path);
165 }
166
167 #if defined(QT5)
168 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
169   return m_roles;
170 }
171
172 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {
173   m_roles = roles;
174 }
175 #endif