]> git.vomp.tv Git - vompclient.git/blob - config.cc
Fix text corruption in live TV OSD clock
[vompclient.git] / config.cc
1 #include <iostream>
2 #include <string>
3 #include <fstream>
4 #include <streambuf>
5
6 // http://open-source-parsers.github.io/jsoncpp-docs/doxygen/namespacemembers.html
7
8
9 #include "config.h"
10
11 Config* Config::instance = NULL;
12
13 Config::Config()
14 : jconfigro(jconfig)
15 {
16   instance = this;
17   applyDefaults();
18 }
19
20 Config::~Config()
21 {
22   instance = NULL;
23 }
24
25 Config* Config::getInstance()
26 {
27   return instance;
28 }
29
30 void Config::applyDefaults()
31 {
32   // Insert the value only if it doesn't already exist
33   auto insertBool   = [&] (const char* s, const char* k, bool v)        { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
34   auto insertString = [&] (const char* s, const char* k, const char* v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
35   auto insertInt    = [&] (const char* s, const char* k, int v)         { if (jconfig[s][k].isNull()) jconfig[s][k] = v; };
36
37   insertBool("main", "daemonize", true);
38
39   insertBool("log", "enabled", false);
40   insertString("log", "filename", "stdout");
41   insertString("log", "level", "debug");
42
43   #ifdef VOMP_PLATFORM_RASPBERRY
44   insertBool("input", "mod_cec_enabled", true);
45   #else
46   insertBool("input", "mod_cec_enabled", false);
47   #endif
48
49   insertBool("input", "mod_udp_enabled", true);
50   insertBool("input", "mod_lirc_enabled", false);
51
52   insertInt("input_udp", "port", 2000);
53
54   insertInt("server-discovery", "prefer-ipv", 6);
55
56   // Not in config.json.sample - VDR login always sets this
57   insertInt("subtitles", "default", 0);
58 }
59
60 bool Config::loadFile()
61 {
62   jconfig.clear();
63
64   std::ifstream configFile("config.json");
65   if (configFile.is_open())
66   {
67     Json::CharReaderBuilder builder;
68     builder["collectComments"] = false;
69     builder["allowTrailingCommas"] = true; // FIXME
70     std::string errs;
71
72     bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
73     if (!ok)
74     {
75       std::cout << errs << std::endl;
76       return false;
77     }
78   }
79
80   applyDefaults();
81   return true;
82 }
83
84 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
85 {
86   Json::Value v = jconfigro[section][key];
87   if (!v.isString()) return false;
88   out = v.asString();
89   return true;
90 }
91
92 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
93 {
94   Json::Value v = jconfigro[section][key];
95   if (!v.isInt64()) return false;
96   out = static_cast<int>(v.asInt64());
97   return true;
98 }
99
100 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
101 {
102   Json::Value v = jconfigro[section][key];
103   if (!v.isBool()) return false;
104   out = v.asBool();
105   return true;
106 }
107 /*
108 bool Config::getBool(const char* section, const char* key, bool& out) const
109 {
110   Json::Value v = jconfigro[section][key];
111   if (!v.isBool()) return false;
112   out = v.asBool();
113   return true;
114 }*/
115
116 void Config::dump() const
117 {
118   std::cout << jconfigro << std::endl;
119 }
120
121 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
122 {
123   const Json::Value& v = jconfigro[section][key];
124   if (!v.isArray()) return false;
125
126   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
127   {
128     const Json::Value& jv = *i;
129     if (jv.isString())
130       callback(jv.asString());
131   }
132
133   return true;
134 }
135
136 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
137 {
138   const Json::Value& v = jconfigro[section][key];
139   if (!v.isObject()) return false;
140
141   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
142   {
143     const Json::Value& jv = *i;
144     if (jv.isString())
145       callback(i.key().asString(), jv.asString());
146   }
147
148   return true;
149 }
150
151 void Config::set(const std::string& section, const std::string& key, const std::string& value)
152 {
153   jconfig[section][key] = value;
154 }
155
156 void Config::set(const std::string& section, const std::string& key, bool value)
157 {
158   jconfig[section][key] = value;
159 }
160
161 void Config::set(const std::string& section, const std::string& key, const char* value)
162 {
163   jconfig[section][key] = value;
164 }
165
166 void Config::set(const std::string& section, const std::string& key, int value)
167 {
168   jconfig[section][key] = value;
169 }