diff --git a/CMakeLists.txt b/CMakeLists.txt index 1aba1e5df..9e888a5d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,6 +354,7 @@ set(TDLIB_SOURCE td/telegram/AnimationsManager.cpp td/telegram/AudiosManager.cpp td/telegram/AuthManager.cpp + td/telegram/AutoDownloadSettings.cpp td/telegram/BackgroundManager.cpp td/telegram/BackgroundType.cpp td/telegram/CallActor.cpp @@ -484,6 +485,7 @@ set(TDLIB_SOURCE td/telegram/AnimationsManager.h td/telegram/AudiosManager.h td/telegram/AuthManager.h + td/telegram/AutoDownloadSettings.h td/telegram/BackgroundId.h td/telegram/BackgroundManager.h td/telegram/BackgroundType.h diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index b9e3ad555..d4dff5ba2 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -2373,9 +2373,26 @@ networkStatisticsEntryCall network_type:NetworkType sent_bytes:int53 received_by networkStatistics since_date:int32 entries:vector = NetworkStatistics; +//@description Contains auto-download settings +//@is_auto_download_enabled True, if the auto-download is enabled +//@max_photo_file_size Maximum size of a photo file to be auto-downloaded +//@max_video_file_size Maximum size of a video file to be auto-downloaded +//@max_other_file_size Maximum size of other file types to be auto-downloaded +//@preload_large_videos True, if the beginning of videos needs to be preloaded for instant playback +//@preload_next_audio True, if the next audio track needs to be preloaded while the user is listening to an audio file +//@use_less_data_for_calls True, if "use less data for calls" option needs to be enabled +autoDownloadSettings is_auto_download_enabled:Bool max_photo_file_size:int32 max_video_file_size:int32 max_other_file_size:int32 preload_large_videos:Bool preload_next_audio:Bool use_less_data_for_calls:Bool = AutoDownloadSettings; + +//@description Contains preset auto-download settings for the user +//@low Preset with lowest settings; supposed to be used by default when roaming +//@medium Preset with medium settings; supposed to be used by default when using mobile data +//@high Preset with highest settings; supposed to be used by default when connected on Wi-Fi +autoDownloadSettingsPresets low:autoDownloadSettings medium:autoDownloadSettings high:autoDownloadSettings = AutoDownloadSettingsPresets; + + //@class ConnectionState @description Describes the current state of the connection to Telegram servers -//@description Currently waiting for the network to become available. Use SetNetworkType to change the available network type +//@description Currently waiting for the network to become available. Use setNetworkType to change the available network type connectionStateWaitingForNetwork = ConnectionState; //@description Currently establishing a connection with a proxy server @@ -3755,6 +3772,12 @@ addNetworkStatistics entry:NetworkStatisticsEntry = Ok; //@description Resets all network data usage statistics to zero. Can be called before authorization resetNetworkStatistics = Ok; +//@description Returns auto-download settings presets for the currently logged in user +getAutoDownloadSettingsPresets = AutoDownloadSettingsPresets; + +//@description Sets auto-download settings @settings New user auto-download settings @type Type of the network for which the new settings are applied +setAutoDownloadSettings settings:autoDownloadSettings type:NetworkType = Ok; + //@description Returns one of the available Telegram Passport elements @type Telegram Passport element type @password Password of the current user getPassportElement type:PassportElementType password:string = PassportElement; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 356e5b9db..d2e13ed6a 100644 Binary files a/td/generate/scheme/td_api.tlo and b/td/generate/scheme/td_api.tlo differ diff --git a/td/telegram/AutoDownloadSettings.cpp b/td/telegram/AutoDownloadSettings.cpp new file mode 100644 index 000000000..b49640079 --- /dev/null +++ b/td/telegram/AutoDownloadSettings.cpp @@ -0,0 +1,136 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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) +// +#include "td/telegram/AutoDownloadSettings.h" + +#include "td/telegram/Global.h" +#include "td/telegram/net/NetQueryCreator.h" +#include "td/telegram/Td.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/logging.h" + +namespace td { + +static td_api::object_ptr convert_auto_download_settings( + const telegram_api::object_ptr &settings) { + CHECK(settings != nullptr); + auto flags = settings->flags_; + auto disabled = (flags & telegram_api::autoDownloadSettings::DISABLED_MASK) != 0; + auto video_preload_large = (flags & telegram_api::autoDownloadSettings::VIDEO_PRELOAD_LARGE_MASK) != 0; + auto audio_preload_next = (flags & telegram_api::autoDownloadSettings::AUDIO_PRELOAD_NEXT_MASK) != 0; + auto phonecalls_less_data = (flags & telegram_api::autoDownloadSettings::PHONECALLS_LESS_DATA_MASK) != 0; + return td_api::make_object( + !disabled, settings->photo_size_max_, settings->video_size_max_, settings->file_size_max_, video_preload_large, + audio_preload_next, phonecalls_less_data); +} + +class GetAutoDownloadSettingsQuery : public Td::ResultHandler { + Promise> promise_; + + public: + explicit GetAutoDownloadSettingsQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send() { + send_query(G()->net_query_creator().create(create_storer(telegram_api::account_getAutoDownloadSettings()))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + auto settings = result_ptr.move_as_ok(); + promise_.set_value(td_api::make_object( + convert_auto_download_settings(settings->low_), convert_auto_download_settings(settings->medium_), + convert_auto_download_settings(settings->high_))); + } + + void on_error(uint64 id, Status status) override { + promise_.set_error(std::move(status)); + } +}; + +telegram_api::object_ptr get_input_auto_download_settings( + const AutoDownloadSettings &settings) { + int32 flags = 0; + if (!settings.is_enabled) { + flags |= telegram_api::autoDownloadSettings::DISABLED_MASK; + } + if (settings.preload_large_videos) { + flags |= telegram_api::autoDownloadSettings::VIDEO_PRELOAD_LARGE_MASK; + } + if (settings.preload_next_audio) { + flags |= telegram_api::autoDownloadSettings::AUDIO_PRELOAD_NEXT_MASK; + } + if (settings.use_less_data_for_calls) { + flags |= telegram_api::autoDownloadSettings::PHONECALLS_LESS_DATA_MASK; + } + return telegram_api::make_object( + flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, settings.max_photo_file_size, + settings.max_video_file_size, settings.max_other_file_size); +} + +class SaveAutoDownloadSettingsQuery : public Td::ResultHandler { + Promise promise_; + + public: + explicit SaveAutoDownloadSettingsQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(NetType type, const AutoDownloadSettings &settings) { + int32 flags = 0; + if (type == NetType::MobileRoaming) { + flags |= telegram_api::account_saveAutoDownloadSettings::LOW_MASK; + } + if (type == NetType::WiFi) { + flags |= telegram_api::account_saveAutoDownloadSettings::HIGH_MASK; + } + send_query(G()->net_query_creator().create(create_storer(telegram_api::account_saveAutoDownloadSettings( + flags, false /*ignored*/, false /*ignored*/, get_input_auto_download_settings(settings))))); + } + + void on_result(uint64 id, BufferSlice packet) override { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(id, result_ptr.move_as_error()); + } + + LOG(INFO) << "SaveAutoDownloadSettingsQuery returned " << result_ptr.ok(); + promise_.set_value(Unit()); + } + + void on_error(uint64 id, Status status) override { + promise_.set_error(std::move(status)); + } +}; + +AutoDownloadSettings get_auto_download_settings(const td_api::object_ptr &settings) { + CHECK(settings != nullptr); + AutoDownloadSettings result; + result.max_photo_file_size = settings->max_photo_file_size_; + result.max_video_file_size = settings->max_video_file_size_; + result.max_other_file_size = settings->max_other_file_size_; + result.is_enabled = settings->is_auto_download_enabled_; + result.preload_large_videos = settings->preload_large_videos_; + result.preload_next_audio = settings->preload_next_audio_; + result.use_less_data_for_calls = settings->use_less_data_for_calls_; + return result; +} + +void get_auto_download_settings_presets(Td *td, + Promise> &&promise) { + td->create_handler(std::move(promise))->send(); +} + +void set_auto_download_settings(Td *td, NetType type, AutoDownloadSettings settings, Promise &&promise) { + td->create_handler(std::move(promise))->send(type, settings); +} + +} // namespace td diff --git a/td/telegram/AutoDownloadSettings.h b/td/telegram/AutoDownloadSettings.h new file mode 100644 index 000000000..3ea5476f3 --- /dev/null +++ b/td/telegram/AutoDownloadSettings.h @@ -0,0 +1,38 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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/net/NetType.h" +#include "td/telegram/td_api.h" + +#include "td/actor/PromiseFuture.h" + +#include "td/utils/common.h" + +namespace td { + +class Td; + +class AutoDownloadSettings { + public: + int32 max_photo_file_size = 0; + int32 max_video_file_size = 0; + int32 max_other_file_size = 0; + bool is_enabled = false; + bool preload_large_videos = false; + bool preload_next_audio = false; + bool use_less_data_for_calls = false; +}; + +AutoDownloadSettings get_auto_download_settings(const td_api::object_ptr &settings); + +void get_auto_download_settings_presets(Td *td, + Promise> &&promise); + +void set_auto_download_settings(Td *td, NetType type, AutoDownloadSettings settings, Promise &&promise); + +} // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 3d1f369dc..057aab256 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -10,6 +10,7 @@ #include "td/telegram/AnimationsManager.h" #include "td/telegram/AudiosManager.h" #include "td/telegram/AuthManager.h" +#include "td/telegram/AutoDownloadSettings.h" #include "td/telegram/BackgroundManager.h" #include "td/telegram/CallbackQueriesManager.h" #include "td/telegram/CallId.h" @@ -5182,12 +5183,28 @@ void Td::on_request(uint64 id, td_api::addNetworkStatistics &request) { send_closure(actor_id(this), &Td::send_result, id, make_tl_object()); } -void Td::on_request(uint64 id, td_api::setNetworkType &request) { +void Td::on_request(uint64 id, const td_api::setNetworkType &request) { CREATE_OK_REQUEST_PROMISE(); send_closure(state_manager_, &StateManager::on_network, from_td_api(request.type_)); promise.set_value(Unit()); } +void Td::on_request(uint64 id, const td_api::getAutoDownloadSettingsPresets &request) { + CHECK_IS_USER(); + CREATE_REQUEST_PROMISE(); + get_auto_download_settings_presets(this, std::move(promise)); +} + +void Td::on_request(uint64 id, const td_api::setAutoDownloadSettings &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + if (request.settings_ == nullptr) { + return send_error_raw(id, 400, "New settings must be non-empty"); + } + set_auto_download_settings(this, from_td_api(request.type_), get_auto_download_settings(request.settings_), + std::move(promise)); +} + void Td::on_request(uint64 id, td_api::getTopChats &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 66caf9c9d..d5ea4794b 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -490,7 +490,11 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, td_api::addNetworkStatistics &request); - void on_request(uint64 id, td_api::setNetworkType &request); + void on_request(uint64 id, const td_api::setNetworkType &request); + + void on_request(uint64 id, const td_api::getAutoDownloadSettingsPresets &request); + + void on_request(uint64 id, const td_api::setAutoDownloadSettings &request); void on_request(uint64 id, td_api::getTopChats &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index c6a759ab6..a8782b3f6 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2114,6 +2114,11 @@ class CliClient final : public Actor { send_request(td_api::make_object()); } else if (op == "snt") { send_request(td_api::make_object(get_network_type(args))); + } else if (op == "gadsp") { + send_request(td_api::make_object()); + } else if (op == "sads") { + send_request(td_api::make_object( + td_api::make_object(), get_network_type(args))); } else if (op == "ansc") { string sent_bytes; string received_bytes; diff --git a/td/telegram/net/NetStatsManager.h b/td/telegram/net/NetStatsManager.h index 924821979..74bcb34ae 100644 --- a/td/telegram/net/NetStatsManager.h +++ b/td/telegram/net/NetStatsManager.h @@ -88,7 +88,7 @@ class NetStatsManager : public Actor { switch (type) { case NetType::Other: return CSlice("other"); - case NetType::Wifi: + case NetType::WiFi: return CSlice("wifi"); case NetType::Mobile: return CSlice("mobile"); diff --git a/td/telegram/net/NetType.h b/td/telegram/net/NetType.h index fc9d4b8c5..3a2539f5e 100644 --- a/td/telegram/net/NetType.h +++ b/td/telegram/net/NetType.h @@ -12,9 +12,9 @@ namespace td { -enum class NetType : int8 { Other, Wifi, Mobile, MobileRoaming, Size, None, Unknown }; +enum class NetType : int8 { Other, WiFi, Mobile, MobileRoaming, Size, None, Unknown }; -inline NetType from_td_api(tl_object_ptr &net_type) { +inline NetType from_td_api(const tl_object_ptr &net_type) { if (net_type == nullptr) { return NetType::Other; } @@ -23,7 +23,7 @@ inline NetType from_td_api(tl_object_ptr &net_type) { case td_api::networkTypeOther::ID: return NetType::Other; case td_api::networkTypeWiFi::ID: - return NetType::Wifi; + return NetType::WiFi; case td_api::networkTypeMobile::ID: return NetType::Mobile; case td_api::networkTypeMobileRoaming::ID: @@ -39,7 +39,7 @@ inline tl_object_ptr as_td_api(NetType net_type) { switch (net_type) { case NetType::Other: return make_tl_object(); - case NetType::Wifi: + case NetType::WiFi: return make_tl_object(); case NetType::Mobile: return make_tl_object();