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