2019-05-07 04:51:56 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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"
|
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/Photo.h"
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
#include <memory>
|
2019-05-07 04:51:56 +02:00
|
|
|
#include <unordered_map>
|
2019-07-19 04:34:49 +02:00
|
|
|
#include <unordered_set>
|
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
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
static Result<string> get_background_url(const string &name,
|
|
|
|
td_api::object_ptr<td_api::BackgroundType> background_type);
|
2019-05-07 16:10:20 +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
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
BackgroundId set_background(const td_api::InputBackground *input_background,
|
2019-06-18 00:39:57 +02:00
|
|
|
const td_api::BackgroundType *background_type, bool for_dark_theme,
|
|
|
|
Promise<Unit> &&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);
|
|
|
|
|
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);
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void on_uploaded_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
2019-12-22 00:56:51 +01:00
|
|
|
telegram_api::object_ptr<telegram_api::WallPaper> wallpaper,
|
2019-05-10 14:36:37 +02:00
|
|
|
Promise<Unit> &&promise);
|
|
|
|
|
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);
|
|
|
|
|
2021-06-09 18:01:16 +02:00
|
|
|
BackgroundId set_background(BackgroundId background_id, BackgroundType type, bool for_dark_theme,
|
2019-06-18 00:39:57 +02:00
|
|
|
Promise<Unit> &&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,
|
|
|
|
Result<Unit> &&result, Promise<Unit> &&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);
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme, Promise<Unit> &&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);
|
|
|
|
|
2019-06-18 00:39:57 +02:00
|
|
|
void do_upload_background_file(FileId file_id, const BackgroundType &type, bool for_dark_theme,
|
2019-05-10 14:36:37 +02:00
|
|
|
tl_object_ptr<telegram_api::InputFile> &&input_file, Promise<Unit> &&promise);
|
|
|
|
|
2019-05-09 03:07:21 +02:00
|
|
|
std::unordered_map<BackgroundId, Background, BackgroundIdHash> backgrounds_;
|
|
|
|
|
|
|
|
std::unordered_map<BackgroundId, std::pair<int64, FileSourceId>, BackgroundIdHash>
|
|
|
|
background_id_to_file_source_id_; // id -> [access_hash, file_source_id]
|
2019-05-07 04:51:56 +02:00
|
|
|
|
2019-05-07 17:21:57 +02:00
|
|
|
std::unordered_map<string, BackgroundId> name_to_background_id_;
|
|
|
|
|
2019-05-10 14:36:37 +02:00
|
|
|
std::unordered_map<FileId, BackgroundId, FileIdHash> file_id_to_background_id_;
|
|
|
|
|
2019-07-19 04:34:49 +02:00
|
|
|
std::unordered_set<string> loaded_from_database_backgrounds_;
|
|
|
|
std::unordered_map<string, vector<Promise<Unit>>> being_loaded_from_database_backgrounds_;
|
|
|
|
|
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 {
|
|
|
|
BackgroundType type;
|
2019-06-18 00:39:57 +02:00
|
|
|
bool for_dark_theme;
|
2019-05-10 14:36:37 +02:00
|
|
|
Promise<Unit> promise;
|
|
|
|
};
|
|
|
|
std::unordered_map<FileId, UploadedFileInfo, FileIdHash> being_uploaded_files_;
|
|
|
|
|
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
|