First attempt at getting rid of tracker for post capture
[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 #include "settings.h"
32
33 #define PATH QString("%1%2MyDocs%2DCIM%2").arg(QDir::homePath()).arg(QDir::separator())
34 #define TEMP_PATH QString("%1%2MyDocs%2.cameraplus%2").arg(QDir::homePath()).arg(QDir::separator())
35
36 FileNaming::FileNaming(QObject *parent) :
37   QObject(parent),
38   m_settings(0) {
39
40 }
41
42 FileNaming::~FileNaming() {
43
44 }
45
46 QString FileNaming::imageSuffix() const {
47   return m_imageSuffix;
48 }
49
50 void FileNaming::setImageSuffix(const QString& suffix) {
51   if (m_imageSuffix != suffix) {
52     m_imageSuffix = suffix;
53     emit imageSuffixChanged();
54   }
55 }
56
57 QString FileNaming::videoSuffix() const {
58   return m_videoSuffix;
59 }
60
61 void FileNaming::setVideoSuffix(const QString& suffix) {
62   if (m_videoSuffix != suffix) {
63     m_videoSuffix = suffix;
64     emit videoSuffixChanged();
65   }
66 }
67
68 QString FileNaming::imageFileName() {
69   return fileName(m_imagePath, m_imageSuffix);
70 }
71
72 QString FileNaming::videoFileName() {
73   return fileName(m_videoPath, m_videoSuffix);
74 }
75
76 QString FileNaming::fileName(const QString& path, const QString& suffix) {
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 = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
93   QDir dir(path);
94
95   // index is the last used index
96   int index = 0;
97
98   if (m_settings->fileNamingStamp() != date) {
99     m_settings->setFileNamingStamp(date);
100   }
101   else {
102     index = m_settings->fileNamingCounter();
103   }
104
105   if (index == 0) {
106     QStringList filters(QString("*%1_*").arg(date));
107     QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name);
108     if (!entries.isEmpty()) {
109       QString name = QFile(entries.last()).fileName();
110       index = name.section('_', 1, 1).section('.', 0, 0).toInt();
111     }
112   }
113
114   while (index < INT_MAX) {
115     ++index;
116
117     QString name = QString("%1%2_%3.%4")
118       .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
119
120     if (!QFile(name).exists()) {
121       m_settings->setFileNamingCounter(index);
122
123       return name;
124     }
125
126   }
127
128   qmlInfo(this) << "Failed to guess a file name";
129
130   return QString();
131 }
132
133 QString FileNaming::canonicalPath(const QString& path) {
134   if (!QDir::root().mkpath(path)) {
135     qmlInfo(this) << "Failed to create path" << path;
136     return QString();
137   }
138
139   QString newPath = QFileInfo(path).canonicalFilePath();
140
141   if (newPath.isEmpty()) {
142     return newPath;
143   }
144
145   if (!newPath.endsWith(QDir::separator())) {
146     newPath.append(QDir::separator());
147   }
148
149   return newPath;
150 }
151
152 QString FileNaming::temporaryVideoFileName() {
153   if (m_temporaryVideoPath.isEmpty()) {
154     return QString();
155   }
156
157   return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
158 }
159
160 QString FileNaming::imagePath() const {
161   return m_imagePath;
162 }
163
164 void FileNaming::setImagePath(const QString& path) {
165   QString p = canonicalPath(path);
166
167   if (m_imagePath != p) {
168     m_imagePath = p;
169     emit imagePathChanged();
170   }
171 }
172
173 QString FileNaming::videoPath() const {
174   return m_videoPath;
175 }
176
177 void FileNaming::setVideoPath(const QString& path) {
178   QString p = canonicalPath(path);
179
180   if (m_videoPath != p) {
181     m_videoPath = p;
182     emit videoPathChanged();
183   }
184 }
185
186 QString FileNaming::temporaryVideoPath() const {
187   return m_temporaryVideoPath;
188 }
189
190 void FileNaming::setTemporaryVideoPath(const QString& path) {
191   QString p = canonicalPath(path);
192
193   if (m_temporaryVideoPath != p) {
194     m_temporaryVideoPath = p;
195     emit temporaryVideoPathChanged();
196   }
197 }
198
199 Settings *FileNaming::settings() const {
200   return m_settings;
201 }
202
203 void FileNaming::setSettings(Settings *settings) {
204   if (m_settings != settings) {
205     m_settings = settings;
206
207     emit settingsChanged();
208   }
209 }