2016-04-09 18:45:55 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-09 18:45:55 +00:00
|
|
|
*/
|
|
|
|
#include "dialogs/dialogs_list.h"
|
|
|
|
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "dialogs/dialogs_entry.h"
|
2016-04-09 18:45:55 +00:00
|
|
|
#include "dialogs/dialogs_layout.h"
|
2016-06-07 19:59:39 +00:00
|
|
|
#include "styles/style_dialogs.h"
|
2016-04-09 18:45:55 +00:00
|
|
|
#include "mainwidget.h"
|
|
|
|
|
|
|
|
namespace Dialogs {
|
|
|
|
|
|
|
|
List::List(SortMode sortMode)
|
2018-01-04 17:15:04 +00:00
|
|
|
: _last(std::make_unique<Row>(nullptr))
|
2016-04-10 19:20:48 +00:00
|
|
|
, _begin(_last.get())
|
|
|
|
, _end(_last.get())
|
2016-04-09 18:45:55 +00:00
|
|
|
, _sortMode(sortMode)
|
2016-04-10 19:20:48 +00:00
|
|
|
, _current(_last.get()) {
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void List::adjustCurrent(int32 y, int32 h) const {
|
|
|
|
if (isEmpty()) return;
|
|
|
|
|
|
|
|
int32 pos = (y > 0) ? (y / h) : 0;
|
|
|
|
while (_current->_pos > pos && _current != _begin) {
|
|
|
|
_current = _current->_prev;
|
|
|
|
}
|
|
|
|
while (_current->_pos + 1 <= pos && _current->_next != _end) {
|
|
|
|
_current = _current->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
Row *List::addToEnd(Key key) {
|
|
|
|
const auto result = new Row(key, _end->_prev, _end, _end->_pos);
|
2016-04-09 18:45:55 +00:00
|
|
|
_end->_pos++;
|
|
|
|
if (_begin == _end) {
|
|
|
|
_begin = _current = result;
|
|
|
|
} else {
|
|
|
|
_end->_prev->_next = result;
|
|
|
|
}
|
2018-01-04 17:15:04 +00:00
|
|
|
_rowByKey.emplace(key, result);
|
2016-04-09 18:45:55 +00:00
|
|
|
++_count;
|
|
|
|
_end->_prev = result;
|
|
|
|
if (_sortMode == SortMode::Date) {
|
|
|
|
adjustByPos(result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool List::insertBefore(Row *row, Row *before) {
|
2018-01-04 17:15:04 +00:00
|
|
|
if (row == before) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-09 18:45:55 +00:00
|
|
|
|
|
|
|
if (_current == row) {
|
|
|
|
_current = row->_prev;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto updateTill = row->_prev;
|
2016-04-09 18:45:55 +00:00
|
|
|
remove(row);
|
|
|
|
|
|
|
|
// insert row
|
|
|
|
row->_next = before; // update row
|
|
|
|
row->_prev = before->_prev;
|
|
|
|
row->_next->_prev = row; // update row->next
|
|
|
|
if (row->_prev) { // update row->prev
|
|
|
|
row->_prev->_next = row;
|
|
|
|
} else {
|
|
|
|
_begin = row;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
// update pos
|
|
|
|
for (auto n = row; n != updateTill; n = n->_next) {
|
2016-04-09 18:45:55 +00:00
|
|
|
n->_next->_pos++;
|
|
|
|
row->_pos--;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool List::insertAfter(Row *row, Row *after) {
|
2018-01-04 17:15:04 +00:00
|
|
|
if (row == after) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-09 18:45:55 +00:00
|
|
|
|
|
|
|
if (_current == row) {
|
|
|
|
_current = row->_next;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto updateFrom = row->_next;
|
2016-04-09 18:45:55 +00:00
|
|
|
remove(row);
|
|
|
|
|
|
|
|
// insert row
|
|
|
|
row->_prev = after; // update row
|
|
|
|
row->_next = after->_next;
|
|
|
|
row->_prev->_next = row; // update row->prev
|
|
|
|
row->_next->_prev = row; // update row->next
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
// update pos
|
|
|
|
for (auto n = updateFrom; n != row; n = n->_next) {
|
2016-04-09 18:45:55 +00:00
|
|
|
n->_pos--;
|
|
|
|
row->_pos++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
Row *List::adjustByName(Key key) {
|
2016-04-09 18:45:55 +00:00
|
|
|
if (_sortMode != SortMode::Name) return nullptr;
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto i = _rowByKey.find(key);
|
|
|
|
if (i == _rowByKey.cend()) return nullptr;
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto row = i->second;
|
2018-01-22 09:33:09 +00:00
|
|
|
const auto name = key.entry()->chatsListName();
|
2018-01-04 17:15:04 +00:00
|
|
|
auto change = row;
|
2018-01-22 09:33:09 +00:00
|
|
|
while (change->_prev
|
|
|
|
&& change->_prev->entry()->chatsListName().compare(name, Qt::CaseInsensitive) < 0) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = change->_prev;
|
|
|
|
}
|
|
|
|
if (!insertBefore(row, change)) {
|
2018-01-22 09:33:09 +00:00
|
|
|
while (change->_next != _end
|
|
|
|
&& change->_next->entry()->chatsListName().compare(name, Qt::CaseInsensitive) < 0) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = change->_next;
|
|
|
|
}
|
|
|
|
insertAfter(row, change);
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
Row *List::addByName(Key key) {
|
2018-01-04 09:40:58 +00:00
|
|
|
if (_sortMode != SortMode::Name) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto row = addToEnd(key);
|
2018-01-04 09:40:58 +00:00
|
|
|
auto change = row;
|
2018-01-22 09:33:09 +00:00
|
|
|
const auto name = key.entry()->chatsListName();
|
|
|
|
while (change->_prev
|
|
|
|
&& change->_prev->entry()->chatsListName().compare(name, Qt::CaseInsensitive) > 0) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = change->_prev;
|
|
|
|
}
|
|
|
|
if (!insertBefore(row, change)) {
|
2018-01-22 09:33:09 +00:00
|
|
|
while (change->_next != _end
|
|
|
|
&& change->_next->entry()->chatsListName().compare(name, Qt::CaseInsensitive) < 0) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = change->_next;
|
|
|
|
}
|
|
|
|
insertAfter(row, change);
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
void List::adjustByPos(Row *row) {
|
|
|
|
if (_sortMode != SortMode::Date || !_begin) return;
|
|
|
|
|
|
|
|
Row *change = row;
|
2018-01-04 17:15:04 +00:00
|
|
|
if (change != _begin && _begin->sortKey() < row->sortKey()) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = _begin;
|
|
|
|
} else {
|
2018-01-04 17:15:04 +00:00
|
|
|
while (change->_prev && change->_prev->sortKey() < row->sortKey()) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = change->_prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!insertBefore(row, change)) {
|
2018-01-04 17:15:04 +00:00
|
|
|
if (change->_next != _end && _end->_prev->sortKey() > row->sortKey()) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = _end->_prev;
|
|
|
|
} else {
|
2018-01-04 17:15:04 +00:00
|
|
|
while (change->_next != _end && change->_next->sortKey() > row->sortKey()) {
|
2016-04-09 18:45:55 +00:00
|
|
|
change = change->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insertAfter(row, change);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
bool List::moveToTop(Key key) {
|
|
|
|
auto i = _rowByKey.find(key);
|
|
|
|
if (i == _rowByKey.cend()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-06 14:45:10 +00:00
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
insertBefore(i->second, _begin);
|
2016-09-06 14:45:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
bool List::del(Key key, Row *replacedBy) {
|
|
|
|
auto i = _rowByKey.find(key);
|
|
|
|
if (i == _rowByKey.cend()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto row = i->second;
|
2016-04-09 18:45:55 +00:00
|
|
|
if (App::main()) {
|
|
|
|
emit App::main()->dialogRowReplaced(row, replacedBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row == _current) {
|
|
|
|
_current = row->_next;
|
|
|
|
}
|
2016-12-05 11:01:08 +00:00
|
|
|
for (auto change = row->_next; change != _end; change = change->_next) {
|
2016-04-09 18:45:55 +00:00
|
|
|
--change->_pos;
|
|
|
|
}
|
|
|
|
--_end->_pos;
|
|
|
|
remove(row);
|
|
|
|
delete row;
|
|
|
|
--_count;
|
2018-01-04 17:15:04 +00:00
|
|
|
_rowByKey.erase(i);
|
2016-04-09 18:45:55 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void List::remove(Row *row) {
|
|
|
|
row->_next->_prev = row->_prev; // update row->next
|
|
|
|
if (row->_prev) { // update row->prev
|
|
|
|
row->_prev->_next = row->_next;
|
|
|
|
} else {
|
|
|
|
_begin = row->_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void List::clear() {
|
|
|
|
while (_begin != _end) {
|
|
|
|
_current = _begin;
|
|
|
|
_begin = _begin->_next;
|
|
|
|
delete _current;
|
|
|
|
}
|
|
|
|
_current = _begin;
|
2018-01-04 17:15:04 +00:00
|
|
|
_rowByKey.clear();
|
2016-04-09 18:45:55 +00:00
|
|
|
_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
List::~List() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dialogs
|