2019-05-09 21:27:36 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-05-09 21:27:36 +02:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2023-03-01 14:36:34 +01:00
|
|
|
#include "td/utils/base64.h"
|
2021-05-26 19:17:05 +02:00
|
|
|
#include "td/utils/HttpUrl.h"
|
2019-05-14 16:26:13 +02:00
|
|
|
#include "td/utils/logging.h"
|
2021-05-21 00:49:59 +02:00
|
|
|
#include "td/utils/misc.h"
|
2019-12-23 18:48:30 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-05-14 16:26:13 +02:00
|
|
|
|
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) {
|
2021-05-21 00:49:59 +02:00
|
|
|
result += "0123456789abcdef"[(color >> i) & 0xF];
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-20 00:57:38 +02:00
|
|
|
static bool is_valid_color(int32 color) {
|
|
|
|
return 0 <= color && color <= 0xFFFFFF;
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:55:01 +02:00
|
|
|
static bool validate_alpha_color(int32 &color) {
|
|
|
|
if (-0x1000000 <= color && color <= 0xFFFFFF) {
|
|
|
|
color &= 0xFFFFFF;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
color = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:49:59 +02:00
|
|
|
static bool is_valid_rotation_angle(int32 rotation_angle) {
|
|
|
|
return 0 <= rotation_angle && rotation_angle < 360 && rotation_angle % 45 == 0;
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:53:59 +02:00
|
|
|
static bool is_valid_intensity(int32 intensity, bool allow_negative) {
|
|
|
|
return (allow_negative ? -100 : 0) <= intensity && intensity <= 100;
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:49:59 +02:00
|
|
|
BackgroundFill::BackgroundFill(const telegram_api::wallPaperSettings *settings) {
|
|
|
|
if (settings == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = settings->flags_;
|
2023-12-19 14:00:50 +01:00
|
|
|
if ((flags & telegram_api::wallPaperSettings::EMOTICON_MASK) != 0) {
|
|
|
|
LOG(ERROR) << "Receive filled background with " << to_string(*settings);
|
|
|
|
}
|
2021-05-21 00:49:59 +02:00
|
|
|
if ((flags & telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK) != 0) {
|
2021-06-10 00:44:46 +02:00
|
|
|
top_color_ = settings->background_color_;
|
2021-09-13 14:55:01 +02:00
|
|
|
if (!validate_alpha_color(top_color_)) {
|
2021-05-21 00:49:59 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(*settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((flags & telegram_api::wallPaperSettings::FOURTH_BACKGROUND_COLOR_MASK) != 0 ||
|
|
|
|
(flags & telegram_api::wallPaperSettings::THIRD_BACKGROUND_COLOR_MASK) != 0) {
|
2021-06-10 00:44:46 +02:00
|
|
|
bottom_color_ = settings->second_background_color_;
|
2021-09-13 14:55:01 +02:00
|
|
|
if (!validate_alpha_color(bottom_color_)) {
|
2021-05-21 00:49:59 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(*settings);
|
|
|
|
}
|
2021-06-10 00:44:46 +02:00
|
|
|
third_color_ = settings->third_background_color_;
|
2021-09-13 14:55:01 +02:00
|
|
|
if (!validate_alpha_color(third_color_)) {
|
2021-05-21 00:49:59 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(*settings);
|
|
|
|
}
|
|
|
|
if ((flags & telegram_api::wallPaperSettings::FOURTH_BACKGROUND_COLOR_MASK) != 0) {
|
2021-06-10 00:44:46 +02:00
|
|
|
fourth_color_ = settings->fourth_background_color_;
|
2021-09-13 14:55:01 +02:00
|
|
|
if (!validate_alpha_color(fourth_color_)) {
|
2021-05-21 00:49:59 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(*settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((flags & telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_MASK) != 0) {
|
2021-06-10 00:44:46 +02:00
|
|
|
bottom_color_ = settings->second_background_color_;
|
2021-09-13 14:55:01 +02:00
|
|
|
if (!validate_alpha_color(bottom_color_)) {
|
2021-05-21 00:49:59 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(*settings);
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:44:46 +02:00
|
|
|
rotation_angle_ = settings->rotation_;
|
|
|
|
if (!is_valid_rotation_angle(rotation_angle_)) {
|
2021-05-21 00:49:59 +02:00
|
|
|
LOG(ERROR) << "Receive " << to_string(*settings);
|
2021-06-10 00:44:46 +02:00
|
|
|
rotation_angle_ = 0;
|
2021-05-21 00:49:59 +02:00
|
|
|
}
|
2022-11-02 05:21:20 +01:00
|
|
|
} else {
|
|
|
|
bottom_color_ = top_color_;
|
2021-05-21 00:49:59 +02:00
|
|
|
}
|
2024-05-02 17:42:08 +02:00
|
|
|
if (get_type() != Type::Gradient) {
|
|
|
|
rotation_angle_ = 0;
|
|
|
|
}
|
2021-05-21 00:49:59 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 01:16:46 +02:00
|
|
|
Result<BackgroundFill> BackgroundFill::get_background_fill(const td_api::BackgroundFill *fill) {
|
2021-05-20 00:57:38 +02:00
|
|
|
if (fill == nullptr) {
|
|
|
|
return Status::Error(400, "Background fill info must be non-empty");
|
|
|
|
}
|
2019-12-22 18:11:54 +01:00
|
|
|
switch (fill->get_id()) {
|
|
|
|
case td_api::backgroundFillSolid::ID: {
|
|
|
|
auto solid = static_cast<const td_api::backgroundFillSolid *>(fill);
|
2021-05-20 00:57:38 +02:00
|
|
|
if (!is_valid_color(solid->color_)) {
|
|
|
|
return Status::Error(400, "Invalid solid fill color value");
|
|
|
|
}
|
2019-12-22 19:01:51 +01:00
|
|
|
return BackgroundFill(solid->color_);
|
2019-12-22 18:11:54 +01:00
|
|
|
}
|
|
|
|
case td_api::backgroundFillGradient::ID: {
|
|
|
|
auto gradient = static_cast<const td_api::backgroundFillGradient *>(fill);
|
2021-05-20 00:57:38 +02:00
|
|
|
if (!is_valid_color(gradient->top_color_)) {
|
|
|
|
return Status::Error(400, "Invalid top gradient color value");
|
|
|
|
}
|
|
|
|
if (!is_valid_color(gradient->bottom_color_)) {
|
|
|
|
return Status::Error(400, "Invalid bottom gradient color value");
|
|
|
|
}
|
2021-05-21 00:49:59 +02:00
|
|
|
if (!is_valid_rotation_angle(gradient->rotation_angle_)) {
|
2021-05-20 00:57:38 +02:00
|
|
|
return Status::Error(400, "Invalid rotation angle value");
|
|
|
|
}
|
2019-12-22 20:32:01 +01:00
|
|
|
return BackgroundFill(gradient->top_color_, gradient->bottom_color_, gradient->rotation_angle_);
|
2019-12-22 18:11:54 +01:00
|
|
|
}
|
2021-05-21 00:49:59 +02:00
|
|
|
case td_api::backgroundFillFreeformGradient::ID: {
|
|
|
|
auto freeform = static_cast<const td_api::backgroundFillFreeformGradient *>(fill);
|
|
|
|
if (freeform->colors_.size() != 3 && freeform->colors_.size() != 4) {
|
|
|
|
return Status::Error(400, "Wrong number of gradient colors");
|
|
|
|
}
|
|
|
|
for (auto &color : freeform->colors_) {
|
|
|
|
if (!is_valid_color(color)) {
|
|
|
|
return Status::Error(400, "Invalid freeform gradient color value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BackgroundFill(freeform->colors_[0], freeform->colors_[1], freeform->colors_[2],
|
|
|
|
freeform->colors_.size() == 3 ? -1 : freeform->colors_[3]);
|
|
|
|
}
|
2019-12-22 18:11:54 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:49:59 +02:00
|
|
|
Result<BackgroundFill> BackgroundFill::get_background_fill(Slice name) {
|
|
|
|
name = name.substr(0, name.find('#'));
|
|
|
|
|
|
|
|
Slice parameters;
|
|
|
|
auto parameters_pos = name.find('?');
|
|
|
|
if (parameters_pos != Slice::npos) {
|
|
|
|
parameters = name.substr(parameters_pos + 1);
|
|
|
|
name = name.substr(0, parameters_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto get_color = [](Slice color_string) -> Result<int32> {
|
2023-03-01 19:26:49 +01:00
|
|
|
auto r_color = hex_to_integer_safe<uint32>(url_decode(color_string, true));
|
2021-05-21 00:49:59 +02:00
|
|
|
if (r_color.is_error() || color_string.size() > 6) {
|
|
|
|
return Status::Error(400, "WALLPAPER_INVALID");
|
|
|
|
}
|
|
|
|
return static_cast<int32>(r_color.ok());
|
|
|
|
};
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
size_t hyphen_pos = name.find('-');
|
2021-05-21 00:49:59 +02:00
|
|
|
if (name.find('~') < name.size()) {
|
|
|
|
vector<Slice> color_strings = full_split(name, '~');
|
2021-05-26 19:17:05 +02:00
|
|
|
CHECK(color_strings.size() >= 2);
|
|
|
|
if (color_strings.size() == 2) {
|
|
|
|
hyphen_pos = color_strings[0].size();
|
|
|
|
} else {
|
|
|
|
if (color_strings.size() > 4) {
|
|
|
|
return Status::Error(400, "WALLPAPER_INVALID");
|
|
|
|
}
|
2021-05-21 00:49:59 +02:00
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
TRY_RESULT(first_color, get_color(color_strings[0]));
|
|
|
|
TRY_RESULT(second_color, get_color(color_strings[1]));
|
|
|
|
TRY_RESULT(third_color, get_color(color_strings[2]));
|
|
|
|
int32 fourth_color = -1;
|
|
|
|
if (color_strings.size() == 4) {
|
|
|
|
TRY_RESULT_ASSIGN(fourth_color, get_color(color_strings[3]));
|
|
|
|
}
|
|
|
|
return BackgroundFill(first_color, second_color, third_color, fourth_color);
|
2021-05-21 00:49:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hyphen_pos < name.size()) {
|
|
|
|
TRY_RESULT(top_color, get_color(name.substr(0, hyphen_pos)));
|
|
|
|
TRY_RESULT(bottom_color, get_color(name.substr(hyphen_pos + 1)));
|
|
|
|
int32 rotation_angle = 0;
|
|
|
|
|
|
|
|
Slice prefix("rotation=");
|
|
|
|
if (begins_with(parameters, prefix)) {
|
2023-03-01 19:26:49 +01:00
|
|
|
rotation_angle = to_integer<int32>(url_decode(parameters.substr(prefix.size()), true));
|
2021-05-21 00:49:59 +02:00
|
|
|
if (!is_valid_rotation_angle(rotation_angle)) {
|
|
|
|
rotation_angle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BackgroundFill(top_color, bottom_color, rotation_angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TRY_RESULT(color, get_color(name));
|
|
|
|
return BackgroundFill(color);
|
|
|
|
}
|
|
|
|
|
2021-06-10 01:16:46 +02:00
|
|
|
string BackgroundFill::get_link(bool is_first) const {
|
|
|
|
switch (get_type()) {
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::Solid:
|
2021-06-10 01:16:46 +02:00
|
|
|
return get_color_hex_string(top_color_);
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::Gradient:
|
2021-06-10 01:16:46 +02:00
|
|
|
return PSTRING() << get_color_hex_string(top_color_) << '-' << get_color_hex_string(bottom_color_)
|
|
|
|
<< (is_first ? '?' : '&') << "rotation=" << rotation_angle_;
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::FreeformGradient: {
|
2021-05-21 00:49:59 +02:00
|
|
|
SliceBuilder sb;
|
2021-06-10 01:16:46 +02:00
|
|
|
sb << get_color_hex_string(top_color_) << '~' << get_color_hex_string(bottom_color_) << '~'
|
|
|
|
<< get_color_hex_string(third_color_);
|
|
|
|
if (fourth_color_ != -1) {
|
|
|
|
sb << '~' << get_color_hex_string(fourth_color_);
|
2021-05-21 00:49:59 +02:00
|
|
|
}
|
|
|
|
return sb.as_cslice().str();
|
|
|
|
}
|
2021-05-20 01:21:20 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string();
|
2019-12-22 20:32:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 00:49:59 +02:00
|
|
|
bool BackgroundFill::is_dark() const {
|
|
|
|
switch (get_type()) {
|
|
|
|
case Type::Solid:
|
2021-06-10 00:44:46 +02:00
|
|
|
return (top_color_ & 0x808080) == 0;
|
2021-05-21 00:49:59 +02:00
|
|
|
case Type::Gradient:
|
2021-06-10 00:44:46 +02:00
|
|
|
return (top_color_ & 0x808080) == 0 && (bottom_color_ & 0x808080) == 0;
|
2021-05-21 00:49:59 +02:00
|
|
|
case Type::FreeformGradient:
|
2021-06-10 00:44:46 +02:00
|
|
|
return (top_color_ & 0x808080) == 0 && (bottom_color_ & 0x808080) == 0 && (third_color_ & 0x808080) == 0 &&
|
|
|
|
(fourth_color_ == -1 || (fourth_color_ & 0x808080) == 0);
|
2021-05-20 01:21:20 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2021-10-19 17:11:16 +02:00
|
|
|
return false;
|
2019-12-22 19:01:51 +01:00
|
|
|
}
|
2019-12-22 20:32:01 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 19:01:51 +01:00
|
|
|
bool operator==(const BackgroundFill &lhs, const BackgroundFill &rhs) {
|
2021-06-10 00:44:46 +02:00
|
|
|
return lhs.top_color_ == rhs.top_color_ && lhs.bottom_color_ == rhs.bottom_color_ &&
|
|
|
|
lhs.rotation_angle_ == rhs.rotation_angle_ && lhs.third_color_ == rhs.third_color_ &&
|
|
|
|
lhs.fourth_color_ == rhs.fourth_color_;
|
2019-12-22 02:34:39 +01:00
|
|
|
}
|
|
|
|
|
2024-05-02 17:30:21 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundFill &fill) {
|
|
|
|
return string_builder << "BackgroundFill[" << fill.top_color_ << '~' << fill.bottom_color_ << '~' << fill.third_color_
|
|
|
|
<< '~' << fill.fourth_color_ << ':' << fill.rotation_angle_ << ']';
|
|
|
|
}
|
|
|
|
|
2021-06-09 19:58:39 +02:00
|
|
|
string BackgroundType::get_mime_type() const {
|
|
|
|
CHECK(has_file());
|
2021-06-10 00:44:46 +02:00
|
|
|
return type_ == Type::Pattern ? "image/png" : "image/jpeg";
|
2021-06-09 19:58:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
void BackgroundType::apply_parameters_from_link(Slice name) {
|
|
|
|
const auto query = parse_url_query(name);
|
|
|
|
|
2021-06-10 00:44:46 +02:00
|
|
|
is_blurred_ = false;
|
|
|
|
is_moving_ = false;
|
2021-05-26 19:17:05 +02:00
|
|
|
auto modes = full_split(query.get_arg("mode"), ' ');
|
|
|
|
for (auto &mode : modes) {
|
2021-06-10 00:44:46 +02:00
|
|
|
if (type_ != Type::Pattern && to_lower(mode) == "blur") {
|
|
|
|
is_blurred_ = true;
|
2021-05-26 19:17:05 +02:00
|
|
|
}
|
|
|
|
if (to_lower(mode) == "motion") {
|
2021-06-10 00:44:46 +02:00
|
|
|
is_moving_ = true;
|
2021-05-26 19:17:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:44:46 +02:00
|
|
|
if (type_ == Type::Pattern) {
|
|
|
|
intensity_ = -101;
|
2021-05-26 19:17:05 +02:00
|
|
|
auto intensity_arg = query.get_arg("intensity");
|
|
|
|
if (!intensity_arg.empty()) {
|
2021-06-10 00:44:46 +02:00
|
|
|
intensity_ = to_integer<int32>(intensity_arg);
|
2021-05-26 19:17:05 +02:00
|
|
|
}
|
2021-09-14 11:53:59 +02:00
|
|
|
if (!is_valid_intensity(intensity_, true)) {
|
2021-06-10 00:44:46 +02:00
|
|
|
intensity_ = 50;
|
2021-05-26 19:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto bg_color = query.get_arg("bg_color");
|
|
|
|
if (!bg_color.empty()) {
|
|
|
|
auto r_fill = BackgroundFill::get_background_fill(
|
|
|
|
PSLICE() << url_encode(bg_color) << "?rotation=" << url_encode(query.get_arg("rotation")));
|
|
|
|
if (r_fill.is_ok()) {
|
2021-06-10 00:44:46 +02:00
|
|
|
fill_ = r_fill.move_as_ok();
|
2021-05-26 19:17:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 19:26:49 +01:00
|
|
|
string BackgroundType::get_link(bool is_first) const {
|
2019-12-22 02:34:39 +01:00
|
|
|
string mode;
|
2021-06-10 00:44:46 +02:00
|
|
|
if (is_blurred_) {
|
2019-12-22 02:34:39 +01:00
|
|
|
mode = "blur";
|
|
|
|
}
|
2021-06-10 00:44:46 +02:00
|
|
|
if (is_moving_) {
|
2019-12-22 02:34:39 +01:00
|
|
|
if (!mode.empty()) {
|
|
|
|
mode += '+';
|
|
|
|
}
|
|
|
|
mode += "motion";
|
|
|
|
}
|
|
|
|
|
2021-06-10 00:44:46 +02:00
|
|
|
switch (type_) {
|
2021-06-09 19:58:39 +02:00
|
|
|
case Type::Wallpaper: {
|
2019-12-22 02:34:39 +01:00
|
|
|
if (!mode.empty()) {
|
|
|
|
return PSTRING() << "mode=" << mode;
|
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
2021-06-09 19:58:39 +02:00
|
|
|
case Type::Pattern: {
|
2021-06-10 01:16:46 +02:00
|
|
|
string link = PSTRING() << "intensity=" << intensity_ << "&bg_color=" << fill_.get_link(false);
|
2019-12-22 02:34:39 +01:00
|
|
|
if (!mode.empty()) {
|
|
|
|
link += "&mode=";
|
|
|
|
link += mode;
|
|
|
|
}
|
|
|
|
return link;
|
|
|
|
}
|
2021-06-09 19:58:39 +02:00
|
|
|
case Type::Fill:
|
2023-03-01 19:26:49 +01:00
|
|
|
return fill_.get_link(is_first);
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::ChatTheme:
|
|
|
|
return string();
|
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) {
|
2021-06-10 00:44:46 +02:00
|
|
|
return lhs.type_ == rhs.type_ && lhs.is_blurred_ == rhs.is_blurred_ && lhs.is_moving_ == rhs.is_moving_ &&
|
2023-12-19 14:00:50 +01:00
|
|
|
lhs.intensity_ == rhs.intensity_ && lhs.fill_ == rhs.fill_ && lhs.theme_name_ == rhs.theme_name_;
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 19:58:39 +02:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const BackgroundType &type) {
|
|
|
|
string_builder << "type ";
|
2021-06-10 00:44:46 +02:00
|
|
|
switch (type.type_) {
|
2019-05-10 14:36:37 +02:00
|
|
|
case BackgroundType::Type::Wallpaper:
|
2021-06-09 19:58:39 +02:00
|
|
|
string_builder << "Wallpaper";
|
|
|
|
break;
|
2019-05-10 14:36:37 +02:00
|
|
|
case BackgroundType::Type::Pattern:
|
2021-06-09 19:58:39 +02:00
|
|
|
string_builder << "Pattern";
|
|
|
|
break;
|
2019-12-22 19:01:51 +01:00
|
|
|
case BackgroundType::Type::Fill:
|
2021-06-09 19:58:39 +02:00
|
|
|
string_builder << "Fill";
|
|
|
|
break;
|
2023-12-19 14:00:50 +01:00
|
|
|
case BackgroundType::Type::ChatTheme:
|
|
|
|
string_builder << "ChatTheme";
|
|
|
|
break;
|
2019-05-10 14:36:37 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2021-06-09 19:58:39 +02:00
|
|
|
break;
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
2024-05-02 17:30:21 +02:00
|
|
|
// string_builder << ' ' << type.is_blurred_ << ' ' << type.is_moving_ << ' ' << type.intensity_ << ' ' << type.fill_
|
|
|
|
// << ' ' << type.theme_name_ << ' ';
|
2021-06-09 19:58:39 +02:00
|
|
|
return string_builder << '[' << type.get_link() << ']';
|
2019-12-22 02:34:39 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 17:45:26 +02:00
|
|
|
Result<BackgroundType> BackgroundType::get_background_type(const td_api::BackgroundType *background_type,
|
2023-04-11 23:59:53 +02:00
|
|
|
int32 dark_theme_dimming) {
|
2021-06-09 20:19:08 +02:00
|
|
|
if (background_type == nullptr) {
|
2023-04-10 19:53:45 +02:00
|
|
|
return BackgroundType();
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
2023-04-11 23:59:53 +02:00
|
|
|
if (dark_theme_dimming < 0 || dark_theme_dimming > 100) {
|
2023-04-10 17:45:26 +02:00
|
|
|
return Status::Error(400, "Invalid dark them brightness specified");
|
|
|
|
}
|
2019-05-09 21:27:36 +02:00
|
|
|
|
2021-06-09 20:19:08 +02:00
|
|
|
switch (background_type->get_id()) {
|
2019-05-09 21:27:36 +02:00
|
|
|
case td_api::backgroundTypeWallpaper::ID: {
|
2021-06-09 20:19:08 +02:00
|
|
|
auto wallpaper_type = static_cast<const td_api::backgroundTypeWallpaper *>(background_type);
|
2023-04-11 23:59:53 +02:00
|
|
|
return BackgroundType(wallpaper_type->is_blurred_, wallpaper_type->is_moving_, dark_theme_dimming);
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
|
|
|
case td_api::backgroundTypePattern::ID: {
|
2021-06-09 20:19:08 +02:00
|
|
|
auto pattern_type = static_cast<const td_api::backgroundTypePattern *>(background_type);
|
2021-06-10 01:16:46 +02:00
|
|
|
TRY_RESULT(background_fill, BackgroundFill::get_background_fill(pattern_type->fill_.get()));
|
2021-09-14 11:53:59 +02:00
|
|
|
if (!is_valid_intensity(pattern_type->intensity_, false)) {
|
2021-05-20 00:57:38 +02:00
|
|
|
return Status::Error(400, "Wrong intensity value");
|
2019-12-22 21:02:36 +01:00
|
|
|
}
|
2021-09-14 11:53:59 +02:00
|
|
|
auto intensity = pattern_type->is_inverted_ ? -max(pattern_type->intensity_, 1) : pattern_type->intensity_;
|
|
|
|
return BackgroundType(pattern_type->is_moving_, std::move(background_fill), intensity);
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
2019-12-22 18:11:54 +01:00
|
|
|
case td_api::backgroundTypeFill::ID: {
|
2021-06-09 20:19:08 +02:00
|
|
|
auto fill_type = static_cast<const td_api::backgroundTypeFill *>(background_type);
|
2021-06-10 01:16:46 +02:00
|
|
|
TRY_RESULT(background_fill, BackgroundFill::get_background_fill(fill_type->fill_.get()));
|
2023-04-11 23:59:53 +02:00
|
|
|
return BackgroundType(std::move(background_fill), dark_theme_dimming);
|
2019-12-21 03:28:07 +01:00
|
|
|
}
|
2023-12-19 14:00:50 +01:00
|
|
|
case td_api::backgroundTypeChatTheme::ID: {
|
|
|
|
auto chat_theme_type = static_cast<const td_api::backgroundTypeChatTheme *>(background_type);
|
|
|
|
return BackgroundType(chat_theme_type->theme_name_);
|
|
|
|
}
|
2019-05-09 21:27:36 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2021-06-09 20:19:08 +02:00
|
|
|
return BackgroundType();
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 03:44:39 +02:00
|
|
|
Result<BackgroundType> BackgroundType::get_local_background_type(Slice name) {
|
|
|
|
TRY_RESULT(fill, BackgroundFill::get_background_fill(name));
|
2023-04-10 17:45:26 +02:00
|
|
|
return BackgroundType(std::move(fill), 0);
|
2021-06-10 03:44:39 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 14:36:34 +01:00
|
|
|
bool BackgroundType::is_background_name_local(Slice name) {
|
|
|
|
return name.size() <= 13u || name.find('?') <= 13u || !is_base64url_characters(name.substr(0, name.find('?')));
|
|
|
|
}
|
|
|
|
|
2023-12-19 14:00:50 +01:00
|
|
|
BackgroundType::BackgroundType(bool has_no_file, bool is_pattern,
|
2021-06-10 01:09:16 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings) {
|
2023-04-10 17:45:26 +02:00
|
|
|
if (settings != nullptr && (settings->flags_ & telegram_api::wallPaperSettings::INTENSITY_MASK) != 0) {
|
|
|
|
intensity_ = settings->intensity_;
|
|
|
|
if (!is_valid_intensity(intensity_, is_pattern)) {
|
|
|
|
LOG(ERROR) << "Receive " << to_string(settings);
|
|
|
|
intensity_ = is_pattern ? 50 : 0;
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 14:00:50 +01:00
|
|
|
if (has_no_file) {
|
2021-06-10 01:09:16 +02:00
|
|
|
CHECK(settings != nullptr);
|
2023-12-19 14:00:50 +01:00
|
|
|
if ((settings->flags_ & telegram_api::wallPaperSettings::EMOTICON_MASK) != 0) {
|
|
|
|
type_ = Type::ChatTheme;
|
|
|
|
theme_name_ = std::move(settings->emoticon_);
|
|
|
|
} else {
|
|
|
|
type_ = Type::Fill;
|
|
|
|
fill_ = BackgroundFill(settings.get());
|
|
|
|
}
|
2021-06-10 01:09:16 +02:00
|
|
|
} else if (is_pattern) {
|
2021-06-10 00:44:46 +02:00
|
|
|
type_ = Type::Pattern;
|
2023-04-10 17:45:26 +02:00
|
|
|
if (settings != nullptr) {
|
2021-06-10 00:44:46 +02:00
|
|
|
fill_ = BackgroundFill(settings.get());
|
|
|
|
is_moving_ = (settings->flags_ & telegram_api::wallPaperSettings::MOTION_MASK) != 0;
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-10 00:44:46 +02:00
|
|
|
type_ = Type::Wallpaper;
|
2023-04-10 17:45:26 +02:00
|
|
|
if (settings != nullptr) {
|
2021-06-10 00:44:46 +02:00
|
|
|
is_blurred_ = (settings->flags_ & telegram_api::wallPaperSettings::BLUR_MASK) != 0;
|
|
|
|
is_moving_ = (settings->flags_ & telegram_api::wallPaperSettings::MOTION_MASK) != 0;
|
2021-06-09 20:19:08 +02:00
|
|
|
}
|
2019-05-09 21:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 01:16:46 +02:00
|
|
|
td_api::object_ptr<td_api::BackgroundFill> BackgroundFill::get_background_fill_object() const {
|
|
|
|
switch (get_type()) {
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::Solid:
|
2021-06-10 01:16:46 +02:00
|
|
|
return td_api::make_object<td_api::backgroundFillSolid>(top_color_);
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::Gradient:
|
2021-06-10 01:16:46 +02:00
|
|
|
return td_api::make_object<td_api::backgroundFillGradient>(top_color_, bottom_color_, rotation_angle_);
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::FreeformGradient: {
|
2021-06-10 01:16:46 +02:00
|
|
|
vector<int32> colors{top_color_, bottom_color_, third_color_, fourth_color_};
|
2021-05-21 00:49:59 +02:00
|
|
|
if (colors.back() == -1) {
|
|
|
|
colors.pop_back();
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::backgroundFillFreeformGradient>(std::move(colors));
|
|
|
|
}
|
2021-05-20 01:21:20 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
2019-12-22 18:11:54 +01:00
|
|
|
}
|
2019-12-22 01:17:52 +01:00
|
|
|
}
|
|
|
|
|
2021-06-09 20:19:08 +02:00
|
|
|
td_api::object_ptr<td_api::BackgroundType> BackgroundType::get_background_type_object() const {
|
2021-06-10 00:44:46 +02:00
|
|
|
switch (type_) {
|
2021-06-09 20:19:08 +02:00
|
|
|
case Type::Wallpaper:
|
2021-06-10 00:44:46 +02:00
|
|
|
return td_api::make_object<td_api::backgroundTypeWallpaper>(is_blurred_, is_moving_);
|
2021-06-09 20:19:08 +02:00
|
|
|
case Type::Pattern:
|
2021-09-14 11:53:59 +02:00
|
|
|
return td_api::make_object<td_api::backgroundTypePattern>(
|
|
|
|
fill_.get_background_fill_object(), intensity_ < 0 ? -intensity_ : intensity_, intensity_ < 0, is_moving_);
|
2021-06-09 20:19:08 +02:00
|
|
|
case Type::Fill:
|
2021-06-10 01:16:46 +02:00
|
|
|
return td_api::make_object<td_api::backgroundTypeFill>(fill_.get_background_fill_object());
|
2023-12-19 14:00:50 +01:00
|
|
|
case Type::ChatTheme:
|
|
|
|
return td_api::make_object<td_api::backgroundTypeChatTheme>(theme_name_);
|
2019-05-09 21:27:36 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 20:19:08 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::wallPaperSettings> BackgroundType::get_input_wallpaper_settings() const {
|
2019-05-10 14:36:37 +02:00
|
|
|
int32 flags = 0;
|
2021-06-10 00:44:46 +02:00
|
|
|
if (is_blurred_) {
|
2019-05-10 14:36:37 +02:00
|
|
|
flags |= telegram_api::wallPaperSettings::BLUR_MASK;
|
|
|
|
}
|
2021-06-10 00:44:46 +02:00
|
|
|
if (is_moving_) {
|
2019-05-10 14:36:37 +02:00
|
|
|
flags |= telegram_api::wallPaperSettings::MOTION_MASK;
|
|
|
|
}
|
2021-06-10 00:44:46 +02:00
|
|
|
switch (fill_.get_type()) {
|
2021-05-21 00:49:59 +02:00
|
|
|
case BackgroundFill::Type::FreeformGradient:
|
2021-06-10 00:44:46 +02:00
|
|
|
if (fill_.fourth_color_ != -1) {
|
2021-05-21 00:49:59 +02:00
|
|
|
flags |= telegram_api::wallPaperSettings::FOURTH_BACKGROUND_COLOR_MASK;
|
|
|
|
}
|
|
|
|
flags |= telegram_api::wallPaperSettings::THIRD_BACKGROUND_COLOR_MASK;
|
|
|
|
// fallthrough
|
2021-05-20 01:21:20 +02:00
|
|
|
case BackgroundFill::Type::Gradient:
|
|
|
|
flags |= telegram_api::wallPaperSettings::SECOND_BACKGROUND_COLOR_MASK;
|
|
|
|
// fallthrough
|
|
|
|
case BackgroundFill::Type::Solid:
|
|
|
|
flags |= telegram_api::wallPaperSettings::BACKGROUND_COLOR_MASK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-12-22 19:01:51 +01:00
|
|
|
}
|
2023-12-19 14:00:50 +01:00
|
|
|
if (!theme_name_.empty()) {
|
|
|
|
flags |= telegram_api::wallPaperSettings::EMOTICON_MASK;
|
|
|
|
}
|
2021-06-10 00:44:46 +02:00
|
|
|
if (intensity_ != 0) {
|
2019-05-10 14:36:37 +02:00
|
|
|
flags |= telegram_api::wallPaperSettings::INTENSITY_MASK;
|
|
|
|
}
|
2021-06-10 00:44:46 +02:00
|
|
|
return telegram_api::make_object<telegram_api::wallPaperSettings>(
|
2023-06-23 16:53:38 +02:00
|
|
|
flags, is_blurred_, is_moving_, fill_.top_color_, fill_.bottom_color_, fill_.third_color_, fill_.fourth_color_,
|
2023-12-19 14:00:50 +01:00
|
|
|
intensity_, fill_.rotation_angle_, theme_name_);
|
2019-05-10 14:36:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 21:27:36 +02:00
|
|
|
} // namespace td
|