silence dbus-send output
[harmattan/cameraplus] / src / filenaming.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "filenaming.h"
22 #include <QDir>
23 #include <QDate>
24 #include <QDateTime>
25 #include <QFile>
26 #include "settings.h"
27 #if defined(QT4)
28 #include <QDeclarativeInfo>
29 #elif defined(QT5)
30 #include <QQmlInfo>
31 #endif
32
33 FileNaming::FileNaming(QObject *parent) :
34   QObject(parent),
35   m_settings(0),
36   m_index(0) {
37
38 }
39
40 FileNaming::~FileNaming() {
41   delete m_index; m_index = 0;
42 }
43
44 QString FileNaming::imageSuffix() const {
45   return m_imageSuffix;
46 }
47
48 void FileNaming::setImageSuffix(const QString& suffix) {
49   if (m_imageSuffix != suffix) {
50     m_imageSuffix = suffix;
51     emit imageSuffixChanged();
52   }
53 }
54
55 QString FileNaming::videoSuffix() const {
56   return m_videoSuffix;
57 }
58
59 void FileNaming::setVideoSuffix(const QString& suffix) {
60   if (m_videoSuffix != suffix) {
61     m_videoSuffix = suffix;
62     emit videoSuffixChanged();
63   }
64 }
65
66 QString FileNaming::imageFileName() {
67   return fileName(m_imagePath, m_imageSuffix, Image);
68 }
69
70 QString FileNaming::videoFileName() {
71   return fileName(m_videoPath, m_videoSuffix, Video);
72 }
73
74 QString FileNaming::fileName(const QString& path, const QString& suffix, const Type& type) {
75   FileIndex::Type indexType = (FileIndex::Type) type;
76
77   if (!m_settings) {
78     qmlInfo(this) << "settings has not been set";
79     return QString();
80   }
81
82   if (suffix.isEmpty()) {
83     qmlInfo(this) << "called with empty suffix";
84     return QString();
85   }
86
87   if (path.isEmpty()) {
88     qmlInfo(this) << "called with empty path";
89     return QString();
90   }
91
92   QString date;
93   if (m_settings && !m_settings->isUtcForFileNamingUsed()) {
94     date = QDate::currentDate().toString("yyyyMMdd");
95   }
96   else {
97     date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
98   }
99   QDir dir(path);
100
101   // index is the last used index
102   int index = 0;
103
104   if (m_index->stamp(indexType) != date) {
105     m_index->setStamp(indexType, date);
106   }
107   else {
108     index = m_index->counter(indexType);
109   }
110
111   if (index == 0) {
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();
117     }
118   }
119
120   while (index < INT_MAX) {
121     ++index;
122
123     QString name = QString("%1%2_%3.%4")
124       .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
125
126     if (!QFile(name).exists()) {
127       m_index->setCounter(indexType, index);
128       return name;
129     }
130
131   }
132
133   qmlInfo(this) << "Failed to guess a file name";
134
135   return QString();
136 }
137
138 QString FileNaming::canonicalPath(const QString& path) {
139   if (!QDir::root().mkpath(path)) {
140     qmlInfo(this) << "Failed to create path" << path;
141     return QString();
142   }
143
144   QString newPath = QFileInfo(path).canonicalFilePath();
145
146   if (newPath.isEmpty()) {
147     return newPath;
148   }
149
150   if (!newPath.endsWith(QDir::separator())) {
151     newPath.append(QDir::separator());
152   }
153
154   return newPath;
155 }
156
157 QString FileNaming::temporaryVideoFileName() {
158   if (m_temporaryVideoPath.isEmpty()) {
159     return QString();
160   }
161
162   return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
163 }
164
165 QString FileNaming::imagePath() const {
166   return m_imagePath;
167 }
168
169 void FileNaming::setImagePath(const QString& path) {
170   QString p = canonicalPath(path);
171
172   if (m_imagePath != p) {
173     m_imagePath = p;
174     emit imagePathChanged();
175   }
176 }
177
178 QString FileNaming::videoPath() const {
179   return m_videoPath;
180 }
181
182 void FileNaming::setVideoPath(const QString& path) {
183   QString p = canonicalPath(path);
184
185   if (m_videoPath != p) {
186     m_videoPath = p;
187     emit videoPathChanged();
188   }
189 }
190
191 QString FileNaming::temporaryVideoPath() const {
192   return m_temporaryVideoPath;
193 }
194
195 void FileNaming::setTemporaryVideoPath(const QString& path) {
196   QString p = canonicalPath(path);
197
198   if (m_temporaryVideoPath != p) {
199     m_temporaryVideoPath = p;
200     emit temporaryVideoPathChanged();
201   }
202 }
203
204 Settings *FileNaming::settings() const {
205   return m_settings;
206 }
207
208 void FileNaming::setSettings(Settings *settings) {
209   if (m_settings != settings) {
210     m_settings = settings;
211
212     emit settingsChanged();
213   }
214 }
215
216 void FileNaming::classBegin() {
217   // Nothing
218 }
219
220 void FileNaming::componentComplete() {
221   if (QDir(m_imagePath).canonicalPath() == QDir(m_videoPath).canonicalPath()) {
222     m_index = new SingleFileIndex(m_settings);
223   }
224   else {
225     m_index = new MultiFileIndex(m_settings);
226   }
227 }