Added night framerate to image and video settings
[harmattan/cameraplus] / lib / qtcamconfig.cpp
1 #include "qtcamconfig.h"
2 #include <QSettings>
3 #include <QStringList>
4 #include <QDebug>
5
6 #define CONFIGURATION_FILE                    "/etc/qtcamera.ini"
7
8 class QtCamConfigPrivate {
9 public:
10   QString element(const QString& name) const {
11     return conf->value(QString("%1/element").arg(name)).toString();
12   }
13
14   QSize readResolution(const QString key) {
15     QList<QString> parts = conf->value(key).toString().trimmed().split("x");
16     return QSize(parts[0].toInt(), parts[1].toInt());
17   }
18
19   QPair<int, int> readFrameRate(const QString& key) {
20     QList<QString> parts = conf->value(key).toString().trimmed().split("/");
21     return qMakePair<int, int>(parts[0].toInt(), parts[1].toInt());
22   }
23
24   QSettings *conf;
25
26   QList<QtCamImageSettings> imageSettings;
27   QList<QtCamVideoSettings> videoSettings;
28 };
29
30 QtCamConfig::QtCamConfig(QObject *parent) :
31   QObject(parent), d_ptr(new QtCamConfigPrivate) {
32
33   d_ptr->conf = new QSettings(CONFIGURATION_FILE, QSettings::IniFormat, this);
34 }
35
36 QtCamConfig::QtCamConfig(const QString& configPath, QObject *parent) :
37   QObject(parent), d_ptr(new QtCamConfigPrivate) {
38
39   d_ptr->conf = new QSettings(configPath, QSettings::IniFormat, this);
40
41   defaultImageSettings();
42 }
43
44 QtCamConfig::~QtCamConfig() {
45   delete d_ptr;
46 }
47
48 QString QtCamConfig::deviceScannerType() const {
49   return d_ptr->conf->value("devices/scanner").toString();
50 }
51
52 QString QtCamConfig::deviceScannerProperty() const {
53   return d_ptr->conf->value("devices/property").toString();
54 }
55
56 QString QtCamConfig::videoSource() const {
57   return d_ptr->element("video-source");
58 }
59
60 QString QtCamConfig::viewfinderSink() const {
61   return d_ptr->element("viewfinder-sink");
62 }
63
64 QString QtCamConfig::viewfinderRenderer() const {
65   return d_ptr->conf->value("viewfinder-sink/renderer").toString();
66 }
67
68 QString QtCamConfig::audioSource() const {
69   return d_ptr->element("audio-source");
70 }
71
72 QString QtCamConfig::wrapperVideoSource() const {
73   return d_ptr->element("wrapper-video-source");
74 }
75
76 QString QtCamConfig::wrapperVideoSourceProperty() const {
77   return d_ptr->conf->value("wrapper-video-source/property").toString();
78 }
79
80 QtCamImageSettings QtCamConfig::defaultImageSettings() {
81
82   QList<QtCamImageSettings> settings = imageSettings();
83
84   QString def = d_ptr->conf->value("image/default").toString();
85   foreach (const QtCamImageSettings& s, settings) {
86     if (s.id() == def) {
87       return s;
88     }
89   }
90
91   // This will crash if no presets have been shipped but you deserve paying
92   // the price of shipping a broken configuration file.
93   return settings[0];
94 }
95
96 QList<QtCamImageSettings> QtCamConfig::imageSettings() {
97   if (d_ptr->imageSettings.isEmpty()) {
98     QStringList presets = d_ptr->conf->value("image/presets").toStringList();
99     foreach (const QString& preset, presets) {
100       d_ptr->conf->beginGroup(preset);
101
102       QPair<int, int> fps = d_ptr->readFrameRate("fps");
103       QPair<int, int> night = d_ptr->readFrameRate("night");
104
105       d_ptr->imageSettings <<
106         QtCamImageSettings(preset, d_ptr->conf->value("name").toString(),
107                            d_ptr->readResolution("capture"),
108                            d_ptr->readResolution("preview"),
109                            d_ptr->readResolution("viewfinder"),
110                            fps.first, fps.second, night.first, night.second);
111
112       d_ptr->conf->endGroup();
113     }
114   }
115
116   return d_ptr->imageSettings;
117 }
118
119 QtCamVideoSettings QtCamConfig::defaultVideoSettings() {
120   QList<QtCamVideoSettings> settings = videoSettings();
121
122   QString def = d_ptr->conf->value("video/default").toString();
123   foreach (const QtCamVideoSettings& s, settings) {
124     if (s.id() == def) {
125       return s;
126     }
127   }
128
129   // This will crash if no presets have been shipped but you deserve paying
130   // the price of shipping a broken configuration file.
131   return settings[0];
132 }
133
134 QList<QtCamVideoSettings> QtCamConfig::videoSettings() {
135   if (d_ptr->videoSettings.isEmpty()) {
136     QStringList presets = d_ptr->conf->value("video/presets").toStringList();
137     foreach (const QString& preset, presets) {
138       d_ptr->conf->beginGroup(preset);
139
140       QPair<int, int> fps = d_ptr->readFrameRate("fps");
141       QPair<int, int> night = d_ptr->readFrameRate("night");
142
143       d_ptr->videoSettings <<
144         QtCamVideoSettings(preset, d_ptr->conf->value("name").toString(),
145                            d_ptr->readResolution("capture"),
146                            d_ptr->readResolution("preview"),
147                            fps.first, fps.second, night.first, night.second);
148
149       d_ptr->conf->endGroup();
150     }
151   }
152
153   return d_ptr->videoSettings;
154 }
155
156 QString QtCamConfig::imageEncodingProfileName() const {
157   return d_ptr->conf->value("image/profile-name").toString();
158 }
159
160 QString QtCamConfig::imageEncodingProfilePath() const {
161   return d_ptr->conf->value("image/profile-path").toString();
162 }
163
164 QString QtCamConfig::videoEncodingProfileName() const {
165   return d_ptr->conf->value("video/profile-name").toString();
166 }
167
168 QString QtCamConfig::videoEncodingProfilePath() const {
169   return d_ptr->conf->value("video/profile-path").toString();
170 }
171
172 QString QtCamConfig::audioCaptureCaps() const {
173   return d_ptr->conf->value("audio-capture-caps/caps").toString();
174 }