Add BackgroundType.{cpp,h}.

GitOrigin-RevId: 1945d764af4b818505960eb82cf5781fb9df973f
This commit is contained in:
levlam 2019-05-09 22:27:36 +03:00
parent 90ab672030
commit 8e669d3c2e
5 changed files with 137 additions and 108 deletions

View File

@ -355,6 +355,7 @@ set(TDLIB_SOURCE
td/telegram/AudiosManager.cpp td/telegram/AudiosManager.cpp
td/telegram/AuthManager.cpp td/telegram/AuthManager.cpp
td/telegram/BackgroundManager.cpp td/telegram/BackgroundManager.cpp
td/telegram/BackgroundType.cpp
td/telegram/CallActor.cpp td/telegram/CallActor.cpp
td/telegram/CallDiscardReason.cpp td/telegram/CallDiscardReason.cpp
td/telegram/CallManager.cpp td/telegram/CallManager.cpp
@ -485,6 +486,7 @@ set(TDLIB_SOURCE
td/telegram/AuthManager.h td/telegram/AuthManager.h
td/telegram/BackgroundId.h td/telegram/BackgroundId.h
td/telegram/BackgroundManager.h td/telegram/BackgroundManager.h
td/telegram/BackgroundType.h
td/telegram/CallActor.h td/telegram/CallActor.h
td/telegram/CallDiscardReason.h td/telegram/CallDiscardReason.h
td/telegram/CallId.h td/telegram/CallId.h

View File

@ -103,44 +103,9 @@ void BackgroundManager::get_backgrounds(Promise<Unit> &&promise) {
} }
} }
Result<BackgroundManager::BackgroundType> BackgroundManager::get_background_type(
td_api::object_ptr<td_api::BackgroundType> type) {
if (type == nullptr) {
return Status::Error(400, "Type must not be empty");
}
BackgroundType result;
switch (type->get_id()) {
case td_api::backgroundTypeWallpaper::ID: {
auto wallpaper = td_api::move_object_as<td_api::backgroundTypeWallpaper>(type);
result = BackgroundType(wallpaper->is_blurred_, wallpaper->is_moving_);
break;
}
case td_api::backgroundTypePattern::ID: {
auto pattern = td_api::move_object_as<td_api::backgroundTypePattern>(type);
result = BackgroundType(pattern->is_moving_, pattern->color_, pattern->intensity_);
break;
}
case td_api::backgroundTypeSolid::ID: {
auto solid = td_api::move_object_as<td_api::backgroundTypeSolid>(type);
result = BackgroundType(solid->color_);
break;
}
default:
UNREACHABLE();
}
if (result.intensity < 0 || result.intensity > 100) {
return Status::Error(400, "Wrong intensity value");
}
if (result.color < 0 || result.color > 0xFFFFFF) {
return Status::Error(400, "Wrong color value");
}
return result;
}
Result<string> BackgroundManager::get_background_url(const string &name, Result<string> BackgroundManager::get_background_url(const string &name,
td_api::object_ptr<td_api::BackgroundType> background_type) const { td_api::object_ptr<td_api::BackgroundType> background_type) const {
TRY_RESULT(type, get_background_type(std::move(background_type))); TRY_RESULT(type, get_background_type(background_type.get()));
vector<string> modes; vector<string> modes;
if (type.is_blurred) { if (type.is_blurred) {
@ -246,38 +211,6 @@ const BackgroundManager::Background *BackgroundManager::get_background(Backgroun
} }
} }
BackgroundManager::BackgroundType BackgroundManager::get_background_type(
bool is_pattern, telegram_api::object_ptr<telegram_api::wallPaperSettings> settings) {
bool is_blurred = false;
bool is_moving = false;
int32 color = 0;
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::BACKGROUND_COLOR_MASK) != 0) {
color = settings->background_color_;
if (color < 0 || color > 0xFFFFFF) {
LOG(ERROR) << "Receive " << to_string(settings);
color = 0;
}
}
if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
intensity = settings->intensity_;
if (intensity < 0 || intensity > 100) {
LOG(ERROR) << "Receive " << to_string(settings);
intensity = 0;
}
}
}
if (is_pattern) {
return BackgroundType(is_moving, color, intensity);
} else {
return BackgroundType(is_blurred, is_moving);
}
}
BackgroundId BackgroundManager::on_get_background(BackgroundId expected_background_id, BackgroundId BackgroundManager::on_get_background(BackgroundId expected_background_id,
telegram_api::object_ptr<telegram_api::wallPaper> wallpaper) { telegram_api::object_ptr<telegram_api::wallPaper> wallpaper) {
CHECK(wallpaper != nullptr); CHECK(wallpaper != nullptr);
@ -379,20 +312,6 @@ void BackgroundManager::on_get_backgrounds(Result<telegram_api::object_ptr<teleg
} }
} }
td_api::object_ptr<td_api::BackgroundType> BackgroundManager::get_background_type_object(const BackgroundType &type) {
switch (type.type) {
case BackgroundType::Type::Wallpaper:
return td_api::make_object<td_api::backgroundTypeWallpaper>(type.is_blurred, type.is_moving);
case BackgroundType::Type::Pattern:
return td_api::make_object<td_api::backgroundTypePattern>(type.is_moving, type.color, type.intensity);
case BackgroundType::Type::Solid:
return td_api::make_object<td_api::backgroundTypeSolid>(type.color);
default:
UNREACHABLE();
return nullptr;
}
}
td_api::object_ptr<td_api::background> BackgroundManager::get_background_object(BackgroundId background_id) const { td_api::object_ptr<td_api::background> BackgroundManager::get_background_object(BackgroundId background_id) const {
auto background = get_background(background_id); auto background = get_background(background_id);
if (background == nullptr) { if (background == nullptr) {

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#include "td/telegram/BackgroundId.h" #include "td/telegram/BackgroundId.h"
#include "td/telegram/BackgroundType.h"
#include "td/telegram/files/FileId.h" #include "td/telegram/files/FileId.h"
#include "td/telegram/files/FileSourceId.h" #include "td/telegram/files/FileSourceId.h"
#include "td/telegram/Photo.h" #include "td/telegram/Photo.h"
@ -48,25 +49,6 @@ class BackgroundManager : public Actor {
FileSourceId get_background_file_source_id(BackgroundId background_id, int64 access_hash); FileSourceId get_background_file_source_id(BackgroundId background_id, int64 access_hash);
private: private:
struct BackgroundType {
enum class Type : int32 { Wallpaper, Pattern, Solid };
Type type = Type::Solid;
bool is_blurred = false;
bool is_moving = false;
int32 color = 0;
int32 intensity = 0;
BackgroundType() = default;
BackgroundType(bool is_blurred, bool is_moving)
: type(Type::Wallpaper), is_blurred(is_blurred), is_moving(is_moving) {
}
BackgroundType(bool is_moving, int32 color, int32 intensity)
: type(Type::Pattern), is_moving(is_moving), color(color), intensity(intensity) {
}
explicit BackgroundType(int32 color) : type(Type::Solid), color(color) {
}
};
struct Background { struct Background {
BackgroundId id; BackgroundId id;
int64 access_hash = 0; int64 access_hash = 0;
@ -91,15 +73,8 @@ class BackgroundManager : public Actor {
const Background *get_background(BackgroundId background_id) const; const Background *get_background(BackgroundId background_id) const;
static Result<BackgroundType> get_background_type(td_api::object_ptr<td_api::BackgroundType> type);
static BackgroundType get_background_type(bool is_pattern,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings);
void on_get_backgrounds(Result<telegram_api::object_ptr<telegram_api::account_WallPapers>> result); void on_get_backgrounds(Result<telegram_api::object_ptr<telegram_api::account_WallPapers>> result);
static td_api::object_ptr<td_api::BackgroundType> get_background_type_object(const BackgroundType &type);
std::unordered_map<BackgroundId, Background, BackgroundIdHash> backgrounds_; std::unordered_map<BackgroundId, Background, BackgroundIdHash> backgrounds_;
std::unordered_map<BackgroundId, std::pair<int64, FileSourceId>, BackgroundIdHash> std::unordered_map<BackgroundId, std::pair<int64, FileSourceId>, BackgroundIdHash>

View File

@ -0,0 +1,91 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
//
// 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/BackgroundType.h"
namespace td {
Result<BackgroundType> get_background_type(const td_api::BackgroundType *type) {
if (type == nullptr) {
return Status::Error(400, "Type must not be empty");
}
BackgroundType result;
switch (type->get_id()) {
case td_api::backgroundTypeWallpaper::ID: {
auto wallpaper = static_cast<const td_api::backgroundTypeWallpaper *>(type);
result = BackgroundType(wallpaper->is_blurred_, wallpaper->is_moving_);
break;
}
case td_api::backgroundTypePattern::ID: {
auto pattern = static_cast<const td_api::backgroundTypePattern *>(type);
result = BackgroundType(pattern->is_moving_, pattern->color_, pattern->intensity_);
break;
}
case td_api::backgroundTypeSolid::ID: {
auto solid = static_cast<const td_api::backgroundTypeSolid *>(type);
result = BackgroundType(solid->color_);
break;
}
default:
UNREACHABLE();
}
if (result.intensity < 0 || result.intensity > 100) {
return Status::Error(400, "Wrong intensity value");
}
if (result.color < 0 || result.color > 0xFFFFFF) {
return Status::Error(400, "Wrong color value");
}
return result;
}
BackgroundType get_background_type(bool is_pattern,
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings) {
bool is_blurred = false;
bool is_moving = false;
int32 color = 0;
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::BACKGROUND_COLOR_MASK) != 0) {
color = settings->background_color_;
if (color < 0 || color > 0xFFFFFF) {
LOG(ERROR) << "Receive " << to_string(settings);
color = 0;
}
}
if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
intensity = settings->intensity_;
if (intensity < 0 || intensity > 100) {
LOG(ERROR) << "Receive " << to_string(settings);
intensity = 0;
}
}
}
if (is_pattern) {
return BackgroundType(is_moving, color, intensity);
} else {
return BackgroundType(is_blurred, is_moving);
}
}
td_api::object_ptr<td_api::BackgroundType> get_background_type_object(const BackgroundType &type) {
switch (type.type) {
case BackgroundType::Type::Wallpaper:
return td_api::make_object<td_api::backgroundTypeWallpaper>(type.is_blurred, type.is_moving);
case BackgroundType::Type::Pattern:
return td_api::make_object<td_api::backgroundTypePattern>(type.is_moving, type.color, type.intensity);
case BackgroundType::Type::Solid:
return td_api::make_object<td_api::backgroundTypeSolid>(type.color);
default:
UNREACHABLE();
return nullptr;
}
}
} // namespace td

View File

@ -0,0 +1,42 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
//
// 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/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/common.h"
#include "td/utils/Status.h"
namespace td {
struct BackgroundType {
enum class Type : int32 { Wallpaper, Pattern, Solid };
Type type = Type::Solid;
bool is_blurred = false;
bool is_moving = false;
int32 color = 0;
int32 intensity = 0;
BackgroundType() = default;
BackgroundType(bool is_blurred, bool is_moving)
: type(Type::Wallpaper), is_blurred(is_blurred), is_moving(is_moving) {
}
BackgroundType(bool is_moving, int32 color, int32 intensity)
: type(Type::Pattern), is_moving(is_moving), color(color), intensity(intensity) {
}
explicit BackgroundType(int32 color) : type(Type::Solid), color(color) {
}
};
Result<BackgroundType> get_background_type(const td_api::BackgroundType *type);
BackgroundType get_background_type(bool is_pattern, telegram_api::object_ptr<telegram_api::wallPaperSettings> settings);
td_api::object_ptr<td_api::BackgroundType> get_background_type_object(const BackgroundType &type);
} // namespace td