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 FileNaming::FileNaming(QObject *parent) :
39 FileNaming::~FileNaming() {
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);
69 QString FileNaming::videoFileName() {
70 return fileName(m_videoPath, m_videoSuffix);
73 QString FileNaming::fileName(const QString& path, const QString& suffix) {
75 qmlInfo(this) << "settings has not been set";
79 if (suffix.isEmpty()) {
80 qmlInfo(this) << "called with empty suffix";
85 qmlInfo(this) << "called with empty path";
89 QString date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
92 // index is the last used index
95 if (m_settings->fileNamingStamp() != date) {
96 m_settings->setFileNamingStamp(date);
99 index = m_settings->fileNamingCounter();
103 QStringList filters(QString("*%1_*").arg(date));
104 QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name);
105 if (!entries.isEmpty()) {
106 QString name = QFile(entries.last()).fileName();
107 index = name.section('_', 1, 1).section('.', 0, 0).toInt();
111 while (index < INT_MAX) {
114 QString name = QString("%1%2_%3.%4")
115 .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
117 if (!QFile(name).exists()) {
118 m_settings->setFileNamingCounter(index);
125 qmlInfo(this) << "Failed to guess a file name";
130 QString FileNaming::canonicalPath(const QString& path) {
131 if (!QDir::root().mkpath(path)) {
132 qmlInfo(this) << "Failed to create path" << path;
136 QString newPath = QFileInfo(path).canonicalFilePath();
138 if (newPath.isEmpty()) {
142 if (!newPath.endsWith(QDir::separator())) {
143 newPath.append(QDir::separator());
149 QString FileNaming::temporaryVideoFileName() {
150 if (m_temporaryVideoPath.isEmpty()) {
154 return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
157 QString FileNaming::imagePath() const {
161 void FileNaming::setImagePath(const QString& path) {
162 QString p = canonicalPath(path);
164 if (m_imagePath != p) {
166 emit imagePathChanged();
170 QString FileNaming::videoPath() const {
174 void FileNaming::setVideoPath(const QString& path) {
175 QString p = canonicalPath(path);
177 if (m_videoPath != p) {
179 emit videoPathChanged();
183 QString FileNaming::temporaryVideoPath() const {
184 return m_temporaryVideoPath;
187 void FileNaming::setTemporaryVideoPath(const QString& path) {
188 QString p = canonicalPath(path);
190 if (m_temporaryVideoPath != p) {
191 m_temporaryVideoPath = p;
192 emit temporaryVideoPathChanged();
196 Settings *FileNaming::settings() const {
200 void FileNaming::setSettings(Settings *settings) {
201 if (m_settings != settings) {
202 m_settings = settings;
204 emit settingsChanged();