Added an option to disable (mute) camera sound
[harmattan/cameraplus] / src / cameraresources.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 "cameraresources.h"
22 #include <QDebug>
23
24 using namespace ResourcePolicy;
25
26 CameraResources::CameraResources(QObject *parent) :
27   QObject(parent),
28   m_set(new ResourceSet("camera", this, true, true)),
29   m_mode(None), m_acquired(false) {
30
31   QObject::connect(m_set, SIGNAL(resourcesReleased()), this, SLOT(resourcesReleased()));
32   QObject::connect(m_set, SIGNAL(lostResources()), this, SLOT(lostResources()));
33   QObject::connect(m_set, SIGNAL(resourcesGranted(const QList<ResourcePolicy::ResourceType>&)),
34                    this, SLOT(resourcesGranted(const QList<ResourcePolicy::ResourceType>&)));
35   QObject::connect(m_set, SIGNAL(updateOK()), this, SLOT(updateOK()));
36
37   if (!m_set->initAndConnect()) {
38     qCritical() << "Failed to connect to resource policy engine";
39   }
40 }
41
42 CameraResources::~CameraResources() {
43   acquire(None);
44 }
45
46 void CameraResources::acquire(const CameraResources::Mode& mode) {
47   if (mode == m_mode) {
48     // We need to emit this because the UI migh be waiting
49     emit acquiredChanged();
50     return;
51   }
52
53   m_mode = mode;
54
55   switch (m_mode) {
56   case None:
57     m_set->release();
58     break;
59
60   case Image:
61     updateSet(QList<ResourcePolicy::ResourceType>()
62               << ResourcePolicy::VideoPlaybackType
63               << ResourcePolicy::VideoRecorderType
64               << ResourcePolicy::ScaleButtonType
65               << ResourcePolicy::SnapButtonType);
66     break;
67
68   case Video:
69     updateSet(QList<ResourcePolicy::ResourceType>()
70               << ResourcePolicy::VideoPlaybackType
71               << ResourcePolicy::VideoRecorderType
72               << ResourcePolicy::ScaleButtonType
73               << ResourcePolicy::SnapButtonType);
74     break;
75
76   case Recording:
77     updateSet(QList<ResourcePolicy::ResourceType>()
78               << ResourcePolicy::VideoPlaybackType
79               << ResourcePolicy::VideoRecorderType
80               << ResourcePolicy::ScaleButtonType
81               << ResourcePolicy::SnapButtonType
82               << ResourcePolicy::AudioRecorderType,
83               QList<ResourcePolicy::ResourceType>()
84               << ResourcePolicy::AudioPlaybackType);
85     break;
86
87   case PostCapture:
88     updateSet(QList<ResourcePolicy::ResourceType>()
89               << ResourcePolicy::VideoPlaybackType
90               << ResourcePolicy::ScaleButtonType,
91               QList<ResourcePolicy::ResourceType>()
92               << ResourcePolicy::AudioPlaybackType);
93
94     break;
95
96   default:
97     qWarning() << "Unknown mode" << mode;
98
99     break;
100   }
101 }
102
103 bool CameraResources::acquired() const {
104   return m_acquired;
105 }
106
107 void CameraResources::resourcesReleased() {
108   m_mode = None;
109   m_acquired = false;
110   emit acquiredChanged();
111 }
112
113 void CameraResources::lostResources() {
114   m_mode = None;
115   m_acquired = false;
116   emit acquiredChanged();
117 }
118
119 void CameraResources::resourcesGranted(const QList<ResourcePolicy::ResourceType>& optional) {
120   Q_UNUSED(optional);
121
122   m_acquired = true;
123   emit acquiredChanged();
124 }
125
126 void CameraResources::updateOK() {
127   m_acquired = true;
128   emit acquiredChanged();
129 }
130
131 QList<ResourcePolicy::ResourceType> CameraResources::listSet() {
132   QList<Resource *> resources = m_set->resources();
133   QList<ResourcePolicy::ResourceType> set;
134
135   foreach (Resource *r, resources) {
136     set << r->type();
137   }
138
139   return set;
140 }
141
142 void CameraResources::updateSet(const QList<ResourcePolicy::ResourceType>& required,
143                                 const QList<ResourcePolicy::ResourceType>& optional) {
144
145   Q_UNUSED(optional);
146
147   bool isEmpty = m_set->resources().isEmpty();
148
149   QList<ResourcePolicy::ResourceType> set = listSet();
150
151   foreach (ResourceType r, set) {
152     // Check for acquired resources that should be dropped.
153     if (required.indexOf(r) == -1) {
154       m_set->deleteResource(r);
155     }
156   }
157
158   foreach (ResourceType r, required) {
159     // TODO: AudioPlayback needs special handling
160     m_set->addResource(r);
161   }
162
163   // TODO: optional resources
164
165   // Odd. If we don't do it that way then policy ignores our requests
166   // when we get minimized then maximized.
167   if (isEmpty) {
168     m_set->update();
169   }
170   else {
171     m_set->acquire();
172   }
173 }