Added a sounds component that implements the Notifications interface and is used to
[harmattan/cameraplus] / imports / camera.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 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 "qtcamgraphicsviewfinder.h"
28 #include "qtcamconfig.h"
29 #include "notifications.h"
30 #include "notificationscontainer.h"
31 #include "sounds.h"
32
33 // TODO: a viewfinder class that inherits QDeclarativeItem
34
35 Camera::Camera(QDeclarativeItem *parent) :
36   QDeclarativeItem(parent),
37   m_cam(new QtCamera(this)),
38   m_dev(0),
39   m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
40   m_mode(Camera::ImageMode),
41   m_notifications(new NotificationsContainer(this)) {
42
43   // TODO:
44 }
45
46 Camera::~Camera() {
47   // TODO:
48 }
49
50 void Camera::componentComplete() {
51   QDeclarativeItem::componentComplete();
52
53   if (m_id.isValid()) {
54     QVariant oldId = m_id;
55     m_id = QVariant();
56     setDeviceId(oldId);
57   }
58
59   emit deviceCountChanged();
60 }
61
62 int Camera::deviceCount() const {
63   return m_cam ? m_cam->devices().size() : 0;
64 }
65
66 QString Camera::deviceName(int index) const {
67   return m_cam->devices().at(index).first;
68 }
69
70 QVariant Camera::deviceId(int index) const {
71   return m_cam->devices().at(index).second;
72 }
73
74 void Camera::setDeviceId(const QVariant& id) {
75   if (id == m_id) {
76     return;
77   }
78
79   if (!isComponentComplete()) {
80     m_id = id;
81     emit deviceIdChanged();
82     return;
83   }
84
85   if (m_dev && m_dev->stop()) {
86     delete m_dev;
87   }
88   else if (m_dev) {
89     qWarning() << "Failed to stop device";
90     return;
91   }
92
93   m_dev = m_cam->device(id, this);
94
95   m_id = id;
96
97   m_vf->setDevice(m_dev);
98
99   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
100                       this, SIGNAL(runningStateChanged()));
101   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
102   QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
103                    this, SIGNAL(error(const QString&, int, const QString&)));
104
105   m_notifications->setDevice(m_dev);
106
107   emit deviceIdChanged();
108   emit deviceChanged();
109 }
110
111 QVariant Camera::deviceId() const {
112   return m_id;
113 }
114
115 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
116   QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
117
118   m_vf->setGeometry(newGeometry);
119 }
120
121 QtCamDevice *Camera::device() const {
122   return m_dev;
123 }
124
125 void Camera::setMode(const Camera::CameraMode& mode) {
126   if (m_mode == mode) {
127     return;
128   }
129
130   m_mode = mode;
131
132   if (!m_dev) {
133     return;
134   }
135
136   if (m_dev->isRunning()) {
137     applyMode();
138   }
139
140   emit modeChanged();
141 }
142
143 Camera::CameraMode Camera::mode() {
144   return m_mode;
145 }
146
147 bool Camera::start() {
148   if (!m_dev) {
149     return false;
150   }
151
152   applyMode();
153
154   return m_dev->start();
155 }
156
157 bool Camera::stop() {
158   if (m_dev) {
159     return m_dev->stop();
160   }
161
162   return true;
163 }
164
165 bool Camera::isIdle() {
166   return m_dev ? m_dev->isIdle() : true;
167 }
168
169 bool Camera::isRunning() {
170   return m_dev ? m_dev->isRunning() : false;
171 }
172
173 void Camera::applyMode() {
174   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
175     m_dev->videoMode()->activate();
176   }
177   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
178     m_dev->imageMode()->activate();
179   }
180 }
181
182 QString Camera::imageSuffix() const {
183   return m_cam->config()->imageSuffix();
184 }
185
186 QString Camera::videoSuffix() const {
187   return m_cam->config()->videoSuffix();
188 }
189
190 Notifications *Camera::notifications() const {
191   return m_notifications->notifications();
192 }
193
194 void Camera::setNotifications(Notifications *notifications) {
195   if (m_notifications->setNotifications(notifications)) {
196
197     if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
198       s->setConfig(m_cam->config());
199       s->reload();
200     }
201
202     emit notificationsChanged();
203   }
204 }