From a79ae236bc93bb7bd0e5d32d19e7e6dddbc33124 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 5 Mar 2022 03:14:31 +0300 Subject: [PATCH] Add new updates about file downloads. --- td/generate/scheme/td_api.tl | 10 +++++++++- td/telegram/DownloadManager.cpp | 19 ++++++++++++++++++- td/telegram/DownloadManager.h | 3 +++ td/telegram/DownloadManagerCallback.cpp | 14 +++++++++++++- td/telegram/DownloadManagerCallback.h | 5 +++++ td/telegram/Td.cpp | 3 +++ td/telegram/files/FileManager.cpp | 4 ++-- 7 files changed, 53 insertions(+), 5 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 041c815a0..87949dcb1 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3950,7 +3950,15 @@ updateFileGenerationStop generation_id:int64 = Update; //@downloaded_size Total downloaded size of files in the file download list, in bytes updateFileDownloads total_size:int53 total_count:int32 downloaded_size:int53 = Update; -//@description A file was removed from the file download list @file_id File identifier +//@description A file was added to the file download list. This update is sent only after file download list is loaded for the first time @file_download The added file download +updateFileAddedToDownloads file_download:fileDownload = Update; + +//@description A file download was changed. This update is sent only after file download list is loaded for the first time @file_id File identifier +//@complete_date Point in time (Unix timestamp) when the file downloading was completed; 0 if the file downloading isn't completed +//@is_paused True, if downloading of the file is paused +updateFileDownload file_id:int32 complete_date:int32 is_paused:Bool = Update; + +//@description A file was removed from the file download list. This update is sent only after file download list is loaded for the first time @file_id File identifier updateFileRemovedFromDownloads file_id:int32 = Update; //@description New call was created or information about a call was updated @call New data about a call diff --git a/td/telegram/DownloadManager.cpp b/td/telegram/DownloadManager.cpp index df76bf52b..f2f786dc1 100644 --- a/td/telegram/DownloadManager.cpp +++ b/td/telegram/DownloadManager.cpp @@ -117,7 +117,9 @@ class DownloadManagerImpl final : public DownloadManager { remove_from_database(file_info); files_.erase(download_id); - callback_->update_file_removed(file_id); + if (is_search_inited_) { + callback_->update_file_removed(file_id); + } update_counters(); on_file_viewed(download_id); @@ -306,6 +308,10 @@ class DownloadManagerImpl final : public DownloadManager { if (is_paused && file_info.is_paused != is_paused) { file_info.is_paused = is_paused; file_info.need_save_to_database = true; + + if (is_search_inited_) { + callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused); + } } }); } @@ -569,6 +575,10 @@ class DownloadManagerImpl final : public DownloadManager { actor_shared(this, it->second->link_token)); } } + if (is_search_inited_) { + callback_->update_file_added(it->second->file_id, it->second->file_source_id, it->second->created_at, + it->second->completed_at, it->second->is_paused); + } } void timeout_expired() final { @@ -613,6 +623,9 @@ class DownloadManagerImpl final : public DownloadManager { } else { callback_->start_file(file_info.internal_file_id, file_info.priority, actor_shared(this, file_info.link_token)); } + if (is_search_inited_) { + callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused); + } } void update_counters() { @@ -689,6 +702,10 @@ class DownloadManagerImpl final : public DownloadManager { if (file_info.is_counted) { unviewed_completed_download_ids_.insert(file_info.download_id); } + + if (is_search_inited_) { + callback_->update_file_changed(file_info.file_id, file_info.completed_at, file_info.is_paused); + } } if (file_info.is_counted && (is_completed(file_info) || !file_info.is_paused)) { counters_.downloaded_size += file_info.downloaded_size; diff --git a/td/telegram/DownloadManager.h b/td/telegram/DownloadManager.h index c237d130a..1ae9c8543 100644 --- a/td/telegram/DownloadManager.h +++ b/td/telegram/DownloadManager.h @@ -45,6 +45,9 @@ class DownloadManager : public Actor { public: virtual ~Callback() = default; virtual void update_counters(Counters counters) = 0; + virtual void update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date, + bool is_paused) = 0; + virtual void update_file_changed(FileId file_id, int32 complete_date, bool is_paused) = 0; virtual void update_file_removed(FileId file_id) = 0; virtual void start_file(FileId file_id, int8 priority, ActorShared download_manager) = 0; virtual void pause_file(FileId file_id) = 0; diff --git a/td/telegram/DownloadManagerCallback.cpp b/td/telegram/DownloadManagerCallback.cpp index 6f00a75ac..831eab3ba 100644 --- a/td/telegram/DownloadManagerCallback.cpp +++ b/td/telegram/DownloadManagerCallback.cpp @@ -22,6 +22,18 @@ void DownloadManagerCallback::update_counters(DownloadManager::Counters counters send_closure(td_->actor_id(td_), &Td::send_update, counters.get_update_file_downloads_object()); } +void DownloadManagerCallback::update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, + int32 complete_date, bool is_paused) { + send_closure(td_->actor_id(td_), &Td::send_update, + td_api::make_object( + get_file_download_object(file_id, file_source_id, add_date, complete_date, is_paused))); +} + +void DownloadManagerCallback::update_file_changed(FileId file_id, int32 complete_date, bool is_paused) { + send_closure(td_->actor_id(td_), &Td::send_update, + td_api::make_object(file_id.get(), complete_date, is_paused)); +} + void DownloadManagerCallback::update_file_removed(FileId file_id) { send_closure(td_->actor_id(td_), &Td::send_update, td_api::make_object(file_id.get())); @@ -73,7 +85,7 @@ std::shared_ptr DownloadManagerCallback::make_dow send_update(file_id, false); } void on_download_ok(FileId file_id) final { - send_update(file_id, true); + send_update(file_id, false); } void on_download_error(FileId file_id, Status error) final { send_update(file_id, true); diff --git a/td/telegram/DownloadManagerCallback.h b/td/telegram/DownloadManagerCallback.h index 32ca9a5da..2eb81b5a0 100644 --- a/td/telegram/DownloadManagerCallback.h +++ b/td/telegram/DownloadManagerCallback.h @@ -26,6 +26,11 @@ class DownloadManagerCallback final : public DownloadManager::Callback { void update_counters(DownloadManager::Counters counters) final; + void update_file_added(FileId file_id, FileSourceId file_source_id, int32 add_date, int32 complete_date, + bool is_paused) final; + + void update_file_changed(FileId file_id, int32 complete_date, bool is_paused) final; + void update_file_removed(FileId file_id) final; void start_file(FileId file_id, int8 priority, ActorShared download_manager) final; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 8a09dd621..53d438893 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4064,6 +4064,9 @@ void Td::send_update(tl_object_ptr &&object) { case td_api::updateChatAction::ID / 2: case td_api::updateChatFilters::ID / 2: case td_api::updateChatPosition::ID / 2: + case td_api::updateFileAddedToDownloads::ID: + case td_api::updateFileDownload::ID: + case td_api::updateFileRemovedFromDownloads::ID: LOG(ERROR) << "Sending update: " << oneline(to_string(object)); break; default: diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index a8663a262..249949bea 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -1120,7 +1120,7 @@ void FileManager::on_file_unlink(const FullLocalFileLocation &location) { auto file_id = it->second; auto file_node = get_sync_file_node(file_id); CHECK(file_node); - send_closure(G()->download_manager(), &DownloadManager::remove_file, file_node->main_file_id_, FileSourceId{}, false); + send_closure(G()->download_manager(), &DownloadManager::remove_file_if_finished, file_node->main_file_id_, FileSourceId{}, false); file_node->drop_local_location(); try_flush_node_info(file_node, "on_file_unlink"); } @@ -2146,7 +2146,7 @@ void FileManager::delete_file(FileId file_id, Promise promise, const char auto file_view = FileView(node); - send_closure(G()->download_manager(), &DownloadManager::remove_file, file_view.file_id(), FileSourceId{}, false); + send_closure(G()->download_manager(), &DownloadManager::remove_file_if_finished, file_view.file_id(), FileSourceId{}, false); // TODO review delete condition if (file_view.has_local_location()) { if (begins_with(file_view.local_location().path_, get_files_dir(file_view.get_type()))) {