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