]> git.vomp.tv Git - vompclient.git/blob - config.cc
Replace TVMedia & OsdVector refcounting with new Image system
[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   insertInt("main", "start_to_live_tv", 0);
39
40   insertBool("log", "enabled", false);
41   insertString("log", "filename", "stdout");
42   insertString("log", "level", "debug");
43
44   #ifdef VOMP_PLATFORM_RASPBERRY
45   insertBool("input", "mod_cec_enabled", true);
46   #else
47   insertBool("input", "mod_cec_enabled", false);
48   #endif
49
50   insertBool("input", "mod_udp_enabled", true);
51   insertBool("input", "mod_lirc_enabled", false);
52
53   insertInt("input_udp", "port", 2000);
54
55   insertInt("server-discovery", "prefer-ipv", 6);
56
57   // Not in config.json.sample - VDR login always sets this
58   insertInt("subtitles", "default", 0);
59 }
60
61 bool Config::loadFile()
62 {
63   jconfig.clear();
64
65   std::ifstream configFile("config.json");
66   if (configFile.is_open())
67   {
68     Json::CharReaderBuilder builder;
69     builder["collectComments"] = false;
70     builder["allowTrailingCommas"] = true; // FIXME
71     std::string errs;
72
73     bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs);
74     if (!ok)
75     {
76       std::cout << errs << std::endl;
77       return false;
78     }
79   }
80
81   applyDefaults();
82   return true;
83 }
84
85 bool Config::getString(const std::string& section, const std::string& key, std::string& out) const
86 {
87   Json::Value v = jconfigro[section][key];
88   if (!v.isString()) return false;
89   out = v.asString();
90   return true;
91 }
92
93 bool Config::getInt(const std::string& section, const std::string& key, int& out) const
94 {
95   Json::Value v = jconfigro[section][key];
96   if (!v.isInt64()) return false;
97   out = static_cast<int>(v.asInt64());
98   return true;
99 }
100
101 bool Config::getBool(const std::string& section, const std::string& key, bool& out) const
102 {
103   Json::Value v = jconfigro[section][key];
104   if (!v.isBool()) return false;
105   out = v.asBool();
106   return true;
107 }
108 /*
109 bool Config::getBool(const char* section, const char* key, bool& out) const
110 {
111   Json::Value v = jconfigro[section][key];
112   if (!v.isBool()) return false;
113   out = v.asBool();
114   return true;
115 }*/
116
117 void Config::dump() const
118 {
119   std::cout << jconfigro << std::endl;
120 }
121
122 bool Config::foreachInArray(const std::string& section, const std::string& key, std::function<void(const std::string&)> callback) const
123 {
124   const Json::Value& v = jconfigro[section][key];
125   if (!v.isArray()) return false;
126
127   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
128   {
129     const Json::Value& jv = *i;
130     if (jv.isString())
131       callback(jv.asString());
132   }
133
134   return true;
135 }
136
137 bool Config::foreachPairInObject(const std::string& section, const std::string& key, std::function<void(const std::string&, const std::string&)> callback) const
138 {
139   const Json::Value& v = jconfigro[section][key];
140   if (!v.isObject()) return false;
141
142   for(Json::Value::const_iterator i = v.begin(); i != v.end(); i++)
143   {
144     const Json::Value& jv = *i;
145     if (jv.isString())
146       callback(i.key().asString(), jv.asString());
147   }
148
149   return true;
150 }
151
152 void Config::set(const std::string& section, const std::string& key, const std::string& value)
153 {
154   jconfig[section][key] = value;
155 }
156
157 void Config::set(const std::string& section, const std::string& key, bool value)
158 {
159   jconfig[section][key] = value;
160 }
161
162 void Config::set(const std::string& section, const std::string& key, const char* value)
163 {
164   jconfig[section][key] = value;
165 }
166
167 void Config::set(const std::string& section, const std::string& key, int value)
168 {
169   jconfig[section][key] = value;
170 }