data_cloud_file.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "base/flags.h"
  9. #include "ui/image/image.h"
  10. #include "ui/image/image_location.h"
  11. class FileLoader;
  12. namespace Storage {
  13. namespace Cache {
  14. class Database;
  15. } // namespace Cache
  16. } // namespace Storage
  17. namespace Main {
  18. class Session;
  19. } // namespace Main
  20. namespace Data {
  21. struct FileOrigin;
  22. struct CloudFile final {
  23. enum class Flag : uchar {
  24. Cancelled = 0x01,
  25. Failed = 0x02,
  26. Loaded = 0x04,
  27. };
  28. friend inline constexpr bool is_flag_type(Flag) { return true; };
  29. ~CloudFile();
  30. void clear();
  31. ImageLocation location;
  32. std::unique_ptr<FileLoader> loader;
  33. int byteSize = 0;
  34. int progressivePartSize = 0;
  35. base::flags<Flag> flags;
  36. };
  37. class CloudImage final {
  38. public:
  39. CloudImage();
  40. CloudImage(
  41. not_null<Main::Session*> session,
  42. const ImageWithLocation &data);
  43. // This method will replace the location and zero the _view pointer.
  44. void set(
  45. not_null<Main::Session*> session,
  46. const ImageWithLocation &data);
  47. void update(
  48. not_null<Main::Session*> session,
  49. const ImageWithLocation &data);
  50. [[nodiscard]] bool empty() const;
  51. [[nodiscard]] bool loading() const;
  52. [[nodiscard]] bool failed() const;
  53. [[nodiscard]] bool loadedOnce() const;
  54. void load(not_null<Main::Session*> session, FileOrigin origin);
  55. [[nodiscard]] const ImageLocation &location() const;
  56. [[nodiscard]] int byteSize() const;
  57. [[nodiscard]] std::shared_ptr<QImage> createView();
  58. [[nodiscard]] std::shared_ptr<QImage> activeView() const;
  59. [[nodiscard]] bool isCurrentView(
  60. const std::shared_ptr<QImage> &view) const;
  61. private:
  62. void setToActive(not_null<Main::Session*> session, QImage image);
  63. CloudFile _file;
  64. std::weak_ptr<QImage> _view;
  65. };
  66. void UpdateCloudFile(
  67. CloudFile &file,
  68. const ImageWithLocation &data,
  69. Storage::Cache::Database &cache,
  70. uint8 cacheTag,
  71. Fn<void(FileOrigin)> restartLoader,
  72. Fn<void(QImage, QByteArray)> usePreloaded = nullptr);
  73. void LoadCloudFile(
  74. not_null<Main::Session*> session,
  75. CloudFile &file,
  76. FileOrigin origin,
  77. LoadFromCloudSetting fromCloud,
  78. bool autoLoading,
  79. uint8 cacheTag,
  80. Fn<bool()> finalCheck,
  81. Fn<void(QImage, QByteArray)> done,
  82. Fn<void(bool)> fail = nullptr,
  83. Fn<void()> progress = nullptr,
  84. int downloadFrontPartSize = 0);
  85. void LoadCloudFile(
  86. not_null<Main::Session*> session,
  87. CloudFile &file,
  88. FileOrigin origin,
  89. LoadFromCloudSetting fromCloud,
  90. bool autoLoading,
  91. uint8 cacheTag,
  92. Fn<bool()> finalCheck,
  93. Fn<void(QByteArray)> done,
  94. Fn<void(bool)> fail = nullptr,
  95. Fn<void()> progress = nullptr);
  96. } // namespace Data