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 {
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
List::List(SortMode sortMode) : _sortMode(sortMode) {
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
List::const_iterator List::cfind(Row *value) const {
|
|
|
|
return value
|
|
|
|
? (cbegin() + value->pos())
|
|
|
|
: cend();
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
not_null<Row*> List::addToEnd(Key key) {
|
|
|
|
if (const auto result = getRow(key)) {
|
|
|
|
return result;
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
2019-04-16 11:37:50 +00:00
|
|
|
const auto result = _rowByKey.emplace(
|
|
|
|
key,
|
|
|
|
std::make_unique<Row>(key, _rows.size())
|
|
|
|
).first->second.get();
|
|
|
|
_rows.push_back(result);
|
2016-04-09 18:45:55 +00:00
|
|
|
if (_sortMode == SortMode::Date) {
|
2019-04-16 11:37:50 +00:00
|
|
|
adjustByDate(result);
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
Row *List::adjustByName(Key key) {
|
2019-04-16 11:37:50 +00:00
|
|
|
Expects(_sortMode == SortMode::Name);
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
const auto row = getRow(key);
|
|
|
|
if (!row) {
|
|
|
|
return nullptr;
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
2019-04-16 11:37:50 +00:00
|
|
|
adjustByName(row);
|
2016-04-09 18:45:55 +00:00
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
not_null<Row*> List::addByName(Key key) {
|
|
|
|
Expects(_sortMode == SortMode::Name);
|
2016-04-09 18:45:55 +00:00
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
const auto row = addToEnd(key);
|
2019-04-16 11:37:50 +00:00
|
|
|
adjustByName(key);
|
2016-04-09 18:45:55 +00:00
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
void List::adjustByName(not_null<Row*> row) {
|
|
|
|
Expects(row->pos() >= 0 && row->pos() < _rows.size());
|
|
|
|
|
|
|
|
const auto &name = row->entry()->chatListName();
|
|
|
|
const auto index = row->pos();
|
|
|
|
const auto i = _rows.begin() + index;
|
|
|
|
const auto before = std::find_if(i + 1, _rows.end(), [&](Row *row) {
|
|
|
|
const auto &greater = row->entry()->chatListName();
|
|
|
|
return greater.compare(name, Qt::CaseInsensitive) >= 0;
|
|
|
|
});
|
|
|
|
if (before != i + 1) {
|
|
|
|
rotate(i, i + 1, before);
|
|
|
|
} else if (i != _rows.begin()) {
|
|
|
|
const auto from = std::make_reverse_iterator(i);
|
|
|
|
const auto after = std::find_if(from, _rows.rend(), [&](Row *row) {
|
|
|
|
const auto &less = row->entry()->chatListName();
|
|
|
|
return less.compare(name, Qt::CaseInsensitive) <= 0;
|
|
|
|
}).base();
|
|
|
|
if (after != i) {
|
|
|
|
rotate(after, i, i + 1);
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 11:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void List::adjustByDate(not_null<Row*> row) {
|
|
|
|
Expects(_sortMode == SortMode::Date);
|
|
|
|
|
|
|
|
const auto key = row->sortKey();
|
|
|
|
const auto index = row->pos();
|
|
|
|
const auto i = _rows.begin() + index;
|
|
|
|
const auto before = std::find_if(i + 1, _rows.end(), [&](Row *row) {
|
|
|
|
return (row->sortKey() <= key);
|
|
|
|
});
|
|
|
|
if (before != i + 1) {
|
|
|
|
rotate(i, i + 1, before);
|
|
|
|
} else {
|
|
|
|
const auto from = std::make_reverse_iterator(i);
|
|
|
|
const auto after = std::find_if(from, _rows.rend(), [&](Row *row) {
|
|
|
|
return (row->sortKey() >= key);
|
|
|
|
}).base();
|
|
|
|
if (after != i) {
|
|
|
|
rotate(after, i, i + 1);
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:15:04 +00:00
|
|
|
bool List::moveToTop(Key key) {
|
2019-04-16 11:37:50 +00:00
|
|
|
const auto i = _rowByKey.find(key);
|
2018-01-04 17:15:04 +00:00
|
|
|
if (i == _rowByKey.cend()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-16 11:37:50 +00:00
|
|
|
const auto index = i->second->pos();
|
|
|
|
const auto begin = _rows.begin();
|
|
|
|
rotate(begin, begin + index, begin + index + 1);
|
2016-09-06 14:45:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
void List::rotate(
|
|
|
|
std::vector<not_null<Row*>>::iterator first,
|
|
|
|
std::vector<not_null<Row*>>::iterator middle,
|
|
|
|
std::vector<not_null<Row*>>::iterator last) {
|
|
|
|
std::rotate(first, middle, last);
|
|
|
|
|
|
|
|
auto count = (last - first);
|
|
|
|
auto index = (first - _rows.begin());
|
|
|
|
while (count--) {
|
|
|
|
(*first++)->_pos = index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
const auto row = i->second.get();
|
2016-04-09 18:45:55 +00:00
|
|
|
if (App::main()) {
|
|
|
|
emit App::main()->dialogRowReplaced(row, replacedBy);
|
|
|
|
}
|
|
|
|
|
2019-04-16 11:37:50 +00:00
|
|
|
const auto index = row->pos();
|
|
|
|
_rows.erase(_rows.begin() + index);
|
|
|
|
for (auto i = index, count = int(_rows.size()); i != count; ++i) {
|
|
|
|
_rows[i]->_pos = i;
|
2016-04-09 18:45:55 +00:00
|
|
|
}
|
2018-01-04 17:15:04 +00:00
|
|
|
_rowByKey.erase(i);
|
2016-04-09 18:45:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dialogs
|