From 7b2c6bdbdb4137a74ff22f2960391bbaadb54eb5 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 9 Jun 2021 21:19:08 +0300 Subject: [PATCH] Move BackgroundType methods inside the class. --- td/telegram/BackgroundManager.cpp | 12 ++-- td/telegram/BackgroundType.cpp | 104 ++++++++++++++---------------- td/telegram/BackgroundType.h | 16 ++--- 3 files changed, 62 insertions(+), 70 deletions(-) diff --git a/td/telegram/BackgroundManager.cpp b/td/telegram/BackgroundManager.cpp index 0b1c5c732..ff40be34b 100644 --- a/td/telegram/BackgroundManager.cpp +++ b/td/telegram/BackgroundManager.cpp @@ -110,7 +110,7 @@ class InstallBackgroundQuery : public Td::ResultHandler { void send(telegram_api::object_ptr input_wallpaper, const BackgroundType &type) { send_query(G()->net_query_creator().create( - telegram_api::account_installWallPaper(std::move(input_wallpaper), get_input_wallpaper_settings(type)))); + telegram_api::account_installWallPaper(std::move(input_wallpaper), type.get_input_wallpaper_settings()))); } void on_result(uint64 id, BufferSlice packet) override { @@ -145,7 +145,7 @@ class UploadBackgroundQuery : public Td::ResultHandler { type_ = type; for_dark_theme_ = for_dark_theme; send_query(G()->net_query_creator().create(telegram_api::account_uploadWallPaper( - std::move(input_file), type_.get_mime_type(), get_input_wallpaper_settings(type)))); + std::move(input_file), type_.get_mime_type(), type.get_input_wallpaper_settings()))); } void on_result(uint64 id, BufferSlice packet) override { @@ -358,7 +358,7 @@ void BackgroundManager::get_backgrounds(Promise &&promise) { Result BackgroundManager::get_background_url(const string &name, td_api::object_ptr background_type) const { - TRY_RESULT(type, get_background_type(background_type.get())); + TRY_RESULT(type, BackgroundType::get_background_type(background_type.get())); auto url = PSTRING() << G()->shared_config().get_option_string("t_me_url", "https://t.me/") << "bg/"; auto link = type.get_link(); if (type.has_file()) { @@ -530,7 +530,7 @@ BackgroundId BackgroundManager::set_background(const td_api::InputBackground *in Promise &&promise) { BackgroundType type; if (background_type != nullptr) { - auto r_type = get_background_type(background_type); + auto r_type = BackgroundType::get_background_type(background_type); if (r_type.is_error()) { promise.set_error(r_type.move_as_error()); return BackgroundId(); @@ -991,7 +991,7 @@ BackgroundId BackgroundManager::on_get_background(BackgroundId expected_backgrou background.is_creator = (flags & telegram_api::wallPaper::CREATOR_MASK) != 0; background.is_default = (flags & telegram_api::wallPaper::DEFAULT_MASK) != 0; background.is_dark = (flags & telegram_api::wallPaper::DARK_MASK) != 0; - background.type = get_background_type(is_pattern, std::move(wallpaper->settings_)); + background.type = BackgroundType(is_pattern, std::move(wallpaper->settings_)); background.name = std::move(wallpaper->slug_); background.file_id = document.file_id; add_background(background); @@ -1069,7 +1069,7 @@ td_api::object_ptr BackgroundManager::get_background_object( return td_api::make_object( background->id.get(), background->is_default, background->is_dark, background->name, td_->documents_manager_->get_document_object(background->file_id, PhotoFormat::Png), - get_background_type_object(*type)); + type->get_background_type_object()); } td_api::object_ptr BackgroundManager::get_backgrounds_object(bool for_dark_theme) const { diff --git a/td/telegram/BackgroundType.cpp b/td/telegram/BackgroundType.cpp index f54b94a7c..b875a5813 100644 --- a/td/telegram/BackgroundType.cpp +++ b/td/telegram/BackgroundType.cpp @@ -359,62 +359,55 @@ StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &t return string_builder << '[' << type.get_link() << ']'; } -Result get_background_type(const td_api::BackgroundType *type) { - if (type == nullptr) { +Result BackgroundType::get_background_type(const td_api::BackgroundType *background_type) { + if (background_type == nullptr) { return Status::Error(400, "Type must be non-empty"); } - BackgroundType result; - switch (type->get_id()) { + switch (background_type->get_id()) { case td_api::backgroundTypeWallpaper::ID: { - auto wallpaper = static_cast(type); - result = BackgroundType(wallpaper->is_blurred_, wallpaper->is_moving_); - break; + auto wallpaper_type = static_cast(background_type); + return BackgroundType(wallpaper_type->is_blurred_, wallpaper_type->is_moving_); } case td_api::backgroundTypePattern::ID: { - auto pattern = static_cast(type); - TRY_RESULT(background_fill, get_background_fill(pattern->fill_.get())); - if (!is_valid_intensity(pattern->intensity_)) { + auto pattern_type = static_cast(background_type); + TRY_RESULT(background_fill, get_background_fill(pattern_type->fill_.get())); + if (!is_valid_intensity(pattern_type->intensity_)) { return Status::Error(400, "Wrong intensity value"); } - result = BackgroundType(pattern->is_moving_, std::move(background_fill), pattern->intensity_); - break; + return BackgroundType(pattern_type->is_moving_, std::move(background_fill), pattern_type->intensity_); } case td_api::backgroundTypeFill::ID: { - auto fill = static_cast(type); - TRY_RESULT(background_fill, get_background_fill(fill->fill_.get())); - result = BackgroundType(std::move(background_fill)); - break; + auto fill_type = static_cast(background_type); + TRY_RESULT(background_fill, get_background_fill(fill_type->fill_.get())); + return BackgroundType(std::move(background_fill)); } default: UNREACHABLE(); + return BackgroundType(); } - return result; } -BackgroundType get_background_type(bool is_pattern, - telegram_api::object_ptr settings) { - bool is_blurred = false; - bool is_moving = false; - BackgroundFill fill(settings.get()); - int32 intensity = 0; - if (settings) { - auto flags = settings->flags_; - is_blurred = (flags & telegram_api::wallPaperSettings::BLUR_MASK) != 0; - is_moving = (flags & telegram_api::wallPaperSettings::MOTION_MASK) != 0; - - if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) { - intensity = settings->intensity_; - if (!is_valid_intensity(intensity)) { - LOG(ERROR) << "Receive " << to_string(settings); - intensity = 50; +BackgroundType::BackgroundType(bool is_pattern, telegram_api::object_ptr settings) { + if (is_pattern) { + type = Type::Pattern; + if (settings) { + fill = BackgroundFill(settings.get()); + is_moving = (settings->flags_ & telegram_api::wallPaperSettings::MOTION_MASK) != 0; + if ((settings->flags_ & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) { + intensity = settings->intensity_; + if (!is_valid_intensity(intensity)) { + LOG(ERROR) << "Receive " << to_string(settings); + intensity = 50; + } } } - } - if (is_pattern) { - return BackgroundType(is_moving, fill, intensity); } else { - return BackgroundType(is_blurred, is_moving); + type = Type::Wallpaper; + if (settings) { + is_blurred = (settings->flags_ & telegram_api::wallPaperSettings::BLUR_MASK) != 0; + is_moving = (settings->flags_ & telegram_api::wallPaperSettings::MOTION_MASK) != 0; + } } } @@ -438,34 +431,33 @@ static td_api::object_ptr get_background_fill_object(con } } -td_api::object_ptr get_background_type_object(const BackgroundType &type) { - switch (type.type) { - case BackgroundType::Type::Wallpaper: - return td_api::make_object(type.is_blurred, type.is_moving); - case BackgroundType::Type::Pattern: - return td_api::make_object(get_background_fill_object(type.fill), type.intensity, - type.is_moving); - case BackgroundType::Type::Fill: - return td_api::make_object(get_background_fill_object(type.fill)); +td_api::object_ptr BackgroundType::get_background_type_object() const { + switch (type) { + case Type::Wallpaper: + return td_api::make_object(is_blurred, is_moving); + case Type::Pattern: + return td_api::make_object(get_background_fill_object(fill), intensity, is_moving); + case Type::Fill: + return td_api::make_object(get_background_fill_object(fill)); default: UNREACHABLE(); return nullptr; } } -telegram_api::object_ptr get_input_wallpaper_settings(const BackgroundType &type) { - CHECK(type.has_file()); +telegram_api::object_ptr BackgroundType::get_input_wallpaper_settings() const { + CHECK(has_file()); int32 flags = 0; - if (type.is_blurred) { + if (is_blurred) { flags |= telegram_api::wallPaperSettings::BLUR_MASK; } - if (type.is_moving) { + if (is_moving) { flags |= telegram_api::wallPaperSettings::MOTION_MASK; } - switch (type.fill.get_type()) { + switch (fill.get_type()) { case BackgroundFill::Type::FreeformGradient: - if (type.fill.fourth_color != -1) { + if (fill.fourth_color != -1) { flags |= telegram_api::wallPaperSettings::FOURTH_BACKGROUND_COLOR_MASK; } flags |= telegram_api::wallPaperSettings::THIRD_BACKGROUND_COLOR_MASK; @@ -479,12 +471,12 @@ telegram_api::object_ptr get_input_wallpaper_se default: UNREACHABLE(); } - if (type.intensity != 0) { + if (intensity != 0) { flags |= telegram_api::wallPaperSettings::INTENSITY_MASK; } - return telegram_api::make_object( - flags, false /*ignored*/, false /*ignored*/, type.fill.top_color, type.fill.bottom_color, type.fill.third_color, - type.fill.fourth_color, type.intensity, type.fill.rotation_angle); + return telegram_api::make_object(flags, false /*ignored*/, false /*ignored*/, + fill.top_color, fill.bottom_color, fill.third_color, + fill.fourth_color, intensity, fill.rotation_angle); } } // namespace td diff --git a/td/telegram/BackgroundType.h b/td/telegram/BackgroundType.h index 702335afc..1601ae6e1 100644 --- a/td/telegram/BackgroundType.h +++ b/td/telegram/BackgroundType.h @@ -75,6 +75,10 @@ struct BackgroundType { explicit BackgroundType(BackgroundFill fill) : type(Type::Fill), fill(fill) { } + BackgroundType(bool is_pattern, telegram_api::object_ptr settings); + + static Result get_background_type(const td_api::BackgroundType *background_type); + bool has_file() const { return type == Type::Wallpaper || type == Type::Pattern; } @@ -84,18 +88,14 @@ struct BackgroundType { void apply_parameters_from_link(Slice name); string get_link() const; + + td_api::object_ptr get_background_type_object() const; + + telegram_api::object_ptr get_input_wallpaper_settings() const; }; bool operator==(const BackgroundType &lhs, const BackgroundType &rhs); StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type); -Result get_background_type(const td_api::BackgroundType *type); - -BackgroundType get_background_type(bool is_pattern, telegram_api::object_ptr settings); - -td_api::object_ptr get_background_type_object(const BackgroundType &type); - -telegram_api::object_ptr get_input_wallpaper_settings(const BackgroundType &type); - } // namespace td