Set media.name to '*'.
[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 "qtcamimagesettings.h"
23 #include "qtcamvideosettings.h"
24 #include "qtcamquirks.h"
25 #include <QSettings>
26 #include <QStringList>
27 #include <QDebug>
28 #include <QMetaEnum>
29
30 #define CONFIGURATION_FILE                    DATA_DIR"/qtcamera.ini"
31
32 class QtCamConfigPrivate {
33 public:
34   QString element(const QString& name) const {
35     return conf->value(QString("%1/element").arg(name)).toString();
36   }
37
38   QSize readResolution(const QString key) {
39     QList<QString> parts = conf->value(key).toString().trimmed().split("x");
40     return QSize(parts[0].toInt(), parts[1].toInt());
41   }
42
43   QVariant readWithFallback(const QString& generic, const QString& specific, const QString& key) {
44     QString genericKey = QString("%1/%2").arg(generic).arg(key);
45     QString specificKey = QString("%1/%2").arg(specific).arg(key);
46
47     QVariant var = conf->value(genericKey);
48
49     return conf->value(specificKey, var);
50   }
51
52   QSettings *conf;
53
54   QList<QtCamImageSettings> imageSettings;
55   QList<QtCamVideoSettings> videoSettings;
56 };
57
58 QtCamConfig::QtCamConfig(QObject *parent) :
59   QObject(parent), d_ptr(new QtCamConfigPrivate) {
60
61   d_ptr->conf = new QSettings(CONFIGURATION_FILE, QSettings::IniFormat, this);
62 }
63
64 QtCamConfig::QtCamConfig(const QString& configPath, QObject *parent) :
65   QObject(parent), d_ptr(new QtCamConfigPrivate) {
66
67   d_ptr->conf = new QSettings(configPath, QSettings::IniFormat, this);
68 }
69
70 QtCamConfig::~QtCamConfig() {
71   delete d_ptr;
72 }
73
74 QString QtCamConfig::deviceScannerType() const {
75   return d_ptr->conf->value("devices/scanner").toString();
76 }
77
78 QString QtCamConfig::deviceScannerProperty() const {
79   return d_ptr->conf->value("devices/property").toString();
80 }
81
82 QString QtCamConfig::videoSource() const {
83   return d_ptr->element("video-source");
84 }
85
86 QString QtCamConfig::viewfinderSink() const {
87   return d_ptr->element("viewfinder-sink");
88 }
89
90 QString QtCamConfig::viewfinderRenderer() const {
91   return d_ptr->conf->value("viewfinder-sink/renderer").toString();
92 }
93
94 bool QtCamConfig::viewfinderUseFence() const {
95   return d_ptr->conf->value("viewfinder-sink/use-fence").toBool();
96 }
97
98 QString QtCamConfig::audioSource() const {
99   return d_ptr->element("audio-source");
100 }
101
102 QString QtCamConfig::wrapperVideoSource() const {
103   return d_ptr->element("wrapper-video-source");
104 }
105
106 QString QtCamConfig::wrapperVideoSourceProperty() const {
107   return d_ptr->conf->value("wrapper-video-source/property").toString();
108 }
109
110 QtCamImageSettings *QtCamConfig::imageSettings(const QVariant& id) {
111   QString generic = "image";
112   QString specific = QString("%1-%2").arg(generic).arg(id.toString());
113
114   QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
115   QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
116   QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
117   QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
118
119   QList<QtCamImageResolution> resolutions;
120
121   foreach (const QString& preset, presets) {
122     d_ptr->conf->beginGroup(preset);
123
124     QString id = preset;
125     QString name = d_ptr->conf->value("name").toString();
126     QSize capture = d_ptr->readResolution("capture");
127     QSize preview = d_ptr->readResolution("preview");
128     QSize viewfinder = d_ptr->readResolution("viewfinder");
129     int fps = d_ptr->conf->value("fps").toInt();
130     int nightFps = d_ptr->conf->value("night").toInt();
131     int megaPixels = d_ptr->conf->value("megapixels").toInt();
132     QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
133
134     d_ptr->conf->endGroup();
135
136     resolutions << QtCamImageResolution(id, name, capture, preview, viewfinder,
137                                         fps, nightFps, megaPixels, aspectRatio);
138   }
139
140   return new QtCamImageSettings(id.toString(), suffix, profileName, profilePath, resolutions);
141 }
142
143 QtCamVideoSettings *QtCamConfig::videoSettings(const QVariant& id) {
144   QString generic = "video";
145   QString specific = QString("%1-%2").arg(generic).arg(id.toString());
146
147   QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
148   QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
149   QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
150   QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
151
152   QList<QtCamVideoResolution> resolutions;
153
154   foreach (const QString& preset, presets) {
155     d_ptr->conf->beginGroup(preset);
156
157     QString id = preset;
158     QString name = d_ptr->conf->value("name").toString();
159     QSize capture = d_ptr->readResolution("capture");
160     QSize preview = d_ptr->readResolution("preview");
161     int fps = d_ptr->conf->value("fps").toInt();
162     int nightFps = d_ptr->conf->value("night").toInt();
163     QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
164     QString resolution = d_ptr->conf->value("resolution").toString();
165
166     d_ptr->conf->endGroup();
167
168     resolutions << QtCamVideoResolution(id, name, capture, preview,
169                                         fps, nightFps, aspectRatio, resolution);
170   }
171
172   return new QtCamVideoSettings(id.toString(), suffix, profileName, profilePath, resolutions);
173 }
174
175 QtCamQuirks *QtCamConfig::quirks(const QVariant& id) {
176   QString group = QString("quirks-%1").arg(id.toString());
177   d_ptr->conf->beginGroup(group);
178   QStringList quirks = d_ptr->conf->value("quirks").toStringList();
179   d_ptr->conf->endGroup();
180
181   QMetaObject m = QtCamQuirks::staticMetaObject;
182   int index = m.indexOfEnumerator("QuirkType");
183   if (index == -1) {
184     qCritical() << "Failed to obtain QuirkType index";
185     return 0;
186   }
187
188   QMetaEnum e = m.enumerator(index);
189
190   int value = 0;
191
192   foreach (const QString& quirk, quirks) {
193     int val = e.keyToValue(quirk.toLatin1().constData());
194
195     if (val == -1) {
196       qWarning() << "Unknown quirk" << quirk;
197     }
198     else {
199       value |= val;
200     }
201   }
202
203   QtCamQuirks::QuirkTypes types = (QtCamQuirks::QuirkTypes)value;
204
205   return new QtCamQuirks(types);
206 }
207
208 QString QtCamConfig::imageEncodingProfileName() const {
209   return d_ptr->conf->value("image/profile-name").toString();
210 }
211
212 QString QtCamConfig::imageEncodingProfilePath() const {
213   return d_ptr->conf->value("image/profile-path").toString();
214 }
215
216 QString QtCamConfig::videoEncodingProfileName() const {
217   return d_ptr->conf->value("video/profile-name").toString();
218 }
219
220 QString QtCamConfig::videoEncodingProfilePath() const {
221   return d_ptr->conf->value("video/profile-path").toString();
222 }
223
224 QString QtCamConfig::audioCaptureCaps() const {
225   return d_ptr->conf->value("audio-capture-caps/caps").toString();
226 }
227
228 QString QtCamConfig::imageSuffix() const {
229   return d_ptr->conf->value("image/extension").toString();
230 }
231
232 QString QtCamConfig::videoSuffix() const {
233   return d_ptr->conf->value("video/extension").toString();
234 }
235
236 QStringList QtCamConfig::viewfinderFilters() const {
237   return d_ptr->conf->value("viewfinder-filters/elements").toStringList();
238 }
239
240 QString QtCamConfig::roiElement() const {
241   return d_ptr->conf->value("roi/element").toString();
242 }
243
244 QString QtCamConfig::roiMessageName() const {
245   return d_ptr->conf->value("roi/message").toString();
246 }
247
248 QString QtCamConfig::roiEnableProperty() const {
249   return d_ptr->conf->value("roi/enable").toString();
250 }
251
252 QString QtCamConfig::roiMessage() const {
253   return d_ptr->conf->value("roi/message").toString();
254 }
255
256 bool QtCamConfig::isPreviewSupported() const {
257   return d_ptr->conf->value("general/preview-supported").toBool();
258 }