data_auto_download.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #pragma once
  8. #include <array>
  9. namespace Data {
  10. namespace AutoDownload {
  11. constexpr auto kMaxBytesLimit = 8000 * int64(512 * 1024);
  12. enum class Source {
  13. User = 0x00,
  14. Group = 0x01,
  15. Channel = 0x02,
  16. };
  17. constexpr auto kSourcesCount = 3;
  18. enum class Type {
  19. Photo = 0x00,
  20. AutoPlayVideo = 0x01,
  21. VoiceMessage = 0x02,
  22. AutoPlayVideoMessage = 0x03,
  23. Music = 0x04,
  24. AutoPlayGIF = 0x05,
  25. File = 0x06,
  26. };
  27. inline constexpr auto kAutoPlayTypes = {
  28. Type::AutoPlayVideo,
  29. Type::AutoPlayVideoMessage,
  30. Type::AutoPlayGIF,
  31. };
  32. inline constexpr auto kStreamedTypes = {
  33. Type::VoiceMessage,
  34. Type::Music,
  35. };
  36. constexpr auto kTypesCount = 7;
  37. class Single {
  38. public:
  39. void setBytesLimit(int64 bytesLimit);
  40. bool hasValue() const;
  41. bool shouldDownload(int64 fileSize) const;
  42. int64 bytesLimit() const;
  43. qint32 serialize() const;
  44. bool setFromSerialized(qint32 serialized);
  45. private:
  46. int _limit = -1; // FileSize: Right now any file size fits 32 bit.
  47. };
  48. class Set {
  49. public:
  50. void setBytesLimit(Type type, int64 bytesLimit);
  51. bool hasValue(Type type) const;
  52. bool shouldDownload(Type type, int64 fileSize) const;
  53. int64 bytesLimit(Type type) const;
  54. qint32 serialize(Type type) const;
  55. bool setFromSerialized(Type type, qint32 serialized);
  56. private:
  57. const Single &single(Type type) const;
  58. Single &single(Type type);
  59. std::array<Single, kTypesCount> _data;
  60. };
  61. class Full {
  62. public:
  63. void setBytesLimit(Source source, Type type, int64 bytesLimit);
  64. [[nodiscard]] bool shouldDownload(
  65. Source source,
  66. Type type,
  67. int64 fileSize) const;
  68. [[nodiscard]] int64 bytesLimit(Source source, Type type) const;
  69. [[nodiscard]] QByteArray serialize() const;
  70. bool setFromSerialized(const QByteArray &serialized);
  71. [[nodiscard]] static Full FullDisabled();
  72. private:
  73. [[nodiscard]] const Set &set(Source source) const;
  74. [[nodiscard]] Set &set(Source source);
  75. [[nodiscard]] const Set &setOrDefault(Source source, Type type) const;
  76. std::array<Set, kSourcesCount> _data;
  77. };
  78. [[nodiscard]] bool Should(
  79. const Full &data,
  80. not_null<PeerData*> peer,
  81. not_null<DocumentData*> document);
  82. [[nodiscard]] bool Should(
  83. const Full &data,
  84. not_null<DocumentData*> document);
  85. [[nodiscard]] bool Should(
  86. const Full &data,
  87. not_null<PeerData*> peer,
  88. not_null<PhotoData*> photo);
  89. [[nodiscard]] bool ShouldAutoPlay(
  90. const Full &data,
  91. not_null<PeerData*> peer,
  92. not_null<DocumentData*> document);
  93. [[nodiscard]] bool ShouldAutoPlay(
  94. const Full &data,
  95. not_null<PeerData*> peer,
  96. not_null<PhotoData*> photo);
  97. [[nodiscard]] Full WithDisabledAutoPlay(const Full &data);
  98. } // namespace AutoDownload
  99. } // namespace Data