First attempt at making Settings class device aware
[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     m_settings->setValue("camera/device", device);
174
175     delete m_device; m_device = 0;
176     emit deviceChanged();
177
178     if (mode() == DEFAULT_MODE) {
179       // image
180       emit imageSceneModeChanged();
181       emit imageColorFilterChanged();
182       emit imageWhiteBalanceChanged();
183       emit imageEvCompChanged();
184       emit imageFlashModeChanged();
185       emit imageIsoChanged();
186       emit imageAspectRatioChanged();
187       emit imageResolutionChanged();
188     }
189     else {
190       // video
191       emit videoSceneModeChanged();
192       emit videoColorFilterChanged();
193       emit videoWhiteBalanceChanged();
194       emit videoEvCompChanged();
195       emit videoAspectRatioChanged();
196       emit videoResolutionChanged();
197       emit videoTorchOnChanged();
198     }
199   }
200 }
201
202 DeviceSettings *Settings::deviceSettings() {
203   if (m_device) {
204     return m_device;
205   }
206
207   int device = Settings::device();
208
209   if (device == 0) {
210     m_device = new PrimaryDeviceSettings;
211   }
212   else {
213     m_device = new SecondaryDeviceSettings;
214   }
215
216   return m_device;
217 }
218
219 QVariant Settings::deviceValue(const char *key, const QVariant& defaultValue) {
220   QString k = QString("%1/%2").arg(deviceSettings()->id()).arg(key);
221
222   return m_settings->value(k, defaultValue);
223 }
224
225 void Settings::setDeviceValue(const char *key, const QVariant& value) {
226   QString k = QString("%1/%2").arg(deviceSettings()->id()).arg(key);
227
228   m_settings->setValue(k, value);
229 }
230
231 // Device dependant settings
232
233 int Settings::imageSceneMode() {
234   return deviceValue("image/sceneMode", deviceSettings()->defaultImageSceneMode()).toInt();
235 }
236
237 void Settings::setImageSceneMode(int mode) {
238   if (mode != imageSceneMode()) {
239     setDeviceValue("image/sceneMode", mode);
240   }
241
242   // We always emit the signal to reset scene and all scene associated values
243   emit imageSceneModeChanged();
244 }
245
246 int Settings::imageColorFilter() {
247   return deviceValue("image/colorFilter", deviceSettings()->defaultImageColorFilter()).toInt();
248 }
249
250 void Settings::setImageColorFilter(int filter) {
251   if (filter != imageColorFilter()) {
252     setDeviceValue("image/colorFilter", filter);
253
254     emit imageColorFilterChanged();
255   }
256 }
257
258 int Settings::imageWhiteBalance() {
259   return deviceValue("image/whiteBalance", deviceSettings()->defaultImageWhiteBalance()).toInt();
260 }
261
262 void Settings::setImageWhiteBalance(int wb) {
263   if (wb != imageWhiteBalance()) {
264     setDeviceValue("image/whiteBalance", wb);
265
266     emit imageWhiteBalanceChanged();
267   }
268 }
269
270 qreal Settings::imageEvComp() {
271   return deviceValue("image/evComp", deviceSettings()->defaultImageEvComp()).toReal();
272 }
273
274 void Settings::setImageEvComp(qreal ev) {
275   if (!qFuzzyCompare(ev, imageEvComp())) {
276     setDeviceValue("image/evComp", ev);
277
278     emit imageEvCompChanged();
279   }
280 }
281
282 int Settings::videoSceneMode() {
283   return deviceValue("video/sceneMode", deviceSettings()->defaultVideoSceneMode()).toInt();
284 }
285
286 void Settings::setVideoSceneMode(int mode) {
287   if (mode != videoSceneMode()) {
288     setDeviceValue("video/sceneMode", mode);
289   }
290
291   emit videoSceneModeChanged();
292 }
293
294 int Settings::videoColorFilter() {
295   return deviceValue("video/colorFilter", deviceSettings()->defaultVideoColorFilter()).toInt();
296 }
297
298 void Settings::setVideoColorFilter(int filter) {
299   if (filter != videoColorFilter()) {
300     setDeviceValue("video/colorFilter", filter);
301
302     emit videoColorFilterChanged();
303   }
304 }
305
306 int Settings::videoWhiteBalance() {
307   return deviceValue("video/whiteBalance", deviceSettings()->defaultVideoWhiteBalance()).toInt();
308 }
309
310 void Settings::setVideoWhiteBalance(int wb) {
311   if (wb != videoWhiteBalance()) {
312     setDeviceValue("video/whiteBalance", wb);
313
314     emit videoWhiteBalanceChanged();
315   }
316 }
317
318 qreal Settings::videoEvComp() {
319   return deviceValue("video/evComp", deviceSettings()->defaultVideoEvComp()).toReal();
320 }
321
322 void Settings::setVideoEvComp(qreal ev) {
323   if (!qFuzzyCompare(ev, videoEvComp())) {
324     setDeviceValue("video/evComp", ev);
325
326     emit videoEvCompChanged();
327   }
328 }
329
330 int Settings::imageFlashMode() {
331   return deviceValue("image/flashMode", deviceSettings()->defaultImageFlashMode()).toInt();
332 }
333
334 void Settings::setImageFlashMode(int mode) {
335   if (mode != imageFlashMode()) {
336     setDeviceValue("image/flashMode", mode);
337
338     emit imageFlashModeChanged();
339   }
340 }
341
342 int Settings::imageIso() {
343   return deviceValue("image/iso", deviceSettings()->defaultImageIso()).toInt();
344 }
345
346 void Settings::setImageIso(int iso) {
347   if (imageIso() != iso) {
348     setDeviceValue("image/iso", iso);
349     emit imageIsoChanged();
350   }
351 }
352
353 QString Settings::imageAspectRatio() {
354   return deviceValue("image/aspectRatio", deviceSettings()->defaultImageAspectRatio()).toString();
355 }
356
357 void Settings::setImageAspectRatio(const QString& aspectRatio) {
358   if (aspectRatio != imageAspectRatio()) {
359     setDeviceValue("image/aspectRatio", aspectRatio);
360     emit imageAspectRatioChanged();
361   }
362 }
363
364 QString Settings::imageResolution() {
365   return deviceValue("image/resolution", deviceSettings()->defaultImageResolution()).toString();
366 }
367
368 void Settings::setImageResolution(const QString& resolution) {
369   if (resolution != imageResolution()) {
370     setDeviceValue("image/resolution", resolution);
371     emit imageResolutionChanged();
372   }
373 }
374
375 QString Settings::videoAspectRatio() {
376   return deviceValue("video/aspectRatio", deviceSettings()->defaultVideoAspectRatio()).toString();
377 }
378
379
380 void Settings::setVideoAspectRatio(const QString& aspectRatio) {
381   if (Settings::videoAspectRatio() != aspectRatio) {
382     setDeviceValue("video/aspectRatio", aspectRatio);
383     emit videoAspectRatioChanged();
384   }
385 }
386
387 QString Settings::videoResolution() {
388   return deviceValue("video/resolution", deviceSettings()->defaultVideoResolution()).toString();
389 }
390
391 void Settings::setVideoResolution(const QString& resolution) {
392   if (resolution != videoResolution()) {
393     setDeviceValue("video/resolution", resolution);
394     emit videoResolutionChanged();
395   }
396 }
397
398 bool Settings::isVideoTorchOn() {
399   return deviceValue("video/torchOn", deviceSettings()->defaultVideoTorchOn()).toBool();
400 }
401
402 void Settings::setVideoTorchOn(bool on) {
403   if (isVideoTorchOn() != on) {
404     setDeviceValue("video/torchOn", on);
405     emit videoTorchOnChanged();
406   }
407 }