From 125556f76650897c9c33c9c0c659ef9fb36a9d4e Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Fri, 26 Aug 2022 22:32:27 +0300 Subject: [PATCH] [Option][JSON] Custom auto-replaces --- .../SourceFiles/kotato/kotato_settings.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Telegram/SourceFiles/kotato/kotato_settings.cpp b/Telegram/SourceFiles/kotato/kotato_settings.cpp index 240985d60..502adae17 100644 --- a/Telegram/SourceFiles/kotato/kotato_settings.cpp +++ b/Telegram/SourceFiles/kotato/kotato_settings.cpp @@ -154,6 +154,33 @@ CheckHandler IntLimitMin(int min) { }; } +CheckHandler ReplacesLimit() { + return [=] (QVariant value) -> QVariant { + auto newArrayValue = QJsonArray(); + if (value.canConvert()) { + auto arrayValue = value.toJsonArray(); + for (auto i = arrayValue.begin(); i != arrayValue.end(); ++i) { + if (!(*i).isArray()) { + continue; + } + + const auto a = (*i).toArray(); + if (a.size() != 2 || !a.at(0).isString() || !a.at(1).isString()) { + continue; + } + + const auto from = a.at(0).toString(); + const auto to = a.at(1).toString(); + Ui::AddCustomReplacement(from, to); + + newArrayValue.append(a); + } + } + + return newArrayValue; + }; +} + struct Definition { SettingScope scope = SettingScope::Global; @@ -252,6 +279,9 @@ const std::map> DefinitionMap { { "disable_up_edit", { .type = SettingType::BoolSetting, .defaultValue = false, }}, + { "replaces", { + .type = SettingType::QJsonArraySetting, + .limitHandler = ReplacesLimit(), }}, }; using OldOptionKey = QString;