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 "filenaming.h"
27 #include <QDeclarativeInfo>
32 FileNaming::FileNaming(QObject *parent) :
39 FileNaming::~FileNaming() {
40 delete m_index; m_index = 0;
43 QString FileNaming::imageSuffix() const {
47 void FileNaming::setImageSuffix(const QString& suffix) {
48 if (m_imageSuffix != suffix) {
49 m_imageSuffix = suffix;
50 emit imageSuffixChanged();
54 QString FileNaming::videoSuffix() const {
58 void FileNaming::setVideoSuffix(const QString& suffix) {
59 if (m_videoSuffix != suffix) {
60 m_videoSuffix = suffix;
61 emit videoSuffixChanged();
65 QString FileNaming::imageFileName() {
66 return fileName(m_imagePath, m_imageSuffix, Image);
69 QString FileNaming::videoFileName() {
70 return fileName(m_videoPath, m_videoSuffix, Video);
73 QString FileNaming::fileName(const QString& path, const QString& suffix, const Type& type) {
74 FileIndex::Type indexType = (FileIndex::Type) type;
77 qmlInfo(this) << "settings has not been set";
81 if (suffix.isEmpty()) {
82 qmlInfo(this) << "called with empty suffix";
87 qmlInfo(this) << "called with empty path";
91 QString date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
94 // index is the last used index
97 if (m_index->stamp(indexType) != date) {
98 m_index->setStamp(indexType, date);
101 index = m_index->counter(indexType);
105 QStringList filters(QString("*%1_*").arg(date));
106 QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name);
107 if (!entries.isEmpty()) {
108 QString name = QFile(entries.last()).fileName();
109 index = name.section('_', 1, 1).section('.', 0, 0).toInt();
113 while (index < INT_MAX) {
116 QString name = QString("%1%2_%3.%4")
117 .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
119 if (!QFile(name).exists()) {
120 m_index->setCounter(indexType, index);
126 qmlInfo(this) << "Failed to guess a file name";
131 QString FileNaming::canonicalPath(const QString& path) {
132 if (!QDir::root().mkpath(path)) {
133 qmlInfo(this) << "Failed to create path" << path;
137 QString newPath = QFileInfo(path).canonicalFilePath();
139 if (newPath.isEmpty()) {
143 if (!newPath.endsWith(QDir::separator())) {
144 newPath.append(QDir::separator());
150 QString FileNaming::temporaryVideoFileName() {
151 if (m_temporaryVideoPath.isEmpty()) {
155 return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
158 QString FileNaming::imagePath() const {
162 void FileNaming::setImagePath(const QString& path) {
163 QString p = canonicalPath(path);
165 if (m_imagePath != p) {
167 emit imagePathChanged();
171 QString FileNaming::videoPath() const {
175 void FileNaming::setVideoPath(const QString& path) {
176 QString p = canonicalPath(path);
178 if (m_videoPath != p) {
180 emit videoPathChanged();
184 QString FileNaming::temporaryVideoPath() const {
185 return m_temporaryVideoPath;
188 void FileNaming::setTemporaryVideoPath(const QString& path) {
189 QString p = canonicalPath(path);
191 if (m_temporaryVideoPath != p) {
192 m_temporaryVideoPath = p;
193 emit temporaryVideoPathChanged();
197 Settings *FileNaming::settings() const {
201 void FileNaming::setSettings(Settings *settings) {
202 if (m_settings != settings) {
203 m_settings = settings;
205 emit settingsChanged();
209 void FileNaming::classBegin() {
213 void FileNaming::componentComplete() {
214 if (QDir(m_imagePath).canonicalPath() == QDir(m_videoPath).canonicalPath()) {
215 m_index = new SingleFileIndex(m_settings);
218 m_index = new MultiFileIndex(m_settings);