2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-05-17 20:08:51 +02:00
|
|
|
|
2019-01-17 21:12:31 +01:00
|
|
|
ConfigShared::ConfigShared(std::shared_ptr<KeyValueSyncInterface> config_pmc) : config_pmc_(std::move(config_pmc)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigShared::set_callback(unique_ptr<Callback> callback) {
|
|
|
|
callback_ = std::move(callback);
|
|
|
|
if (callback_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
for (const auto &key_value : config_pmc_->get_all()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_option_updated(key_value.first);
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 21:12:31 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 23:46:16 +02:00
|
|
|
void ConfigShared::set_option_integer(Slice name, int64 value) {
|
2022-05-14 18:59:11 +02:00
|
|
|
if (set_option(name, PSLICE() << 'I' << value)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_option_updated(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigShared::set_option_string(Slice name, Slice value) {
|
2022-05-14 18:59:11 +02:00
|
|
|
if (set_option(name, PSLICE() << 'S' << value)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2022-02-09 15:05:27 +01:00
|
|
|
std::unordered_map<string, string> ConfigShared::get_options() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-05-14 18:59:11 +02:00
|
|
|
LOG(ERROR) << "Found \"" << value << "\" instead of boolean option " << name;
|
2018-05-03 12:18:07 +02:00
|
|
|
return default_value;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 23:46:16 +02:00
|
|
|
int64 ConfigShared::get_option_integer(Slice name, int64 default_value) const {
|
2022-05-14 18:59:11 +02:00
|
|
|
auto value = get_option(name);
|
|
|
|
if (value.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return default_value;
|
|
|
|
}
|
2022-05-14 18:59:11 +02:00
|
|
|
if (value[0] != 'I') {
|
|
|
|
LOG(ERROR) << "Found \"" << value << "\" instead of integer option " << name;
|
2018-12-31 20:04:05 +01:00
|
|
|
return default_value;
|
|
|
|
}
|
2022-05-14 18:59:11 +02:00
|
|
|
return to_integer<int64>(value.substr(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-05-17 20:08:51 +02:00
|
|
|
string ConfigShared::get_option_string(Slice name, string default_value) const {
|
2022-05-14 18:59:11 +02:00
|
|
|
auto value = get_option(name);
|
|
|
|
if (value.empty()) {
|
2018-06-28 01:50:46 +02:00
|
|
|
return default_value;
|
2018-05-17 20:08:51 +02:00
|
|
|
}
|
2022-05-14 18:59:11 +02:00
|
|
|
if (value[0] != 'S') {
|
|
|
|
LOG(ERROR) << "Found \"" << value << "\" instead of string option " << name;
|
2018-06-28 01:50:46 +02:00
|
|
|
return default_value;
|
2018-05-17 20:08:51 +02:00
|
|
|
}
|
2022-05-14 18:59:11 +02:00
|
|
|
return value.substr(1);
|
2018-05-17 20:08:51 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 13:09:58 +02:00
|
|
|
void ConfigShared::on_option_updated(Slice name) const {
|
2019-01-17 21:12:31 +01:00
|
|
|
if (callback_ != nullptr) {
|
|
|
|
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
|