Added a hidden setting (fileNaming/useUtc) to disable UTC usage for file names
[harmattan/cameraplus] / src / settings.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 "settings.h"
22 #include <QSettings>
23 #include <QDir>
24 #include "devicesettings.h"
25
26 #define PATH QString("%1%2.config%2/cameraplus.conf").arg(QDir::homePath()).arg(QDir::separator())
27
28 #define DEFAULT_MODE                    1
29 #define DEFAULT_USE_GPS                 true
30 #define DEFAULT_USE_GEOTAGS             true
31 #define DEFAULT_SOUND_ENABLED           true
32 #define DEFAULT_SHOW_TOOL_BAR           false
33 #define DEFAULT_VIDEO_MUTE              false
34 #define DEFAULT_GRID_ENABLED            false
35 #define DEFAULT_FACE_DETECTION_ENABLED  true
36 #define DEFAULT_ZOOM_AS_SHUTTER         false
37 #define DEFAULT_PROXIMITY_AS_SHUTTER    false
38 #define DEFAULT_DEVICE                  0
39 #define DEFAULT_ENABLE_PREVIEW          true
40 #define DEFAULT_FILE_NAMING_UTC         true
41
42 Settings::Settings(QObject *parent) :
43   QObject(parent),
44   m_settings(new QSettings(PATH, QSettings::IniFormat, this)),
45   m_device(0) {
46
47 }
48
49 Settings::~Settings() {
50   delete m_settings; m_settings = 0;
51   delete m_device; m_device = 0;
52 }
53
54 int Settings::mode() const {
55   return m_settings->value("camera/mode", DEFAULT_MODE).toInt();
56 }
57
58 void Settings::setMode(int mode) {
59   if (mode != Settings::mode()) {
60     m_settings->setValue("camera/mode", mode);
61
62     emit modeChanged();
63   }
64 }
65
66 QString Settings::creatorName() const {
67   return m_settings->value("camera/creatorName").toString();
68 }
69
70 void Settings::setCreatorName(const QString& name) {
71   if (name != creatorName()) {
72     m_settings->setValue("camera/creatorName", name);
73
74     emit creatorNameChanged();
75   }
76 }
77
78 bool Settings::useGps() const {
79   return m_settings->value("camera/useGps", DEFAULT_USE_GPS).toBool();
80 }
81
82 void Settings::setUseGps(bool enable) {
83   if (enable != useGps()) {
84     m_settings->setValue("camera/useGps", enable);
85
86     emit useGpsChanged();
87   }
88 }
89
90 bool Settings::useGeotags() const {
91   return m_settings->value("camera/useGeotags", DEFAULT_USE_GEOTAGS).toBool();
92 }
93
94 void Settings::setUseGeotags(bool enable) {
95   if (enable != useGeotags()) {
96     m_settings->setValue("camera/useGeotags", enable);
97
98     emit useGeotagsChanged();
99   }
100 }
101
102 bool Settings::isSoundEnabled() const {
103   return m_settings->value("camera/soundEnabled", DEFAULT_SOUND_ENABLED).toBool();
104 }
105
106 void Settings::setSoundEnabled(bool enabled) {
107   if (isSoundEnabled() != enabled) {
108     m_settings->setValue("camera/soundEnabled", enabled);
109     emit soundEnabledChanged();
110   }
111 }
112
113 bool Settings::isToolBarShown() const {
114   return m_settings->value("camera/showToolBar", DEFAULT_SHOW_TOOL_BAR).toBool();
115 }
116
117 void Settings::setToolBarShown(bool shown) {
118   if (isToolBarShown() != shown) {
119     m_settings->setValue("camera/showToolBar", shown);
120
121     emit toolBarShownChanged();
122   }
123 }
124
125 bool Settings::isVideoMuted() const {
126   return m_settings->value("video/mute", DEFAULT_VIDEO_MUTE).toBool();
127 }
128
129 void Settings::setVideoMuted(bool muted) {
130   if (isVideoMuted() != muted) {
131     m_settings->setValue("video/mute", muted);
132     emit videoMutedChanged();
133   }
134 }
135
136 bool Settings::isGridEnabled() const {
137   return m_settings->value("camera/gridEnabled", DEFAULT_GRID_ENABLED).toBool();
138 }
139
140 void Settings::setGridEnabled(bool enabled) {
141   if (enabled != isGridEnabled()) {
142     m_settings->setValue("camera/gridEnabled", enabled);
143     emit gridEnabledChanged();
144   }
145 }
146
147 bool Settings::isFaceDetectionEnabled() const {
148   return m_settings->value("image/faceDetectionEnabled", DEFAULT_FACE_DETECTION_ENABLED).toBool();
149 }
150
151 void Settings::setFaceDetectionEnabled(bool enabled) {
152   if (isFaceDetectionEnabled() != enabled) {
153     m_settings->setValue("image/faceDetectionEnabled", enabled);
154     emit faceDetectionEnabledChanged();
155   }
156 }
157
158 bool Settings::isZoomAsShutterEnabled() const {
159   return m_settings->value("camera/zoomAsShutter", DEFAULT_ZOOM_AS_SHUTTER).toBool();
160 }
161
162 void Settings::setZoomAsShutterEnabled(bool enabled) {
163   if (isZoomAsShutterEnabled() != enabled) {
164     m_settings->setValue("camera/zoomAsShutter", enabled);
165
166     emit zoomAsShutterChanged();
167   }
168 }
169
170 bool Settings::isProximityAsShutterEnabled() const {
171   return m_settings->value("camera/proximityAsShutter", DEFAULT_PROXIMITY_AS_SHUTTER).toBool();
172 }
173
174 void Settings::setProximityAsShutterEnabled(bool enabled) {
175   if (isProximityAsShutterEnabled() != enabled) {
176     m_settings->setValue("camera/proximityAsShutter", enabled);
177
178     emit proximityAsShutterChanged();
179   }
180 }
181
182 int Settings::device() const {
183   return m_settings->value("camera/device", DEFAULT_DEVICE).toInt();
184 }
185
186 void Settings::setDevice(int device) {
187   if (device != Settings::device()) {
188     emit deviceAboutToChange();
189
190     m_settings->setValue("camera/device", device);
191
192     delete m_device; m_device = 0;
193
194     if (mode() == DEFAULT_MODE) {
195       // image
196       emit imageSceneModeChanged();
197       emit imageColorFilterChanged();
198       emit imageWhiteBalanceChanged();
199       emit imageEvCompChanged();
200       emit imageFlashModeChanged();
201       emit imageIsoChanged();
202       emit imageAspectRatioChanged();
203       emit imageResolutionChanged();
204     }
205     else {
206       // video
207       emit videoSceneModeChanged();
208       emit videoColorFilterChanged();
209       emit videoWhiteBalanceChanged();
210       emit videoEvCompChanged();
211       emit videoAspectRatioChanged();
212       emit videoResolutionChanged();
213       emit videoTorchOnChanged();
214     }
215
216     emit deviceChanged();
217   }
218 }
219
220 DeviceSettings *Settings::deviceSettings() {
221   if (m_device) {
222     return m_device;
223   }
224
225   int device = Settings::device();
226
227   if (device == 0) {
228     m_device = new PrimaryDeviceSettings;
229   }
230   else {
231     m_device = new SecondaryDeviceSettings;
232   }
233
234   return m_device;
235 }
236
237 QVariant Settings::deviceValue(const char *key, const QVariant& defaultValue) {
238   QString k = QString("%1/%2").arg(deviceSettings()->id()).arg(key);
239
240   return m_settings->value(k, defaultValue);
241 }
242
243 void Settings::setDeviceValue(const char *key, const QVariant& value) {
244   QString k = QString("%1/%2").arg(deviceSettings()->id()).arg(key);
245
246   m_settings->setValue(k, value);
247 }
248
249 // Device dependant settings
250
251 int Settings::imageSceneMode() {
252   return deviceValue("image/sceneMode", deviceSettings()->defaultImageSceneMode()).toInt();
253 }
254
255 void Settings::setImageSceneMode(int mode) {
256   if (mode != imageSceneMode()) {
257     setDeviceValue("image/sceneMode", mode);
258   }
259
260   // We always emit the signal to reset scene and all scene associated values
261   emit imageSceneModeChanged();
262 }
263
264 int Settings::imageColorFilter() {
265   return deviceValue("image/colorFilter", deviceSettings()->defaultImageColorFilter()).toInt();
266 }
267
268 void Settings::setImageColorFilter(int filter) {
269   if (filter != imageColorFilter()) {
270     setDeviceValue("image/colorFilter", filter);
271
272     emit imageColorFilterChanged();
273   }
274 }
275
276 int Settings::imageWhiteBalance() {
277   return deviceValue("image/whiteBalance", deviceSettings()->defaultImageWhiteBalance()).toInt();
278 }
279
280 void Settings::setImageWhiteBalance(int wb) {
281   if (wb != imageWhiteBalance()) {
282     setDeviceValue("image/whiteBalance", wb);
283
284     emit imageWhiteBalanceChanged();
285   }
286 }
287
288 qreal Settings::imageEvComp() {
289   return deviceValue("image/evComp", deviceSettings()->defaultImageEvComp()).toReal();
290 }
291
292 void Settings::setImageEvComp(qreal ev) {
293   if (!qFuzzyCompare(ev, imageEvComp())) {
294     setDeviceValue("image/evComp", ev);
295
296     emit imageEvCompChanged();
297   }
298 }
299
300 int Settings::videoSceneMode() {
301   return deviceValue("video/sceneMode", deviceSettings()->defaultVideoSceneMode()).toInt();
302 }
303
304 void Settings::setVideoSceneMode(int mode) {
305   if (mode != videoSceneMode()) {
306     setDeviceValue("video/sceneMode", mode);
307   }
308
309   emit videoSceneModeChanged();
310 }
311
312 int Settings::videoColorFilter() {
313   return deviceValue("video/colorFilter", deviceSettings()->defaultVideoColorFilter()).toInt();
314 }
315
316 void Settings::setVideoColorFilter(int filter) {
317   if (filter != videoColorFilter()) {
318     setDeviceValue("video/colorFilter", filter);
319
320     emit videoColorFilterChanged();
321   }
322 }
323
324 int Settings::videoWhiteBalance() {
325   return deviceValue("video/whiteBalance", deviceSettings()->defaultVideoWhiteBalance()).toInt();
326 }
327
328 void Settings::setVideoWhiteBalance(int wb) {
329   if (wb != videoWhiteBalance()) {
330     setDeviceValue("video/whiteBalance", wb);
331
332     emit videoWhiteBalanceChanged();
333   }
334 }
335
336 qreal Settings::videoEvComp() {
337   return deviceValue("video/evComp", deviceSettings()->defaultVideoEvComp()).toReal();
338 }
339
340 void Settings::setVideoEvComp(qreal ev) {
341   if (!qFuzzyCompare(ev, videoEvComp())) {
342     setDeviceValue("video/evComp", ev);
343
344     emit videoEvCompChanged();
345   }
346 }
347
348 int Settings::imageFlashMode() {
349   return deviceValue("image/flashMode", deviceSettings()->defaultImageFlashMode()).toInt();
350 }
351
352 void Settings::setImageFlashMode(int mode) {
353   if (mode != imageFlashMode()) {
354     setDeviceValue("image/flashMode", mode);
355
356     emit imageFlashModeChanged();
357   }
358 }
359
360 int Settings::imageIso() {
361   return deviceValue("image/iso", deviceSettings()->defaultImageIso()).toInt();
362 }
363
364 void Settings::setImageIso(int iso) {
365   if (imageIso() != iso) {
366     setDeviceValue("image/iso", iso);
367     emit imageIsoChanged();
368   }
369 }
370
371 QString Settings::imageAspectRatio() {
372   return deviceValue("image/aspectRatio", deviceSettings()->defaultImageAspectRatio()).toString();
373 }
374
375 void Settings::setImageAspectRatio(const QString& aspectRatio) {
376   if (aspectRatio != imageAspectRatio()) {
377     setDeviceValue("image/aspectRatio", aspectRatio);
378     emit imageAspectRatioChanged();
379   }
380 }
381
382 QString Settings::imageResolution() {
383   return deviceValue("image/resolution", deviceSettings()->defaultImageResolution()).toString();
384 }
385
386 void Settings::setImageResolution(const QString& resolution) {
387   if (resolution != imageResolution()) {
388     setDeviceValue("image/resolution", resolution);
389     emit imageResolutionChanged();
390   }
391 }
392
393 QString Settings::videoAspectRatio() {
394   return deviceValue("video/aspectRatio", deviceSettings()->defaultVideoAspectRatio()).toString();
395 }
396
397
398 void Settings::setVideoAspectRatio(const QString& aspectRatio) {
399   if (Settings::videoAspectRatio() != aspectRatio) {
400     setDeviceValue("video/aspectRatio", aspectRatio);
401     emit videoAspectRatioChanged();
402   }
403 }
404
405 QString Settings::videoResolution() {
406   return deviceValue("video/resolution", deviceSettings()->defaultVideoResolution()).toString();
407 }
408
409 void Settings::setVideoResolution(const QString& resolution) {
410   if (resolution != videoResolution()) {
411     setDeviceValue("video/resolution", resolution);
412     emit videoResolutionChanged();
413   }
414 }
415
416 bool Settings::isVideoTorchOn() {
417   return deviceValue("video/torchOn", deviceSettings()->defaultVideoTorchOn()).toBool();
418 }
419
420 void Settings::setVideoTorchOn(bool on) {
421   if (isVideoTorchOn() != on) {
422     setDeviceValue("video/torchOn", on);
423     emit videoTorchOnChanged();
424   }
425 }
426
427 QString Settings::fileNamingStamp(const QString& id) const {
428   QString key = QString("fileNaming/%1").arg(id);
429   return m_settings->value(key).toString();
430 }
431
432 void Settings::setFileNamingStamp(const QString& id, const QString& stamp) {
433   QString key = QString("fileNaming/%1").arg(id);
434   m_settings->setValue(key, stamp);
435 }
436
437 int Settings::fileNamingCounter(const QString& id) const {
438   QString key = QString("fileNaming/%1").arg(id);
439   return m_settings->value(key).toInt();
440 }
441
442 void Settings::setFileNamingCounter(const QString& id, int counter) {
443   QString key = QString("fileNaming/%1").arg(id);
444   m_settings->setValue(key, counter);
445 }
446
447 bool Settings::isPreviewEnabled() const {
448   return m_settings->value("camera/enablePreview", DEFAULT_ENABLE_PREVIEW).toBool();
449 }
450
451 void Settings::setPreviewEnabled(bool enabled) {
452   if (enabled != isPreviewEnabled()) {
453     m_settings->setValue("camera/enablePreview", enabled);
454
455     emit previewEnabledChanged();
456   }
457 }
458
459 bool Settings::isUtcForFileNamingUsed() const {
460   return m_settings->value("fileNaming/useUtc", DEFAULT_FILE_NAMING_UTC).toBool();
461 }