data_auto_download.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "data/data_auto_download.h"
  8. #include "data/data_peer.h"
  9. #include "data/data_photo.h"
  10. #include "data/data_document.h"
  11. #include <QtCore/QBuffer>
  12. namespace Data {
  13. namespace AutoDownload {
  14. namespace {
  15. constexpr auto kDefaultMaxSize = 8 * int64(1024 * 1024);
  16. constexpr auto kDefaultAutoPlaySize = 50 * int64(1024 * 1024);
  17. constexpr auto kVersion1 = char(1);
  18. constexpr auto kVersion = char(2);
  19. template <typename Enum>
  20. auto enums_view(int from, int till) {
  21. using namespace ranges::views;
  22. return ints(from, till) | transform([](int index) {
  23. return static_cast<Enum>(index);
  24. });
  25. }
  26. template <typename Enum>
  27. auto enums_view(int till) {
  28. return enums_view<Enum>(0, till);
  29. }
  30. void SetDefaultsForSource(Full &data, Source source) {
  31. data.setBytesLimit(source, Type::Photo, kDefaultMaxSize);
  32. data.setBytesLimit(source, Type::VoiceMessage, kDefaultMaxSize);
  33. data.setBytesLimit(
  34. source,
  35. Type::AutoPlayVideoMessage,
  36. kDefaultAutoPlaySize);
  37. data.setBytesLimit(source, Type::AutoPlayGIF, kDefaultAutoPlaySize);
  38. const auto channelsFileLimit = (source == Source::Channel)
  39. ? 0
  40. : kDefaultMaxSize;
  41. data.setBytesLimit(source, Type::File, channelsFileLimit);
  42. data.setBytesLimit(source, Type::AutoPlayVideo, kDefaultAutoPlaySize);
  43. data.setBytesLimit(source, Type::Music, channelsFileLimit);
  44. }
  45. const Full &Defaults() {
  46. static auto Result = [] {
  47. auto result = Full::FullDisabled();
  48. for (const auto source : enums_view<Source>(kSourcesCount)) {
  49. SetDefaultsForSource(result, source);
  50. }
  51. return result;
  52. }();
  53. return Result;
  54. }
  55. Source SourceFromPeer(not_null<PeerData*> peer) {
  56. if (peer->isUser()) {
  57. return Source::User;
  58. } else if (peer->isChat() || peer->isMegagroup()) {
  59. return Source::Group;
  60. } else {
  61. return Source::Channel;
  62. }
  63. }
  64. Type AutoPlayTypeFromDocument(not_null<DocumentData*> document) {
  65. return document->isVideoFile()
  66. ? Type::AutoPlayVideo
  67. : document->isVideoMessage()
  68. ? Type::AutoPlayVideoMessage
  69. : Type::AutoPlayGIF;
  70. }
  71. } // namespace
  72. void Single::setBytesLimit(int64 bytesLimit) {
  73. Expects(bytesLimit >= 0 && bytesLimit <= kMaxBytesLimit);
  74. _limit = int32(uint32(bytesLimit));
  75. Ensures(hasValue());
  76. }
  77. bool Single::hasValue() const {
  78. return (_limit != -1);
  79. }
  80. bool Single::shouldDownload(int64 fileSize) const {
  81. Expects(hasValue());
  82. const auto realLimit = bytesLimit();
  83. return (realLimit > 0) && (fileSize <= realLimit);
  84. }
  85. int64 Single::bytesLimit() const {
  86. Expects(hasValue());
  87. return uint32(_limit);
  88. }
  89. qint32 Single::serialize() const {
  90. return _limit;
  91. }
  92. bool Single::setFromSerialized(qint32 serialized) {
  93. auto realLimit = quint32(serialized);
  94. if (serialized != -1 && int64(realLimit) > kMaxBytesLimit) {
  95. return false;
  96. }
  97. _limit = serialized;
  98. return true;
  99. }
  100. const Single &Set::single(Type type) const {
  101. Expects(static_cast<int>(type) >= 0
  102. && static_cast<int>(type) < kTypesCount);
  103. return _data[static_cast<int>(type)];
  104. }
  105. Single &Set::single(Type type) {
  106. return const_cast<Single&>(static_cast<const Set*>(this)->single(type));
  107. }
  108. void Set::setBytesLimit(Type type, int64 bytesLimit) {
  109. single(type).setBytesLimit(bytesLimit);
  110. }
  111. bool Set::hasValue(Type type) const {
  112. return single(type).hasValue();
  113. }
  114. bool Set::shouldDownload(Type type, int64 fileSize) const {
  115. return single(type).shouldDownload(fileSize);
  116. }
  117. int64 Set::bytesLimit(Type type) const {
  118. return single(type).bytesLimit();
  119. }
  120. qint32 Set::serialize(Type type) const {
  121. return single(type).serialize();
  122. }
  123. bool Set::setFromSerialized(Type type, qint32 serialized) {
  124. if (static_cast<int>(type) < 0
  125. || static_cast<int>(type) >= kTypesCount) {
  126. return false;
  127. }
  128. return single(type).setFromSerialized(serialized);
  129. }
  130. const Set &Full::set(Source source) const {
  131. Expects(static_cast<int>(source) >= 0
  132. && static_cast<int>(source) < kSourcesCount);
  133. return _data[static_cast<int>(source)];
  134. }
  135. Set &Full::set(Source source) {
  136. return const_cast<Set&>(static_cast<const Full*>(this)->set(source));
  137. }
  138. const Set &Full::setOrDefault(Source source, Type type) const {
  139. const auto &my = set(source);
  140. const auto &result = my.hasValue(type) ? my : Defaults().set(source);
  141. Ensures(result.hasValue(type));
  142. return result;
  143. }
  144. void Full::setBytesLimit(Source source, Type type, int64 bytesLimit) {
  145. set(source).setBytesLimit(type, bytesLimit);
  146. }
  147. bool Full::shouldDownload(Source source, Type type, int64 fileSize) const {
  148. if (ranges::find(kStreamedTypes, type) != end(kStreamedTypes)) {
  149. // With streaming we disable autodownload and hide them in Settings.
  150. return false;
  151. }
  152. return setOrDefault(source, type).shouldDownload(type, fileSize);
  153. }
  154. int64 Full::bytesLimit(Source source, Type type) const {
  155. return setOrDefault(source, type).bytesLimit(type);
  156. }
  157. QByteArray Full::serialize() const {
  158. auto result = QByteArray();
  159. auto size = sizeof(qint8);
  160. size += kSourcesCount * kTypesCount * sizeof(qint32);
  161. result.reserve(size);
  162. {
  163. auto buffer = QBuffer(&result);
  164. buffer.open(QIODevice::WriteOnly);
  165. auto stream = QDataStream(&buffer);
  166. stream << qint8(kVersion);
  167. for (const auto source : enums_view<Source>(kSourcesCount)) {
  168. for (const auto type : enums_view<Type>(kTypesCount)) {
  169. stream << set(source).serialize(type);
  170. }
  171. }
  172. }
  173. return result;
  174. }
  175. bool Full::setFromSerialized(const QByteArray &serialized) {
  176. if (serialized.isEmpty()) {
  177. return false;
  178. }
  179. auto stream = QDataStream(serialized);
  180. auto version = qint8();
  181. stream >> version;
  182. if (stream.status() != QDataStream::Ok) {
  183. return false;
  184. } else if (version != kVersion && version != kVersion1) {
  185. return false;
  186. }
  187. auto temp = Full();
  188. for (const auto source : enums_view<Source>(kSourcesCount)) {
  189. for (const auto type : enums_view<Type>(kTypesCount)) {
  190. auto value = qint32();
  191. stream >> value;
  192. if (!temp.set(source).setFromSerialized(type, value)) {
  193. return false;
  194. }
  195. }
  196. }
  197. if (version == kVersion1) {
  198. for (const auto source : enums_view<Source>(kSourcesCount)) {
  199. for (const auto type : kAutoPlayTypes) {
  200. temp.setBytesLimit(source, type, std::max(
  201. temp.bytesLimit(source, type),
  202. kDefaultAutoPlaySize));
  203. }
  204. }
  205. }
  206. _data = temp._data;
  207. return true;
  208. }
  209. Full Full::FullDisabled() {
  210. auto result = Full();
  211. for (const auto source : enums_view<Source>(kSourcesCount)) {
  212. for (const auto type : enums_view<Type>(kTypesCount)) {
  213. result.setBytesLimit(source, type, 0);
  214. }
  215. }
  216. return result;
  217. }
  218. bool Should(
  219. const Full &data,
  220. Source source,
  221. not_null<DocumentData*> document) {
  222. if (document->sticker() || document->isGifv()) {
  223. return true;
  224. } else if (document->isVoiceMessage()
  225. || document->isVideoMessage()
  226. || document->isSong()
  227. || document->isVideoFile()) {
  228. return false;
  229. }
  230. return data.shouldDownload(source, Type::File, document->size);
  231. }
  232. bool Should(
  233. const Full &data,
  234. not_null<PeerData*> peer,
  235. not_null<DocumentData*> document) {
  236. return Should(data, SourceFromPeer(peer), document);
  237. }
  238. bool Should(
  239. const Full &data,
  240. not_null<DocumentData*> document) {
  241. if (document->sticker()) {
  242. return true;
  243. }
  244. return Should(data, Source::User, document)
  245. || Should(data, Source::Group, document)
  246. || Should(data, Source::Channel, document);
  247. }
  248. bool Should(
  249. const Full &data,
  250. not_null<PeerData*> peer,
  251. not_null<PhotoData*> photo) {
  252. return data.shouldDownload(
  253. SourceFromPeer(peer),
  254. Type::Photo,
  255. photo->imageByteSize(PhotoSize::Large));
  256. }
  257. bool ShouldAutoPlay(
  258. const Full &data,
  259. not_null<PeerData*> peer,
  260. not_null<DocumentData*> document) {
  261. return document->sticker() || data.shouldDownload(
  262. SourceFromPeer(peer),
  263. AutoPlayTypeFromDocument(document),
  264. document->size);
  265. }
  266. bool ShouldAutoPlay(
  267. const Full &data,
  268. not_null<PeerData*> peer,
  269. not_null<PhotoData*> photo) {
  270. const auto source = SourceFromPeer(peer);
  271. const auto size = photo->videoByteSize(PhotoSize::Large);
  272. return photo->hasVideo()
  273. && (data.shouldDownload(source, Type::AutoPlayGIF, size)
  274. || data.shouldDownload(source, Type::AutoPlayVideo, size)
  275. || data.shouldDownload(source, Type::AutoPlayVideoMessage, size));
  276. }
  277. Full WithDisabledAutoPlay(const Full &data) {
  278. auto result = data;
  279. for (const auto source : enums_view<Source>(kSourcesCount)) {
  280. for (const auto type : kAutoPlayTypes) {
  281. result.setBytesLimit(source, type, 0);
  282. }
  283. }
  284. return result;
  285. }
  286. } // namespace AutoDownload
  287. } // namespace Data