2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
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.
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.
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
21 #include "postcapturemodel.h"
22 #include "fileinfomodel.h"
27 static QHash<QString, QString> m_mime;
29 PostCaptureModel::PostCaptureModel(QObject *parent) :
30 QSortFilterProxyModel(parent),
31 m_model(new FileInfoModel(this)) {
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");
40 setSourceModel(m_model);
42 setDynamicSortFilter(true);
43 setSortRole(CreatedRole);
47 if (m_mime.isEmpty()) {
48 m_mime.insert("jpg", "image/jpeg");
49 m_mime.insert("mp4", "video/mp4");
53 PostCaptureModel::~PostCaptureModel() {
57 QString PostCaptureModel::imagePath() const {
61 void PostCaptureModel::setImagePath(const QString& path) {
62 if (m_imagePath != path) {
64 emit imagePathChanged();
68 QString PostCaptureModel::videoPath() const {
72 void PostCaptureModel::setVideoPath(const QString& path) {
73 if (m_videoPath != path) {
75 emit videoPathChanged();
79 QStringList PostCaptureModel::loadDir(QDir& dir) {
81 QStringList entries(dir.entryList(QDir::Files | QDir::NoDotAndDotDot));
83 foreach (const QString& entry, entries) {
84 files << dir.absoluteFilePath(entry);
90 void PostCaptureModel::reload() {
93 QDir images(m_imagePath);
94 QDir videos(m_videoPath);
96 if (images.canonicalPath() == videos.canonicalPath()) {
97 files += loadDir(images);
100 files += loadDir(images);
101 files += loadDir(videos);
106 m_model->setFiles(files);
108 sort(0, Qt::DescendingOrder);
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();
116 QVariant PostCaptureModel::data(const QModelIndex& index, int role) const {
117 if (!index.isValid() || index.row() < 0) {
121 QFileInfo inf = info(QSortFilterProxyModel::data(index, Qt::DisplayRole).toString());
124 return QUrl::fromLocalFile(inf.absoluteFilePath());
127 return inf.baseName();
130 if (m_mime.contains(inf.completeSuffix())) {
131 return m_mime[inf.completeSuffix()];
137 return inf.created().toString();
140 return inf.fileName();
149 QFileInfo PostCaptureModel::info(const QString& path) const {
150 if (m_data.contains(path)) {
155 m_data.insert(path, inf);
160 void PostCaptureModel::remove(const QUrl& file) {
161 QString path(file.toLocalFile());
164 m_model->removeFile(path);
168 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
172 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {