tdesktop/Telegram/SourceFiles/dialogs/dialogs_entry.cpp

221 lines
4.8 KiB
C++

/*
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"
#include "data/data_session.h"
#include "mainwidget.h"
#include "auth_session.h"
#include "history/history_item.h"
#include "history/history.h"
#include "styles/style_dialogs.h" // st::dialogsTextWidthMin
namespace Dialogs {
namespace {
auto DialogsPosToTopShift = 0;
uint64 DialogPosFromDate(TimeId date) {
if (!date) {
return 0;
}
return (uint64(date) << 32) | (++DialogsPosToTopShift);
}
uint64 ProxyPromotedDialogPos() {
return 0xFFFFFFFFFFFF0001ULL;
}
uint64 PinnedDialogPos(int pinnedIndex) {
return 0xFFFFFFFF00000000ULL + pinnedIndex;
}
} // namespace
Entry::Entry(not_null<Data::Session*> owner, const Key &key)
: lastItemTextCache(st::dialogsTextWidthMin)
, _owner(owner)
, _key(key) {
}
Data::Session &Entry::owner() const {
return *_owner;
}
AuthSession &Entry::session() const {
return _owner->session();
}
void Entry::cachePinnedIndex(int index) {
if (_pinnedIndex != index) {
const auto wasPinned = isPinnedDialog();
_pinnedIndex = index;
updateChatListSortPosition();
updateChatListEntry();
if (wasPinned != isPinnedDialog()) {
changedChatListPinHook();
}
}
}
void Entry::cacheProxyPromoted(bool promoted) {
if (_isProxyPromoted != promoted) {
_isProxyPromoted = promoted;
updateChatListSortPosition();
updateChatListEntry();
if (!_isProxyPromoted) {
updateChatListExistence();
}
}
}
bool Entry::needUpdateInChatList() const {
return inChatList(Dialogs::Mode::All) || shouldBeInChatList();
}
void Entry::updateChatListSortPosition() {
if (Auth().supportMode()
&& _sortKeyInChatList != 0
&& Auth().settings().supportFixChatsOrder()) {
updateChatListEntry();
return;
}
_sortKeyInChatList = useProxyPromotion()
? ProxyPromotedDialogPos()
: isPinnedDialog()
? PinnedDialogPos(_pinnedIndex)
: DialogPosFromDate(adjustChatListTimeId());
if (needUpdateInChatList()) {
setChatListExistence(true);
}
}
void Entry::updateChatListExistence() {
setChatListExistence(shouldBeInChatList());
}
void Entry::setChatListExistence(bool exists) {
if (const auto main = App::main()) {
if (exists && _sortKeyInChatList) {
main->refreshDialog(_key);
updateChatListEntry();
} else {
main->removeDialog(_key);
}
}
}
TimeId Entry::adjustChatListTimeId() const {
return chatListTimeId();
}
void Entry::changedInChatListHook(Dialogs::Mode list, bool added) {
}
void Entry::changedChatListPinHook() {
}
RowsByLetter &Entry::chatListLinks(Mode list) {
return _chatListLinks[static_cast<int>(list)];
}
const RowsByLetter &Entry::chatListLinks(Mode list) const {
return _chatListLinks[static_cast<int>(list)];
}
Row *Entry::mainChatListLink(Mode list) const {
auto it = chatListLinks(list).find(0);
Assert(it != chatListLinks(list).cend());
return it->second;
}
PositionChange Entry::adjustByPosInChatList(
Mode list,
not_null<IndexedList*> indexed) {
const auto lnk = mainChatListLink(list);
const auto from = lnk->pos();
indexed->adjustByDate(chatListLinks(list));
const auto to = lnk->pos();
return { from, to };
}
void Entry::setChatListTimeId(TimeId date) {
if (_timeId && _timeId >= date) {
if (!inChatList(Dialogs::Mode::All)) {
return;
}
}
_timeId = date;
updateChatListSortPosition();
}
int Entry::posInChatList(Dialogs::Mode list) const {
return mainChatListLink(list)->pos();
}
not_null<Row*> Entry::addToChatList(
Mode list,
not_null<IndexedList*> indexed) {
if (!inChatList(list)) {
chatListLinks(list) = indexed->addToEnd(_key);
changedInChatListHook(list, true);
}
return mainChatListLink(list);
}
void Entry::removeFromChatList(
Dialogs::Mode list,
not_null<Dialogs::IndexedList*> indexed) {
if (inChatList(list)) {
indexed->del(_key);
chatListLinks(list).clear();
changedInChatListHook(list, false);
}
}
void Entry::removeChatListEntryByLetter(Mode list, QChar letter) {
Expects(letter != 0);
if (inChatList(list)) {
chatListLinks(list).remove(letter);
}
}
void Entry::addChatListEntryByLetter(
Mode list,
QChar letter,
not_null<Row*> row) {
Expects(letter != 0);
if (inChatList(list)) {
chatListLinks(list).emplace(letter, row);
}
}
void Entry::updateChatListEntry() const {
if (const auto main = App::main()) {
if (inChatList(Mode::All)) {
main->repaintDialogRow(
Mode::All,
mainChatListLink(Mode::All));
if (inChatList(Mode::Important)) {
main->repaintDialogRow(
Mode::Important,
mainChatListLink(Mode::Important));
}
}
if (Auth().supportMode()
&& !Auth().settings().supportAllSearchResults()) {
main->repaintDialogRow({ _key, FullMsgId() });
}
}
}
} // namespace Dialogs