Reworked how resolution is being specified in the configuration.
[harmattan/cameraplus] / lib / qtcamconfig.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 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                    "/etc/qtcamera/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 QString QtCamConfig::audioSource() const {
91   return d_ptr->element("audio-source");
92 }
93
94 QString QtCamConfig::wrapperVideoSource() const {
95   return d_ptr->element("wrapper-video-source");
96 }
97
98 QString QtCamConfig::wrapperVideoSourceProperty() const {
99   return d_ptr->conf->value("wrapper-video-source/property").toString();
100 }
101
102 QtCamImageSettings *QtCamConfig::imageSettings(const QVariant& id) {
103   QString generic = "image";
104   QString specific = QString("%1-%2").arg(generic).arg(id.toString());
105
106   QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
107   QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
108   QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
109   QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
110
111   QList<QtCamImageResolution> resolutions;
112
113   foreach (const QString& preset, presets) {
114     d_ptr->conf->beginGroup(preset);
115
116     QString id = preset;
117     QString name = d_ptr->conf->value("name").toString();
118     QSize capture = d_ptr->readResolution("capture");
119     QSize preview = d_ptr->readResolution("preview");
120     QSize viewfinder = d_ptr->readResolution("viewfinder");
121     int fps = d_ptr->conf->value("fps").toInt();
122     int nightFps = d_ptr->conf->value("night").toInt();
123     int megaPixels = d_ptr->conf->value("megapixels").toInt();
124     QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
125
126     d_ptr->conf->endGroup();
127
128     resolutions << QtCamImageResolution(id, name, capture, preview, viewfinder,
129                                         fps, nightFps, megaPixels, aspectRatio);
130   }
131
132   return new QtCamImageSettings(id.toString(), suffix, profileName, profilePath, resolutions);
133 }
134
135 QtCamVideoSettings *QtCamConfig::videoSettings(const QVariant& id) {
136   QString generic = "video";
137   QString specific = QString("%1-%2").arg(generic).arg(id.toString());
138
139   QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
140   QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
141   QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
142   QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
143
144   QList<QtCamVideoResolution> resolutions;
145
146   foreach (const QString& preset, presets) {
147     d_ptr->conf->beginGroup(preset);
148
149     QString id = preset;
150     QString name = d_ptr->conf->value("name").toString();
151     QSize capture = d_ptr->readResolution("capture");
152     QSize preview = d_ptr->readResolution("preview");
153     int fps = d_ptr->conf->value("fps").toInt();
154     int nightFps = d_ptr->conf->value("night").toInt();
155     QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
156     QString resolution = d_ptr->conf->value("resolution").toString();
157
158     d_ptr->conf->endGroup();
159
160     resolutions << QtCamVideoResolution(id, name, capture, preview,
161                                         fps, nightFps, aspectRatio, resolution);
162   }
163
164   return new QtCamVideoSettings(id.toString(), suffix, profileName, profilePath, resolutions);
165 }
166
167 QString QtCamConfig::imageEncodingProfileName() const {
168   return d_ptr->conf->value("image/profile-name").toString();
169 }
170
171 QString QtCamConfig::imageEncodingProfilePath() const {
172   return d_ptr->conf->value("image/profile-path").toString();
173 }
174
175 QString QtCamConfig::videoEncodingProfileName() const {
176   return d_ptr->conf->value("video/profile-name").toString();
177 }
178
179 QString QtCamConfig::videoEncodingProfilePath() const {
180   return d_ptr->conf->value("video/profile-path").toString();
181 }
182
183 QString QtCamConfig::audioCaptureCaps() const {
184   return d_ptr->conf->value("audio-capture-caps/caps").toString();
185 }
186
187 QString QtCamConfig::imageSuffix() const {
188   return d_ptr->conf->value("image/extension").toString();
189 }
190
191 QString QtCamConfig::videoSuffix() const {
192   return d_ptr->conf->value("video/extension").toString();
193 }