-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileManager.cpp
55 lines (48 loc) · 1.92 KB
/
FileManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "FileManager.hpp"
#include "td/telegram/td_api.h"
#include "td/tl/TlObject.h"
namespace tdcurses {
FileManager &file_manager() {
static FileManager file_manager;
return file_manager;
}
td::int64 FileManager::subscribe_to_file_updates(td::int64 file_id, FileUpdatedCallback callback) {
auto id = ++last_subscription_id_;
subscriptions_[file_id].emplace(id, callback);
return id;
}
void FileManager::unsubscribe_from_file_updates(td::int64 file_id, td::int64 subscription_id) {
auto it = subscriptions_.find(file_id);
CHECK(it != subscriptions_.end());
CHECK(it->second.erase(subscription_id));
if (!it->second.size()) {
subscriptions_.erase(it);
}
}
void FileManager::process_update(const td::td_api::updateFile &update) {
auto it = subscriptions_.find(update.file_->id_);
if (it != subscriptions_.end()) {
auto copy = it->second;
for (auto &x : copy) {
x.second(update);
}
}
}
td::tl_object_ptr<td::td_api::file> clone_td_file(const td::td_api::file &file_in) {
td::tl_object_ptr<td::td_api::localFile> l;
if (file_in.local_) {
l = td::make_tl_object<td::td_api::localFile>(
file_in.local_->path_, file_in.local_->can_be_downloaded_, file_in.local_->can_be_deleted_,
file_in.local_->is_downloading_active_, file_in.local_->is_downloading_completed_,
file_in.local_->download_offset_, file_in.local_->downloaded_prefix_size_, file_in.local_->downloaded_size_);
}
td::tl_object_ptr<td::td_api::remoteFile> r;
if (file_in.remote_) {
r = td::make_tl_object<td::td_api::remoteFile>(
file_in.remote_->id_, file_in.remote_->unique_id_, file_in.remote_->is_uploading_active_,
file_in.remote_->is_uploading_completed_, file_in.remote_->uploaded_size_);
}
return td::make_tl_object<td::td_api::file>(file_in.id_, file_in.size_, file_in.expected_size_, std::move(l),
std::move(r));
}
} // namespace tdcurses