a0016474394dd57e3bb226939903385ed4f0a106
[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 <QFile>
25 #if defined(QT4)
26 #include <QDeclarativeInfo>
27 #elif defined(QT5)
28 #include <QQmlInfo>
29 #endif
30 #include "settings.h"
31
32 #define PATH QString("%1%2MyDocs%2DCIM%2").arg(QDir::homePath()).arg(QDir::separator())
33 #define TEMP_PATH QString("%1%2MyDocs%2.cameraplus%2").arg(QDir::homePath()).arg(QDir::separator())
34
35 FileNaming::FileNaming(QObject *parent) :
36   QObject(parent),
37   m_settings(0) {
38
39 }
40
41 FileNaming::~FileNaming() {
42
43 }
44
45 QString FileNaming::imageSuffix() const {
46   return m_imageSuffix;
47 }
48
49 void FileNaming::setImageSuffix(const QString& suffix) {
50   if (m_imageSuffix != suffix) {
51     m_imageSuffix = suffix;
52     emit imageSuffixChanged();
53   }
54 }
55
56 QString FileNaming::videoSuffix() const {
57   return m_videoSuffix;
58 }
59
60 void FileNaming::setVideoSuffix(const QString& suffix) {
61   if (m_videoSuffix != suffix) {
62     m_videoSuffix = suffix;
63     emit videoSuffixChanged();
64   }
65 }
66
67 QString FileNaming::imageFileName() {
68   return fileName(m_imagePath, m_imageSuffix);
69 }
70
71 QString FileNaming::videoFileName() {
72   return fileName(m_videoPath, m_videoSuffix);
73 }
74
75 QString FileNaming::fileName(const QString& path, const QString& suffix) {
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 = QDate::currentDate().toString("yyyyMMdd");
92   QDir dir(path);
93
94   // index is the last used index
95   int index = 0;
96
97   if (m_settings->fileNamingStamp() != date) {
98     m_settings->setFileNamingStamp(date);
99   }
100   else {
101     index = m_settings->fileNamingCounter();
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_settings->setFileNamingCounter(index);
121
122       return name;
123     }
124
125   }
126
127   qmlInfo(this) << "Failed to guess a file name";
128
129   return QString();
130 }
131
132 QString FileNaming::canonicalPath(const QString& path) {
133   if (!QDir::root().mkpath(path)) {
134     qmlInfo(this) << "Failed to create path" << path;
135     return QString();
136   }
137
138   QString newPath = QFileInfo(path).canonicalFilePath();
139
140   if (newPath.isEmpty()) {
141     return newPath;
142   }
143
144   if (!newPath.endsWith(QDir::separator())) {
145     newPath.append(QDir::separator());
146   }
147
148   return newPath;
149 }
150
151 QString FileNaming::temporaryVideoFileName() {
152   if (m_temporaryVideoPath.isEmpty()) {
153     return QString();
154   }
155
156   return QString("%1.cameraplus_video.tmp").arg(m_temporaryVideoPath);
157 }
158
159 QString FileNaming::imagePath() const {
160   return m_imagePath;
161 }
162
163 void FileNaming::setImagePath(const QString& path) {
164   QString p = canonicalPath(path);
165
166   if (m_imagePath != p) {
167     m_imagePath = p;
168     emit imagePathChanged();
169   }
170 }
171
172 QString FileNaming::videoPath() const {
173   return m_videoPath;
174 }
175
176 void FileNaming::setVideoPath(const QString& path) {
177   QString p = canonicalPath(path);
178
179   if (m_videoPath != p) {
180     m_videoPath = p;
181     emit videoPathChanged();
182   }
183 }
184
185 QString FileNaming::temporaryVideoPath() const {
186   return m_temporaryVideoPath;
187 }
188
189 void FileNaming::setTemporaryVideoPath(const QString& path) {
190   QString p = canonicalPath(path);
191
192   if (m_temporaryVideoPath != p) {
193     m_temporaryVideoPath = p;
194     emit temporaryVideoPathChanged();
195   }
196 }
197
198 Settings *FileNaming::settings() const {
199   return m_settings;
200 }
201
202 void FileNaming::setSettings(Settings *settings) {
203   if (m_settings != settings) {
204     m_settings = settings;
205
206     emit settingsChanged();
207   }
208 }