2019-05-07 04:51:56 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-05-07 04:51:56 +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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/BackgroundId.h"
|
2019-05-09 21:27:36 +02:00
|
|
|
#include "td/telegram/BackgroundType.h"
|
2023-04-11 16:46:55 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/files/FileSourceId.h"
|
2021-09-24 15:03:01 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2022-03-11 19:38:48 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2019-05-07 04:51:56 +02:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
#include <memory>
|
2019-05-14 16:26:13 +02:00
|
|
|
#include <utility>
|
2019-05-07 04:51:56 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Td;
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class BackgroundManager final : public Actor {
|
2019-05-07 04:51:56 +02:00
|
|
|
public:
|
|
|
|
BackgroundManager(Td *td, ActorShared<> parent);
|
|
|
|
|
2021-09-03 08:07:18 +02:00
|
|
|
void get_backgrounds(bool for_dark_theme, Promise<td_api::object_ptr<td_api::backgrounds>> &&promise);
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
void reload_background(BackgroundId background_id, int64 access_hash, Promise<Unit> &&promise);
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
std::pair<BackgroundId, BackgroundType> search_background(const string &name, Promise<Unit> &&promise);
|
2019-05-07 17:21:57 +02:00
|
|
|
|
2022-11-17 16:48:48 +01:00
|
|
|
void set_background(const td_api::InputBackground *input_background, const td_api::BackgroundType *background_type,
|
|
|
|
bool for_dark_theme, Promise<td_api::object_ptr<td_api::background>> &&promise);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2019-05-10 22:55:26 +02:00
|
|
|
void remove_background(BackgroundId background_id, Promise<Unit> &&promise);
|
|
|
|
|
2019-05-12 03:10:18 +02:00
|
|
|
void reset_backgrounds(Promise<Unit> &&promise);
|
|
|
|
|
2023-04-11 16:46:55 +02:00
|
|
|
void set_dialog_background(DialogId dialog_id, const td_api::InputBackground *input_background,
|
2023-04-11 23:59:53 +02:00
|
|
|
const td_api::BackgroundType *background_type, int32 dark_theme_dimming,
|
2023-04-11 16:46:55 +02:00
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
2021-05-26 19:17:05 +02:00
|
|
|
td_api::object_ptr<td_api::background> get_background_object(BackgroundId background_id, bool for_dark_theme,
|
2021-08-24 16:13:51 +02:00
|
|
|
const BackgroundType *type) const;
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
std::pair<BackgroundId, BackgroundType> on_get_background(
|
|
|
|
BackgroundId expected_background_id, const string &expected_background_name,
|
2021-08-27 11:10:51 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper_ptr, bool replace_type);
|
2019-05-07 17:21:57 +02:00
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
FileSourceId get_background_file_source_id(BackgroundId background_id, int64 access_hash);
|
|
|
|
|
2023-04-11 16:46:55 +02:00
|
|
|
void on_uploaded_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id, bool for_dark_theme,
|
2019-12-22 00:56:51 +01:00
|
|
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2019-05-10 18:04:14 +02:00
|
|
|
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
|
|
|
|
|
2021-09-24 15:03:01 +02:00
|
|
|
void store_background(BackgroundId background_id, LogEventStorerCalcLength &storer);
|
|
|
|
|
|
|
|
void store_background(BackgroundId background_id, LogEventStorerUnsafe &storer);
|
|
|
|
|
|
|
|
void parse_background(BackgroundId &background_id, LogEventParser &parser);
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
private:
|
|
|
|
struct Background {
|
|
|
|
BackgroundId id;
|
|
|
|
int64 access_hash = 0;
|
|
|
|
string name;
|
|
|
|
FileId file_id;
|
|
|
|
bool is_creator = false;
|
|
|
|
bool is_default = false;
|
|
|
|
bool is_dark = false;
|
2021-06-10 02:57:34 +02:00
|
|
|
bool has_new_local_id = true;
|
2019-05-07 04:51:56 +02:00
|
|
|
BackgroundType type;
|
2019-05-09 03:07:21 +02:00
|
|
|
FileSourceId file_source_id;
|
2019-07-18 21:12:46 +02:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
2019-05-07 04:51:56 +02:00
|
|
|
};
|
|
|
|
|
2019-05-10 16:58:44 +02:00
|
|
|
class BackgroundLogEvent;
|
2021-06-10 17:58:19 +02:00
|
|
|
class BackgroundsLogEvent;
|
2019-05-10 16:58:44 +02:00
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
class UploadBackgroundFileCallback;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
2019-05-10 16:58:44 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final;
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
static string get_background_database_key(bool for_dark_theme);
|
2019-05-10 16:58:44 +02:00
|
|
|
|
2021-06-10 17:58:19 +02:00
|
|
|
static string get_local_backgrounds_database_key(bool for_dark_theme);
|
|
|
|
|
|
|
|
void save_background_id(bool for_dark_theme);
|
|
|
|
|
|
|
|
void save_local_backgrounds(bool for_dark_theme);
|
2019-05-10 16:58:44 +02:00
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
void reload_background_from_server(BackgroundId background_id, const string &background_name,
|
2019-05-09 03:07:21 +02:00
|
|
|
telegram_api::object_ptr<telegram_api::InputWallPaper> &&input_wallpaper,
|
|
|
|
Promise<Unit> &&promise) const;
|
2019-05-07 17:21:57 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
td_api::object_ptr<td_api::updateSelectedBackground> get_update_selected_background_object(bool for_dark_theme) const;
|
2019-05-10 17:55:55 +02:00
|
|
|
|
2021-09-03 08:07:18 +02:00
|
|
|
td_api::object_ptr<td_api::backgrounds> get_backgrounds_object(bool for_dark_theme) const;
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void send_update_selected_background(bool for_dark_theme) const;
|
2019-05-10 17:55:55 +02:00
|
|
|
|
2021-06-10 02:57:34 +02:00
|
|
|
void set_max_local_background_id(BackgroundId background_id);
|
|
|
|
|
|
|
|
BackgroundId get_next_local_background_id();
|
|
|
|
|
2021-06-10 03:44:39 +02:00
|
|
|
BackgroundId add_local_background(const BackgroundType &type);
|
2019-05-10 15:06:53 +02:00
|
|
|
|
2021-08-27 11:10:51 +02:00
|
|
|
void add_background(const Background &background, bool replace_type);
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
Background *get_background_ref(BackgroundId background_id);
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
const Background *get_background(BackgroundId background_id) const;
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
static string get_background_name_database_key(const string &name);
|
|
|
|
|
|
|
|
void on_load_background_from_database(string name, string value);
|
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
void on_get_backgrounds(Result<telegram_api::object_ptr<telegram_api::account_WallPapers>> result);
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
Result<FileId> prepare_input_file(const tl_object_ptr<td_api::InputFile> &input_file);
|
|
|
|
|
2023-04-11 16:46:55 +02:00
|
|
|
void do_set_dialog_background(DialogId dialog_id, BackgroundId background_id, BackgroundType type,
|
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void send_set_dialog_background_query(DialogId dialog_id,
|
|
|
|
telegram_api::object_ptr<telegram_api::InputWallPaper> input_wallpaper,
|
|
|
|
telegram_api::object_ptr<telegram_api::wallPaperSettings> settings,
|
2023-04-11 23:02:42 +02:00
|
|
|
MessageId old_message_id, Promise<Unit> &&promise);
|
2023-04-11 16:46:55 +02:00
|
|
|
|
2022-11-17 16:48:48 +01:00
|
|
|
void set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void on_installed_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
2022-11-17 16:48:48 +01:00
|
|
|
Result<Unit> &&result, Promise<td_api::object_ptr<td_api::background>> &&promise);
|
2019-05-11 22:27:17 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void set_background_id(BackgroundId background_id, const BackgroundType &type, bool for_dark_theme);
|
2019-05-11 22:27:17 +02:00
|
|
|
|
2019-05-10 22:55:26 +02:00
|
|
|
void on_removed_background(BackgroundId background_id, Result<Unit> &&result, Promise<Unit> &&promise);
|
|
|
|
|
2019-05-12 03:10:18 +02:00
|
|
|
void on_reset_background(Result<Unit> &&result, Promise<Unit> &&promise);
|
|
|
|
|
2023-04-11 16:46:55 +02:00
|
|
|
void upload_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id, bool for_dark_theme,
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
|
|
|
void on_upload_background_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file);
|
|
|
|
|
|
|
|
void on_upload_background_file_error(FileId file_id, Status status);
|
|
|
|
|
2023-04-11 16:46:55 +02:00
|
|
|
void do_upload_background_file(FileId file_id, const BackgroundType &type, DialogId dialog_id, bool for_dark_theme,
|
2022-11-17 16:48:48 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFile> &&input_file,
|
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise);
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2022-02-11 17:27:32 +01:00
|
|
|
FlatHashMap<BackgroundId, unique_ptr<Background>, BackgroundIdHash> backgrounds_;
|
2019-05-09 03:07:21 +02:00
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<BackgroundId, std::pair<int64, FileSourceId>, BackgroundIdHash>
|
2023-01-16 10:59:43 +01:00
|
|
|
background_id_to_file_source_id_; // background_id -> [access_hash, file_source_id]
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, BackgroundId> name_to_background_id_;
|
2019-05-07 17:21:57 +02:00
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<FileId, BackgroundId, FileIdHash> file_id_to_background_id_;
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<string> loaded_from_database_backgrounds_;
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, vector<Promise<Unit>>> being_loaded_from_database_backgrounds_;
|
2019-07-19 04:34:49 +02:00
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
BackgroundId set_background_id_[2];
|
|
|
|
BackgroundType set_background_type_[2];
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2021-08-24 16:13:51 +02:00
|
|
|
vector<std::pair<BackgroundId, BackgroundType>> installed_backgrounds_;
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2021-09-03 08:07:18 +02:00
|
|
|
vector<std::pair<bool, Promise<td_api::object_ptr<td_api::backgrounds>>>> pending_get_backgrounds_queries_;
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
std::shared_ptr<UploadBackgroundFileCallback> upload_background_file_callback_;
|
|
|
|
|
|
|
|
struct UploadedFileInfo {
|
2021-11-11 15:39:09 +01:00
|
|
|
BackgroundType type_;
|
2023-04-11 16:46:55 +02:00
|
|
|
DialogId dialog_id_;
|
2021-11-11 15:39:09 +01:00
|
|
|
bool for_dark_theme_;
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> promise_;
|
2021-11-11 15:39:09 +01:00
|
|
|
|
2023-04-11 16:46:55 +02:00
|
|
|
UploadedFileInfo(BackgroundType type, DialogId dialog_id, bool for_dark_theme,
|
2022-11-17 16:48:48 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::background>> &&promise)
|
2023-04-11 16:46:55 +02:00
|
|
|
: type_(type), dialog_id_(dialog_id), for_dark_theme_(for_dark_theme), promise_(std::move(promise)) {
|
2021-11-11 15:39:09 +01:00
|
|
|
}
|
2019-05-10 14:36:37 +02:00
|
|
|
};
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<FileId, UploadedFileInfo, FileIdHash> being_uploaded_files_;
|
2019-05-10 14:36:37 +02:00
|
|
|
|
2021-06-10 02:57:34 +02:00
|
|
|
BackgroundId max_local_background_id_;
|
2021-06-10 17:58:19 +02:00
|
|
|
vector<BackgroundId> local_background_ids_[2];
|
2021-06-10 02:57:34 +02:00
|
|
|
|
2019-05-07 04:51:56 +02:00
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|