Don't try to reacquire resources if we already have the correct ones
[harmattan/cameraplus] / qml / PipelineManager.qml
1 // -*- qml -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012-2013 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 currentItem
38     property bool error: false
39     property int currentPolicyMode: CameraResources.None
40
41     onCurrentItemChanged: {
42         if (state == "on" || state == "policyLost") {
43             startCamera()
44         }
45     }
46
47     Connections {
48         target: currentItem
49         onPolicyModeChanged: {
50             if (state == "on" || state == "policyLost") {
51                 startCamera()
52             }
53         }
54     }
55
56     CameraResources {
57         id: policy
58     }
59
60     function startCamera() {
61         if (error) {
62             return
63         } else if ((currentPolicyMode == currentItem.policyMode) && cam.running) {
64             return
65         } else if (!policy.acquire(currentItem.policyMode)) {
66             console.log("Failed to acquire policy resources")
67             return
68         } else if (!camera.start()) {
69             showError(qsTr("Failed to start camera. Please restart the application."))
70         } else {
71             currentPolicyMode = currentItem.policyMode
72         }
73     }
74
75     function stopCamera() {
76         if (camera.stop(false)) {
77             policy.acquire(CameraResources.None)
78             currentPolicyMode = CameraResources.None
79             error = false
80         }
81     }
82
83     function forceStopCamera() {
84         // We don't release resources here so we can get them back
85         // when they become available
86         currentItem.policyLost()
87         camera.stop(true)
88         currentPolicyMode = CameraResources.None
89         error = false
90     }
91
92     state: "off"
93
94 //    onStateChanged: console.log("New state " + handler.state);
95
96     states: [
97         State {
98             name: "on"
99             when: Qt.application.active && currentItem && currentItem.policyMode != CameraResources.None && !policy.hijacked
100         },
101         State {
102             name: "off"
103             when: (!Qt.application.active && camera.idle) || (currentItem && currentItem.policyMode == CameraResources.None && camera.idle)
104         },
105         State {
106             name: "policyLost"
107             when: policy.hijacked
108         },
109         State {
110             name: "error"
111         }
112     ]
113
114     transitions: [
115         Transition {
116             to: "off"
117             ScriptAction {
118                 script: stopCamera()
119             }
120         },
121         Transition {
122             from: "off"
123             to: "on"
124             ScriptAction {
125                 script: handler.startCamera()
126             }
127         },
128         Transition {
129             from: "on"
130             to: "policyLost"
131             ScriptAction {
132                 script: forceStopCamera()
133             }
134         },
135         Transition {
136             from: "policyLost"
137             to: "off"
138             ScriptAction {
139                 script: stopCamera()
140             }
141         },
142         Transition {
143             from: "policyLost"
144             to: "on"
145             ScriptAction {
146                 script: startCamera()
147             }
148         }
149     ]
150 }