data_cloud_themes.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/timer.h"
  9. #include "data/data_wall_paper.h"
  10. class DocumentData;
  11. namespace Main {
  12. class Session;
  13. } // namespace Main
  14. namespace Window {
  15. class Controller;
  16. } // namespace Window
  17. namespace Data {
  18. class DocumentMedia;
  19. enum class CloudThemeType {
  20. Dark,
  21. Light,
  22. };
  23. struct CloudTheme {
  24. uint64 id = 0;
  25. uint64 accessHash = 0;
  26. QString slug;
  27. QString title;
  28. DocumentId documentId = 0;
  29. UserId createdBy = 0;
  30. int usersCount = 0;
  31. QString emoticon;
  32. using Type = CloudThemeType;
  33. struct Settings {
  34. std::optional<WallPaper> paper;
  35. QColor accentColor;
  36. std::optional<QColor> outgoingAccentColor;
  37. std::vector<QColor> outgoingMessagesColors;
  38. };
  39. base::flat_map<Type, Settings> settings;
  40. static CloudTheme Parse(
  41. not_null<Main::Session*> session,
  42. const MTPDtheme &data,
  43. bool parseSettings = false);
  44. static CloudTheme Parse(
  45. not_null<Main::Session*> session,
  46. const MTPTheme &data,
  47. bool parseSettings = false);
  48. };
  49. class CloudThemes final {
  50. public:
  51. explicit CloudThemes(not_null<Main::Session*> session);
  52. [[nodiscard]] static QString Format();
  53. void refresh();
  54. [[nodiscard]] rpl::producer<> updated() const;
  55. [[nodiscard]] const std::vector<CloudTheme> &list() const;
  56. void savedFromEditor(const CloudTheme &data);
  57. void remove(uint64 cloudThemeId);
  58. void refreshChatThemes();
  59. [[nodiscard]] const std::vector<CloudTheme> &chatThemes() const;
  60. [[nodiscard]] rpl::producer<> chatThemesUpdated() const;
  61. [[nodiscard]] std::optional<CloudTheme> themeForEmoji(
  62. const QString &emoticon) const;
  63. [[nodiscard]] auto themeForEmojiValue(const QString &emoticon)
  64. -> rpl::producer<std::optional<CloudTheme>>;
  65. [[nodiscard]] static bool TestingColors();
  66. static void SetTestingColors(bool testing);
  67. [[nodiscard]] QString prepareTestingLink(const CloudTheme &theme) const;
  68. [[nodiscard]] std::optional<CloudTheme> updateThemeFromLink(
  69. const QString &emoticon,
  70. const QMap<QString, QString> &params);
  71. void applyUpdate(const MTPTheme &theme);
  72. void resolve(
  73. not_null<Window::Controller*> controller,
  74. const QString &slug,
  75. const FullMsgId &clickFromMessageId);
  76. void showPreview(
  77. not_null<Window::Controller*> controller,
  78. const MTPTheme &data);
  79. void showPreview(
  80. not_null<Window::Controller*> controller,
  81. const CloudTheme &cloud);
  82. void applyFromDocument(const CloudTheme &cloud);
  83. private:
  84. struct LoadingDocument {
  85. CloudTheme theme;
  86. DocumentData *document = nullptr;
  87. std::shared_ptr<Data::DocumentMedia> documentMedia;
  88. rpl::lifetime subscription;
  89. Fn<void(std::shared_ptr<Data::DocumentMedia>)> callback;
  90. };
  91. void parseThemes(const QVector<MTPTheme> &list);
  92. void checkCurrentTheme();
  93. void install();
  94. void setupReload();
  95. [[nodiscard]] bool needReload() const;
  96. void scheduleReload();
  97. void reloadCurrent();
  98. void previewFromDocument(
  99. not_null<Window::Controller*> controller,
  100. const CloudTheme &cloud);
  101. void loadDocumentAndInvoke(
  102. LoadingDocument &value,
  103. const CloudTheme &cloud,
  104. not_null<DocumentData*> document,
  105. Fn<void(std::shared_ptr<Data::DocumentMedia>)> callback);
  106. void invokeForLoaded(LoadingDocument &value);
  107. void parseChatThemes(const QVector<MTPTheme> &list);
  108. const not_null<Main::Session*> _session;
  109. uint64 _hash = 0;
  110. mtpRequestId _refreshRequestId = 0;
  111. mtpRequestId _resolveRequestId = 0;
  112. std::vector<CloudTheme> _list;
  113. rpl::event_stream<> _updates;
  114. uint64 _chatThemesHash = 0;
  115. mtpRequestId _chatThemesRequestId = 0;
  116. std::vector<CloudTheme> _chatThemes;
  117. rpl::event_stream<> _chatThemesUpdates;
  118. base::Timer _reloadCurrentTimer;
  119. LoadingDocument _updatingFrom;
  120. LoadingDocument _previewFrom;
  121. uint64 _installedDayThemeId = 0;
  122. uint64 _installedNightThemeId = 0;
  123. rpl::lifetime _lifetime;
  124. };
  125. } // namespace Data