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