Added mime types for png and avi to PostCaptureModel
[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("png", "image/png");
101     m_mime.insert("mp4", "video/mp4");
102     m_mime.insert("avi", "video/x-msvideo");
103   }
104 }
105
106 PostCaptureModel::~PostCaptureModel() {
107
108 }
109
110 QString PostCaptureModel::imagePath() const {
111   return m_imagePath;
112 }
113
114 void PostCaptureModel::setImagePath(const QString& path) {
115   if (m_imagePath != path) {
116     m_imagePath = path;
117     emit imagePathChanged();
118   }
119 }
120
121 QString PostCaptureModel::videoPath() const {
122   return m_videoPath;
123 }
124
125 void PostCaptureModel::setVideoPath(const QString& path) {
126   if (m_videoPath != path) {
127     m_videoPath = path;
128     emit videoPathChanged();
129   }
130 }
131
132 void PostCaptureModel::loadDir(const QDir& dir, QList<QStandardItem *>& out) {
133   QStringList entries(dir.entryList(QDir::Files | QDir::NoDotAndDotDot));
134
135   foreach (const QString& entry, entries) {
136     out << new PostCaptureModelItem(dir.absoluteFilePath(entry));
137   }
138 }
139
140 void PostCaptureModel::reload() {
141   QList<QStandardItem *> files;
142
143   QDir images(m_imagePath);
144   QDir videos(m_videoPath);
145
146   loadDir(images, files);
147
148   if (images.canonicalPath() != videos.canonicalPath()) {
149     loadDir(videos, files);
150   }
151
152   qSort(files.begin(), files.end(), lessThan);
153
154   invisibleRootItem()->appendRows(files);
155 }
156
157 void PostCaptureModel::clear() {
158   QStandardItemModel::clear();
159 }
160
161 void PostCaptureModel::remove(const QUrl& file) {
162   QString path(file.toLocalFile());
163
164   int count = invisibleRootItem()->rowCount();
165
166   for (int x = 0; x < count; x++) {
167     if (dynamic_cast<PostCaptureModelItem *>(invisibleRootItem()->child(x))->path() == path) {
168       invisibleRootItem()->removeRow(x);
169       return;
170     }
171   }
172 }
173
174 #if defined(QT5)
175 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
176   return m_roles;
177 }
178
179 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {
180   m_roles = roles;
181 }
182 #endif