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 #include <sys/types.h>
29 #include <QDeclarativeInfo>
34 static QHash<QString, QString> m_mime;
36 class PostCaptureModelItem : public QStandardItem {
38 PostCaptureModelItem(const QString& path, const char *file) :
40 m_name(QString::fromLocal8Bit(file)),
45 ~PostCaptureModelItem() {
56 const QFileInfo *info() const {
58 m_info = new QFileInfo(path());
64 QString path() const {
65 return QString("%1%2%3").arg(m_dir).arg(QDir::separator()).arg(m_name);
68 QVariant data (int role = Qt::UserRole + 1) const {
70 case PostCaptureModel::UrlRole:
71 return QUrl::fromLocalFile(path());
73 case PostCaptureModel::TitleRole:
74 return info()->baseName();
76 case PostCaptureModel::MimeTypeRole:
77 if (m_mime.contains(info()->completeSuffix())) {
78 return m_mime[info()->completeSuffix()];
83 case PostCaptureModel::CreatedRole:
84 return info()->created().toString();
86 case PostCaptureModel::FileNameRole:
87 return info()->fileName();
97 mutable QFileInfo *m_info;
100 static bool lessThan(const QStandardItem *a1, const QStandardItem *a2) {
101 // we sort descending
102 return dynamic_cast<const PostCaptureModelItem *>(a1)->info()->created() >
103 dynamic_cast<const PostCaptureModelItem *>(a2)->info()->created();
106 PostCaptureModel::PostCaptureModel(QObject *parent) :
107 QStandardItemModel(parent) {
109 QHash<int, QByteArray> roles;
110 roles.insert(UrlRole, "url");
111 roles.insert(TitleRole, "title");
112 roles.insert(MimeTypeRole, "mimeType");
113 roles.insert(CreatedRole, "created");
114 roles.insert(FileNameRole, "fileName");
118 if (m_mime.isEmpty()) {
119 m_mime.insert("jpg", "image/jpeg");
120 m_mime.insert("png", "image/png");
121 m_mime.insert("mp4", "video/mp4");
122 m_mime.insert("avi", "video/x-msvideo");
126 PostCaptureModel::~PostCaptureModel() {
130 QString PostCaptureModel::imagePath() const {
134 void PostCaptureModel::setImagePath(const QString& path) {
135 if (m_imagePath != path) {
137 emit imagePathChanged();
141 QString PostCaptureModel::videoPath() const {
145 void PostCaptureModel::setVideoPath(const QString& path) {
146 if (m_videoPath != path) {
148 emit videoPathChanged();
152 void PostCaptureModel::loadDir(const QDir& dir, QList<QStandardItem *>& out) {
153 QString path = dir.canonicalPath();
155 DIR *d = opendir(path.toLocal8Bit().constData());
157 qmlInfo(this) << "Failed to open dir" << path;
162 struct dirent *entry = readdir(d);
167 if (QLatin1String(".") == entry->d_name || QLatin1String("..") == entry->d_name) {
171 if (!(entry->d_type == DT_LNK || entry->d_type == DT_REG)) {
175 out << new PostCaptureModelItem(path, entry->d_name);
181 void PostCaptureModel::reload() {
182 QList<QStandardItem *> files;
184 QDir images(m_imagePath);
185 QDir videos(m_videoPath);
187 loadDir(images, files);
189 if (images.canonicalPath() != videos.canonicalPath()) {
190 loadDir(videos, files);
193 qSort(files.begin(), files.end(), lessThan);
195 invisibleRootItem()->appendRows(files);
198 void PostCaptureModel::clear() {
199 QStandardItemModel::clear();
202 void PostCaptureModel::remove(const QUrl& file) {
203 QString path(file.toLocalFile());
205 int count = invisibleRootItem()->rowCount();
207 for (int x = 0; x < count; x++) {
208 if (dynamic_cast<PostCaptureModelItem *>(invisibleRootItem()->child(x))->path() == path) {
209 invisibleRootItem()->removeRow(x);
216 QHash<int, QByteArray> PostCaptureModel::roleNames() const {
220 void PostCaptureModel::setRoleNames(const QHash<int, QByteArray>& roles) {