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