2022-02-23 19:34:09 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
|
|
|
//
|
|
|
|
// 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
|
2022-02-25 14:18:23 +01:00
|
|
|
|
2022-02-23 19:34:09 +01:00
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/files/FileSourceId.h"
|
2022-02-25 13:11:10 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
2022-02-25 14:18:23 +01:00
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2022-02-23 19:34:09 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-02-25 14:18:23 +01:00
|
|
|
#include "td/utils/Status.h"
|
2022-02-26 19:55:12 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2022-02-23 19:34:09 +01:00
|
|
|
|
|
|
|
namespace td {
|
2022-02-25 14:18:23 +01:00
|
|
|
|
|
|
|
class DownloadManager : public Actor {
|
2022-02-23 19:34:09 +01:00
|
|
|
public:
|
|
|
|
// creates, but do not stats the actor
|
|
|
|
static td::unique_ptr<DownloadManager> create();
|
|
|
|
|
|
|
|
struct Counters {
|
|
|
|
int64 total_size{};
|
|
|
|
int32 total_count{};
|
|
|
|
int64 downloaded_size{};
|
2022-02-25 15:57:08 +01:00
|
|
|
|
2022-02-26 19:55:12 +01:00
|
|
|
bool operator==(const Counters &other) const {
|
|
|
|
return total_size == other.total_size && total_count == other.total_count && downloaded_size == other.downloaded_size;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:57:08 +01:00
|
|
|
tl_object_ptr<td_api::updateFileDownloads> to_td_api() const;
|
2022-02-26 19:55:12 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const;
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser);
|
2022-02-23 19:34:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FileDownload {
|
|
|
|
FileId file_id;
|
|
|
|
FileSourceId file_source_id;
|
|
|
|
bool is_paused{};
|
|
|
|
int32 add_date{};
|
|
|
|
int32 complete_date{};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FoundFileDownloads {
|
|
|
|
int32 total_count{};
|
2022-02-25 14:18:23 +01:00
|
|
|
vector<FileDownload> file_downloads;
|
|
|
|
string offset;
|
2022-02-25 13:11:10 +01:00
|
|
|
tl_object_ptr<td_api::foundFileDownloads> to_td_api() const;
|
2022-02-23 19:34:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Trying to make DownloadManager testable, so all interactions with G() will be hidden is this probably monstrous interface
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
virtual void update_counters(Counters counters) = 0;
|
|
|
|
virtual void start_file(FileId file_id, int8 priority) = 0;
|
|
|
|
virtual void pause_file(FileId file_id) = 0;
|
|
|
|
virtual void delete_file(FileId file_id) = 0;
|
|
|
|
virtual FileId dup_file_id(FileId file_id) = 0;
|
|
|
|
|
2022-02-25 14:18:23 +01:00
|
|
|
virtual string get_unique_file_id(FileId file_id) = 0;
|
2022-02-23 19:34:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// public interface for user
|
|
|
|
//
|
|
|
|
|
|
|
|
// sets callback to handle all updates
|
|
|
|
virtual void set_callback(unique_ptr<Callback> callback) = 0;
|
|
|
|
|
|
|
|
virtual Status toggle_is_paused(FileId, bool is_paused) = 0;
|
2022-02-25 13:11:10 +01:00
|
|
|
virtual Status toggle_all_is_paused(bool is_paused) = 0;
|
|
|
|
virtual Status remove_file(FileId file_id, FileSourceId file_source_id, bool delete_from_cache) = 0;
|
|
|
|
virtual Status remove_all_files(bool only_active, bool only_completed, bool delete_from_cache) = 0;
|
2022-02-23 19:34:09 +01:00
|
|
|
// Files are always added in is_paused = false state
|
2022-02-25 14:18:23 +01:00
|
|
|
virtual Status add_file(FileId file_id, FileSourceId file_source_id, string search_by, int8 priority) = 0;
|
|
|
|
virtual void search(std::string query, bool only_active, bool only_completed, string offset, int32 limit,
|
2022-02-25 13:11:10 +01:00
|
|
|
Promise<FoundFileDownloads> promise) = 0;
|
2022-02-23 19:34:09 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// private interface to handle all kinds of updates
|
|
|
|
//
|
|
|
|
virtual void update_file_download_state(FileId file_id, int64 download_size, int64 size, bool is_paused) = 0;
|
|
|
|
virtual void update_file_deleted(FileId file_id) = 0;
|
|
|
|
};
|
2022-02-25 14:18:23 +01:00
|
|
|
|
|
|
|
} // namespace td
|