8c4c9e1a35786023f96e5bb179e28279d4cda2d8
[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 <QDir>
23 #include <QDateTime>
24 #include <QUrl>
25 #include <QStandardItem>
26
27 static QHash<QString, QString> m_mime;
28
29 class PostCaptureModelItem : public QStandardItem {
30 public:
31   PostCaptureModelItem(const QString& path) :
32     m_info(QFileInfo(path)),
33     m_path(path) {
34
35   }
36
37   int type() const {
38     return UserType;
39   }
40
41   const QFileInfo *info() const {
42     return &m_info;
43   }
44
45   QString path() {
46     return m_path;
47   }
48
49   QVariant data (int role = Qt::UserRole + 1) const {
50     switch (role) {
51     case PostCaptureModel::UrlRole:
52       return QUrl::fromLocalFile(m_info.absoluteFilePath());
53
54     case PostCaptureModel::TitleRole:
55       return m_info.baseName();
56
57     case PostCaptureModel::MimeTypeRole:
58       if (m_mime.contains(m_info.completeSuffix())) {
59         return m_mime[m_info.completeSuffix()];
60       }
61
62       return QString();
63
64     case PostCaptureModel::CreatedRole:
65       return m_info.created().toString();
66
67     case PostCaptureModel::FileNameRole:
68       return m_info.fileName();
69
70     default:
71       return QVariant();
72     }
73   }
74
75 private:
76   QFileInfo m_info;
77   QString m_path;
78 };
79
80 static bool lessThan(const QStandardItem *a1, const QStandardItem *a2) {
81   // we sort descending
82   return dynamic_cast<const PostCaptureModelItem *>(a1)->info()->created() >
83     dynamic_cast<const PostCaptureModelItem *>(a2)->info()->created();
84 }
85
86 PostCaptureModel::PostCaptureModel(QObject *parent) :
87   QStandardItemModel(parent) {
88
89   QHash<int, QByteArray> roles;
90   roles.insert(UrlRole, "url");
91   roles.insert(TitleRole, "title");
92   roles.insert(MimeTypeRole, "mimeType");
93   roles.insert(CreatedRole, "created");
94   roles.insert(FileNameRole, "fileName");
95
96   setRoleNames(roles);
97
98   if (m_mime.isEmpty()) {
99     m_mime.insert("jpg", "image/jpeg");
100     m_mime.insert("mp4", "video/mp4");
101   }
102 }
103
104 PostCaptureModel::~PostCaptureModel() {
105
106 }
107
108 QString PostCaptureModel::imagePath() const {
109   return m_imagePath;
110 }
111
112 void PostCaptureModel::setImagePath(const QString& path) {
113   if (m_imagePath != path) {
114     m_imagePath = path;
115     emit imagePathChanged();
116   }
117 }
118
119 QString PostCaptureModel::videoPath() const {
120   return m_videoPath;
121 }
122
123 void PostCaptureModel::setVideoPath(const QString& path) {
124   if (m_videoPath != path) {
125     m_videoPath = path;
126     emit videoPathChanged();
127   }
128 }
129
130 void PostCaptureModel::loadDir(const QDir& dir, QList<QStandardItem *>& out) {
131   QStringList entries(dir.entryList(QDir::Files | QDir::NoDotAndDotDot));
132
133   foreach (const QString& entry, entries) {
134     out << new PostCaptureModelItem(dir.absoluteFilePath(entry));
135   }
136 }
137
138 void PostCaptureModel::reload() {
139   QList<QStandardItem *> files;
140
141   QDir images(m_imagePath);
142   QDir videos(m_videoPath);
143
144   loadDir(images, files);
145
146   if (images.canonicalPath() != videos.canonicalPath()) {
147     loadDir(videos, files);
148   }
149
150   qSort(files.begin(), files.end(), lessThan);
151
152   invisibleRootItem()->appendRows(files);
153 }
154
155 void PostCaptureModel::clear() {
156   QStandardItemModel::clear();
157 }
158
159 void PostCaptureModel::remove(const QUrl& file) {
160   QString path(file.toLocalFile());
161
162   int count = invisibleRootItem()->rowCount();
163
164   for (int x = 0; x < count; x++) {
165     if (dynamic_cast<PostCaptureModelItem *>(invisibleRootItem()->child(x))->path() == path) {
166       invisibleRootItem()->removeRow(x);
167       return;
168     }
169   }
170 }
171
172 #if defined(QT5)
173 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
174   return m_roles;
175 }
176
177 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {
178   m_roles = roles;
179 }
180 #endif