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 void PostCaptureModel::clear() {
112 if (m_model->rowCount(QModelIndex()) == 0) {
118 m_model->setFiles(QStringList());
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();
126 QVariant PostCaptureModel::data(const QModelIndex& index, int role) const {
127 if (!index.isValid() || index.row() < 0) {
131 QFileInfo inf = info(QSortFilterProxyModel::data(index, Qt::DisplayRole).toString());
134 return QUrl::fromLocalFile(inf.absoluteFilePath());
137 return inf.baseName();
140 if (m_mime.contains(inf.completeSuffix())) {
141 return m_mime[inf.completeSuffix()];
147 return inf.created().toString();
150 return inf.fileName();
159 QFileInfo PostCaptureModel::info(const QString& path) const {
160 if (m_data.contains(path)) {
165 m_data.insert(path, inf);
170 void PostCaptureModel::remove(const QUrl& file) {
171 QString path(file.toLocalFile());
174 m_model->removeFile(path);
178 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
182 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {