Fixed VideoPlayerPage.qml failure to set cameraConfig
[harmattan/cameraplus] / lib / qtcamconfig.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "qtcamconfig.h"
22 #include <QSettings>
23 #include <QStringList>
24 #include <QDebug>
25
26 #define CONFIGURATION_FILE                    DATA_DIR"/qtcamera.ini"
27
28 class QtCamConfigPrivate {
29 public:
30   QString element(const QString& name) const {
31     return conf->value(QString("%1/element").arg(name)).toString();
32   }
33
34   QSize readResolution(const QString key) {
35     QList<QString> parts = conf->value(key).toString().trimmed().split("x");
36     return QSize(parts[0].toInt(), parts[1].toInt());
37   }
38
39   QVariant readWithFallback(const QString& generic, const QString& specific, const QString& key) {
40     QString genericKey = QString("%1/%2").arg(generic).arg(key);
41     QString specificKey = QString("%1/%2").arg(specific).arg(key);
42
43     QVariant var = conf->value(genericKey);
44
45     return conf->value(specificKey, var);
46   }
47
48   QSettings *conf;
49
50   QList<QtCamImageSettings> imageSettings;
51   QList<QtCamVideoSettings> videoSettings;
52 };
53
54 QtCamConfig::QtCamConfig(QObject *parent) :
55   QObject(parent), d_ptr(new QtCamConfigPrivate) {
56
57   d_ptr->conf = new QSettings(CONFIGURATION_FILE, QSettings::IniFormat, this);
58 }
59
60 QtCamConfig::QtCamConfig(const QString& configPath, QObject *parent) :
61   QObject(parent), d_ptr(new QtCamConfigPrivate) {
62
63   d_ptr->conf = new QSettings(configPath, QSettings::IniFormat, this);
64 }
65
66 QtCamConfig::~QtCamConfig() {
67   delete d_ptr;
68 }
69
70 QString QtCamConfig::deviceScannerType() const {
71   return d_ptr->conf->value("devices/scanner").toString();
72 }
73
74 QString QtCamConfig::deviceScannerProperty() const {
75   return d_ptr->conf->value("devices/property").toString();
76 }
77
78 QString QtCamConfig::videoSource() const {
79   return d_ptr->element("video-source");
80 }
81
82 QString QtCamConfig::viewfinderSink() const {
83   return d_ptr->element("viewfinder-sink");
84 }
85
86 QString QtCamConfig::viewfinderRenderer() const {
87   return d_ptr->conf->value("viewfinder-sink/renderer").toString();
88 }
89
90 bool QtCamConfig::viewfinderUseFence() const {
91   return d_ptr->conf->value("viewfinder-sink/use-fence").toBool();
92 }
93
94 QString QtCamConfig::audioSource() const {
95   return d_ptr->element("audio-source");
96 }
97
98 QString QtCamConfig::wrapperVideoSource() const {
99   return d_ptr->element("wrapper-video-source");
100 }
101
102 QString QtCamConfig::wrapperVideoSourceProperty() const {
103   return d_ptr->conf->value("wrapper-video-source/property").toString();
104 }
105
106 QtCamImageSettings *QtCamConfig::imageSettings(const QVariant& id) {
107   QString generic = "image";
108   QString specific = QString("%1-%2").arg(generic).arg(id.toString());
109
110   QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
111   QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
112   QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
113   QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
114
115   QList<QtCamImageResolution> resolutions;
116
117   foreach (const QString& preset, presets) {
118     d_ptr->conf->beginGroup(preset);
119
120     QString id = preset;
121     QString name = d_ptr->conf->value("name").toString();
122     QSize capture = d_ptr->readResolution("capture");
123     QSize preview = d_ptr->readResolution("preview");
124     QSize viewfinder = d_ptr->readResolution("viewfinder");
125     int fps = d_ptr->conf->value("fps").toInt();
126     int nightFps = d_ptr->conf->value("night").toInt();
127     int megaPixels = d_ptr->conf->value("megapixels").toInt();
128     QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
129
130     d_ptr->conf->endGroup();
131
132     resolutions << QtCamImageResolution(id, name, capture, preview, viewfinder,
133                                         fps, nightFps, megaPixels, aspectRatio);
134   }
135
136   return new QtCamImageSettings(id.toString(), suffix, profileName, profilePath, resolutions);
137 }
138
139 QtCamVideoSettings *QtCamConfig::videoSettings(const QVariant& id) {
140   QString generic = "video";
141   QString specific = QString("%1-%2").arg(generic).arg(id.toString());
142
143   QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
144   QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
145   QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
146   QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
147
148   QList<QtCamVideoResolution> resolutions;
149
150   foreach (const QString& preset, presets) {
151     d_ptr->conf->beginGroup(preset);
152
153     QString id = preset;
154     QString name = d_ptr->conf->value("name").toString();
155     QSize capture = d_ptr->readResolution("capture");
156     QSize preview = d_ptr->readResolution("preview");
157     int fps = d_ptr->conf->value("fps").toInt();
158     int nightFps = d_ptr->conf->value("night").toInt();
159     QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
160     QString resolution = d_ptr->conf->value("resolution").toString();
161
162     d_ptr->conf->endGroup();
163
164     resolutions << QtCamVideoResolution(id, name, capture, preview,
165                                         fps, nightFps, aspectRatio, resolution);
166   }
167
168   return new QtCamVideoSettings(id.toString(), suffix, profileName, profilePath, resolutions);
169 }
170
171 QString QtCamConfig::imageEncodingProfileName() const {
172   return d_ptr->conf->value("image/profile-name").toString();
173 }
174
175 QString QtCamConfig::imageEncodingProfilePath() const {
176   return d_ptr->conf->value("image/profile-path").toString();
177 }
178
179 QString QtCamConfig::videoEncodingProfileName() const {
180   return d_ptr->conf->value("video/profile-name").toString();
181 }
182
183 QString QtCamConfig::videoEncodingProfilePath() const {
184   return d_ptr->conf->value("video/profile-path").toString();
185 }
186
187 QString QtCamConfig::audioCaptureCaps() const {
188   return d_ptr->conf->value("audio-capture-caps/caps").toString();
189 }
190
191 QString QtCamConfig::imageSuffix() const {
192   return d_ptr->conf->value("image/extension").toString();
193 }
194
195 QString QtCamConfig::videoSuffix() const {
196   return d_ptr->conf->value("video/extension").toString();
197 }
198
199 QStringList QtCamConfig::viewfinderFilters() const {
200   return d_ptr->conf->value("viewfinder-filters/elements").toStringList();
201 }
202
203 QString QtCamConfig::roiElement() const {
204   return d_ptr->conf->value("roi/element").toString();
205 }
206
207 QString QtCamConfig::roiMessageName() const {
208   return d_ptr->conf->value("roi/message").toString();
209 }
210
211 QString QtCamConfig::roiEnableProperty() const {
212   return d_ptr->conf->value("roi/enable").toString();
213 }
214
215 QString QtCamConfig::roiMessage() const {
216   return d_ptr->conf->value("roi/message").toString();
217 }