data_photo.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "data/data_types.h"
  9. #include "data/data_cloud_file.h"
  10. namespace Main {
  11. class Session;
  12. } // namespace Main
  13. namespace Media {
  14. namespace Streaming {
  15. class Loader;
  16. } // namespace Streaming
  17. } // namespace Media
  18. namespace Data {
  19. class Session;
  20. class ReplyPreview;
  21. class PhotoMedia;
  22. inline constexpr auto kPhotoSizeCount = 3;
  23. enum class PhotoSize : uchar {
  24. Small,
  25. Thumbnail,
  26. Large,
  27. };
  28. [[nodiscard]] inline int PhotoSizeIndex(PhotoSize size) {
  29. Expects(static_cast<int>(size) < kPhotoSizeCount);
  30. return static_cast<int>(size);
  31. }
  32. } // namespace Data
  33. class PhotoData final {
  34. public:
  35. PhotoData(not_null<Data::Session*> owner, PhotoId id);
  36. ~PhotoData();
  37. [[nodiscard]] Data::Session &owner() const;
  38. [[nodiscard]] Main::Session &session() const;
  39. [[nodiscard]] bool isNull() const;
  40. void automaticLoadSettingsChanged();
  41. [[nodiscard]] TimeId date() const;
  42. [[nodiscard]] bool loading() const;
  43. [[nodiscard]] bool displayLoading() const;
  44. void cancel();
  45. [[nodiscard]] float64 progress() const;
  46. [[nodiscard]] int32 loadOffset() const;
  47. [[nodiscard]] bool uploading() const;
  48. [[nodiscard]] bool cancelled() const;
  49. void setWaitingForAlbum();
  50. [[nodiscard]] bool waitingForAlbum() const;
  51. [[nodiscard]] Image *getReplyPreview(
  52. Data::FileOrigin origin,
  53. not_null<PeerData*> context,
  54. bool spoiler);
  55. [[nodiscard]] Image *getReplyPreview(not_null<HistoryItem*> item);
  56. [[nodiscard]] bool replyPreviewLoaded(bool spoiler) const;
  57. void setRemoteLocation(
  58. int32 dc,
  59. uint64 access,
  60. const QByteArray &fileReference);
  61. [[nodiscard]] MTPInputPhoto mtpInput() const;
  62. [[nodiscard]] QByteArray fileReference() const;
  63. void refreshFileReference(const QByteArray &value);
  64. // When we have some client-side generated photo
  65. // (for example for displaying an external inline bot result)
  66. // and it has downloaded full image, we can collect image from it
  67. // to (this) received from the server "same" photo.
  68. void collectLocalData(not_null<PhotoData*> local);
  69. [[nodiscard]] std::shared_ptr<Data::PhotoMedia> createMediaView();
  70. [[nodiscard]] auto activeMediaView() const
  71. -> std::shared_ptr<Data::PhotoMedia>;
  72. void setFields(TimeId date, bool hasAttachedStickers);
  73. void setExtendedMediaPreview(
  74. QSize dimensions,
  75. const QByteArray &inlineThumbnailBytes,
  76. std::optional<TimeId> videoDuration);
  77. [[nodiscard]] bool extendedMediaPreview() const;
  78. [[nodiscard]] std::optional<TimeId> extendedMediaVideoDuration() const;
  79. void updateImages(
  80. const QByteArray &inlineThumbnailBytes,
  81. const ImageWithLocation &small,
  82. const ImageWithLocation &thumbnail,
  83. const ImageWithLocation &large,
  84. const ImageWithLocation &videoSmall,
  85. const ImageWithLocation &videoLarge,
  86. crl::time videoStartTime);
  87. [[nodiscard]] int validSizeIndex(Data::PhotoSize size) const;
  88. [[nodiscard]] int existingSizeIndex(Data::PhotoSize size) const;
  89. [[nodiscard]] QByteArray inlineThumbnailBytes() const {
  90. return _inlineThumbnailBytes;
  91. }
  92. void clearInlineThumbnailBytes() {
  93. _inlineThumbnailBytes = QByteArray();
  94. }
  95. void load(
  96. Data::FileOrigin origin,
  97. LoadFromCloudSetting fromCloud = LoadFromCloudOrLocal,
  98. bool autoLoading = false);
  99. [[nodiscard]] static int SideLimit();
  100. [[nodiscard]] bool hasExact(Data::PhotoSize size) const;
  101. [[nodiscard]] bool loading(Data::PhotoSize size) const;
  102. [[nodiscard]] bool failed(Data::PhotoSize size) const;
  103. void clearFailed(Data::PhotoSize size);
  104. void load(
  105. Data::PhotoSize size,
  106. Data::FileOrigin origin,
  107. LoadFromCloudSetting fromCloud = LoadFromCloudOrLocal,
  108. bool autoLoading = false);
  109. [[nodiscard]] const ImageLocation &location(Data::PhotoSize size) const;
  110. [[nodiscard]] std::optional<QSize> size(Data::PhotoSize size) const;
  111. [[nodiscard]] int imageByteSize(Data::PhotoSize size) const;
  112. [[nodiscard]] bool hasVideo() const;
  113. [[nodiscard]] bool hasVideoSmall() const;
  114. [[nodiscard]] bool videoLoading(Data::PhotoSize size) const;
  115. [[nodiscard]] bool videoFailed(Data::PhotoSize size) const;
  116. void loadVideo(Data::PhotoSize size, Data::FileOrigin origin);
  117. [[nodiscard]] const ImageLocation &videoLocation(
  118. Data::PhotoSize size) const;
  119. [[nodiscard]] int videoByteSize(Data::PhotoSize size) const;
  120. [[nodiscard]] crl::time videoStartPosition() const;
  121. void setVideoPlaybackFailed();
  122. [[nodiscard]] bool videoPlaybackFailed() const;
  123. [[nodiscard]] bool videoCanBePlayed() const;
  124. [[nodiscard]] auto createStreamingLoader(
  125. Data::FileOrigin origin,
  126. bool forceRemoteLoader) const
  127. -> std::unique_ptr<Media::Streaming::Loader>;
  128. [[nodiscard]] bool hasAttachedStickers() const;
  129. void setHasAttachedStickers(bool value);
  130. // For now they return size of the 'large' image.
  131. [[nodiscard]] int width() const;
  132. [[nodiscard]] int height() const;
  133. PhotoId id = 0;
  134. PeerData *peer = nullptr; // for chat and channel photos connection
  135. // geo, caption
  136. std::unique_ptr<Data::UploadState> uploadingData;
  137. private:
  138. [[nodiscard]] Data::CloudFile &videoFile(Data::PhotoSize size);
  139. [[nodiscard]] const Data::CloudFile &videoFile(
  140. Data::PhotoSize size) const;
  141. TimeId _dateOrExtendedVideoDuration = 0;
  142. struct VideoSizes {
  143. Data::CloudFile small;
  144. Data::CloudFile large;
  145. crl::time startTime = 0;
  146. bool playbackFailed = false;
  147. };
  148. QByteArray _inlineThumbnailBytes;
  149. std::array<Data::CloudFile, Data::kPhotoSizeCount> _images;
  150. std::unique_ptr<VideoSizes> _videoSizes;
  151. int32 _dc = 0;
  152. uint64 _access = 0;
  153. bool _hasStickers = false;
  154. bool _extendedMediaPreview = false;
  155. QByteArray _fileReference;
  156. std::unique_ptr<Data::ReplyPreview> _replyPreview;
  157. std::weak_ptr<Data::PhotoMedia> _media;
  158. not_null<Data::Session*> _owner;
  159. };