2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/ConfigShared.h"
|
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-05-17 20:08:51 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
ConfigShared::ConfigShared(BinlogPmcPtr config_pmc, unique_ptr<Callback> callback)
|
|
|
|
: config_pmc_(config_pmc), callback_(std::move(callback)) {
|
|
|
|
for (auto key_value : config_pmc_->get_all()) {
|
|
|
|
on_option_updated(key_value.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void ConfigShared::set_option_boolean(Slice name, bool value) {
|
|
|
|
if (set_option(name, value ? Slice("Btrue") : Slice("Bfalse"))) {
|
|
|
|
on_option_updated(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigShared::set_option_empty(Slice name) {
|
|
|
|
if (set_option(name, Slice())) {
|
|
|
|
on_option_updated(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigShared::set_option_integer(Slice name, int32 value) {
|
|
|
|
if (set_option(name, PSLICE() << "I" << value)) {
|
|
|
|
on_option_updated(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigShared::set_option_string(Slice name, Slice value) {
|
|
|
|
if (set_option(name, PSLICE() << "S" << value)) {
|
|
|
|
on_option_updated(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 16:01:45 +01:00
|
|
|
bool ConfigShared::have_option(Slice name) const {
|
|
|
|
return config_pmc_->isset(name.str());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
string ConfigShared::get_option(Slice name) const {
|
|
|
|
return config_pmc_->get(name.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<string, string> ConfigShared::get_options(Slice prefix) const {
|
|
|
|
return config_pmc_->prefix_get(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<string, string> ConfigShared::get_options() const {
|
|
|
|
return config_pmc_->get_all();
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:18:07 +02:00
|
|
|
bool ConfigShared::get_option_boolean(Slice name, bool default_value) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto value = get_option(name);
|
|
|
|
if (value.empty()) {
|
2018-05-03 12:18:07 +02:00
|
|
|
return default_value;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (value == "Btrue") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (value == "Bfalse") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "Found \"" << value << "\" instead of boolean option";
|
2018-05-03 12:18:07 +02:00
|
|
|
return default_value;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 ConfigShared::get_option_integer(Slice name, int32 default_value) const {
|
|
|
|
auto str_value = get_option(name);
|
|
|
|
if (str_value.empty()) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
if (str_value[0] != 'I') {
|
|
|
|
LOG(ERROR) << "Found \"" << str_value << "\" instead of integer option";
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
return to_integer<int32>(str_value.substr(1));
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:08:51 +02:00
|
|
|
string ConfigShared::get_option_string(Slice name, string default_value) const {
|
|
|
|
auto str_value = get_option(name);
|
|
|
|
if (str_value.empty()) {
|
2018-06-28 01:50:46 +02:00
|
|
|
return default_value;
|
2018-05-17 20:08:51 +02:00
|
|
|
}
|
|
|
|
if (str_value[0] != 'S') {
|
|
|
|
LOG(ERROR) << "Found \"" << str_value << "\" instead of string option";
|
2018-06-28 01:50:46 +02:00
|
|
|
return default_value;
|
2018-05-17 20:08:51 +02:00
|
|
|
}
|
|
|
|
return str_value.substr(1);
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:41:22 +02:00
|
|
|
tl_object_ptr<td_api::OptionValue> ConfigShared::get_option_value(Slice name) const {
|
|
|
|
return get_option_value_object(get_option(name));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigShared::set_option(Slice name, Slice value) {
|
|
|
|
if (value.empty()) {
|
|
|
|
return config_pmc_->erase(name.str()) != 0;
|
|
|
|
} else {
|
|
|
|
return config_pmc_->set(name.str(), value.str()) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::OptionValue> ConfigShared::get_option_value_object(Slice value) {
|
|
|
|
if (value.empty()) {
|
|
|
|
return make_tl_object<td_api::optionValueEmpty>();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (value[0]) {
|
|
|
|
case 'B':
|
|
|
|
if (value == "Btrue") {
|
|
|
|
return make_tl_object<td_api::optionValueBoolean>(true);
|
|
|
|
}
|
|
|
|
if (value == "Bfalse") {
|
|
|
|
return make_tl_object<td_api::optionValueBoolean>(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
return make_tl_object<td_api::optionValueInteger>(to_integer<int32>(value.substr(1)));
|
|
|
|
case 'S':
|
|
|
|
return make_tl_object<td_api::optionValueString>(value.substr(1).str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_tl_object<td_api::optionValueString>(value.str());
|
|
|
|
}
|
|
|
|
|
2018-05-22 13:09:58 +02:00
|
|
|
void ConfigShared::on_option_updated(Slice name) const {
|
|
|
|
callback_->on_option_updated(name.str(), get_option(name));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-17 20:08:51 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|