2018-01-05 15:57:18 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "dialogs/dialogs_entry.h"
|
|
|
|
|
|
|
|
#include "dialogs/dialogs_key.h"
|
|
|
|
#include "dialogs/dialogs_indexed_list.h"
|
2020-06-12 14:09:04 +00:00
|
|
|
#include "data/data_changes.h"
|
2019-04-16 14:05:56 +00:00
|
|
|
#include "data/data_session.h"
|
2019-04-17 13:22:37 +00:00
|
|
|
#include "data/data_folder.h"
|
2022-09-20 18:12:30 +00:00
|
|
|
#include "data/data_forum_topic.h"
|
2020-03-09 11:17:56 +00:00
|
|
|
#include "data/data_chat_filters.h"
|
2022-10-20 08:57:12 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2018-01-05 15:57:18 +00:00
|
|
|
#include "mainwidget.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-06-18 12:47:09 +00:00
|
|
|
#include "main/main_session_settings.h"
|
2022-08-09 11:12:19 +00:00
|
|
|
#include "ui/text/text_options.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2022-10-07 13:56:07 +00:00
|
|
|
#include "history/history_item.h"
|
2019-04-16 14:05:56 +00:00
|
|
|
#include "styles/style_dialogs.h" // st::dialogsTextWidthMin
|
2018-01-05 15:57:18 +00:00
|
|
|
|
|
|
|
namespace Dialogs {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
auto DialogsPosToTopShift = 0;
|
|
|
|
|
2018-07-31 14:34:14 +00:00
|
|
|
uint64 DialogPosFromDate(TimeId date) {
|
|
|
|
if (!date) {
|
2018-01-05 15:57:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-31 14:34:14 +00:00
|
|
|
return (uint64(date) << 32) | (++DialogsPosToTopShift);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 11:31:30 +00:00
|
|
|
uint64 FixedOnTopDialogPos(int index) {
|
|
|
|
return 0xFFFFFFFFFFFF000FULL - index;
|
2018-05-11 14:03:53 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
uint64 PinnedDialogPos(int pinnedIndex) {
|
2019-04-19 08:47:49 +00:00
|
|
|
return 0xFFFFFFFF000000FFULL - pinnedIndex;
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-10-20 08:57:12 +00:00
|
|
|
BadgesState BadgesForUnread(
|
|
|
|
const UnreadState &state,
|
|
|
|
CountInBadge count,
|
|
|
|
IncludeInBadge include) {
|
|
|
|
const auto countMessages = (count == CountInBadge::Messages)
|
|
|
|
|| ((count == CountInBadge::Default)
|
|
|
|
&& Core::App().settings().countUnreadMessages());
|
|
|
|
const auto counterFull = state.marks
|
|
|
|
+ (countMessages ? state.messages : state.chats);
|
|
|
|
const auto counterMuted = state.marksMuted
|
|
|
|
+ (countMessages ? state.messagesMuted : state.chatsMuted);
|
|
|
|
const auto unreadMuted = (counterFull <= counterMuted);
|
|
|
|
|
|
|
|
const auto includeMuted = (include == IncludeInBadge::All)
|
|
|
|
|| (include == IncludeInBadge::UnmutedOrAll && unreadMuted)
|
|
|
|
|| ((include == IncludeInBadge::Default)
|
|
|
|
&& Core::App().settings().includeMutedCounter());
|
|
|
|
|
|
|
|
const auto marks = state.marks - (includeMuted ? 0 : state.marksMuted);
|
|
|
|
const auto counter = counterFull - (includeMuted ? 0 : counterMuted);
|
|
|
|
const auto mark = (counter == 1) && (marks == 1);
|
|
|
|
return {
|
|
|
|
.unreadCounter = mark ? 0 : counter,
|
|
|
|
.unread = (counter > 0),
|
|
|
|
.unreadMuted = includeMuted && (counter <= counterMuted),
|
|
|
|
.mention = (state.mentions > 0),
|
|
|
|
.reaction = (state.reactions > 0),
|
|
|
|
.reactionMuted = (state.reactions <= state.reactionsMuted),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:09:04 +00:00
|
|
|
Entry::Entry(not_null<Data::Session*> owner, Type type)
|
2021-10-01 12:42:44 +00:00
|
|
|
: _owner(owner)
|
2022-10-19 13:55:33 +00:00
|
|
|
, _flags((type == Type::History)
|
|
|
|
? (Flag::IsThread | Flag::IsHistory)
|
|
|
|
: (type == Type::ForumTopic)
|
|
|
|
? Flag::IsThread
|
|
|
|
: Flag(0)) {
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
Entry::~Entry() = default;
|
|
|
|
|
2019-04-16 14:05:56 +00:00
|
|
|
Data::Session &Entry::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Main::Session &Entry::session() const {
|
2019-04-16 14:05:56 +00:00
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:09:04 +00:00
|
|
|
History *Entry::asHistory() {
|
2022-10-19 13:55:33 +00:00
|
|
|
return (_flags & Flag::IsHistory) ? static_cast<History*>(this) : nullptr;
|
2020-06-12 14:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Data::Folder *Entry::asFolder() {
|
2022-10-19 13:55:33 +00:00
|
|
|
return (_flags & Flag::IsThread)
|
|
|
|
? nullptr
|
|
|
|
: static_cast<Data::Folder*>(this);
|
2022-09-20 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:32:03 +00:00
|
|
|
Data::Thread *Entry::asThread() {
|
2022-10-19 13:55:33 +00:00
|
|
|
return (_flags & Flag::IsThread)
|
2022-10-13 10:32:03 +00:00
|
|
|
? static_cast<Data::Thread*>(this)
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:21:31 +00:00
|
|
|
Data::ForumTopic *Entry::asTopic() {
|
2022-10-19 13:55:33 +00:00
|
|
|
return ((_flags & Flag::IsThread) && !(_flags & Flag::IsHistory))
|
2022-09-20 18:12:30 +00:00
|
|
|
? static_cast<Data::ForumTopic*>(this)
|
|
|
|
: nullptr;
|
2020-06-12 14:09:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:34:04 +00:00
|
|
|
const History *Entry::asHistory() const {
|
|
|
|
return const_cast<Entry*>(this)->asHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Data::Folder *Entry::asFolder() const {
|
|
|
|
return const_cast<Entry*>(this)->asFolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Data::Thread *Entry::asThread() const {
|
|
|
|
return const_cast<Entry*>(this)->asThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Data::ForumTopic *Entry::asTopic() const {
|
|
|
|
return const_cast<Entry*>(this)->asTopic();
|
|
|
|
}
|
|
|
|
|
2021-12-02 07:51:54 +00:00
|
|
|
void Entry::pinnedIndexChanged(FilterId filterId, int was, int now) {
|
|
|
|
if (!filterId && session().supportMode()) {
|
2020-03-17 13:04:30 +00:00
|
|
|
// Force reorder in support mode.
|
|
|
|
_sortKeyInChatList = 0;
|
|
|
|
}
|
|
|
|
updateChatListSortPosition();
|
|
|
|
updateChatListEntry();
|
|
|
|
if ((was != 0) != (now != 0)) {
|
|
|
|
changedChatListPinHook();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Entry::cachePinnedIndex(FilterId filterId, int index) {
|
|
|
|
const auto i = _pinnedIndex.find(filterId);
|
|
|
|
const auto was = (i != end(_pinnedIndex)) ? i->second : 0;
|
|
|
|
if (index == was) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!index) {
|
|
|
|
_pinnedIndex.erase(i);
|
2021-12-02 07:51:54 +00:00
|
|
|
} else if (!was) {
|
|
|
|
_pinnedIndex.emplace(filterId, index);
|
2020-03-17 13:04:30 +00:00
|
|
|
} else {
|
2021-12-02 07:51:54 +00:00
|
|
|
i->second = index;
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
2021-12-02 07:51:54 +00:00
|
|
|
pinnedIndexChanged(filterId, was, index);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 16:51:12 +00:00
|
|
|
bool Entry::needUpdateInChatList() const {
|
2019-04-22 14:22:39 +00:00
|
|
|
return inChatList() || shouldBeInChatList();
|
2018-01-23 16:51:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
void Entry::updateChatListSortPosition() {
|
2019-04-30 10:01:34 +00:00
|
|
|
if (session().supportMode()
|
2018-09-29 12:18:26 +00:00
|
|
|
&& _sortKeyInChatList != 0
|
2019-04-30 10:01:34 +00:00
|
|
|
&& session().settings().supportFixChatsOrder()) {
|
2018-12-30 09:16:50 +00:00
|
|
|
updateChatListEntry();
|
2018-09-29 12:18:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-09 11:17:56 +00:00
|
|
|
_sortKeyByDate = DialogPosFromDate(adjustedChatListTimeId());
|
2019-04-18 11:31:30 +00:00
|
|
|
const auto fixedIndex = fixedOnTopIndex();
|
|
|
|
_sortKeyInChatList = fixedIndex
|
|
|
|
? FixedOnTopDialogPos(fixedIndex)
|
2020-03-17 13:04:30 +00:00
|
|
|
: computeSortPosition(0);
|
2018-01-23 16:51:12 +00:00
|
|
|
if (needUpdateInChatList()) {
|
|
|
|
setChatListExistence(true);
|
2019-04-30 10:01:34 +00:00
|
|
|
} else {
|
2020-03-09 11:17:56 +00:00
|
|
|
_sortKeyInChatList = _sortKeyByDate = 0;
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 13:04:30 +00:00
|
|
|
int Entry::lookupPinnedIndex(FilterId filterId) const {
|
|
|
|
if (filterId) {
|
|
|
|
const auto i = _pinnedIndex.find(filterId);
|
|
|
|
return (i != end(_pinnedIndex)) ? i->second : 0;
|
|
|
|
} else if (!_pinnedIndex.empty()) {
|
|
|
|
return _pinnedIndex.front().first
|
|
|
|
? 0
|
|
|
|
: _pinnedIndex.front().second;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Entry::computeSortPosition(FilterId filterId) const {
|
|
|
|
const auto index = lookupPinnedIndex(filterId);
|
|
|
|
return index ? PinnedDialogPos(index) : _sortKeyByDate;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:51:12 +00:00
|
|
|
void Entry::updateChatListExistence() {
|
|
|
|
setChatListExistence(shouldBeInChatList());
|
|
|
|
}
|
|
|
|
|
2019-04-22 14:22:39 +00:00
|
|
|
void Entry::notifyUnreadStateChange(const UnreadState &wasState) {
|
2020-03-16 10:20:18 +00:00
|
|
|
Expects(folderKnown());
|
|
|
|
Expects(inChatList());
|
|
|
|
|
|
|
|
const auto nowState = chatListUnreadState();
|
2022-10-07 11:46:27 +00:00
|
|
|
owner().chatsListFor(this)->unreadStateChanged(wasState, nowState);
|
2020-03-16 10:20:18 +00:00
|
|
|
auto &filters = owner().chatsFilters();
|
|
|
|
for (const auto &[filterId, links] : _chatListLinks) {
|
2022-10-20 08:57:12 +00:00
|
|
|
if (filterId) {
|
|
|
|
filters.chatsList(filterId)->unreadStateChanged(
|
|
|
|
wasState,
|
|
|
|
nowState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto history = asHistory()) {
|
|
|
|
session().changes().historyUpdated(
|
|
|
|
history,
|
|
|
|
Data::HistoryUpdate::Flag::UnreadView);
|
|
|
|
const auto isForFilters = [](UnreadState state) {
|
|
|
|
return state.messages || state.marks || state.mentions;
|
|
|
|
};
|
|
|
|
if (isForFilters(wasState) != isForFilters(nowState)) {
|
|
|
|
owner().chatsFilters().refreshHistory(history);
|
|
|
|
}
|
2020-03-16 10:20:18 +00:00
|
|
|
}
|
2022-10-20 08:57:12 +00:00
|
|
|
updateChatListEntryPostponed();
|
2019-04-22 14:22:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 11:12:19 +00:00
|
|
|
const Ui::Text::String &Entry::chatListNameText() const {
|
|
|
|
const auto version = chatListNameVersion();
|
|
|
|
if (_chatListNameVersion < version) {
|
|
|
|
_chatListNameVersion = version;
|
|
|
|
_chatListNameText.setText(
|
2022-09-30 07:25:02 +00:00
|
|
|
st::semiboldTextStyle,
|
2022-08-09 11:12:19 +00:00
|
|
|
chatListName(),
|
|
|
|
Ui::NameTextOptions());
|
|
|
|
}
|
|
|
|
return _chatListNameText;
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:51:12 +00:00
|
|
|
void Entry::setChatListExistence(bool exists) {
|
2020-06-12 14:09:04 +00:00
|
|
|
if (exists && _sortKeyInChatList) {
|
|
|
|
owner().refreshChatListEntry(this);
|
|
|
|
updateChatListEntry();
|
|
|
|
} else {
|
|
|
|
owner().removeChatListEntry(this);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:00:38 +00:00
|
|
|
TimeId Entry::adjustedChatListTimeId() const {
|
2019-01-15 11:57:45 +00:00
|
|
|
return chatListTimeId();
|
2018-01-23 16:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Entry::changedChatListPinHook() {
|
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
RowsByLetter *Entry::chatListLinks(FilterId filterId) {
|
|
|
|
const auto i = _chatListLinks.find(filterId);
|
|
|
|
return (i != end(_chatListLinks)) ? &i->second : nullptr;
|
2018-01-23 16:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
const RowsByLetter *Entry::chatListLinks(FilterId filterId) const {
|
|
|
|
const auto i = _chatListLinks.find(filterId);
|
|
|
|
return (i != end(_chatListLinks)) ? &i->second : nullptr;
|
2018-01-23 16:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
not_null<Row*> Entry::mainChatListLink(FilterId filterId) const {
|
|
|
|
const auto links = chatListLinks(filterId);
|
|
|
|
Assert(links != nullptr);
|
|
|
|
return links->main;
|
2018-01-23 16:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
Row *Entry::maybeMainChatListLink(FilterId filterId) const {
|
|
|
|
const auto links = chatListLinks(filterId);
|
|
|
|
return links ? links->main.get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-16 10:20:18 +00:00
|
|
|
PositionChange Entry::adjustByPosInChatList(
|
|
|
|
FilterId filterId,
|
|
|
|
not_null<MainList*> list) {
|
2020-02-07 09:43:12 +00:00
|
|
|
const auto links = chatListLinks(filterId);
|
|
|
|
Assert(links != nullptr);
|
|
|
|
const auto from = links->main->pos();
|
2020-03-16 10:20:18 +00:00
|
|
|
list->indexed()->adjustByDate(*links);
|
2020-02-07 09:43:12 +00:00
|
|
|
const auto to = links->main->pos();
|
2019-04-16 14:05:56 +00:00
|
|
|
return { from, to };
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 11:57:45 +00:00
|
|
|
void Entry::setChatListTimeId(TimeId date) {
|
|
|
|
_timeId = date;
|
2018-01-05 15:57:18 +00:00
|
|
|
updateChatListSortPosition();
|
2019-04-18 09:00:38 +00:00
|
|
|
if (const auto folder = this->folder()) {
|
|
|
|
folder->updateChatListSortPosition();
|
|
|
|
}
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
int Entry::posInChatList(FilterId filterId) const {
|
|
|
|
return mainChatListLink(filterId)->pos();
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 10:20:18 +00:00
|
|
|
not_null<Row*> Entry::addToChatList(
|
|
|
|
FilterId filterId,
|
|
|
|
not_null<MainList*> list) {
|
2020-02-07 09:43:12 +00:00
|
|
|
if (const auto main = maybeMainChatListLink(filterId)) {
|
|
|
|
return main;
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
2020-03-16 10:20:18 +00:00
|
|
|
return _chatListLinks.emplace(
|
2020-02-07 09:43:12 +00:00
|
|
|
filterId,
|
2020-06-12 14:09:04 +00:00
|
|
|
list->addEntry(this)
|
2020-02-07 09:43:12 +00:00
|
|
|
).first->second.main;
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 10:20:18 +00:00
|
|
|
void Entry::removeFromChatList(
|
|
|
|
FilterId filterId,
|
|
|
|
not_null<MainList*> list) {
|
2022-09-23 19:21:31 +00:00
|
|
|
if (!asTopic() && isPinnedDialog(filterId)) {
|
2020-06-12 14:09:04 +00:00
|
|
|
owner().setChatPinned(this, filterId, false);
|
2020-03-29 08:33:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
const auto i = _chatListLinks.find(filterId);
|
|
|
|
if (i == end(_chatListLinks)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_chatListLinks.erase(i);
|
2020-06-12 14:09:04 +00:00
|
|
|
list->removeEntry(this);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:43:12 +00:00
|
|
|
void Entry::removeChatListEntryByLetter(FilterId filterId, QChar letter) {
|
|
|
|
const auto i = _chatListLinks.find(filterId);
|
|
|
|
if (i != end(_chatListLinks)) {
|
|
|
|
i->second.letters.remove(letter);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Entry::addChatListEntryByLetter(
|
2020-02-07 09:43:12 +00:00
|
|
|
FilterId filterId,
|
2018-01-05 15:57:18 +00:00
|
|
|
QChar letter,
|
|
|
|
not_null<Row*> row) {
|
2020-02-07 09:43:12 +00:00
|
|
|
const auto i = _chatListLinks.find(filterId);
|
|
|
|
if (i != end(_chatListLinks)) {
|
|
|
|
i->second.letters.emplace(letter, row);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:09:04 +00:00
|
|
|
void Entry::updateChatListEntry() {
|
2022-10-19 13:55:33 +00:00
|
|
|
_flags &= ~Flag::UpdatePostponed;
|
2020-06-12 14:09:04 +00:00
|
|
|
session().changes().entryUpdated(this, Data::EntryUpdate::Flag::Repaint);
|
2018-01-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 13:55:33 +00:00
|
|
|
void Entry::updateChatListEntryPostponed() {
|
|
|
|
if (_flags & Flag::UpdatePostponed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_flags |= Flag::UpdatePostponed;
|
|
|
|
Ui::PostponeCall(this, [=] {
|
|
|
|
if (_flags & Flag::UpdatePostponed) {
|
|
|
|
updateChatListEntry();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-05 15:57:18 +00:00
|
|
|
} // namespace Dialogs
|