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"
28 #include <QDeclarativeInfo>
33 FileNaming::FileNaming(QObject *parent) :
40 FileNaming::~FileNaming() {
41 delete m_index; m_index = 0;
44 QString FileNaming::imageSuffix() const {
48 void FileNaming::setImageSuffix(const QString& suffix) {
49 if (m_imageSuffix != suffix) {
50 m_imageSuffix = suffix;
51 emit imageSuffixChanged();
55 QString FileNaming::videoSuffix() const {
59 void FileNaming::setVideoSuffix(const QString& suffix) {
60 if (m_videoSuffix != suffix) {
61 m_videoSuffix = suffix;
62 emit videoSuffixChanged();
66 QString FileNaming::imageFileName() {
67 return fileName(m_imagePath, m_imageSuffix, Image);
70 QString FileNaming::videoFileName() {
71 return fileName(m_videoPath, m_videoSuffix, Video);
74 QString FileNaming::fileName(const QString& path, const QString& suffix, const Type& type) {
75 FileIndex::Type indexType = (FileIndex::Type) type;
78 qmlInfo(this) << "settings has not been set";
82 if (suffix.isEmpty()) {
83 qmlInfo(this) << "called with empty suffix";
88 qmlInfo(this) << "called with empty path";
93 if (m_settings && !m_settings->isUtcForFileNamingUsed()) {
94 date = QDate::currentDate().toString("yyyyMMdd");
97 date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
101 // index is the last used index
104 if (m_index->stamp(indexType) != date) {
105 m_index->setStamp(indexType, date);
108 index = m_index->counter(indexType);
112 QStringList filters(QString("*%1_*").arg(date));
113 QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name);
114 if (!entries.isEmpty()) {
115 QString name = QFile(entries.last()).fileName();
116 index = name.section('_', 1, 1).section('.', 0, 0).toInt();
120 while (index < INT_MAX) {
123 QString name = QString("%1%2_%3.%4")
124 .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
126 if (!QFile(name).exists()) {
127 m_index->setCounter(indexType, index);
133 qmlInfo(this) << "Failed to guess a file name";
138 QString FileNaming::canonicalPath(const QString& path) {
139 if (!QDir::root().mkpath(path)) {
140 qmlInfo(this) << "Failed to create path" << path;
144 QString newPath = QFileInfo(path).canonicalFilePath();
146 if (newPath.isEmpty()) {
150 if (!newPath.endsWith(QDir::separator())) {
151 newPath.append(QDir::separator());
157 QString FileNaming::temporaryVideoFileName() {
158 if (m_temporaryVideoPath.isEmpty()) {
162 return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
165 QString FileNaming::imagePath() const {
169 void FileNaming::setImagePath(const QString& path) {
170 QString p = canonicalPath(path);
172 if (m_imagePath != p) {
174 emit imagePathChanged();
178 QString FileNaming::videoPath() const {
182 void FileNaming::setVideoPath(const QString& path) {
183 QString p = canonicalPath(path);
185 if (m_videoPath != p) {
187 emit videoPathChanged();
191 QString FileNaming::temporaryVideoPath() const {
192 return m_temporaryVideoPath;
195 void FileNaming::setTemporaryVideoPath(const QString& path) {
196 QString p = canonicalPath(path);
198 if (m_temporaryVideoPath != p) {
199 m_temporaryVideoPath = p;
200 emit temporaryVideoPathChanged();
204 Settings *FileNaming::settings() const {
208 void FileNaming::setSettings(Settings *settings) {
209 if (m_settings != settings) {
210 m_settings = settings;
212 emit settingsChanged();
216 void FileNaming::classBegin() {
220 void FileNaming::componentComplete() {
221 if (QDir(m_imagePath).canonicalPath() == QDir(m_videoPath).canonicalPath()) {
222 m_index = new SingleFileIndex(m_settings);
225 m_index = new MultiFileIndex(m_settings);