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"
25 #include <QStandardItem>
27 static QHash<QString, QString> m_mime;
29 class PostCaptureModelItem : public QStandardItem {
31 PostCaptureModelItem(const QString& path) :
32 m_info(QFileInfo(path)),
41 const QFileInfo *info() const {
49 QVariant data (int role = Qt::UserRole + 1) const {
51 case PostCaptureModel::UrlRole:
52 return QUrl::fromLocalFile(m_info.absoluteFilePath());
54 case PostCaptureModel::TitleRole:
55 return m_info.baseName();
57 case PostCaptureModel::MimeTypeRole:
58 if (m_mime.contains(m_info.completeSuffix())) {
59 return m_mime[m_info.completeSuffix()];
64 case PostCaptureModel::CreatedRole:
65 return m_info.created().toString();
67 case PostCaptureModel::FileNameRole:
68 return m_info.fileName();
80 static bool lessThan(const QStandardItem *a1, const QStandardItem *a2) {
82 return dynamic_cast<const PostCaptureModelItem *>(a1)->info()->created() >
83 dynamic_cast<const PostCaptureModelItem *>(a2)->info()->created();
86 PostCaptureModel::PostCaptureModel(QObject *parent) :
87 QStandardItemModel(parent) {
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");
98 if (m_mime.isEmpty()) {
99 m_mime.insert("jpg", "image/jpeg");
100 m_mime.insert("mp4", "video/mp4");
104 PostCaptureModel::~PostCaptureModel() {
108 QString PostCaptureModel::imagePath() const {
112 void PostCaptureModel::setImagePath(const QString& path) {
113 if (m_imagePath != path) {
115 emit imagePathChanged();
119 QString PostCaptureModel::videoPath() const {
123 void PostCaptureModel::setVideoPath(const QString& path) {
124 if (m_videoPath != path) {
126 emit videoPathChanged();
130 void PostCaptureModel::loadDir(const QDir& dir, QList<QStandardItem *>& out) {
131 QStringList entries(dir.entryList(QDir::Files | QDir::NoDotAndDotDot));
133 foreach (const QString& entry, entries) {
134 out << new PostCaptureModelItem(dir.absoluteFilePath(entry));
138 void PostCaptureModel::reload() {
139 QList<QStandardItem *> files;
141 QDir images(m_imagePath);
142 QDir videos(m_videoPath);
144 loadDir(images, files);
146 if (images.canonicalPath() != videos.canonicalPath()) {
147 loadDir(videos, files);
150 qSort(files.begin(), files.end(), lessThan);
152 invisibleRootItem()->appendRows(files);
155 void PostCaptureModel::clear() {
156 QStandardItemModel::clear();
159 void PostCaptureModel::remove(const QUrl& file) {
160 QString path(file.toLocalFile());
162 int count = invisibleRootItem()->rowCount();
164 for (int x = 0; x < count; x++) {
165 if (dynamic_cast<PostCaptureModelItem *>(invisibleRootItem()->child(x))->path() == path) {
166 invisibleRootItem()->removeRow(x);
173 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
177 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {