"Fossies" - the Fresh Open Source Software Archive

Member "tdesktop-2.6.1/Telegram/SourceFiles/dialogs/dialogs_main_list.cpp" (24 Feb 2021, 5360 Bytes) of package /linux/misc/tdesktop-2.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dialogs_main_list.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2 This file is part of Telegram Desktop,
    3 the official desktop application for the Telegram messaging service.
    4 
    5 For license and copyright information please follow this link:
    6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
    7 */
    8 #include "dialogs/dialogs_main_list.h"
    9 
   10 #include "data/data_changes.h"
   11 #include "main/main_session.h"
   12 #include "history/history.h"
   13 
   14 namespace Dialogs {
   15 
   16 MainList::MainList(
   17     not_null<Main::Session*> session,
   18     FilterId filterId,
   19     rpl::producer<int> pinnedLimit)
   20 : _filterId(filterId)
   21 , _all(SortMode::Date, filterId)
   22 , _pinned(filterId, 1) {
   23     _unreadState.known = true;
   24 
   25     std::move(
   26         pinnedLimit
   27     ) | rpl::start_with_next([=](int limit) {
   28         _pinned.setLimit(limit);
   29     }, _lifetime);
   30 
   31     session->changes().realtimeNameUpdates(
   32     ) | rpl::start_with_next([=](const Data::NameUpdate &update) {
   33         _all.peerNameChanged(_filterId, update.peer, update.oldFirstLetters);
   34     }, _lifetime);
   35 }
   36 
   37 bool MainList::empty() const {
   38     return _all.empty();
   39 }
   40 
   41 bool MainList::loaded() const {
   42     return _loaded;
   43 }
   44 
   45 void MainList::setLoaded(bool loaded) {
   46     if (_loaded == loaded) {
   47         return;
   48     }
   49     const auto recomputer = gsl::finally([&] {
   50         recomputeFullListSize();
   51     });
   52     const auto notifier = unreadStateChangeNotifier(true);
   53     _loaded = loaded;
   54 }
   55 
   56 void MainList::setAllAreMuted(bool allAreMuted) {
   57     if (_allAreMuted == allAreMuted) {
   58         return;
   59     }
   60     const auto notifier = unreadStateChangeNotifier(true);
   61     _allAreMuted = allAreMuted;
   62 }
   63 
   64 void MainList::setCloudListSize(int size) {
   65     if (_cloudListSize == size) {
   66         return;
   67     }
   68     _cloudListSize = size;
   69     recomputeFullListSize();
   70 }
   71 
   72 const rpl::variable<int> &MainList::fullSize() const {
   73     return _fullListSize;
   74 }
   75 
   76 void MainList::clear() {
   77     const auto recomputer = gsl::finally([&] {
   78         recomputeFullListSize();
   79     });
   80     const auto notifier = unreadStateChangeNotifier(true);
   81     _all.clear();
   82     _unreadState = UnreadState();
   83     _cloudUnreadState = UnreadState();
   84     _unreadState.known = true;
   85     _cloudUnreadState.known = true;
   86     _cloudListSize = 0;
   87 }
   88 
   89 RowsByLetter MainList::addEntry(const Key &key) {
   90     const auto result = _all.addToEnd(key);
   91 
   92     const auto unread = key.entry()->chatListUnreadState();
   93     unreadEntryChanged(unread, true);
   94     recomputeFullListSize();
   95 
   96     return result;
   97 }
   98 
   99 void MainList::removeEntry(const Key &key) {
  100     _all.del(key);
  101 
  102     const auto unread = key.entry()->chatListUnreadState();
  103     unreadEntryChanged(unread, false);
  104     recomputeFullListSize();
  105 }
  106 
  107 void MainList::recomputeFullListSize() {
  108     _fullListSize = std::max(_all.size(), loaded() ? 0 : _cloudListSize);
  109 }
  110 
  111 void MainList::unreadStateChanged(
  112         const UnreadState &wasState,
  113         const UnreadState &nowState) {
  114     const auto useClouded = _cloudUnreadState.known && !loaded();
  115     const auto updateCloudUnread = _cloudUnreadState.known && wasState.known;
  116     const auto notify = !useClouded || wasState.known;
  117     const auto notifier = unreadStateChangeNotifier(notify);
  118     _unreadState += nowState - wasState;
  119     if (updateCloudUnread) {
  120         Assert(nowState.known);
  121         _cloudUnreadState += nowState - wasState;
  122         finalizeCloudUnread();
  123     }
  124 }
  125 
  126 void MainList::unreadEntryChanged(
  127         const Dialogs::UnreadState &state,
  128         bool added) {
  129     if (state.empty()) {
  130         return;
  131     }
  132     const auto updateCloudUnread = _cloudUnreadState.known && state.known;
  133     const auto notify = !_cloudUnreadState.known || loaded() || state.known;
  134     const auto notifier = unreadStateChangeNotifier(notify);
  135     if (added) {
  136         _unreadState += state;
  137     } else {
  138         _unreadState -= state;
  139     }
  140     if (updateCloudUnread) {
  141         if (added) {
  142             _cloudUnreadState += state;
  143         } else {
  144             _cloudUnreadState -= state;
  145         }
  146         finalizeCloudUnread();
  147     }
  148 }
  149 
  150 void MainList::updateCloudUnread(const MTPDdialogFolder &data) {
  151     const auto notifier = unreadStateChangeNotifier(!loaded());
  152 
  153     _cloudUnreadState.messages = data.vunread_muted_messages_count().v
  154         + data.vunread_unmuted_messages_count().v;
  155     _cloudUnreadState.chats = data.vunread_muted_peers_count().v
  156         + data.vunread_unmuted_peers_count().v;
  157     finalizeCloudUnread();
  158 
  159     _cloudUnreadState.known = true;
  160 }
  161 
  162 bool MainList::cloudUnreadKnown() const {
  163     return _cloudUnreadState.known;
  164 }
  165 
  166 void MainList::finalizeCloudUnread() {
  167     // Cloud state for archive folder always counts everything as muted.
  168     _cloudUnreadState.messagesMuted = _cloudUnreadState.messages;
  169     _cloudUnreadState.chatsMuted = _cloudUnreadState.chats;
  170 
  171     // We don't know the real value of marked chats counts in cloud unread.
  172     _cloudUnreadState.marksMuted = _cloudUnreadState.marks = 0;
  173 }
  174 
  175 UnreadState MainList::unreadState() const {
  176     const auto useCloudState = _cloudUnreadState.known && !loaded();
  177     auto result = useCloudState ? _cloudUnreadState : _unreadState;
  178 
  179     // We don't know the real value of marked chats counts in cloud unread.
  180     if (useCloudState) {
  181         result.marks = _unreadState.marks;
  182         result.marksMuted = _unreadState.marksMuted;
  183     }
  184     if (_allAreMuted) {
  185         result.messagesMuted = result.messages;
  186         result.chatsMuted = result.chats;
  187         result.marksMuted = result.marks;
  188     }
  189     return result;
  190 }
  191 
  192 rpl::producer<UnreadState> MainList::unreadStateChanges() const {
  193     return _unreadStateChanges.events();
  194 }
  195 
  196 not_null<IndexedList*> MainList::indexed() {
  197     return &_all;
  198 }
  199 
  200 not_null<const IndexedList*> MainList::indexed() const {
  201     return &_all;
  202 }
  203 
  204 not_null<PinnedList*> MainList::pinned() {
  205     return &_pinned;
  206 }
  207 
  208 not_null<const PinnedList*> MainList::pinned() const {
  209     return &_pinned;
  210 }
  211 
  212 } // namespace Dialogs