Don't reuse file names
[harmattan/cameraplus] / lib / qtcamimagemode.cpp
index 4fddcab..6aae13b 100644 (file)
@@ -1,3 +1,23 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012-2013 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 "qtcamimagemode.h"
 #include "qtcammode_p.h"
 #include "qtcamdevice_p.h"
@@ -8,31 +28,25 @@ class QtCamImageModePrivate : public QtCamModePrivate {
 public:
   QtCamImageModePrivate(QtCamDevicePrivate *dev) :
   QtCamModePrivate(dev),
-  settings(dev->conf->defaultImageSettings()) {
+  settings(dev->conf->imageSettings(dev->id)),
+  resolution(settings->defaultResolution()) {
 
   }
 
   ~QtCamImageModePrivate() {
+    delete settings;
   }
 
-  bool wrapperIsReady() {
-    if (!dev->wrapperVideoSource) {
-      return false;
-    }
-
-    gboolean ready = FALSE;
-    g_object_get(dev->wrapperVideoSource, "ready-for-capture", &ready, NULL);
-
-    return ready == TRUE;
-  }
-
-  QtCamImageSettings settings;
+  QtCamImageSettings *settings;
+  QtCamImageResolution resolution;
 };
 
-QtCamImageMode::QtCamImageMode(QtCamDevicePrivate *d, QObject *parent) :
-  QtCamMode(new QtCamImageModePrivate(d), "mode-image", "image-done", parent) {
+QtCamImageMode::QtCamImageMode(QtCamDevicePrivate *dev, QObject *parent) :
+  QtCamMode(new QtCamImageModePrivate(dev), "mode-image", parent) {
 
-  d_ptr = (QtCamImageModePrivate *)QtCamMode::d_ptr;
+  d_ptr->init(new DoneHandler(d_ptr, "image-done", this));
+
+  d = (QtCamImageModePrivate *)d_ptr;
 
   QString name = d_ptr->dev->conf->imageEncodingProfileName();
   QString path = d_ptr->dev->conf->imageEncodingProfilePath();
@@ -46,23 +60,35 @@ QtCamImageMode::QtCamImageMode(QtCamDevicePrivate *d, QObject *parent) :
 }
 
 QtCamImageMode::~QtCamImageMode() {
-
+  d = 0;
 }
 
 bool QtCamImageMode::canCapture() {
-  return QtCamMode::canCapture() && d_ptr->wrapperIsReady();
+  return QtCamMode::canCapture() && d_ptr->dev->isReadyForCapture();
 }
 
 void QtCamImageMode::applySettings() {
-  setCaps("image-capture-caps", d_ptr->settings.captureResolution(),
-         d_ptr->settings.frameRate());
-  setCaps("viewfinder-caps", d_ptr->settings.viewfinderResolution(),
-         d_ptr->settings.frameRate());
-  setPreviewSize(d_ptr->settings.previewResolution());
+  bool night = d_ptr->inNightMode();
+
+  int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
+
+  d_ptr->setCaps("viewfinder-caps", d->resolution.viewfinderResolution(), fps);
+
+  // FIXME:
+  // Ideally, we should query the image-capture-supported-caps and get a proper framerate
+  // as it seems that subdevsrc2 can only capture 15 FPS for at least the highest resolution
+  // we use. For now we will not set any FPS.
+  d_ptr->setCaps("image-capture-caps", d->resolution.captureResolution(), -1);
+
+  d_ptr->setPreviewSize(d->resolution.previewResolution());
+
+  // If we don't reset the caps then: if we switch from video to image then we fail
+  // the next time we restart the pipeline.
+  d_ptr->resetCaps("video-capture-caps");
 }
 
 void QtCamImageMode::start() {
-  // Nothing
+  d_ptr->enableViewfinderFilters();
 }
 
 void QtCamImageMode::stop() {
@@ -74,7 +100,11 @@ bool QtCamImageMode::capture(const QString& fileName) {
     return false;
   }
 
-  setFileName(fileName);
+  if (fileName.isEmpty()) {
+    return false;
+  }
+
+  d_ptr->setFileName(fileName);
 
   g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
@@ -82,8 +112,15 @@ bool QtCamImageMode::capture(const QString& fileName) {
   return true;
 }
 
-bool QtCamImageMode::setSettings(const QtCamImageSettings& settings) {
-  d_ptr->settings = settings;
+bool QtCamImageMode::setResolution(const QtCamImageResolution& resolution) {
+  d->resolution = resolution;
+
+  if (!d_ptr->dev->q_ptr->isRunning()) {
+    // We will return true here because setting the resolution on a non-running pipeline
+    // doesn't make much sense (Probably the only use case is as a kind of optimization only).
+    // We will set it anyway when the pipeline gets started.
+    return true;
+  }
 
   if (!d_ptr->dev->q_ptr->isIdle()) {
     return false;
@@ -102,3 +139,11 @@ void QtCamImageMode::setProfile(GstEncodingProfile *profile) {
 
   g_object_set(d_ptr->dev->cameraBin, "image-profile", profile, NULL);
 }
+
+QtCamImageSettings *QtCamImageMode::settings() const {
+  return d->settings;
+}
+
+QtCamImageResolution QtCamImageMode::currentResolution() {
+  return d->resolution;
+}