X-Git-Url: https://cgit.sxemacs.org/?a=blobdiff_plain;f=src%2Ffilenaming.cpp;h=fa64a6c8756f406959cb05f1f1db1bc41db07cd2;hb=HEAD;hp=d652f49946a79a5649df47452fd848b4b717fd8b;hpb=f35981c42be67bd2f591fe61d0d2d7a427f7dbe6;p=harmattan%2Fcameraplus diff --git a/src/filenaming.cpp b/src/filenaming.cpp index d652f49..fa64a6c 100644 --- a/src/filenaming.cpp +++ b/src/filenaming.cpp @@ -1,64 +1,227 @@ +/*! + * This file is part of CameraPlus. + * + * Copyright (C) 2012-2013 Mohammed Sameer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #include "filenaming.h" #include -#include #include +#include #include - -#define PATH QString("%1%2MyDocs%2cameraplus%2").arg(QDir::homePath()).arg(QDir::separator()) +#include "settings.h" +#if defined(QT4) +#include +#elif defined(QT5) +#include +#endif FileNaming::FileNaming(QObject *parent) : - QObject(parent) { + QObject(parent), + m_settings(0), + m_index(0) { } FileNaming::~FileNaming() { + delete m_index; m_index = 0; +} +QString FileNaming::imageSuffix() const { + return m_imageSuffix; } void FileNaming::setImageSuffix(const QString& suffix) { - m_image = suffix; + if (m_imageSuffix != suffix) { + m_imageSuffix = suffix; + emit imageSuffixChanged(); + } +} + +QString FileNaming::videoSuffix() const { + return m_videoSuffix; } void FileNaming::setVideoSuffix(const QString& suffix) { - m_video = suffix; + if (m_videoSuffix != suffix) { + m_videoSuffix = suffix; + emit videoSuffixChanged(); + } } QString FileNaming::imageFileName() { - return fileName(m_image); + return fileName(m_imagePath, m_imageSuffix, Image); } QString FileNaming::videoFileName() { - return fileName(m_video); + return fileName(m_videoPath, m_videoSuffix, Video); } -QString FileNaming::fileName(const QString& suffix) { - if (suffix.isEmpty()) { +QString FileNaming::fileName(const QString& path, const QString& suffix, const Type& type) { + FileIndex::Type indexType = (FileIndex::Type) type; + + if (!m_settings) { + qmlInfo(this) << "settings has not been set"; return QString(); } - if (!QDir::root().mkpath(PATH)) { - qWarning() << "Failed to create" << PATH; + if (suffix.isEmpty()) { + qmlInfo(this) << "called with empty suffix"; return QString(); } - // Shamelessly stolen from Aura - QDir dir(PATH); - QString date = QDate::currentDate().toString("yyyyMMdd"); + if (path.isEmpty()) { + qmlInfo(this) << "called with empty path"; + return QString(); + } - QStringList filters(QString("*%1_*").arg(date)); - QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name); + QString date; + if (m_settings && !m_settings->isUtcForFileNamingUsed()) { + date = QDate::currentDate().toString("yyyyMMdd"); + } + else { + date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd"); + } + QDir dir(path); + // index is the last used index int index = 0; - if (!entries.isEmpty()) { - QString name = QFile(entries.last()).fileName(); - index = name.section('_', 1, 1).section('.', 0, 0).toInt(); + if (m_index->stamp(indexType) != date) { + m_index->setStamp(indexType, date); + } + else { + index = m_index->counter(indexType); + } + + if (index == 0) { + QStringList filters(QString("*%1_*").arg(date)); + QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name); + if (!entries.isEmpty()) { + QString name = QFile(entries.last()).fileName(); + index = name.section('_', 1, 1).section('.', 0, 0).toInt(); + } + } + + while (index < INT_MAX) { + ++index; + + QString name = QString("%1%2_%3.%4") + .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix); + + if (!QFile(name).exists()) { + m_index->setCounter(indexType, index); + return name; + } + + } + + qmlInfo(this) << "Failed to guess a file name"; + + return QString(); +} + +QString FileNaming::canonicalPath(const QString& path) { + if (!QDir::root().mkpath(path)) { + qmlInfo(this) << "Failed to create path" << path; + return QString(); + } + + QString newPath = QFileInfo(path).canonicalFilePath(); + + if (newPath.isEmpty()) { + return newPath; + } + + if (!newPath.endsWith(QDir::separator())) { + newPath.append(QDir::separator()); } - ++index; + return newPath; +} + +QString FileNaming::temporaryVideoFileName() { + if (m_temporaryVideoPath.isEmpty()) { + return QString(); + } + + return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath); +} + +QString FileNaming::imagePath() const { + return m_imagePath; +} + +void FileNaming::setImagePath(const QString& path) { + QString p = canonicalPath(path); - QString name = QString("%1%2_%3.%4").arg(PATH).arg(date).arg(QString().sprintf("%03i", index)). - arg(suffix); + if (m_imagePath != p) { + m_imagePath = p; + emit imagePathChanged(); + } +} + +QString FileNaming::videoPath() const { + return m_videoPath; +} + +void FileNaming::setVideoPath(const QString& path) { + QString p = canonicalPath(path); + + if (m_videoPath != p) { + m_videoPath = p; + emit videoPathChanged(); + } +} - return name; +QString FileNaming::temporaryVideoPath() const { + return m_temporaryVideoPath; +} + +void FileNaming::setTemporaryVideoPath(const QString& path) { + QString p = canonicalPath(path); + + if (m_temporaryVideoPath != p) { + m_temporaryVideoPath = p; + emit temporaryVideoPathChanged(); + } +} + +Settings *FileNaming::settings() const { + return m_settings; +} + +void FileNaming::setSettings(Settings *settings) { + if (m_settings != settings) { + m_settings = settings; + + emit settingsChanged(); + } +} + +void FileNaming::classBegin() { + // Nothing +} + +void FileNaming::componentComplete() { + if (QDir(m_imagePath).canonicalPath() == QDir(m_videoPath).canonicalPath()) { + m_index = new SingleFileIndex(m_settings); + } + else { + m_index = new MultiFileIndex(m_settings); + } }