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