2 * This file is part of CameraPlus.
4 * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
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.
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.
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
21 #include "cameraresources.h"
24 using namespace ResourcePolicy;
26 CameraResources::CameraResources(QObject *parent) :
28 m_set(new ResourceSet("camera", this, true, true)),
29 m_mode(None), m_acquired(false) {
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()));
37 if (!m_set->initAndConnect()) {
38 qCritical() << "Failed to connect to resource policy engine";
42 CameraResources::~CameraResources() {
46 void CameraResources::acquire(const CameraResources::Mode& mode) {
48 // We need to emit this because the UI migh be waiting
49 emit acquiredChanged();
61 updateSet(QList<ResourcePolicy::ResourceType>()
62 << ResourcePolicy::VideoPlaybackType
63 << ResourcePolicy::VideoRecorderType
64 << ResourcePolicy::ScaleButtonType
65 << ResourcePolicy::SnapButtonType);
69 updateSet(QList<ResourcePolicy::ResourceType>()
70 << ResourcePolicy::VideoPlaybackType
71 << ResourcePolicy::VideoRecorderType
72 << ResourcePolicy::ScaleButtonType
73 << ResourcePolicy::SnapButtonType);
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);
88 updateSet(QList<ResourcePolicy::ResourceType>()
89 << ResourcePolicy::VideoPlaybackType
90 << ResourcePolicy::ScaleButtonType,
91 QList<ResourcePolicy::ResourceType>()
92 << ResourcePolicy::AudioPlaybackType);
97 qWarning() << "Unknown mode" << mode;
103 bool CameraResources::acquired() const {
107 void CameraResources::resourcesReleased() {
110 emit acquiredChanged();
113 void CameraResources::lostResources() {
116 emit acquiredChanged();
119 void CameraResources::resourcesGranted(const QList<ResourcePolicy::ResourceType>& optional) {
123 emit acquiredChanged();
126 void CameraResources::updateOK() {
128 emit acquiredChanged();
131 QList<ResourcePolicy::ResourceType> CameraResources::listSet() {
132 QList<Resource *> resources = m_set->resources();
133 QList<ResourcePolicy::ResourceType> set;
135 foreach (Resource *r, resources) {
142 void CameraResources::updateSet(const QList<ResourcePolicy::ResourceType>& required,
143 const QList<ResourcePolicy::ResourceType>& optional) {
147 bool isEmpty = m_set->resources().isEmpty();
149 QList<ResourcePolicy::ResourceType> set = listSet();
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);
158 foreach (ResourceType r, required) {
159 // TODO: AudioPlayback needs special handling
160 m_set->addResource(r);
163 // TODO: optional resources
165 // Odd. If we don't do it that way then policy ignores our requests
166 // when we get minimized then maximized.