low space handling:
authorMohammed Sameer <msameer@foolab.org>
Sun, 7 Oct 2012 00:20:56 +0000 (03:20 +0300)
committerMohammed Sameer <msameer@foolab.org>
Sun, 7 Oct 2012 00:20:56 +0000 (03:20 +0300)
1) Don't capture or record if we have less than 100MB free
2) Stop recording if we have less than 100MB free

qml/CameraPage.qml
qml/ImagePage.qml
qml/VideoPage.qml
qml/main.qml
src/filenaming.cpp
src/filenaming.h
src/fsmonitor.cpp
src/fsmonitor.h

index e643b2f..df75179 100644 (file)
@@ -158,6 +158,10 @@ Page {
                 visible: controlsVisible
         }
 
+        function checkDiskSpace() {
+                return fileSystem.hasFreeSpace(fileNaming.path);
+        }
+
         function checkBattery() {
                 // We are fine if we are connected to the charger:
                 if (batteryMonitor.chargingState == BatteryInfo.Charging) {
index 67ccee0..9302fb7 100644 (file)
@@ -49,6 +49,11 @@ CameraPage {
                                 return;
                         }
 
+                        if (!checkDiskSpace()) {
+                                showError(qsTr("Not enough space to capture images."));
+                                return;
+                        }
+
                         if (!fileSystem.available) {
                                 showError(qsTr("Camera cannot capture images in mass storage mode."));
                                 return;
index 9b861ba..cedab0c 100644 (file)
@@ -26,8 +26,6 @@ import QtCamera 1.0
 import CameraPlus 1.0
 import "data.js" as Data
 
-// TODO: stop recording when disk is low
-
 CameraPage {
         id: page
 
@@ -80,6 +78,11 @@ CameraPage {
                                         return;
                                 }
 
+                                if (!checkDiskSpace()) {
+                                        showError(qsTr("Not enough space to record video."));
+                                        return;
+                                }
+
                                 policyMode = CameraResources.Recording;
                         }
                         else if (videoMode.recording) {
@@ -262,6 +265,10 @@ CameraPage {
                                         videoMode.stopRecording();
                                         showError(qsTr("Maximum recording time reached."));
                                 }
+                                else if (!checkDiskSpace()) {
+                                        videoMode.stopRecording();
+                                        showError(qsTr("Not enough space to continue recording."));
+                                }
                         }
 
                         onRunningChanged: {
index eb388ca..a24f4a0 100644 (file)
@@ -29,7 +29,6 @@ import QtMobility.systeminfo 1.2
 import QtMobility.location 1.2
 
 // TODO: postcapture
-// TODO: disk space
 // TODO: flash not ready
 // TODO: focus, caf, ...
 // TODO: portrait/landscape
index dcfa623..65331e6 100644 (file)
@@ -96,3 +96,7 @@ QString FileNaming::fileName(const QString& suffix) {
 
   return name;
 }
+
+QString FileNaming::path() const {
+  return PATH;
+}
index d641ae9..383efdc 100644 (file)
@@ -30,6 +30,7 @@ class FileNaming : public QObject {
 
   Q_PROPERTY(QString imageSuffix READ imageSuffix WRITE setImageSuffix NOTIFY imageSuffixChanged);
   Q_PROPERTY(QString videoSuffix READ videoSuffix WRITE setVideoSuffix NOTIFY videoSuffixChanged);
+  Q_PROPERTY(QString path READ path CONSTANT);
 
 public:
   FileNaming(QObject *parent = 0);
@@ -44,6 +45,8 @@ public:
   Q_INVOKABLE QString imageFileName();
   Q_INVOKABLE QString videoFileName();
 
+  QString path() const;
+
 signals:
   void imageSuffixChanged();
   void videoSuffixChanged();
index 20ff0af..0174d2a 100644 (file)
 
 #include "fsmonitor.h"
 #include <qmusbmode.h>
+#include <sys/statvfs.h>
+#include <cstdio>
+
+#define MIN_SPACE 100 * 1024 * 1024 // 100 MB
 
 FSMonitor::FSMonitor(QObject *parent) :
   QObject(parent), m_mode(new MeeGo::QmUSBMode(this)) {
@@ -51,3 +55,14 @@ void FSMonitor::modeChanged() {
   setAvailable(m_mode->mountStatus(MeeGo::QmUSBMode::DocumentDirectoryMount)
               .testFlag(MeeGo::QmUSBMode::ReadWriteMount));
 }
+
+bool FSMonitor::hasFreeSpace(const QString& path) {
+  struct statvfs buf;
+
+  if (statvfs(path.toLocal8Bit().data(), &buf) == -1) {
+    std::perror("statvfs");
+    return false;
+  }
+
+  return (buf.f_bsize * buf.f_bavail >= MIN_SPACE);
+}
index b7c4c4a..57a7ade 100644 (file)
@@ -41,6 +41,8 @@ public:
 
   void setAvailable(bool available);
 
+  Q_INVOKABLE bool hasFreeSpace(const QString& path);
+
 signals:
   void availabilityChanged();