Set media.name to '*'.
[harmattan/cameraplus] / lib / qtcammode_p.h
index 201d0ac..b724ffd 100644 (file)
@@ -3,7 +3,7 @@
 /*!
  * This file is part of CameraPlus.
  *
- * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ * 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
 #define QT_CAM_MODE_P_H
 
 #include <QSize>
+#include <QFileInfo>
+#include <QDir>
 #include "qtcamdevice_p.h"
+#include "qtcamanalysisbin.h"
 #include <gst/pbutils/encoding-profile.h>
 #include <gst/pbutils/encoding-target.h>
+#include "qtcamgstreamermessagehandler.h"
 
 #ifndef GST_USE_UNSTABLE_API
 #define GST_USE_UNSTABLE_API
@@ -44,6 +48,10 @@ public:
   QtCamModePrivate(QtCamDevicePrivate *d) : id(-1), dev(d) {}
   virtual ~QtCamModePrivate() {}
 
+  void init(DoneHandler *handler) {
+    doneHandler = handler;
+  }
+
   int modeId(const char *mode) {
     if (!dev->cameraBin) {
       return -1;
@@ -73,8 +81,17 @@ public:
 
   GstEncodingProfile *loadProfile(const QString& path, const QString& name) {
     GError *error = NULL;
+    QString targetPath;
+    QFileInfo info(path);
+    if (!info.isAbsolute()) {
+      targetPath = QDir(DATA_DIR).absoluteFilePath(path);
+    }
+    else {
+      targetPath = info.filePath();
+    }
 
-    GstEncodingTarget *target = gst_encoding_target_load_from_file(path.toUtf8().data(), &error);
+    GstEncodingTarget *target = gst_encoding_target_load_from_file(targetPath.toUtf8().constData(),
+                                                                  &error);
     if (!target) {
       qCritical() << "Failed to load encoding target from" << path << error->message;
       g_error_free(error);
@@ -168,7 +185,13 @@ public:
     else {
       QString preview = QString(PREVIEW_CAPS).arg(size.width()).arg(size.height());
 
-      GstCaps *caps = gst_caps_from_string(preview.toAscii());
+      GstCaps *caps = gst_caps_from_string(preview.toLatin1());
+
+      if (!dev->conf->isPreviewSupported()) {
+       qWarning() << "Cannot set preview caps. Preview not supported";
+       gst_caps_unref(caps);
+       return;
+      }
 
       g_object_set(dev->cameraBin, "preview-caps", caps, "post-previews", TRUE, NULL);
 
@@ -179,10 +202,23 @@ public:
   void setFileName(const QString& file) {
     fileName = file;
   }
+
   void setTempFileName(const QString& file) {
     tempFileName = file;
   }
 
+  void enableViewfinderFilters() {
+    if (dev->viewfinderFilters) {
+      dev->viewfinderFilters->setBlocked(false);
+    }
+  }
+
+  void disableViewfinderFilters() {
+    if (dev->viewfinderFilters) {
+      dev->viewfinderFilters->setBlocked(true);
+    }
+  }
+
   int id;
   QtCamMode *q_ptr;
   QtCamDevicePrivate *dev;
@@ -192,4 +228,41 @@ public:
   QString tempFileName;
 };
 
+class DoneHandler : public QtCamGStreamerMessageHandler {
+public:
+  DoneHandler(QtCamModePrivate *m, const char *done, QObject *parent = 0) :
+    QtCamGStreamerMessageHandler(done, parent) {
+    mode = m;
+  }
+
+  virtual ~DoneHandler() { }
+
+  virtual void handleMessage(GstMessage *message) {
+    // If we have a temp file then we rename it:
+    if (!mode->tempFileName.isEmpty() && !mode->fileName.isEmpty()) {
+      if (!QFile::rename(mode->tempFileName, mode->fileName)) {
+       qCritical() << "Failed to rename" << mode->tempFileName << "to" << mode->fileName;
+      }
+    }
+
+    QString fileName;
+    const GstStructure *s = gst_message_get_structure(message);
+    if (gst_structure_has_field(s, "filename")) {
+      const char *str = gst_structure_get_string(s, "filename");
+      if (str) {
+       fileName = QString::fromUtf8(str);
+      }
+    }
+
+    if (fileName.isEmpty()) {
+      fileName = mode->fileName;
+    }
+
+    QMetaObject::invokeMethod(mode->q_ptr, "saved", Qt::QueuedConnection,
+                             Q_ARG(QString, fileName));
+  }
+
+  QtCamModePrivate *mode;
+};
+
 #endif /* QT_CAM_MODE_P_H */