Added TrackerStore to be used for creating a virtual file before image/video gets...
[harmattan/cameraplus] / qml / RecordingPage.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 com.nokia.meego 1.1
25 import QtCamera 1.0
26 import CameraPlus 1.0
27 import "data.js" as Data
28
29 // TODO: on error ?
30 // TODO: resources lost?
31 // TODO: closing camera in the middle of recording will hang camera
32 CameraPage {
33         id: page
34         modesVisible: false
35
36         Component.onCompleted: startRecording();
37         Component.onDestruction: videoMode.stopRecording();
38
39         function startRecording() {
40                 if (!resourcePolicy.acquired || resourcePolicy.hijacked) {
41                         pageStack.pop(undefined, true);
42                         return;
43                 }
44
45                 metaData.setMetaData();
46
47                 if (!mountProtector.lock()) {
48                         showError(qsTr("Failed to lock images directory."));
49                         pageStack.pop(undefined, true);
50                         return;
51                 }
52
53                 var file = fileNaming.videoFileName();
54                 var tmpFile = fileNaming.temporaryVideoFileName();
55
56                 if (!videoMode.startRecording(file, tmpFile)) {
57                         showError(qsTr("Failed to record video. Please restart the camera."));
58                         pageStack.pop(undefined, true);
59                         mountProtector.unlock();
60                 }
61
62                 trackerStore.storeVideo(file);
63         }
64
65         function stopRecording() {
66                 mountProtector.unlock();
67                 pageStack.pop(undefined, true);
68         }
69
70         policyMode: CameraResources.Recording
71
72         controlsVisible: cam.running && videoMode.recording && !cameraMode.animationRunning && !previewAnimationRunning && !standbyWidget.visible
73
74         orientationLock: PageOrientation.LockLandscape
75
76         DisplayState {
77                 inhibitDim: true
78         }
79
80         onBatteryLow: {
81                 if (!checkBattery()) {
82                         page.stopRecording();
83                         showError(qsTr("Not enough battery to record video."));
84                 }
85         }
86
87         Button {
88                 id: recording
89                 anchors.right: parent.right
90                 anchors.rightMargin: 20
91                 anchors.verticalCenter: parent.verticalCenter
92                 iconSource: "image://theme/icon-m-camera-video-record"
93                 width: 75
94                 height: 75
95                 opacity: 0.5
96
97                 onClicked: page.stopRecording();
98                 visible: controlsVisible
99         }
100
101         Connections {
102                 target: Qt.application
103                 onActiveChanged: {
104                         if (!Qt.application.active) {
105                                 page.stopRecording();
106                         }
107                 }
108         }
109
110         VideoMode {
111                 id: videoMode
112                 camera: cam
113         }
114
115         VideoTorchButton {
116                 id: torch
117                 camera: cam
118                 visible: controlsVisible
119                 anchors.top: parent.top
120                 anchors.left: parent.left
121                 anchors.topMargin: 20
122                 anchors.leftMargin: 20
123                 opacity: 0.5
124         }
125
126         VideoEvCompButton {
127                 id: evComp
128                 visible: controlsVisible
129                 anchors.top: torch.bottom
130                 anchors.left: parent.left
131                 anchors.topMargin: 10
132                 anchors.leftMargin: 20
133         }
134
135         Rectangle {
136                 anchors.left: parent.left
137                 anchors.bottom: parent.bottom
138                 anchors.leftMargin: 20
139                 anchors.bottomMargin: 20
140
141                 visible: controlsVisible
142
143                 color: "black"
144                 opacity: 0.5
145                 width: 100
146                 height: 30
147
148                 Timer {
149                         id: recordingDuration
150
151                         property int duration: 0
152
153                         running: videoMode.recording
154                         interval: 1000
155                         repeat: true
156
157                         onTriggered: {
158                                 duration = duration + 1;
159                                 if (duration == 3600) {
160                                         page.stopRecording();
161                                         showError(qsTr("Maximum recording time reached."));
162                                 }
163                                 else if (!checkDiskSpace()) {
164                                         page.stopRecording();
165                                         showError(qsTr("Not enough space to continue recording."));
166                                 }
167                         }
168                 }
169
170                 Image {
171                         id: recordingIcon
172                         source: "image://theme/icon-m-camera-ongoing-recording"
173                         width: 20
174                         height: 20
175                         anchors.verticalCenter: parent.verticalCenter
176                         anchors.left: parent.left
177                         anchors.leftMargin: 5
178                         sourceSize.width: 20
179                         sourceSize.height: 20
180                 }
181
182                 Label {
183                         function formatDuration(dur) {
184                                 var secs = parseInt(recordingDuration.duration);
185                                 var minutes = Math.floor(secs / 60);
186                                 var seconds = secs - (minutes * 60);
187
188                                 var date = new Date();
189                                 date.setSeconds(seconds);
190                                 date.setMinutes(minutes);
191                                 return Qt.formatTime(date, "mm:ss");
192                         }
193
194                         id: durationLabel
195                         text: formatDuration(recordingDuration.duration);
196                         anchors.left: recordingIcon.right
197                         anchors.leftMargin: 5
198                 }
199         }
200 }