info_memento.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "info/info_memento.h"
  8. #include "info/global_media/info_global_media_widget.h"
  9. #include "info/profile/info_profile_widget.h"
  10. #include "info/media/info_media_widget.h"
  11. #include "info/members/info_members_widget.h"
  12. #include "info/common_groups/info_common_groups_widget.h"
  13. #include "info/saved/info_saved_sublists_widget.h"
  14. #include "info/settings/info_settings_widget.h"
  15. #include "info/similar_peers/info_similar_peers_widget.h"
  16. #include "info/reactions_list/info_reactions_list_widget.h"
  17. #include "info/requests_list/info_requests_list_widget.h"
  18. #include "info/peer_gifts/info_peer_gifts_widget.h"
  19. #include "info/polls/info_polls_results_widget.h"
  20. #include "info/info_section_widget.h"
  21. #include "info/info_layer_widget.h"
  22. #include "info/info_controller.h"
  23. #include "ui/ui_utility.h"
  24. #include "boxes/peer_list_box.h"
  25. #include "data/data_channel.h"
  26. #include "data/data_chat.h"
  27. #include "data/data_forum_topic.h"
  28. #include "data/data_session.h"
  29. #include "main/main_session.h"
  30. namespace Info {
  31. Memento::Memento(not_null<PeerData*> peer)
  32. : Memento(peer, Section::Type::Profile) {
  33. }
  34. Memento::Memento(not_null<PeerData*> peer, Section section)
  35. : Memento(DefaultStack(peer, section)) {
  36. }
  37. Memento::Memento(not_null<Data::ForumTopic*> topic)
  38. : Memento(topic, Section::Type::Profile) {
  39. }
  40. Memento::Memento(not_null<Data::ForumTopic*> topic, Section section)
  41. : Memento(DefaultStack(topic, section)) {
  42. }
  43. Memento::Memento(Settings::Tag settings, Section section)
  44. : Memento(DefaultStack(settings, section)) {
  45. }
  46. Memento::Memento(not_null<PollData*> poll, FullMsgId contextId)
  47. : Memento(DefaultStack(poll, contextId)) {
  48. }
  49. Memento::Memento(
  50. std::shared_ptr<Api::WhoReadList> whoReadIds,
  51. FullMsgId contextId,
  52. Data::ReactionId selected)
  53. : Memento(DefaultStack(std::move(whoReadIds), contextId, selected)) {
  54. }
  55. Memento::Memento(std::vector<std::shared_ptr<ContentMemento>> stack)
  56. : _stack(std::move(stack)) {
  57. auto topics = base::flat_set<not_null<Data::ForumTopic*>>();
  58. for (auto &entry : _stack) {
  59. if (const auto topic = entry->topic()) {
  60. topics.emplace(topic);
  61. }
  62. }
  63. for (const auto &topic : topics) {
  64. topic->destroyed(
  65. ) | rpl::start_with_next([=] {
  66. for (auto i = begin(_stack); i != end(_stack);) {
  67. if (i->get()->topic() == topic) {
  68. i = _stack.erase(i);
  69. } else {
  70. ++i;
  71. }
  72. }
  73. if (_stack.empty()) {
  74. _removeRequests.fire({});
  75. }
  76. }, _lifetime);
  77. }
  78. }
  79. std::vector<std::shared_ptr<ContentMemento>> Memento::DefaultStack(
  80. not_null<PeerData*> peer,
  81. Section section) {
  82. auto result = std::vector<std::shared_ptr<ContentMemento>>();
  83. result.push_back(DefaultContent(peer, section));
  84. return result;
  85. }
  86. std::vector<std::shared_ptr<ContentMemento>> Memento::DefaultStack(
  87. not_null<Data::ForumTopic*> topic,
  88. Section section) {
  89. auto result = std::vector<std::shared_ptr<ContentMemento>>();
  90. result.push_back(DefaultContent(topic, section));
  91. return result;
  92. }
  93. std::vector<std::shared_ptr<ContentMemento>> Memento::DefaultStack(
  94. Settings::Tag settings,
  95. Section section) {
  96. auto result = std::vector<std::shared_ptr<ContentMemento>>();
  97. result.push_back(std::make_shared<Settings::Memento>(
  98. settings.self,
  99. section.settingsType()));
  100. return result;
  101. }
  102. std::vector<std::shared_ptr<ContentMemento>> Memento::DefaultStack(
  103. not_null<PollData*> poll,
  104. FullMsgId contextId) {
  105. auto result = std::vector<std::shared_ptr<ContentMemento>>();
  106. result.push_back(std::make_shared<Polls::Memento>(poll, contextId));
  107. return result;
  108. }
  109. std::vector<std::shared_ptr<ContentMemento>> Memento::DefaultStack(
  110. std::shared_ptr<Api::WhoReadList> whoReadIds,
  111. FullMsgId contextId,
  112. Data::ReactionId selected) {
  113. auto result = std::vector<std::shared_ptr<ContentMemento>>();
  114. result.push_back(std::make_shared<ReactionsList::Memento>(
  115. std::move(whoReadIds),
  116. contextId,
  117. selected));
  118. return result;
  119. }
  120. Section Memento::DefaultSection(not_null<PeerData*> peer) {
  121. if (peer->savedSublistsInfo()) {
  122. return Section(Section::Type::SavedSublists);
  123. } else if (peer->sharedMediaInfo()) {
  124. return Section(Section::MediaType::Photo);
  125. }
  126. return Section(Section::Type::Profile);
  127. }
  128. std::shared_ptr<Memento> Memento::Default(not_null<PeerData*> peer) {
  129. return std::make_shared<Memento>(peer, DefaultSection(peer));
  130. }
  131. std::shared_ptr<ContentMemento> Memento::DefaultContent(
  132. not_null<PeerData*> peer,
  133. Section section) {
  134. if (auto to = peer->migrateTo()) {
  135. peer = to;
  136. }
  137. auto migrated = peer->migrateFrom();
  138. auto migratedPeerId = migrated ? migrated->id : PeerId(0);
  139. switch (section.type()) {
  140. case Section::Type::Profile:
  141. return std::make_shared<Profile::Memento>(
  142. peer,
  143. migratedPeerId);
  144. case Section::Type::Media:
  145. return std::make_shared<Media::Memento>(
  146. peer,
  147. migratedPeerId,
  148. section.mediaType());
  149. case Section::Type::GlobalMedia:
  150. return std::make_shared<GlobalMedia::Memento>(
  151. peer->asUser(),
  152. section.mediaType());
  153. case Section::Type::CommonGroups:
  154. return std::make_shared<CommonGroups::Memento>(peer->asUser());
  155. case Section::Type::SimilarPeers:
  156. return std::make_shared<SimilarPeers::Memento>(peer);
  157. case Section::Type::RequestsList:
  158. return std::make_shared<RequestsList::Memento>(peer);
  159. case Section::Type::PeerGifts:
  160. return std::make_shared<PeerGifts::Memento>(peer);
  161. case Section::Type::SavedSublists:
  162. return std::make_shared<Saved::SublistsMemento>(&peer->session());
  163. case Section::Type::Members:
  164. return std::make_shared<Members::Memento>(
  165. peer,
  166. migratedPeerId);
  167. }
  168. Unexpected("Wrong section type in Info::Memento::DefaultContent()");
  169. }
  170. std::shared_ptr<ContentMemento> Memento::DefaultContent(
  171. not_null<Data::ForumTopic*> topic,
  172. Section section) {
  173. const auto peer = topic->peer();
  174. const auto migrated = peer->migrateFrom();
  175. const auto migratedPeerId = migrated ? migrated->id : PeerId(0);
  176. switch (section.type()) {
  177. case Section::Type::Profile:
  178. return std::make_shared<Profile::Memento>(topic);
  179. case Section::Type::Media:
  180. return std::make_shared<Media::Memento>(topic, section.mediaType());
  181. case Section::Type::Members:
  182. return std::make_shared<Members::Memento>(peer, migratedPeerId);
  183. }
  184. Unexpected("Wrong section type in Info::Memento::DefaultContent()");
  185. }
  186. object_ptr<Window::SectionWidget> Memento::createWidget(
  187. QWidget *parent,
  188. not_null<Window::SessionController*> controller,
  189. Window::Column column,
  190. const QRect &geometry) {
  191. auto wrap = (column == Window::Column::Third)
  192. ? Wrap::Side
  193. : Wrap::Narrow;
  194. auto result = object_ptr<SectionWidget>(
  195. parent,
  196. controller,
  197. wrap,
  198. this);
  199. result->setGeometry(geometry);
  200. return result;
  201. }
  202. object_ptr<Ui::LayerWidget> Memento::createLayer(
  203. not_null<Window::SessionController*> controller,
  204. const QRect &geometry) {
  205. if (geometry.width() >= LayerWidget::MinimalSupportedWidth()) {
  206. return object_ptr<LayerWidget>(controller, this);
  207. }
  208. return nullptr;
  209. }
  210. std::vector<std::shared_ptr<ContentMemento>> Memento::takeStack() {
  211. return std::move(_stack);
  212. }
  213. Memento::~Memento() = default;
  214. MoveMemento::MoveMemento(object_ptr<WrapWidget> content)
  215. : _content(std::move(content)) {
  216. _content->hide();
  217. _content->setParent(nullptr);
  218. }
  219. object_ptr<Window::SectionWidget> MoveMemento::createWidget(
  220. QWidget *parent,
  221. not_null<Window::SessionController*> controller,
  222. Window::Column column,
  223. const QRect &geometry) {
  224. auto wrap = (column == Window::Column::Third)
  225. ? Wrap::Side
  226. : Wrap::Narrow;
  227. auto result = object_ptr<SectionWidget>(
  228. parent,
  229. controller,
  230. wrap,
  231. this);
  232. result->setGeometry(geometry);
  233. return result;
  234. }
  235. object_ptr<Ui::LayerWidget> MoveMemento::createLayer(
  236. not_null<Window::SessionController*> controller,
  237. const QRect &geometry) {
  238. if (geometry.width() < LayerWidget::MinimalSupportedWidth()) {
  239. return nullptr;
  240. }
  241. return object_ptr<LayerWidget>(controller, this);
  242. }
  243. object_ptr<WrapWidget> MoveMemento::takeContent(
  244. QWidget *parent,
  245. Wrap wrap) {
  246. Ui::AttachParentChild(parent, _content);
  247. _content->setWrap(wrap);
  248. return std::move(_content);
  249. }
  250. } // namespace Info