Set media.name to '*'.
[harmattan/cameraplus] / lib / qtcamconfig.cpp
index bc47b04..41da44c 100644 (file)
@@ -1,9 +1,33 @@
+/*!
+ * 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 "qtcamconfig.h"
+#include "qtcamimagesettings.h"
+#include "qtcamvideosettings.h"
+#include "qtcamquirks.h"
 #include <QSettings>
 #include <QStringList>
 #include <QDebug>
+#include <QMetaEnum>
 
-#define CONFIGURATION_FILE                    "/etc/qtcamera.ini"
+#define CONFIGURATION_FILE                    DATA_DIR"/qtcamera.ini"
 
 class QtCamConfigPrivate {
 public:
@@ -16,9 +40,13 @@ public:
     return QSize(parts[0].toInt(), parts[1].toInt());
   }
 
-  QPair<int, int> readFrameRate(const QString& key) {
-    QList<QString> parts = conf->value(key).toString().trimmed().split("/");
-    return qMakePair<int, int>(parts[0].toInt(), parts[1].toInt());
+  QVariant readWithFallback(const QString& generic, const QString& specific, const QString& key) {
+    QString genericKey = QString("%1/%2").arg(generic).arg(key);
+    QString specificKey = QString("%1/%2").arg(specific).arg(key);
+
+    QVariant var = conf->value(genericKey);
+
+    return conf->value(specificKey, var);
   }
 
   QSettings *conf;
@@ -37,8 +65,6 @@ QtCamConfig::QtCamConfig(const QString& configPath, QObject *parent) :
   QObject(parent), d_ptr(new QtCamConfigPrivate) {
 
   d_ptr->conf = new QSettings(configPath, QSettings::IniFormat, this);
-
-  defaultImageSettings();
 }
 
 QtCamConfig::~QtCamConfig() {
@@ -65,6 +91,10 @@ QString QtCamConfig::viewfinderRenderer() const {
   return d_ptr->conf->value("viewfinder-sink/renderer").toString();
 }
 
+bool QtCamConfig::viewfinderUseFence() const {
+  return d_ptr->conf->value("viewfinder-sink/use-fence").toBool();
+}
+
 QString QtCamConfig::audioSource() const {
   return d_ptr->element("audio-source");
 }
@@ -77,80 +107,102 @@ QString QtCamConfig::wrapperVideoSourceProperty() const {
   return d_ptr->conf->value("wrapper-video-source/property").toString();
 }
 
-QtCamImageSettings QtCamConfig::defaultImageSettings() {
+QtCamImageSettings *QtCamConfig::imageSettings(const QVariant& id) {
+  QString generic = "image";
+  QString specific = QString("%1-%2").arg(generic).arg(id.toString());
 
-  QList<QtCamImageSettings> settings = imageSettings();
+  QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
+  QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
+  QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
+  QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
 
-  QString def = d_ptr->conf->value("image/default").toString();
-  foreach (const QtCamImageSettings& s, settings) {
-    if (s.id() == def) {
-      return s;
-    }
+  QList<QtCamImageResolution> resolutions;
+
+  foreach (const QString& preset, presets) {
+    d_ptr->conf->beginGroup(preset);
+
+    QString id = preset;
+    QString name = d_ptr->conf->value("name").toString();
+    QSize capture = d_ptr->readResolution("capture");
+    QSize preview = d_ptr->readResolution("preview");
+    QSize viewfinder = d_ptr->readResolution("viewfinder");
+    int fps = d_ptr->conf->value("fps").toInt();
+    int nightFps = d_ptr->conf->value("night").toInt();
+    int megaPixels = d_ptr->conf->value("megapixels").toInt();
+    QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
+
+    d_ptr->conf->endGroup();
+
+    resolutions << QtCamImageResolution(id, name, capture, preview, viewfinder,
+                                       fps, nightFps, megaPixels, aspectRatio);
   }
 
-  // This will crash if no presets have been shipped but you deserve paying
-  // the price of shipping a broken configuration file.
-  return settings[0];
+  return new QtCamImageSettings(id.toString(), suffix, profileName, profilePath, resolutions);
 }
 
-QList<QtCamImageSettings> QtCamConfig::imageSettings() {
-  if (d_ptr->imageSettings.isEmpty()) {
-    QStringList presets = d_ptr->conf->value("image/presets").toStringList();
-    foreach (const QString& preset, presets) {
-      d_ptr->conf->beginGroup(preset);
+QtCamVideoSettings *QtCamConfig::videoSettings(const QVariant& id) {
+  QString generic = "video";
+  QString specific = QString("%1-%2").arg(generic).arg(id.toString());
 
-      QPair<int, int> fps = d_ptr->readFrameRate("fps");
-      QPair<int, int> night = d_ptr->readFrameRate("night");
+  QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
+  QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
+  QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
+  QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
 
-      d_ptr->imageSettings <<
-       QtCamImageSettings(preset, d_ptr->conf->value("name").toString(),
-                          d_ptr->readResolution("capture"),
-                          d_ptr->readResolution("preview"),
-                          d_ptr->readResolution("viewfinder"),
-                          fps.first, fps.second, night.first, night.second);
+  QList<QtCamVideoResolution> resolutions;
 
-      d_ptr->conf->endGroup();
-    }
-  }
+  foreach (const QString& preset, presets) {
+    d_ptr->conf->beginGroup(preset);
 
-  return d_ptr->imageSettings;
-}
+    QString id = preset;
+    QString name = d_ptr->conf->value("name").toString();
+    QSize capture = d_ptr->readResolution("capture");
+    QSize preview = d_ptr->readResolution("preview");
+    int fps = d_ptr->conf->value("fps").toInt();
+    int nightFps = d_ptr->conf->value("night").toInt();
+    QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
+    QString resolution = d_ptr->conf->value("resolution").toString();
 
-QtCamVideoSettings QtCamConfig::defaultVideoSettings() {
-  QList<QtCamVideoSettings> settings = videoSettings();
+    d_ptr->conf->endGroup();
 
-  QString def = d_ptr->conf->value("video/default").toString();
-  foreach (const QtCamVideoSettings& s, settings) {
-    if (s.id() == def) {
-      return s;
-    }
+    resolutions << QtCamVideoResolution(id, name, capture, preview,
+                                       fps, nightFps, aspectRatio, resolution);
   }
 
-  // This will crash if no presets have been shipped but you deserve paying
-  // the price of shipping a broken configuration file.
-  return settings[0];
+  return new QtCamVideoSettings(id.toString(), suffix, profileName, profilePath, resolutions);
 }
 
-QList<QtCamVideoSettings> QtCamConfig::videoSettings() {
-  if (d_ptr->videoSettings.isEmpty()) {
-    QStringList presets = d_ptr->conf->value("video/presets").toStringList();
-    foreach (const QString& preset, presets) {
-      d_ptr->conf->beginGroup(preset);
+QtCamQuirks *QtCamConfig::quirks(const QVariant& id) {
+  QString group = QString("quirks-%1").arg(id.toString());
+  d_ptr->conf->beginGroup(group);
+  QStringList quirks = d_ptr->conf->value("quirks").toStringList();
+  d_ptr->conf->endGroup();
+
+  QMetaObject m = QtCamQuirks::staticMetaObject;
+  int index = m.indexOfEnumerator("QuirkType");
+  if (index == -1) {
+    qCritical() << "Failed to obtain QuirkType index";
+    return 0;
+  }
+
+  QMetaEnum e = m.enumerator(index);
 
-      QPair<int, int> fps = d_ptr->readFrameRate("fps");
-      QPair<int, int> night = d_ptr->readFrameRate("night");
+  int value = 0;
 
-      d_ptr->videoSettings <<
-       QtCamVideoSettings(preset, d_ptr->conf->value("name").toString(),
-                          d_ptr->readResolution("capture"),
-                          d_ptr->readResolution("preview"),
-                          fps.first, fps.second, night.first, night.second);
+  foreach (const QString& quirk, quirks) {
+    int val = e.keyToValue(quirk.toLatin1().constData());
 
-      d_ptr->conf->endGroup();
+    if (val == -1) {
+      qWarning() << "Unknown quirk" << quirk;
+    }
+    else {
+      value |= val;
     }
   }
 
-  return d_ptr->videoSettings;
+  QtCamQuirks::QuirkTypes types = (QtCamQuirks::QuirkTypes)value;
+
+  return new QtCamQuirks(types);
 }
 
 QString QtCamConfig::imageEncodingProfileName() const {
@@ -172,3 +224,35 @@ QString QtCamConfig::videoEncodingProfilePath() const {
 QString QtCamConfig::audioCaptureCaps() const {
   return d_ptr->conf->value("audio-capture-caps/caps").toString();
 }
+
+QString QtCamConfig::imageSuffix() const {
+  return d_ptr->conf->value("image/extension").toString();
+}
+
+QString QtCamConfig::videoSuffix() const {
+  return d_ptr->conf->value("video/extension").toString();
+}
+
+QStringList QtCamConfig::viewfinderFilters() const {
+  return d_ptr->conf->value("viewfinder-filters/elements").toStringList();
+}
+
+QString QtCamConfig::roiElement() const {
+  return d_ptr->conf->value("roi/element").toString();
+}
+
+QString QtCamConfig::roiMessageName() const {
+  return d_ptr->conf->value("roi/message").toString();
+}
+
+QString QtCamConfig::roiEnableProperty() const {
+  return d_ptr->conf->value("roi/enable").toString();
+}
+
+QString QtCamConfig::roiMessage() const {
+  return d_ptr->conf->value("roi/message").toString();
+}
+
+bool QtCamConfig::isPreviewSupported() const {
+  return d_ptr->conf->value("general/preview-supported").toBool();
+}