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>
33 #define PATH QString("%1%2MyDocs%2DCIM%2").arg(QDir::homePath()).arg(QDir::separator())
34 #define TEMP_PATH QString("%1%2MyDocs%2.cameraplus%2").arg(QDir::homePath()).arg(QDir::separator())
36 FileNaming::FileNaming(QObject *parent) :
42 FileNaming::~FileNaming() {
46 QString FileNaming::imageSuffix() const {
50 void FileNaming::setImageSuffix(const QString& suffix) {
51 if (m_imageSuffix != suffix) {
52 m_imageSuffix = suffix;
53 emit imageSuffixChanged();
57 QString FileNaming::videoSuffix() const {
61 void FileNaming::setVideoSuffix(const QString& suffix) {
62 if (m_videoSuffix != suffix) {
63 m_videoSuffix = suffix;
64 emit videoSuffixChanged();
68 QString FileNaming::imageFileName() {
69 return fileName(m_imagePath, m_imageSuffix);
72 QString FileNaming::videoFileName() {
73 return fileName(m_videoPath, m_videoSuffix);
76 QString FileNaming::fileName(const QString& path, const QString& suffix) {
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";
92 QString date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
95 // index is the last used index
98 if (m_settings->fileNamingStamp() != date) {
99 m_settings->setFileNamingStamp(date);
102 index = m_settings->fileNamingCounter();
106 QStringList filters(QString("*%1_*").arg(date));
107 QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name);
108 if (!entries.isEmpty()) {
109 QString name = QFile(entries.last()).fileName();
110 index = name.section('_', 1, 1).section('.', 0, 0).toInt();
114 while (index < INT_MAX) {
117 QString name = QString("%1%2_%3.%4")
118 .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
120 if (!QFile(name).exists()) {
121 m_settings->setFileNamingCounter(index);
128 qmlInfo(this) << "Failed to guess a file name";
133 QString FileNaming::canonicalPath(const QString& path) {
134 if (!QDir::root().mkpath(path)) {
135 qmlInfo(this) << "Failed to create path" << path;
139 QString newPath = QFileInfo(path).canonicalFilePath();
141 if (newPath.isEmpty()) {
145 if (!newPath.endsWith(QDir::separator())) {
146 newPath.append(QDir::separator());
152 QString FileNaming::temporaryVideoFileName() {
153 if (m_temporaryVideoPath.isEmpty()) {
157 return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
160 QString FileNaming::imagePath() const {
164 void FileNaming::setImagePath(const QString& path) {
165 QString p = canonicalPath(path);
167 if (m_imagePath != p) {
169 emit imagePathChanged();
173 QString FileNaming::videoPath() const {
177 void FileNaming::setVideoPath(const QString& path) {
178 QString p = canonicalPath(path);
180 if (m_videoPath != p) {
182 emit videoPathChanged();
186 QString FileNaming::temporaryVideoPath() const {
187 return m_temporaryVideoPath;
190 void FileNaming::setTemporaryVideoPath(const QString& path) {
191 QString p = canonicalPath(path);
193 if (m_temporaryVideoPath != p) {
194 m_temporaryVideoPath = p;
195 emit temporaryVideoPathChanged();
199 Settings *FileNaming::settings() const {
203 void FileNaming::setSettings(Settings *settings) {
204 if (m_settings != settings) {
205 m_settings = settings;
207 emit settingsChanged();