Added copyright headers and COPYING file.
[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   QSettings *conf;
40
41   QList<QtCamImageSettings> imageSettings;
42   QList<QtCamVideoSettings> videoSettings;
43 };
44
45 QtCamConfig::QtCamConfig(QObject *parent) :
46   QObject(parent), d_ptr(new QtCamConfigPrivate) {
47
48   d_ptr->conf = new QSettings(CONFIGURATION_FILE, QSettings::IniFormat, this);
49 }
50
51 QtCamConfig::QtCamConfig(const QString& configPath, QObject *parent) :
52   QObject(parent), d_ptr(new QtCamConfigPrivate) {
53
54   d_ptr->conf = new QSettings(configPath, QSettings::IniFormat, this);
55
56   defaultImageSettings();
57 }
58
59 QtCamConfig::~QtCamConfig() {
60   delete d_ptr;
61 }
62
63 QString QtCamConfig::deviceScannerType() const {
64   return d_ptr->conf->value("devices/scanner").toString();
65 }
66
67 QString QtCamConfig::deviceScannerProperty() const {
68   return d_ptr->conf->value("devices/property").toString();
69 }
70
71 QString QtCamConfig::videoSource() const {
72   return d_ptr->element("video-source");
73 }
74
75 QString QtCamConfig::viewfinderSink() const {
76   return d_ptr->element("viewfinder-sink");
77 }
78
79 QString QtCamConfig::viewfinderRenderer() const {
80   return d_ptr->conf->value("viewfinder-sink/renderer").toString();
81 }
82
83 QString QtCamConfig::audioSource() const {
84   return d_ptr->element("audio-source");
85 }
86
87 QString QtCamConfig::wrapperVideoSource() const {
88   return d_ptr->element("wrapper-video-source");
89 }
90
91 QString QtCamConfig::wrapperVideoSourceProperty() const {
92   return d_ptr->conf->value("wrapper-video-source/property").toString();
93 }
94
95 QtCamImageSettings QtCamConfig::defaultImageSettings() {
96
97   QList<QtCamImageSettings> settings = imageSettings();
98
99   QString def = d_ptr->conf->value("image/default").toString();
100   foreach (const QtCamImageSettings& s, settings) {
101     if (s.id() == def) {
102       return s;
103     }
104   }
105
106   // This will crash if no presets have been shipped but you deserve paying
107   // the price of shipping a broken configuration file.
108   return settings[0];
109 }
110
111 QList<QtCamImageSettings> QtCamConfig::imageSettings() {
112   if (d_ptr->imageSettings.isEmpty()) {
113     QStringList presets = d_ptr->conf->value("image/presets").toStringList();
114     foreach (const QString& preset, presets) {
115       d_ptr->conf->beginGroup(preset);
116
117       d_ptr->imageSettings <<
118         QtCamImageSettings(preset, d_ptr->conf->value("name").toString(),
119                            d_ptr->readResolution("capture"),
120                            d_ptr->readResolution("preview"),
121                            d_ptr->readResolution("viewfinder"),
122                            d_ptr->conf->value("fps").toInt(),
123                            d_ptr->conf->value("night").toInt());
124
125       d_ptr->conf->endGroup();
126     }
127   }
128
129   return d_ptr->imageSettings;
130 }
131
132 QtCamVideoSettings QtCamConfig::defaultVideoSettings() {
133   QList<QtCamVideoSettings> settings = videoSettings();
134
135   QString def = d_ptr->conf->value("video/default").toString();
136   foreach (const QtCamVideoSettings& s, settings) {
137     if (s.id() == def) {
138       return s;
139     }
140   }
141
142   // This will crash if no presets have been shipped but you deserve paying
143   // the price of shipping a broken configuration file.
144   return settings[0];
145 }
146
147 QList<QtCamVideoSettings> QtCamConfig::videoSettings() {
148   if (d_ptr->videoSettings.isEmpty()) {
149     QStringList presets = d_ptr->conf->value("video/presets").toStringList();
150     foreach (const QString& preset, presets) {
151       d_ptr->conf->beginGroup(preset);
152
153       d_ptr->videoSettings <<
154         QtCamVideoSettings(preset, d_ptr->conf->value("name").toString(),
155                            d_ptr->readResolution("capture"),
156                            d_ptr->readResolution("preview"),
157                            d_ptr->conf->value("fps").toInt(),
158                            d_ptr->conf->value("night").toInt());
159
160       d_ptr->conf->endGroup();
161     }
162   }
163
164   return d_ptr->videoSettings;
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 }