2019-05-09 21:27:36 +02:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2019-05-14 16:26:13 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2019-05-09 21:27:36 +02:00
|
|
|
namespace td {
|
|
|
|
|
2019-12-22 02:42:22 +01:00
|
|
|
static string get_color_hex_string(int32 color) {
|
2019-05-10 14:36:37 +02:00
|
|
|
string result;
|
|
|
|
for (int i = 20; i >= 0; i -= 4) {
|
|
|
|
result += "0123456789abcdef"[(color >> i) & 0xf];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-22 18:11:54 +01:00
|
|
|
static GradientInfo get_gradient_info(const td_api::BackgroundFill *fill) {
|
|
|
|
CHECK(fill != nullptr);
|
|
|
|
switch (fill->get_id()) {
|
|
|
|
case td_api::backgroundFillSolid::ID: {
|
|
|
|
auto solid = static_cast<const td_api::backgroundFillSolid *>(fill);
|
|
|
|
return GradientInfo(solid->color_, solid->color_);
|
|
|
|
}
|
|
|
|
case td_api::backgroundFillGradient::ID: {
|
|
|
|
auto gradient = static_cast<const td_api::backgroundFillGradient *>(fill);
|
|
|
|
return GradientInfo(gradient->top_color_, gradient->bottom_color_);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 02:34:39 +01:00
|
|
|
bool operator==(const GradientInfo &lhs, const GradientInfo &rhs) {
|
|
|
|
return lhs.top_color == rhs.top_color && lhs.bottom_color == rhs.bottom_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
string BackgroundType::get_link() const {
|
|
|
|
string mode;
|
|
|
|
if (is_blurred) {
|
|
|
|
mode = "blur";
|
|
|
|
}
|
|
|
|
if (is_moving) {
|
|
|
|
if (!mode.empty()) {
|
|
|
|
mode += '+';
|
|
|
|
}
|
|
|
|
mode += "motion";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case BackgroundType::Type::Wallpaper: {
|
|
|
|
if (!mode.empty()) {
|
|
|
|
return PSTRING() << "mode=" << mode;
|
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
case BackgroundType::Type::Pattern: {
|
2019-12-22 02:42:22 +01:00
|
|
|
string link = PSTRING() << "intensity=" << intensity << "&bg_color=" << get_color_hex_string(color);
|
2019-12-22 02:34:39 +01:00
|
|
|
if (!mode.empty()) {
|
|
|
|
link += "&mode=";
|
|
|
|
link += mode;
|
|
|
|
}
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
case BackgroundType::Type::Solid:
|
2019-12-22 02:42:22 +01:00
|
|
|
return get_color_hex_string(color);
|
2019-12-22 02:34:39 +01:00
|
|
|
case BackgroundType::Type::Gradient:
|
2019-12-22 02:42:22 +01:00
|
|
|
return PSTRING() << get_color_hex_string(gradient.top_color) << '-'
|
|
|
|
<< get_color_hex_string(gradient.bottom_color);
|
2019-12-22 02:34:39 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
bool operator==(const BackgroundType &lhs, const BackgroundType &rhs) {
|
|
|
|
return lhs.type == rhs.type && lhs.is_blurred == rhs.is_blurred && lhs.is_moving == rhs.is_moving &&
|
2019-12-22 02:34:39 +01:00
|
|
|
lhs.color == rhs.color && lhs.intensity == rhs.intensity && lhs.gradient == rhs.gradient;
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 02:34:39 +01:00
|
|
|
static StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType::Type &type) {
|
|
|
|
switch (type) {
|
2019-05-10 14:36:37 +02:00
|
|
|
case BackgroundType::Type::Wallpaper:
|
2019-12-22 02:34:39 +01:00
|
|
|
return string_builder << "Wallpaper";
|
2019-05-10 14:36:37 +02:00
|
|
|
case BackgroundType::Type::Pattern:
|
2019-12-22 02:34:39 +01:00
|
|
|
return string_builder << "Pattern";
|
2019-05-10 14:36:37 +02:00
|
|
|
case BackgroundType::Type::Solid:
|
2019-12-22 02:34:39 +01:00
|
|
|
return string_builder << "Solid";
|
2019-12-21 03:28:07 +01:00
|
|
|
case BackgroundType::Type::Gradient:
|
2019-12-22 02:34:39 +01:00
|
|
|
return string_builder << "Gradient";
|
2019-05-10 14:36:37 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 02:34:39 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) {
|
|
|
|
return string_builder << "type " << type.type << '[' << type.get_link() << ']';
|
|
|
|
}
|
|
|
|
|
2019-12-22 02:02:39 +01:00
|
|
|
static bool is_valid_color(int32 color) {
|
|
|
|
return 0 <= color && color <= 0xFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_valid_intensity(int32 intensity) {
|
|
|
|
return 0 <= intensity && intensity <= 100;
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:27:36 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-12-22 18:11:54 +01:00
|
|
|
case td_api::backgroundTypeFill::ID: {
|
|
|
|
auto fill = static_cast<const td_api::backgroundTypeFill *>(type);
|
|
|
|
if (fill->fill_ == nullptr) {
|
|
|
|
return Status::Error(400, "Fill info must not be empty");
|
2019-12-22 01:17:52 +01:00
|
|
|
}
|
2019-12-22 18:11:54 +01:00
|
|
|
result = BackgroundType(get_gradient_info(fill->fill_.get()));
|
2019-12-21 03:28:07 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-05-09 21:27:36 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2019-12-22 02:02:39 +01:00
|
|
|
if (!is_valid_intensity(result.intensity)) {
|
|
|
|
return Status::Error(400, "Wrong intensity value");
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
2019-12-22 02:02:39 +01:00
|
|
|
if (!is_valid_color(result.color)) {
|
2019-05-09 21:27:36 +02:00
|
|
|
return Status::Error(400, "Wrong color value");
|
|
|
|
}
|
2019-12-22 02:02:39 +01:00
|
|
|
if (!is_valid_color(result.gradient.top_color)) {
|
|
|
|
return Status::Error(400, "Wrong top color value");
|
|
|
|
}
|
|
|
|
if (!is_valid_color(result.gradient.bottom_color)) {
|
|
|
|
return Status::Error(400, "Wrong bottom color value");
|
|
|
|
}
|
2019-05-09 21:27:36 +02:00
|
|
|
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_;
|
2019-12-22 02:02:39 +01:00
|
|
|
if (!is_valid_color(color)) {
|
2019-05-09 21:27:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(settings);
|
|
|
|
color = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((flags & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
|
|
|
|
intensity = settings->intensity_;
|
2019-12-22 02:02:39 +01:00
|
|
|
if (!is_valid_intensity(intensity)) {
|
2019-05-09 21:27:36 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(settings);
|
|
|
|
intensity = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_pattern) {
|
|
|
|
return BackgroundType(is_moving, color, intensity);
|
|
|
|
} else {
|
|
|
|
return BackgroundType(is_blurred, is_moving);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 18:11:54 +01:00
|
|
|
static td_api::object_ptr<td_api::BackgroundFill> get_background_fill_object(int32 color) {
|
|
|
|
return td_api::make_object<td_api::backgroundFillSolid>(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
static td_api::object_ptr<td_api::BackgroundFill> get_background_fill_object(const GradientInfo &gradient) {
|
|
|
|
if (gradient.is_solid()) {
|
|
|
|
return get_background_fill_object(gradient.top_color);
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::backgroundFillGradient>(gradient.top_color, gradient.bottom_color);
|
2019-12-22 01:17:52 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 21:27:36 +02:00
|
|
|
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:
|
2019-12-22 18:11:54 +01:00
|
|
|
return td_api::make_object<td_api::backgroundTypeFill>(get_background_fill_object(type.color));
|
2019-12-21 03:28:07 +01:00
|
|
|
case BackgroundType::Type::Gradient:
|
2019-12-22 18:11:54 +01:00
|
|
|
return td_api::make_object<td_api::backgroundTypeFill>(get_background_fill_object(type.gradient));
|
2019-05-09 21:27:36 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::wallPaperSettings> get_input_wallpaper_settings(const BackgroundType &type) {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (type.is_blurred) {
|
|
|
|
flags |= telegram_api::wallPaperSettings::BLUR_MASK;
|
|
|
|
}
|
|
|
|
if (type.is_moving) {
|
|
|
|
flags |= telegram_api::wallPaperSettings::MOTION_MASK;
|
|
|
|
}
|
|
|
|
if (type.color != 0) {
|
|
|
|
flags |= telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK;
|
|
|
|
}
|
|
|
|
if (type.intensity) {
|
|
|
|
flags |= telegram_api::wallPaperSettings::INTENSITY_MASK;
|
|
|
|
}
|
2019-12-22 02:34:39 +01:00
|
|
|
if (type.is_server()) {
|
|
|
|
return telegram_api::make_object<telegram_api::wallPaperSettings>(flags, false /*ignored*/, false /*ignored*/,
|
|
|
|
type.color, 0, type.intensity, 0);
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
2019-12-22 02:34:39 +01:00
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 21:27:36 +02:00
|
|
|
} // namespace td
|