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