2019-04-19 08:47:49 +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_pinned_list.h"
|
|
|
|
|
|
|
|
#include "dialogs/dialogs_key.h"
|
|
|
|
#include "dialogs/dialogs_entry.h"
|
2020-03-17 13:04:30 +00:00
|
|
|
#include "history/history.h"
|
2019-04-19 08:47:49 +00:00
|
|
|
#include "data/data_session.h"
|
2022-11-08 13:57:54 +00:00
|
|
|
#include "data/data_forum.h"
|
2019-04-19 08:47:49 +00:00
|
|
|
|
|
|
|
namespace Dialogs {
|
|
|
|
|
2020-03-17 13:04:30 +00:00
|
|
|
PinnedList::PinnedList(FilterId filterId, int limit)
|
|
|
|
: _filterId(filterId)
|
|
|
|
, _limit(limit) {
|
2019-04-19 08:47:49 +00:00
|
|
|
Expects(limit > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinnedList::setLimit(int limit) {
|
|
|
|
Expects(limit > 0);
|
|
|
|
|
|
|
|
if (_limit == limit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_limit = limit;
|
|
|
|
applyLimit(_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinnedList::addPinned(const Key &key) {
|
|
|
|
Expects(key.entry()->folderKnown());
|
|
|
|
|
|
|
|
addPinnedGetPosition(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PinnedList::addPinnedGetPosition(const Key &key) {
|
|
|
|
const auto already = ranges::find(_data, key);
|
|
|
|
if (already != end(_data)) {
|
|
|
|
return already - begin(_data);
|
|
|
|
}
|
|
|
|
applyLimit(_limit - 1);
|
|
|
|
const auto position = int(_data.size());
|
|
|
|
_data.push_back(key);
|
2020-03-17 13:04:30 +00:00
|
|
|
key.entry()->cachePinnedIndex(_filterId, position + 1);
|
2019-04-19 08:47:49 +00:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinnedList::setPinned(const Key &key, bool pinned) {
|
2020-05-04 15:06:32 +00:00
|
|
|
Expects(key.entry()->folderKnown() || _filterId != 0);
|
2019-04-19 08:47:49 +00:00
|
|
|
|
|
|
|
if (pinned) {
|
|
|
|
const int position = addPinnedGetPosition(key);
|
|
|
|
if (position) {
|
|
|
|
const auto begin = _data.begin();
|
|
|
|
std::rotate(begin, begin + position, begin + position + 1);
|
|
|
|
for (auto i = 0; i != position + 1; ++i) {
|
2020-03-17 13:04:30 +00:00
|
|
|
_data[i].entry()->cachePinnedIndex(_filterId, i + 1);
|
2019-04-19 08:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (const auto it = ranges::find(_data, key); it != end(_data)) {
|
2019-04-23 15:23:13 +00:00
|
|
|
const auto index = int(it - begin(_data));
|
2019-04-19 08:47:49 +00:00
|
|
|
_data.erase(it);
|
2020-03-17 13:04:30 +00:00
|
|
|
key.entry()->cachePinnedIndex(_filterId, 0);
|
2019-04-19 08:47:49 +00:00
|
|
|
for (auto i = index, count = int(size(_data)); i != count; ++i) {
|
2020-03-17 13:04:30 +00:00
|
|
|
_data[i].entry()->cachePinnedIndex(_filterId, i + 1);
|
2019-04-19 08:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinnedList::applyLimit(int limit) {
|
|
|
|
Expects(limit >= 0);
|
|
|
|
|
|
|
|
while (_data.size() > limit) {
|
|
|
|
setPinned(_data.back(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinnedList::clear() {
|
|
|
|
applyLimit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PinnedList::applyList(
|
|
|
|
not_null<Data::Session*> owner,
|
|
|
|
const QVector<MTPDialogPeer> &list) {
|
|
|
|
clear();
|
2020-03-17 13:04:30 +00:00
|
|
|
for (const auto &peer : list) {
|
2019-04-19 08:47:49 +00:00
|
|
|
peer.match([&](const MTPDdialogPeer &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (const auto peerId = peerFromMTP(data.vpeer())) {
|
2020-03-17 13:04:30 +00:00
|
|
|
addPinned(owner->history(peerId));
|
2019-04-19 08:47:49 +00:00
|
|
|
}
|
|
|
|
}, [&](const MTPDdialogPeerFolder &data) {
|
2020-03-17 13:04:30 +00:00
|
|
|
addPinned(owner->folder(data.vfolder_id().v));
|
2019-04-19 08:47:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 13:57:54 +00:00
|
|
|
void PinnedList::applyList(
|
|
|
|
not_null<Data::Forum*> forum,
|
|
|
|
const QVector<MTPint> &list) {
|
|
|
|
clear();
|
|
|
|
for (const auto &topicId : list) {
|
|
|
|
addPinned(forum->topicFor(topicId.v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 13:04:30 +00:00
|
|
|
void PinnedList::applyList(const std::vector<not_null<History*>> &list) {
|
|
|
|
Expects(_filterId != 0);
|
|
|
|
|
2020-05-04 15:06:32 +00:00
|
|
|
const auto old = base::take(_data);
|
|
|
|
|
2020-03-17 13:04:30 +00:00
|
|
|
const auto count = int(list.size());
|
|
|
|
_data.reserve(count);
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
const auto history = list[i];
|
2020-03-17 14:52:01 +00:00
|
|
|
_data.emplace_back(history);
|
|
|
|
history->cachePinnedIndex(_filterId, i + 1);
|
2020-03-17 13:04:30 +00:00
|
|
|
}
|
2020-05-04 15:06:32 +00:00
|
|
|
|
|
|
|
for (const auto &key : old) {
|
|
|
|
const auto history = key.history();
|
|
|
|
if (!history || !ranges::contains(_data, history, &Key::history)) {
|
|
|
|
key.entry()->cachePinnedIndex(_filterId, 0);
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 13:04:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 08:47:49 +00:00
|
|
|
void PinnedList::reorder(const Key &key1, const Key &key2) {
|
|
|
|
const auto index1 = ranges::find(_data, key1) - begin(_data);
|
|
|
|
const auto index2 = ranges::find(_data, key2) - begin(_data);
|
|
|
|
Assert(index1 >= 0 && index1 < _data.size());
|
|
|
|
Assert(index2 >= 0 && index2 < _data.size());
|
|
|
|
Assert(index1 != index2);
|
|
|
|
std::swap(_data[index1], _data[index2]);
|
2020-03-17 13:04:30 +00:00
|
|
|
key1.entry()->cachePinnedIndex(_filterId, index2 + 1);
|
|
|
|
key2.entry()->cachePinnedIndex(_filterId, index1 + 1);
|
2019-04-19 08:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dialogs
|