Added mount protector component.
authorMohammed Sameer <msameer@foolab.org>
Sun, 7 Oct 2012 16:07:00 +0000 (19:07 +0300)
committerMohammed Sameer <msameer@foolab.org>
Sun, 7 Oct 2012 16:07:00 +0000 (19:07 +0300)
Use it to prevent going to mass storage mode while capturing or recording.

qml/ImagePage.qml
qml/VideoPage.qml
qml/main.qml
src/main.cpp
src/mountprotector.cpp [new file with mode: 0644]
src/mountprotector.h [new file with mode: 0644]
src/src.pro

index 9302fb7..817b930 100644 (file)
@@ -59,6 +59,11 @@ CameraPage {
                                 return;
                         }
 
+                        if (!mountProtector.lock()) {
+                                showError(qsTr("Failed to lock images directory."));
+                                return;
+                        }
+
                         metaData.setMetaData();
 
                         if (!imageMode.capture(fileNaming.imageFileName())) {
@@ -73,6 +78,7 @@ CameraPage {
                 id: imageMode
                 camera: cam
                 onPreviewAvailable: page.setPreview(preview);
+                onSaved: mountProtector.unlock();
         }
 
         FlashButton {
index 3a6f58c..3d79b0a 100644 (file)
@@ -105,6 +105,14 @@ CameraPage {
                         onAcquiredChanged: {
                                 if (resourcePolicy.acquired && policyMode == CameraResources.Recording) {
                                         metaData.setMetaData();
+
+                                        if (!mountProtector.lock()) {
+                                                showError(qsTr("Failed to lock images directory."));
+                                                policyMode = CameraResources.Video
+                                                return;
+                                        }
+
+
                                         if (!videoMode.startRecording(fileNaming.videoFileName())) {
                                                 showError(qsTr("Failed to record video. Please restart the camera."));
                                                 policyMode = CameraResources.Video
@@ -133,6 +141,8 @@ CameraPage {
                                 page.setPreview(preview);
                         }
                 }
+
+                onSaved: mountProtector.unlock();
         }
 
         VideoTorchButton {
index 5826628..8883028 100644 (file)
@@ -34,7 +34,6 @@ import QtMobility.location 1.2
 // TODO: portrait/landscape
 // TODO: record video in a hidden directory and then copy the video to avoid tracker indexing it.
 // TODO: stop viewfinder in settings pages ?
-// TODO: prevent going to mass storage while recording and capturing
 // TODO: grid lines, face tracking, ambr
 // TODO: complete settings pages
 // TODO: stop camera properly when we get closed.
@@ -154,6 +153,11 @@ PageStackWindow {
                 videoSuffix: cam.videoSuffix
         }
 
+        MountProtector {
+                id: mountProtector
+                path: fileNaming.path
+        }
+
         function replacePage(file) {
                 pageStack.replace(Qt.resolvedUrl(file), {cam: cam}, true);
         }
@@ -233,6 +237,13 @@ PageStackWindow {
                         showError(qsTr("Camera error. Please restart the application."));
                         cam.stop();
                         resourcePolicy.acquire(CameraResources.None);
+                        mountProtector.unlock();
+                }
+
+                onRunningChanged: {
+                        if (!cam.running) {
+                                mountProtector.unlock();
+                        }
                 }
 
                 // TODO: hardcoding device id
index eea6480..17ab6bb 100644 (file)
@@ -36,6 +36,7 @@
 #include "compass.h"
 #include "orientation.h"
 #include "geocode.h"
+#include "mountprotector.h"
 
 Q_DECL_EXPORT int main(int argc, char *argv[]) {
   QApplication::setAttribute(Qt::AA_X11InitThreads, true);
@@ -56,6 +57,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) {
   qmlRegisterType<Compass>("CameraPlus", 1, 0, "Compass");
   qmlRegisterType<Orientation>("CameraPlus", 1, 0, "Orientation");
   qmlRegisterType<Geocode>("CameraPlus", 1, 0, "ReverseGeocode");
+  qmlRegisterType<MountProtector>("CameraPlus", 1, 0, "MountProtector");
 
   QUrl sourceUrl = QUrl::fromLocalFile(QDir::currentPath() + "/main.qml");
   view.setSource(sourceUrl);
diff --git a/src/mountprotector.cpp b/src/mountprotector.cpp
new file mode 100644 (file)
index 0000000..51bb35e
--- /dev/null
@@ -0,0 +1,71 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "mountprotector.h"
+#include <QDir>
+#include <QDebug>
+
+MountProtector::MountProtector(QObject *parent) :
+  QObject(parent), m_fd(-1) {
+
+}
+
+MountProtector::~MountProtector() {
+  unlock();
+}
+
+QString MountProtector::path() const {
+  return m_path;
+}
+
+void MountProtector::setPath(const QString& path) {
+  if (m_path != path) {
+    m_path = path;
+    emit pathChanged();
+  }
+}
+
+bool MountProtector::lock() {
+  if (m_fd != -1) {
+    return true;
+  }
+
+  if (m_path.isEmpty()) {
+    return false;
+  }
+
+  QString path = QString("%1%2.cameraplus_tmp_XXXXXX").arg(m_path).arg(QDir::separator());
+
+  m_fd = mkstemp(path.toLocal8Bit().data());
+
+  if (m_fd == -1) {
+    perror("mkstemp");
+    return false;
+  }
+
+  return true;
+}
+
+void MountProtector::unlock() {
+  if (m_fd != -1) {
+    close(m_fd);
+    m_fd = -1;
+  }
+}
diff --git a/src/mountprotector.h b/src/mountprotector.h
new file mode 100644 (file)
index 0000000..677860b
--- /dev/null
@@ -0,0 +1,53 @@
+// -*- c++ -*-
+
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef MOUNT_PROTECTOR_H
+#define MOUNT_PROTECTOR_H
+
+#include <QObject>
+
+class MountProtector : public QObject {
+  Q_OBJECT
+
+  Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged);
+
+public:
+  MountProtector(QObject *parent = 0);
+  ~MountProtector();
+
+  QString path() const;
+  void setPath(const QString& path);
+
+  Q_INVOKABLE bool lock();
+
+public slots:
+  void unlock();
+
+signals:
+  void pathChanged();
+
+private:
+  int m_fd;
+  QString m_path;
+};
+
+#endif /* MOUNT_PROTECTOR_H */
index 81dd0cc..487a913 100644 (file)
@@ -15,7 +15,7 @@ PKGCONFIG = gstreamer-0.10 gstreamer-interfaces-0.10 gstreamer-video-0.10 gstrea
 LIBS +=  -L../imports/ -limports -L../lib/ -lqtcamera
 
 SOURCES += main.cpp settings.cpp filenaming.cpp quillitem.cpp displaystate.cpp fsmonitor.cpp \
-           cameraresources.cpp compass.cpp orientation.cpp geocode.cpp
+           cameraresources.cpp compass.cpp orientation.cpp geocode.cpp mountprotector.cpp
 
 HEADERS += settings.h filenaming.h quillitem.h displaystate.h fsmonitor.h \
-           cameraresources.h compass.h orientation.h geocode.h
+           cameraresources.h compass.h orientation.h geocode.h mountprotector.h