Make that transition a catch all for all transitions to off.
[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
35         property Camera camera: null
36         property Item currentPage: pageStack.currentPage
37
38         onCurrentPageChanged: {
39                 if (state == "on" || state == "policyLost") {
40                         startCamera();
41                 }
42         }
43
44         CameraResources {
45                 id: policy
46         }
47
48         function startCamera() {
49                 if (!policy.acquire(currentPage.policyMode)) {
50                         console.log("Failed to acquire policy resources");
51                         return;
52                 }
53
54                 if (!camera.start()) {
55                         showError(qsTr("Failed to start camera. Please restart the application."));
56                 }
57         }
58
59         function stopCamera() {
60                 if (camera.stop(false)) {
61                         policy.acquire(CameraResources.None);
62                 }
63         }
64
65         function forceStopCamera() {
66                 // We don't release resources here so we can get them back
67                 // when they become available
68                 camera.stop(true);
69         }
70
71         state: "off"
72
73 //        onStateChanged: console.log("New state " + handler.state);
74
75         states: [
76         State {
77                 name: "on"
78                 when: Qt.application.active && currentPage && currentPage.needsPipeline && !policy.hijacked
79         },
80         State {
81                 name: "off"
82                 when: (!Qt.application.active && camera.idle) || (currentPage && !currentPage.needsPipeline)
83         },
84         State {
85                 name: "policyLost"
86                 when: policy.hijacked
87         },
88         State {
89                 name: "error"
90         }
91         ]
92
93         transitions: [
94         Transition {
95                 to: "off"
96                 ScriptAction {
97                         script: stopCamera();
98                 }
99         },
100         Transition {
101                 from: "off"
102                 to: "on"
103                 ScriptAction {
104                         script: handler.startCamera();
105                 }
106         },
107
108         Transition {
109                 from: "on"
110                 to: "policyLost"
111                 ScriptAction {
112                         script: forceStopCamera();
113                 }
114         },
115
116         Transition {
117                 from: "policyLost"
118                 to: "off"
119                 ScriptAction {
120                         script: stopCamera();
121                 }
122         },
123         Transition {
124                 from: "policyLost"
125                 to: "on"
126                 ScriptAction {
127                         script: startCamera();
128                 }
129         }
130         ]
131 }