Remove PATH and TEMP_PATH
[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 FileNaming::FileNaming(QObject *parent) :
34   QObject(parent),
35   m_settings(0) {
36
37 }
38
39 FileNaming::~FileNaming() {
40
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);
67 }
68
69 QString FileNaming::videoFileName() {
70   return fileName(m_videoPath, m_videoSuffix);
71 }
72
73 QString FileNaming::fileName(const QString& path, const QString& suffix) {
74   if (!m_settings) {
75     qmlInfo(this) << "settings has not been set";
76     return QString();
77   }
78
79   if (suffix.isEmpty()) {
80     qmlInfo(this) << "called with empty suffix";
81     return QString();
82   }
83
84   if (path.isEmpty()) {
85     qmlInfo(this) << "called with empty path";
86     return QString();
87   }
88
89   QString date = QDateTime::currentDateTime().toUTC().date().toString("yyyyMMdd");
90   QDir dir(path);
91
92   // index is the last used index
93   int index = 0;
94
95   if (m_settings->fileNamingStamp() != date) {
96     m_settings->setFileNamingStamp(date);
97   }
98   else {
99     index = m_settings->fileNamingCounter();
100   }
101
102   if (index == 0) {
103     QStringList filters(QString("*%1_*").arg(date));
104     QStringList entries = dir.entryList(filters, QDir::Files, QDir::Name);
105     if (!entries.isEmpty()) {
106       QString name = QFile(entries.last()).fileName();
107       index = name.section('_', 1, 1).section('.', 0, 0).toInt();
108     }
109   }
110
111   while (index < INT_MAX) {
112     ++index;
113
114     QString name = QString("%1%2_%3.%4")
115       .arg(path).arg(date).arg(QString().sprintf("%03i", index)).arg(suffix);
116
117     if (!QFile(name).exists()) {
118       m_settings->setFileNamingCounter(index);
119
120       return name;
121     }
122
123   }
124
125   qmlInfo(this) << "Failed to guess a file name";
126
127   return QString();
128 }
129
130 QString FileNaming::canonicalPath(const QString& path) {
131   if (!QDir::root().mkpath(path)) {
132     qmlInfo(this) << "Failed to create path" << path;
133     return QString();
134   }
135
136   QString newPath = QFileInfo(path).canonicalFilePath();
137
138   if (newPath.isEmpty()) {
139     return newPath;
140   }
141
142   if (!newPath.endsWith(QDir::separator())) {
143     newPath.append(QDir::separator());
144   }
145
146   return newPath;
147 }
148
149 QString FileNaming::temporaryVideoFileName() {
150   if (m_temporaryVideoPath.isEmpty()) {
151     return QString();
152   }
153
154   return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
155 }
156
157 QString FileNaming::imagePath() const {
158   return m_imagePath;
159 }
160
161 void FileNaming::setImagePath(const QString& path) {
162   QString p = canonicalPath(path);
163
164   if (m_imagePath != p) {
165     m_imagePath = p;
166     emit imagePathChanged();
167   }
168 }
169
170 QString FileNaming::videoPath() const {
171   return m_videoPath;
172 }
173
174 void FileNaming::setVideoPath(const QString& path) {
175   QString p = canonicalPath(path);
176
177   if (m_videoPath != p) {
178     m_videoPath = p;
179     emit videoPathChanged();
180   }
181 }
182
183 QString FileNaming::temporaryVideoPath() const {
184   return m_temporaryVideoPath;
185 }
186
187 void FileNaming::setTemporaryVideoPath(const QString& path) {
188   QString p = canonicalPath(path);
189
190   if (m_temporaryVideoPath != p) {
191     m_temporaryVideoPath = p;
192     emit temporaryVideoPathChanged();
193   }
194 }
195
196 Settings *FileNaming::settings() const {
197   return m_settings;
198 }
199
200 void FileNaming::setSettings(Settings *settings) {
201   if (m_settings != settings) {
202     m_settings = settings;
203
204     emit settingsChanged();
205   }
206 }