tdesktop/Telegram/SourceFiles/data/data_folder.cpp

388 lines
9.7 KiB
C++
Raw Normal View History

2018-01-04 09:40:58 +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
*/
2019-04-15 11:54:03 +00:00
#include "data/data_folder.h"
2018-01-04 09:40:58 +00:00
#include "data/data_session.h"
#include "data/data_channel.h"
2020-02-21 07:58:50 +00:00
#include "data/data_histories.h"
#include "data/data_changes.h"
#include "dialogs/dialogs_key.h"
#include "dialogs/ui/dialogs_layout.h"
#include "history/history.h"
#include "history/history_item.h"
2022-09-16 20:23:27 +00:00
#include "ui/painter.h"
#include "lang/lang_keys.h"
#include "storage/storage_facade.h"
2019-04-22 14:22:39 +00:00
#include "core/application.h"
#include "core/core_settings.h"
2019-07-24 08:46:23 +00:00
#include "main/main_account.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "mtproto/mtproto_config.h"
2018-01-29 17:13:24 +00:00
#include "apiwrap.h"
#include "mainwidget.h"
#include "styles/style_dialogs.h"
2018-01-04 09:40:58 +00:00
namespace Data {
2019-04-17 12:08:02 +00:00
namespace {
constexpr auto kLoadedChatsMinCount = 20;
2019-05-08 08:50:39 +00:00
constexpr auto kShowChatNamesCount = 8;
2019-04-17 12:08:02 +00:00
} // namespace
2018-01-04 09:40:58 +00:00
2019-04-15 11:54:03 +00:00
Folder::Folder(not_null<Data::Session*> owner, FolderId id)
2020-06-12 14:09:04 +00:00
: Entry(owner, Type::Folder)
, _id(id)
, _chatsList(
&owner->session(),
FilterId(),
2022-05-09 13:09:46 +00:00
owner->maxPinnedChatsLimitValue(this, FilterId()))
2022-09-20 18:12:30 +00:00
, _name(tr::lng_archived_name(tr::now)) {
indexNameParts();
session().changes().peerUpdates(
PeerUpdate::Flag::Name
) | rpl::filter([=](const PeerUpdate &update) {
return ranges::contains(_lastHistories, update.peer, &History::peer);
}) | rpl::start_with_next([=] {
++_chatListViewVersion;
updateChatListEntryPostponed();
}, _lifetime);
_chatsList.setAllAreMuted(true);
_chatsList.unreadStateChanges(
) | rpl::filter([=] {
return inChatList();
}) | rpl::start_with_next([=](const Dialogs::UnreadState &old) {
++_chatListViewVersion;
notifyUnreadStateChange(old);
updateChatListEntryPostponed();
}, _lifetime);
_chatsList.fullSize().changes(
) | rpl::start_with_next([=] {
updateChatListEntryPostponed();
}, _lifetime);
}
void Folder::updateChatListEntryPostponed() {
if (_updateChatListEntryPostponed) {
return;
}
_updateChatListEntryPostponed = true;
Ui::PostponeCall(this, [=] {
updateChatListEntry();
_updateChatListEntryPostponed = false;
});
}
2019-04-15 11:54:03 +00:00
FolderId Folder::id() const {
return _id;
}
2019-04-15 11:54:03 +00:00
void Folder::indexNameParts() {
// We don't want archive to be filtered in the chats list.
//_nameWords.clear();
//_nameFirstLetters.clear();
//auto toIndexList = QStringList();
//auto appendToIndex = [&](const QString &value) {
// if (!value.isEmpty()) {
// toIndexList.push_back(TextUtilities::RemoveAccents(value));
// }
//};
//appendToIndex(_name);
//const auto appendTranslit = !toIndexList.isEmpty()
// && cRussianLetters().match(toIndexList.front()).hasMatch();
//if (appendTranslit) {
// appendToIndex(translitRusEng(toIndexList.front()));
//}
//auto toIndex = toIndexList.join(' ');
//toIndex += ' ' + rusKeyboardLayoutSwitch(toIndex);
//const auto namesList = TextUtilities::PrepareSearchWords(toIndex);
//for (const auto &name : namesList) {
// _nameWords.insert(name);
// _nameFirstLetters.insert(name[0]);
//}
2018-01-04 09:40:58 +00:00
}
void Folder::registerOne(not_null<History*> history) {
2019-04-22 14:22:39 +00:00
if (_chatsList.indexed()->size() == 1) {
2019-04-18 11:31:30 +00:00
updateChatListSortPosition();
if (!_chatsList.cloudUnreadKnown()) {
2020-02-21 07:58:50 +00:00
owner().histories().requestDialogEntry(this);
2019-04-24 14:53:12 +00:00
}
} else {
updateChatListEntry();
}
applyChatListMessage(history->chatListMessage());
2019-05-08 08:50:39 +00:00
reorderLastHistories();
2018-01-04 09:40:58 +00:00
}
void Folder::unregisterOne(not_null<History*> history) {
if (_chatsList.empty()) {
updateChatListExistence();
}
if (_chatListMessage && _chatListMessage->history() == history) {
computeChatListMessage();
}
2019-05-08 08:50:39 +00:00
reorderLastHistories();
}
2022-08-09 11:12:19 +00:00
int Folder::chatListNameVersion() const {
return 1;
}
void Folder::oneListMessageChanged(HistoryItem *from, HistoryItem *to) {
if (!applyChatListMessage(to) && _chatListMessage == from) {
computeChatListMessage();
}
if (from || to) {
2019-05-08 08:50:39 +00:00
reorderLastHistories();
}
}
bool Folder::applyChatListMessage(HistoryItem *item) {
if (!item) {
return false;
} else if (_chatListMessage
&& _chatListMessage->date() >= item->date()) {
return false;
}
_chatListMessage = item;
updateChatListEntry();
return true;
}
void Folder::computeChatListMessage() {
auto &&items = ranges::views::all(
*_chatsList.indexed()
) | ranges::views::filter([](not_null<Dialogs::Row*> row) {
return row->entry()->chatListMessage() != nullptr;
});
const auto chatListDate = [](not_null<Dialogs::Row*> row) {
return row->entry()->chatListMessage()->date();
};
const auto top = ranges::max_element(
items,
ranges::less(),
chatListDate);
if (top == items.end()) {
_chatListMessage = nullptr;
} else {
_chatListMessage = (*top)->entry()->chatListMessage();
}
updateChatListEntry();
}
2019-05-08 08:50:39 +00:00
void Folder::reorderLastHistories() {
// We want first kShowChatNamesCount histories, by last message date.
2019-05-08 08:50:39 +00:00
const auto pred = [](not_null<History*> a, not_null<History*> b) {
const auto aItem = a->chatListMessage();
const auto bItem = b->chatListMessage();
const auto aDate = aItem ? aItem->date() : TimeId(0);
const auto bDate = bItem ? bItem->date() : TimeId(0);
return aDate > bDate;
};
2019-05-08 08:50:39 +00:00
_lastHistories.erase(_lastHistories.begin(), _lastHistories.end());
_lastHistories.reserve(kShowChatNamesCount + 1);
auto &&histories = ranges::views::all(
2019-05-08 08:50:39 +00:00
*_chatsList.indexed()
) | ranges::views::transform([](not_null<Dialogs::Row*> row) {
2019-05-08 08:50:39 +00:00
return row->history();
}) | ranges::views::filter([](History *history) {
2019-05-08 08:50:39 +00:00
return (history != nullptr);
}) | ranges::views::transform([](History *history) {
2019-05-08 08:50:39 +00:00
return not_null<History*>(history);
});
for (const auto history : histories) {
const auto i = ranges::upper_bound(_lastHistories, history, pred);
if (size(_lastHistories) < kShowChatNamesCount
|| i != end(_lastHistories)) {
_lastHistories.insert(i, history);
}
2019-05-08 08:50:39 +00:00
if (size(_lastHistories) > kShowChatNamesCount) {
_lastHistories.pop_back();
}
}
++_chatListViewVersion;
updateChatListEntry();
}
2019-04-22 14:22:39 +00:00
not_null<Dialogs::MainList*> Folder::chatsList() {
return &_chatsList;
}
2019-04-15 11:54:03 +00:00
void Folder::loadUserpic() {
}
2019-04-15 11:54:03 +00:00
void Folder::paintUserpic(
Painter &p,
2020-05-28 14:32:10 +00:00
std::shared_ptr<Data::CloudImageView> &view,
const Dialogs::Ui::PaintContext &context) const {
paintUserpic(
p,
context.st->padding.left(),
context.st->padding.top(),
context.st->photoSize);
}
void Folder::paintUserpic(Painter &p, int x, int y, int size) const {
2019-06-17 13:26:08 +00:00
paintUserpic(p, x, y, size, nullptr, nullptr);
}
void Folder::paintUserpic(
Painter &p,
int x,
int y,
int size,
const style::color &bg,
const style::color &fg) const {
2019-06-17 13:26:08 +00:00
paintUserpic(p, x, y, size, &bg, &fg);
}
void Folder::paintUserpic(
Painter &p,
int x,
int y,
int size,
const style::color *overrideBg,
const style::color *overrideFg) const {
p.setPen(Qt::NoPen);
2019-06-17 13:26:08 +00:00
p.setBrush(overrideBg ? *overrideBg : st::historyPeerArchiveUserpicBg);
{
PainterHighQualityEnabler hq(p);
p.drawEllipse(x, y, size, size);
}
if (size == st::defaultDialogRow.photoSize) {
2019-06-17 13:26:08 +00:00
const auto rect = QRect{ x, y, size, size };
if (overrideFg) {
st::dialogsArchiveUserpic.paintInCenter(
p,
rect,
(*overrideFg)->c);
} else {
st::dialogsArchiveUserpic.paintInCenter(p, rect);
}
} else {
p.save();
const auto ratio = size / float64(st::defaultDialogRow.photoSize);
p.translate(x + size / 2., y + size / 2.);
p.scale(ratio, ratio);
const auto skip = st::defaultDialogRow.photoSize;
2019-06-17 13:26:08 +00:00
const auto rect = QRect{ -skip, -skip, 2 * skip, 2 * skip };
if (overrideFg) {
st::dialogsArchiveUserpic.paintInCenter(
p,
rect,
(*overrideFg)->c);
} else {
st::dialogsArchiveUserpic.paintInCenter(p, rect);
}
p.restore();
}
}
2019-05-08 08:50:39 +00:00
const std::vector<not_null<History*>> &Folder::lastHistories() const {
return _lastHistories;
}
uint32 Folder::chatListViewVersion() const {
return _chatListViewVersion;
}
2019-04-15 11:54:03 +00:00
void Folder::requestChatListMessage() {
2019-01-15 11:57:45 +00:00
if (!chatListMessageKnown()) {
2020-02-21 07:58:50 +00:00
owner().histories().requestDialogEntry(this);
2019-01-15 11:57:45 +00:00
}
}
TimeId Folder::adjustedChatListTimeId() const {
return _chatListMessage ? _chatListMessage->date() : chatListTimeId();
}
void Folder::applyDialog(const MTPDdialogFolder &data) {
_chatsList.updateCloudUnread(data);
2019-07-05 13:38:38 +00:00
if (const auto peerId = peerFromMTP(data.vpeer())) {
const auto history = owner().history(peerId);
const auto fullId = FullMsgId(peerId, data.vtop_message().v);
2019-04-25 12:45:15 +00:00
history->setFolder(this, owner().message(fullId));
} else {
_chatsList.clear();
updateChatListExistence();
}
2019-04-22 14:22:39 +00:00
if (_chatsList.indexed()->size() < kLoadedChatsMinCount) {
session().api().requestDialogs(this);
2019-04-19 08:47:49 +00:00
}
}
void Folder::applyPinnedUpdate(const MTPDupdateDialogPinned &data) {
2019-07-05 13:38:38 +00:00
const auto folderId = data.vfolder_id().value_or_empty();
2019-04-19 08:47:49 +00:00
if (folderId != 0) {
LOG(("API Error: Nested folders detected."));
2019-04-17 12:08:02 +00:00
}
2020-03-17 13:04:30 +00:00
owner().setChatPinned(this, FilterId(), data.is_pinned());
}
2019-04-18 11:31:30 +00:00
int Folder::fixedOnTopIndex() const {
return kArchiveFixOnTopIndex;
}
2019-04-15 11:54:03 +00:00
bool Folder::shouldBeInChatList() const {
return !_chatsList.empty();
}
2019-04-15 11:54:03 +00:00
int Folder::chatListUnreadCount() const {
const auto state = chatListUnreadState();
return state.marks
+ (Core::App().settings().countUnreadMessages()
? state.messages
: state.chats);
}
Dialogs::UnreadState Folder::chatListUnreadState() const {
return _chatsList.unreadState();
}
2019-04-15 11:54:03 +00:00
bool Folder::chatListUnreadMark() const {
2021-02-25 13:03:51 +00:00
return false;
2018-06-26 18:03:45 +00:00
}
2019-04-15 11:54:03 +00:00
bool Folder::chatListMutedBadge() const {
2019-04-22 14:22:39 +00:00
return true;
}
2019-04-15 11:54:03 +00:00
HistoryItem *Folder::chatListMessage() const {
return _chatListMessage;
2019-01-15 11:57:45 +00:00
}
2019-04-15 11:54:03 +00:00
bool Folder::chatListMessageKnown() const {
return true;
}
2019-04-15 11:54:03 +00:00
const QString &Folder::chatListName() const {
return _name;
}
2019-04-15 11:54:03 +00:00
const base::flat_set<QString> &Folder::chatListNameWords() const {
return _nameWords;
}
2019-04-15 11:54:03 +00:00
const base::flat_set<QChar> &Folder::chatListFirstLetters() const {
return _nameFirstLetters;
}
const QString &Folder::chatListNameSortKey() const {
2022-09-20 18:12:30 +00:00
static const auto empty = QString();
return empty;
}
2018-01-04 09:40:58 +00:00
} // namespace Data