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