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"
26 #include <QDeclarativeInfo>
32 #define PATH QString("%1%2MyDocs%2DCIM%2").arg(QDir::homePath()).arg(QDir::separator())
33 #define TEMP_PATH QString("%1%2MyDocs%2.cameraplus%2").arg(QDir::homePath()).arg(QDir::separator())
35 FileNaming::FileNaming(QObject *parent) :
41 FileNaming::~FileNaming() {
45 QString FileNaming::imageSuffix() const {
49 void FileNaming::setImageSuffix(const QString& suffix) {
50 if (m_imageSuffix != suffix) {
51 m_imageSuffix = suffix;
52 emit imageSuffixChanged();
56 QString FileNaming::videoSuffix() const {
60 void FileNaming::setVideoSuffix(const QString& suffix) {
61 if (m_videoSuffix != suffix) {
62 m_videoSuffix = suffix;
63 emit videoSuffixChanged();
67 QString FileNaming::imageFileName() {
68 return fileName(m_imagePath, m_imageSuffix);
71 QString FileNaming::videoFileName() {
72 return fileName(m_videoPath, m_videoSuffix);
75 QString FileNaming::fileName(const QString& path, const QString& suffix) {
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 = QDate::currentDate().toString("yyyyMMdd");
94 // index is the last used index
97 if (m_settings->fileNamingStamp() != date) {
98 m_settings->setFileNamingStamp(date);
101 index = m_settings->fileNamingCounter();
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_settings->setFileNamingCounter(index);
127 qmlInfo(this) << "Failed to guess a file name";
132 QString FileNaming::canonicalPath(const QString& path) {
133 if (!QDir::root().mkpath(path)) {
134 qmlInfo(this) << "Failed to create path" << path;
138 QString newPath = QFileInfo(path).canonicalFilePath();
140 if (newPath.isEmpty()) {
144 if (!newPath.endsWith(QDir::separator())) {
145 newPath.append(QDir::separator());
151 QString FileNaming::temporaryVideoFileName() {
152 if (m_temporaryVideoPath.isEmpty()) {
156 return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
159 QString FileNaming::imagePath() const {
163 void FileNaming::setImagePath(const QString& path) {
164 QString p = canonicalPath(path);
166 if (m_imagePath != p) {
168 emit imagePathChanged();
172 QString FileNaming::videoPath() const {
176 void FileNaming::setVideoPath(const QString& path) {
177 QString p = canonicalPath(path);
179 if (m_videoPath != p) {
181 emit videoPathChanged();
185 QString FileNaming::temporaryVideoPath() const {
186 return m_temporaryVideoPath;
189 void FileNaming::setTemporaryVideoPath(const QString& path) {
190 QString p = canonicalPath(path);
192 if (m_temporaryVideoPath != p) {
193 m_temporaryVideoPath = p;
194 emit temporaryVideoPathChanged();
198 Settings *FileNaming::settings() const {
202 void FileNaming::setSettings(Settings *settings) {
203 if (m_settings != settings) {
204 m_settings = settings;
206 emit settingsChanged();