From 64b2cb4e1253fdb703c39b42fb6f2dd4bfc492c5 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 10 Apr 2023 16:32:59 +0300 Subject: [PATCH] Add class BackgroundInfo. --- CMakeLists.txt | 3 ++ td/telegram/BackgroundInfo.cpp | 25 +++++++++++++++ td/telegram/BackgroundInfo.h | 56 ++++++++++++++++++++++++++++++++++ td/telegram/BackgroundInfo.hpp | 30 ++++++++++++++++++ td/telegram/MessageContent.cpp | 40 ++++++++++-------------- td/telegram/ThemeManager.cpp | 27 ++++++---------- td/telegram/ThemeManager.h | 6 ++-- 7 files changed, 141 insertions(+), 46 deletions(-) create mode 100644 td/telegram/BackgroundInfo.cpp create mode 100644 td/telegram/BackgroundInfo.h create mode 100644 td/telegram/BackgroundInfo.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fe7ab5b7a..c27d2fa40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -293,6 +293,7 @@ set(TDLIB_SOURCE td/telegram/AuthManager.cpp td/telegram/AutoDownloadSettings.cpp td/telegram/AutosaveManager.cpp + td/telegram/BackgroundInfo.cpp td/telegram/BackgroundManager.cpp td/telegram/BackgroundType.cpp td/telegram/BotCommand.cpp @@ -526,6 +527,7 @@ set(TDLIB_SOURCE td/telegram/AutoDownloadSettings.h td/telegram/AutosaveManager.h td/telegram/BackgroundId.h + td/telegram/BackgroundInfo.h td/telegram/BackgroundManager.h td/telegram/BackgroundType.h td/telegram/BotCommand.h @@ -776,6 +778,7 @@ set(TDLIB_SOURCE td/telegram/AnimationsManager.hpp td/telegram/AudiosManager.hpp td/telegram/AuthManager.hpp + td/telegram/BackgroundInfo.hpp td/telegram/BackgroundType.hpp td/telegram/DialogNotificationSettings.hpp td/telegram/DialogFilter.hpp diff --git a/td/telegram/BackgroundInfo.cpp b/td/telegram/BackgroundInfo.cpp new file mode 100644 index 000000000..c085869a5 --- /dev/null +++ b/td/telegram/BackgroundInfo.cpp @@ -0,0 +1,25 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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/BackgroundInfo.h" + +#include "td/telegram/BackgroundManager.h" +#include "td/telegram/Td.h" + +namespace td { + +BackgroundInfo::BackgroundInfo(Td *td, telegram_api::object_ptr &&wallpaper_ptr) { + auto background = + td->background_manager_->on_get_background(BackgroundId(), string(), std::move(wallpaper_ptr), false); + background_id_ = background.first; + background_type_ = std::move(background.second); +} + +td_api::object_ptr BackgroundInfo::get_background_object(const Td *td) const { + return td->background_manager_->get_background_object(background_id_, false, &background_type_); +} + +} // namespace td diff --git a/td/telegram/BackgroundInfo.h b/td/telegram/BackgroundInfo.h new file mode 100644 index 000000000..17a5c396d --- /dev/null +++ b/td/telegram/BackgroundInfo.h @@ -0,0 +1,56 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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) +// +#pragma once + +#include "td/telegram/BackgroundId.h" +#include "td/telegram/BackgroundType.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class Td; + +class BackgroundInfo { + BackgroundId background_id_; + BackgroundType background_type_; + + friend StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundInfo &background_info); + + public: + BackgroundInfo() : background_id_(), background_type_() { + } + + BackgroundInfo(Td *td, telegram_api::object_ptr &&wallpaper_ptr); + + td_api::object_ptr get_background_object(const Td *td) const; + + bool is_valid() const { + return background_id_.is_valid(); + } + + bool operator==(const BackgroundInfo &other) const { + return background_id_ == other.background_id_ && background_type_ == other.background_type_; + } + + bool operator!=(const BackgroundInfo &other) const { + return !(*this == other); + } + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); +}; + +inline StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundInfo &background_info) { + return string_builder << background_info.background_id_ << " with type " << background_info.background_type_; +} + +} // namespace td diff --git a/td/telegram/BackgroundInfo.hpp b/td/telegram/BackgroundInfo.hpp new file mode 100644 index 000000000..c91ffc300 --- /dev/null +++ b/td/telegram/BackgroundInfo.hpp @@ -0,0 +1,30 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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) +// +#pragma once + +#include "td/telegram/BackgroundInfo.h" +#include "td/telegram/BackgroundManager.h" +#include "td/telegram/BackgroundType.hpp" +#include "td/telegram/Td.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void BackgroundInfo::store(StorerT &storer) const { + storer.context()->td().get_actor_unsafe()->background_manager_->store_background(background_id_, storer); + td::store(background_type_, storer); +} + +template +void BackgroundInfo::parse(ParserT &parser) { + parser.context()->td().get_actor_unsafe()->background_manager_->parse_background(background_id_, parser); + td::parse(background_type_, parser); +} + +} // namespace td diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 489cf9079..9bc5adc9d 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -11,9 +11,8 @@ #include "td/telegram/AudiosManager.h" #include "td/telegram/AudiosManager.hpp" #include "td/telegram/AuthManager.h" -#include "td/telegram/BackgroundId.h" -#include "td/telegram/BackgroundManager.h" -#include "td/telegram/BackgroundType.h" +#include "td/telegram/BackgroundInfo.h" +#include "td/telegram/BackgroundInfo.hpp" #include "td/telegram/CallDiscardReason.h" #include "td/telegram/ChannelId.h" #include "td/telegram/ChatId.h" @@ -902,12 +901,11 @@ class MessageWebViewWriteAccessAllowed final : public MessageContent { class MessageSetBackground final : public MessageContent { public: MessageId old_message_id; - BackgroundId background_id; - BackgroundType background_type; + BackgroundInfo background_info; MessageSetBackground() = default; - MessageSetBackground(MessageId old_message_id, BackgroundId background_id, BackgroundType background_type) - : old_message_id(old_message_id), background_id(background_id), background_type(std::move(background_type)) { + MessageSetBackground(MessageId old_message_id, BackgroundInfo background_info) + : old_message_id(old_message_id), background_info(std::move(background_info)) { } MessageContentType get_type() const final { @@ -1304,8 +1302,7 @@ static void store(const MessageContent *content, StorerT &storer) { if (has_message_id) { store(m->old_message_id, storer); } - storer.context()->td().get_actor_unsafe()->background_manager_->store_background(m->background_id, storer); - store(m->background_type, storer); + store(m->background_info, storer); break; } default: @@ -1835,8 +1832,7 @@ static void parse(unique_ptr &content, ParserT &parser) { if (has_message_id) { parse(m->old_message_id, parser); } - parser.context()->td().get_actor_unsafe()->background_manager_->parse_background(m->background_id, parser); - parse(m->background_type, parser); + parse(m->background_info, parser); content = std::move(m); break; } @@ -4038,8 +4034,7 @@ void merge_message_contents(Td *td, const MessageContent *old_content, MessageCo case MessageContentType::SetBackground: { const auto *old_ = static_cast(old_content); const auto *new_ = static_cast(new_content); - if (old_->old_message_id != new_->old_message_id || old_->background_id != new_->background_id || - old_->background_type != new_->background_type) { + if (old_->old_message_id != new_->old_message_id || old_->background_info != new_->background_info) { need_update = true; } break; @@ -5527,12 +5522,11 @@ unique_ptr get_action_message_content(Td *td, tl_object_ptr(action_ptr); - auto background = - td->background_manager_->on_get_background(BackgroundId(), string(), std::move(action->wallpaper_), false); - if (!background.first.is_valid()) { + BackgroundInfo background_info(td, std::move(action->wallpaper_)); + if (!background_info.is_valid()) { break; } - return make_unique(MessageId(), background.first, background.second); + return make_unique(MessageId(), std::move(background_info)); } case telegram_api::messageActionSetSameChatWallPaper::ID: { if (reply_in_dialog_id.is_valid() && reply_in_dialog_id != owner_dialog_id) { @@ -5542,12 +5536,11 @@ unique_ptr get_action_message_content(Td *td, tl_object_ptr(action_ptr); - auto background = - td->background_manager_->on_get_background(BackgroundId(), string(), std::move(action->wallpaper_), false); - if (!background.first.is_valid()) { + BackgroundInfo background_info(td, std::move(action->wallpaper_)); + if (!background_info.is_valid()) { break; } - return make_unique(reply_to_message_id, background.first, background.second); + return make_unique(reply_to_message_id, std::move(background_info)); } default: UNREACHABLE(); @@ -5878,9 +5871,8 @@ tl_object_ptr get_message_content_object(const MessageCo } case MessageContentType::SetBackground: { const auto *m = static_cast(content); - return td_api::make_object( - m->old_message_id.get(), - td->background_manager_->get_background_object(m->background_id, false, &m->background_type)); + return td_api::make_object(m->old_message_id.get(), + m->background_info.get_background_object(td)); } default: UNREACHABLE(); diff --git a/td/telegram/ThemeManager.cpp b/td/telegram/ThemeManager.cpp index ddff878c0..b02783100 100644 --- a/td/telegram/ThemeManager.cpp +++ b/td/telegram/ThemeManager.cpp @@ -7,8 +7,7 @@ #include "td/telegram/ThemeManager.h" #include "td/telegram/AuthManager.h" -#include "td/telegram/BackgroundManager.h" -#include "td/telegram/BackgroundType.hpp" +#include "td/telegram/BackgroundInfo.hpp" #include "td/telegram/Global.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/net/NetQueryCreator.h" @@ -54,9 +53,8 @@ class GetChatThemesQuery final : public Td::ResultHandler { bool operator==(const ThemeManager::ThemeSettings &lhs, const ThemeManager::ThemeSettings &rhs) { return lhs.accent_color == rhs.accent_color && lhs.message_accent_color == rhs.message_accent_color && - lhs.background_id == rhs.background_id && lhs.background_type == rhs.background_type && - lhs.base_theme == rhs.base_theme && lhs.message_colors == rhs.message_colors && - lhs.animate_message_colors == rhs.animate_message_colors; + lhs.background_info == rhs.background_info && lhs.base_theme == rhs.base_theme && + lhs.message_colors == rhs.message_colors && lhs.animate_message_colors == rhs.animate_message_colors; } bool operator!=(const ThemeManager::ThemeSettings &lhs, const ThemeManager::ThemeSettings &rhs) { @@ -67,7 +65,7 @@ template void ThemeManager::ThemeSettings::store(StorerT &storer) const { using td::store; bool has_message_accent_color = message_accent_color != accent_color; - bool has_background = background_id.is_valid(); + bool has_background = background_info.is_valid(); BEGIN_STORE_FLAGS(); STORE_FLAG(animate_message_colors); STORE_FLAG(has_message_accent_color); @@ -78,8 +76,7 @@ void ThemeManager::ThemeSettings::store(StorerT &storer) const { store(message_accent_color, storer); } if (has_background) { - storer.context()->td().get_actor_unsafe()->background_manager_->store_background(background_id, storer); - store(background_type, storer); + store(background_info, storer); } store(base_theme, storer); store(message_colors, storer); @@ -102,8 +99,7 @@ void ThemeManager::ThemeSettings::parse(ParserT &parser) { message_accent_color = accent_color; } if (has_background) { - parser.context()->td().get_actor_unsafe()->background_manager_->parse_background(background_id, parser); - parse(background_type, parser); + parse(background_info, parser); } parse(base_theme, parser); parse(message_colors, parser); @@ -304,9 +300,8 @@ td_api::object_ptr ThemeManager::get_theme_settings_objec // ignore settings.base_theme for now return td_api::make_object( - settings.accent_color, - td_->background_manager_->get_background_object(settings.background_id, false, &settings.background_type), - std::move(fill), settings.animate_message_colors, settings.message_accent_color); + settings.accent_color, settings.background_info.get_background_object(td_), std::move(fill), + settings.animate_message_colors, settings.message_accent_color); } td_api::object_ptr ThemeManager::get_chat_theme_object(const ChatTheme &theme) const { @@ -415,14 +410,10 @@ ThemeManager::ThemeSettings ThemeManager::get_chat_theme_settings( telegram_api::object_ptr settings) { ThemeSettings result; if (settings != nullptr && !settings->message_colors_.empty() && settings->message_colors_.size() <= 4) { - auto background = - td_->background_manager_->on_get_background(BackgroundId(), string(), std::move(settings->wallpaper_), false); - result.accent_color = settings->accent_color_; bool has_outbox_accent_color = (settings->flags_ & telegram_api::themeSettings::OUTBOX_ACCENT_COLOR_MASK) != 0; result.message_accent_color = (has_outbox_accent_color ? settings->outbox_accent_color_ : result.accent_color); - result.background_id = background.first; - result.background_type = std::move(background.second); + result.background_info = BackgroundInfo(td_, std::move(settings->wallpaper_)); result.base_theme = get_base_theme(settings->base_theme_); result.message_colors = std::move(settings->message_colors_); result.animate_message_colors = settings->message_colors_animated_; diff --git a/td/telegram/ThemeManager.h b/td/telegram/ThemeManager.h index 610b908cd..831bc53d8 100644 --- a/td/telegram/ThemeManager.h +++ b/td/telegram/ThemeManager.h @@ -6,8 +6,7 @@ // #pragma once -#include "td/telegram/BackgroundId.h" -#include "td/telegram/BackgroundType.h" +#include "td/telegram/BackgroundInfo.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" @@ -43,8 +42,7 @@ class ThemeManager final : public Actor { struct ThemeSettings { int32 accent_color = 0; int32 message_accent_color = 0; - BackgroundId background_id; - BackgroundType background_type; + BackgroundInfo background_info; BaseTheme base_theme = BaseTheme::Classic; vector message_colors; bool animate_message_colors = false;