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