silence dbus-send output
[harmattan/cameraplus] / declarative / camera.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 "camera.h"
22 #include "qtcamera.h"
23 #include "qtcamdevice.h"
24 #include "qtcammode.h"
25 #include "qtcamimagemode.h"
26 #include "qtcamvideomode.h"
27 #include "qtcamconfig.h"
28 #include "sounds.h"
29 #include "notificationscontainer.h"
30 #include "sounds.h"
31 #if defined(QT4)
32 #include <QDeclarativeInfo>
33 #elif defined(QT5)
34 #include <QQmlInfo>
35 #endif
36 #include "zoom.h"
37 #include "flash.h"
38 #include "scene.h"
39 #include "evcomp.h"
40 #include "whitebalance.h"
41 #include "colortone.h"
42 #include "iso.h"
43 #include "exposure.h"
44 #include "aperture.h"
45 #include "noisereduction.h"
46 #include "flickerreduction.h"
47 #include "focus.h"
48 #include "autofocus.h"
49 #include "roi.h"
50
51 #include "videomute.h"
52 #include "videotorch.h"
53 #include "cameraconfig.h"
54 #include "quirks.h"
55
56 Camera::Camera(QObject *parent) :
57   QObject(parent),
58   m_cam(new QtCamera(this)),
59   m_dev(0),
60   m_mode(Camera::UnknownMode),
61   m_notifications(new NotificationsContainer(this)),
62   m_zoom(0),
63   m_flash(0),
64   m_scene(0),
65   m_evComp(0),
66   m_whiteBalance(0),
67   m_colorTone(0),
68   m_iso(0),
69   m_exposure(0),
70   m_aperture(0),
71   m_noiseReduction(0),
72   m_flickerReduction(0),
73   m_focus(0),
74   m_autoFocus(0),
75   m_roi(0),
76   m_videoMute(0),
77   m_videoTorch(0),
78   m_config(new CameraConfig(this)),
79   m_quirks(0) {
80
81   m_config->componentComplete();
82 }
83
84 Camera::~Camera() {
85   if (m_dev) {
86     if (m_dev->activeMode()) {
87       m_dev->activeMode()->deactivate();
88     }
89
90     m_dev->stop(true);
91     m_dev->deleteLater();
92     m_dev = 0;
93   }
94
95   delete m_zoom;
96   delete m_flash;
97   delete m_scene;
98   delete m_evComp;
99   delete m_whiteBalance;
100   delete m_colorTone;
101   delete m_iso;
102   delete m_exposure;
103   delete m_aperture;
104   delete m_noiseReduction;
105   delete m_flickerReduction;
106   delete m_focus;
107   delete m_autoFocus;
108   delete m_roi;
109   delete m_videoMute;
110   delete m_videoTorch;
111   delete m_quirks;
112 }
113
114 int Camera::deviceCount() const {
115   return m_cam ? m_cam->devices().size() : 0;
116 }
117
118 QString Camera::deviceName(int index) const {
119   return m_cam->devices().at(index).first;
120 }
121
122 QVariant Camera::deviceId(int index) const {
123   return m_cam->devices().at(index).second;
124 }
125
126 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
127   if (mode == Camera::UnknownMode) {
128     qmlInfo(this) << "Cannot set mode to unknown";
129     return false;
130   }
131
132   QVariant oldId = m_id;
133   Camera::CameraMode oldMode = m_mode;
134
135   if (setDeviceId(deviceId) && setMode(mode)) {
136     if (oldId != m_id) {
137       emit deviceIdChanged();
138       emit deviceChanged();
139
140       resetCapabilities();
141     }
142
143     if (oldMode != m_mode) {
144       emit modeChanged();
145     }
146
147     return true;
148   }
149
150   return false;
151 }
152
153 bool Camera::setDeviceId(const QVariant& deviceId) {
154   if (deviceId == m_id) {
155     return true;
156   }
157
158   if (m_dev && m_dev->stop(false)) {
159     emit prepareForDeviceChange();
160     delete m_dev;
161   }
162   else if (m_dev) {
163     qmlInfo(this) << "Failed to stop device";
164     return false;
165   }
166
167   m_dev = m_cam->device(deviceId, this);
168
169   m_id = deviceId;
170
171   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
172                       this, SIGNAL(runningStateChanged()));
173   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
174   QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
175                    this, SIGNAL(error(const QString&, int, const QString&)));
176
177   m_notifications->setDevice(m_dev);
178
179   return true;
180 }
181
182 QVariant Camera::deviceId() const {
183   return m_id;
184 }
185
186 QtCamDevice *Camera::device() const {
187   return m_dev;
188 }
189
190 bool Camera::setMode(const Camera::CameraMode& mode) {
191   if (m_mode == mode) {
192     return true;
193   }
194
195   if (!m_dev) {
196     return false;
197   }
198
199   m_mode = mode;
200
201   if (m_dev->isRunning()) {
202     applyMode();
203   }
204
205   return true;
206 }
207
208 Camera::CameraMode Camera::mode() {
209   return m_mode;
210 }
211
212 bool Camera::start() {
213   if (!m_dev) {
214     return false;
215   }
216
217   if (!applyMode()) {
218     return false;
219   }
220
221   return m_dev->start();
222 }
223
224 bool Camera::stop(bool force) {
225   if (m_dev) {
226     return m_dev->stop(force);
227   }
228
229   return true;
230 }
231
232 bool Camera::isIdle() {
233   return m_dev ? m_dev->isIdle() : true;
234 }
235
236 bool Camera::isRunning() {
237   return m_dev ? m_dev->isRunning() : false;
238 }
239
240 bool Camera::applyMode() {
241   if (m_mode == Camera::UnknownMode) {
242     return false;
243   }
244
245   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
246     m_dev->videoMode()->activate();
247   }
248   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
249     m_dev->imageMode()->activate();
250   }
251
252   return true;
253 }
254
255 QString Camera::imageSuffix() const {
256   return m_cam->config()->imageSuffix();
257 }
258
259 QString Camera::videoSuffix() const {
260   return m_cam->config()->videoSuffix();
261 }
262
263 Sounds *Camera::sounds() const {
264   return m_notifications->sounds();
265 }
266 void Camera::setSounds(Sounds *sounds) {
267   if (m_notifications->setSounds(sounds)) {
268     sounds->reload();
269     emit soundsChanged();
270   }
271 }
272
273 void Camera::resetCapabilities() {
274   QtCamDevice *dev = device();
275
276   delete m_zoom;
277   m_zoom = new Zoom(dev, this);
278   emit zoomChanged();
279
280   delete m_flash;
281   m_flash = new Flash(dev, this);
282   emit flashChanged();
283
284   delete m_scene;
285   m_scene = new Scene(dev, this);
286   emit sceneChanged();
287
288   delete m_evComp;
289   m_evComp = new EvComp(dev, this);
290   emit evCompChanged();
291
292   delete m_whiteBalance;
293   m_whiteBalance = new WhiteBalance(dev, this);
294   emit whiteBalanceChanged();
295
296   delete m_colorTone;
297   m_colorTone = new ColorTone(dev, this);
298   emit colorToneChanged();
299
300   delete m_iso;
301   m_iso = new Iso(dev, this);
302   emit isoChanged();
303
304   delete m_exposure;
305   m_exposure = new Exposure(dev, this);
306   emit exposureChanged();
307
308   delete m_aperture;
309   m_aperture = new Aperture(dev, this);
310   emit apertureChanged();
311
312   delete m_noiseReduction;
313   m_noiseReduction = new NoiseReduction(dev, this);
314   emit noiseReductionChanged();
315
316   delete m_flickerReduction;
317   m_flickerReduction = new FlickerReduction(dev, this);
318   emit flickerReductionChanged();
319
320   delete m_focus;
321   m_focus = new Focus(dev, this);
322   emit focusChanged();
323
324   delete m_autoFocus;
325   m_autoFocus = new AutoFocus(dev, this);
326   emit autoFocusChanged();
327
328   delete m_roi;
329   m_roi = new Roi(dev, this);
330   emit roiChanged();
331
332   delete m_videoMute;
333   m_videoMute = new VideoMute(dev, this);
334   emit videoMuteChanged();
335
336   delete m_videoTorch;
337   m_videoTorch = new VideoTorch(dev, this);
338   emit videoTorchChanged();
339
340   delete m_quirks;
341   m_quirks = new Quirks(dev->config()->quirks(dev->id()), this);
342   emit quirksChanged();
343 }
344
345 Zoom *Camera::zoom() const {
346   return m_zoom;
347 }
348
349 Flash *Camera::flash() const {
350   return m_flash;
351 }
352
353 Scene *Camera::scene() const {
354   return m_scene;
355 }
356
357 EvComp *Camera::evComp() const {
358   return m_evComp;
359 }
360
361 WhiteBalance *Camera::whiteBalance() const {
362   return m_whiteBalance;
363 }
364
365 ColorTone *Camera::colorTone() const {
366   return m_colorTone;
367 }
368
369 Iso *Camera::iso() const {
370   return m_iso;
371 }
372
373 Exposure *Camera::exposure() const {
374   return m_exposure;
375 }
376
377 Aperture *Camera::aperture() const {
378   return m_aperture;
379 }
380
381 NoiseReduction *Camera::noiseReduction() const {
382   return m_noiseReduction;
383 }
384
385 FlickerReduction *Camera::flickerReduction() const {
386   return m_flickerReduction;
387 }
388
389 Focus *Camera::focus() const {
390   return m_focus;
391 }
392
393 AutoFocus *Camera::autoFocus() const {
394   return m_autoFocus;
395 }
396
397 Roi *Camera::roi() const {
398   return m_roi;
399 }
400
401 VideoMute *Camera::videoMute() const {
402   return m_videoMute;
403 }
404
405 VideoTorch *Camera::videoTorch() const {
406   return m_videoTorch;
407 }
408
409 CameraConfig *Camera::cameraConfig() const {
410   return m_config;
411 }
412
413 Quirks *Camera::quirks() const {
414   return m_quirks;
415 }