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