4a599d2e09adcbe094aa358d397d51bb24e7676f
[harmattan/cameraplus] / qml / PipelineManager.qml
1 // -*- qml -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 import QtQuick 1.1
24 import QtCamera 1.0
25 import CameraPlus 1.0
26
27 Item {
28         id: handler
29
30         property bool showStandBy: state != "on"
31
32         property alias acquired: policy.acquired
33         property alias hijacked: policy.hijacked
34         property alias scaleAcquired: policy.scaleAcquired
35
36         property Camera camera: null
37         property Item currentPage: pageStack.currentPage
38         property bool error: false
39
40         onCurrentPageChanged: {
41                 if (state == "on" || state == "policyLost") {
42                         startCamera();
43                 }
44         }
45
46         CameraResources {
47                 id: policy
48         }
49
50         function startCamera() {
51                 if (error) {
52                         return;
53                 }
54
55                 if (!policy.acquire(currentPage.policyMode)) {
56                         console.log("Failed to acquire policy resources");
57                         return;
58                 }
59
60                 if (!camera.start()) {
61                         showError(qsTr("Failed to start camera. Please restart the application."));
62                 }
63         }
64
65         function stopCamera() {
66                 if (camera.stop(false)) {
67                         policy.acquire(CameraResources.None);
68                         error = false;
69                 }
70         }
71
72         function forceStopCamera() {
73                 // We don't release resources here so we can get them back
74                 // when they become available
75                 pageStack.currentPage.policyLost();
76                 camera.stop(true);
77                 error = false;
78         }
79
80         state: "off"
81
82 //        onStateChanged: console.log("New state " + handler.state);
83
84         states: [
85         State {
86                 name: "on"
87                 when: Qt.application.active && currentPage && currentPage.policyMode != CameraResources.None && !policy.hijacked
88         },
89         State {
90                 name: "off"
91                 when: (!Qt.application.active && camera.idle) || (currentPage && currentPage.policyMode == CameraResources.None && camera.idle)
92         },
93         State {
94                 name: "policyLost"
95                 when: policy.hijacked
96         },
97         State {
98                 name: "error"
99         }
100         ]
101
102         transitions: [
103         Transition {
104                 to: "off"
105                 ScriptAction {
106                         script: stopCamera();
107                 }
108         },
109         Transition {
110                 from: "off"
111                 to: "on"
112                 ScriptAction {
113                         script: handler.startCamera();
114                 }
115         },
116
117         Transition {
118                 from: "on"
119                 to: "policyLost"
120                 ScriptAction {
121                         script: forceStopCamera();
122                 }
123         },
124
125         Transition {
126                 from: "policyLost"
127                 to: "off"
128                 ScriptAction {
129                         script: stopCamera();
130                 }
131         },
132         Transition {
133                 from: "policyLost"
134                 to: "on"
135                 ScriptAction {
136                         script: startCamera();
137                 }
138         }
139         ]
140 }