info_downloads_widget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/downloads/info_downloads_widget.h"
  8. #include "info/downloads/info_downloads_inner_widget.h"
  9. #include "info/info_controller.h"
  10. #include "info/info_memento.h"
  11. #include "ui/boxes/confirm_box.h"
  12. #include "ui/search_field_controller.h"
  13. #include "ui/widgets/menu/menu_add_action_callback.h"
  14. #include "ui/widgets/scroll_area.h"
  15. #include "ui/ui_utility.h"
  16. #include "data/data_download_manager.h"
  17. #include "data/data_user.h"
  18. #include "core/application.h"
  19. #include "lang/lang_keys.h"
  20. #include "styles/style_info.h"
  21. #include "styles/style_layers.h"
  22. #include "styles/style_menu_icons.h"
  23. namespace Info::Downloads {
  24. Memento::Memento(not_null<Controller*> controller)
  25. : ContentMemento(Tag{})
  26. , _media(controller) {
  27. }
  28. Memento::Memento(not_null<UserData*> self)
  29. : ContentMemento(Tag{})
  30. , _media(self, 0, Media::Type::File) {
  31. }
  32. Memento::~Memento() = default;
  33. Section Memento::section() const {
  34. return Section(Section::Type::Downloads);
  35. }
  36. object_ptr<ContentWidget> Memento::createWidget(
  37. QWidget *parent,
  38. not_null<Controller*> controller,
  39. const QRect &geometry) {
  40. auto result = object_ptr<Widget>(parent, controller);
  41. result->setInternalState(geometry, this);
  42. return result;
  43. }
  44. Widget::Widget(
  45. QWidget *parent,
  46. not_null<Controller*> controller)
  47. : ContentWidget(parent, controller) {
  48. _inner = setInnerWidget(object_ptr<InnerWidget>(
  49. this,
  50. controller));
  51. _inner->setScrollHeightValue(scrollHeightValue());
  52. _inner->scrollToRequests(
  53. ) | rpl::start_with_next([this](Ui::ScrollToRequest request) {
  54. scrollTo(request);
  55. }, _inner->lifetime());
  56. }
  57. bool Widget::showInternal(not_null<ContentMemento*> memento) {
  58. if (auto downloadsMemento = dynamic_cast<Memento*>(memento.get())) {
  59. restoreState(downloadsMemento);
  60. return true;
  61. }
  62. return false;
  63. }
  64. void Widget::setInternalState(
  65. const QRect &geometry,
  66. not_null<Memento*> memento) {
  67. setGeometry(geometry);
  68. Ui::SendPendingMoveResizeEvents(this);
  69. restoreState(memento);
  70. }
  71. std::shared_ptr<ContentMemento> Widget::doCreateMemento() {
  72. auto result = std::make_shared<Memento>(controller());
  73. saveState(result.get());
  74. return result;
  75. }
  76. void Widget::saveState(not_null<Memento*> memento) {
  77. memento->setScrollTop(scrollTopSave());
  78. _inner->saveState(memento);
  79. }
  80. void Widget::restoreState(not_null<Memento*> memento) {
  81. _inner->restoreState(memento);
  82. scrollTopRestore(memento->scrollTop());
  83. }
  84. rpl::producer<SelectedItems> Widget::selectedListValue() const {
  85. return _inner->selectedListValue();
  86. }
  87. void Widget::selectionAction(SelectionAction action) {
  88. _inner->selectionAction(action);
  89. }
  90. void Widget::fillTopBarMenu(const Ui::Menu::MenuCallback &addAction) {
  91. const auto window = controller()->parentController();
  92. const auto deleteAll = [=] {
  93. auto &manager = Core::App().downloadManager();
  94. const auto phrase = tr::lng_downloads_delete_sure_all(tr::now);
  95. const auto added = manager.loadedHasNonCloudFile()
  96. ? QString()
  97. : tr::lng_downloads_delete_in_cloud(tr::now);
  98. const auto deleteSure = [=, &manager](Fn<void()> close) {
  99. Ui::PostponeCall(this, close);
  100. manager.deleteAll();
  101. };
  102. window->show(Ui::MakeConfirmBox({
  103. .text = phrase + (added.isEmpty() ? QString() : "\n\n" + added),
  104. .confirmed = deleteSure,
  105. .confirmText = tr::lng_box_delete(tr::now),
  106. .confirmStyle = &st::attentionBoxButton,
  107. }));
  108. };
  109. addAction(
  110. tr::lng_context_delete_all_files(tr::now),
  111. deleteAll,
  112. &st::menuIconDelete);
  113. }
  114. rpl::producer<QString> Widget::title() {
  115. return tr::lng_downloads_section();
  116. }
  117. std::shared_ptr<Info::Memento> Make(not_null<UserData*> self) {
  118. return std::make_shared<Info::Memento>(
  119. std::vector<std::shared_ptr<ContentMemento>>(
  120. 1,
  121. std::make_shared<Memento>(self)));
  122. }
  123. } // namespace Info::Downloads