1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge pull request #5847

Mick Sayson (1):
      fix(chatlog): Remove invalid usages of raw chatlog indexes
This commit is contained in:
sudden6 2019-09-28 14:21:55 +02:00
commit 9e6bf39526
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
7 changed files with 30 additions and 50 deletions

View File

@ -35,7 +35,7 @@ class QTimer;
class ChatLineContent;
struct ToxFile;
static const auto DEF_NUM_MSG_TO_LOAD = 100;
static const size_t DEF_NUM_MSG_TO_LOAD = 100;
class ChatLog : public QGraphicsView
{

View File

@ -226,15 +226,6 @@ std::vector<IChatLog::DateChatLogIdxPair> ChatHistory::getDateIdxs(const QDate&
}
}
std::size_t ChatHistory::size() const
{
if (canUseHistory()) {
return history->getNumMessagesForFriend(f.getPublicKey());
}
return sessionChatLog.size();
}
void ChatHistory::onFileUpdated(const ToxPk& sender, const ToxFile& file)
{
if (canUseHistory()) {

View File

@ -42,7 +42,6 @@ public:
ChatLogIdx getFirstIdx() const override;
ChatLogIdx getNextIdx() const override;
std::vector<DateChatLogIdxPair> getDateIdxs(const QDate& startDate, size_t maxDates) const override;
std::size_t size() const override;
public slots:
void onFileUpdated(const ToxPk& sender, const ToxFile& file);

View File

@ -138,8 +138,6 @@ public:
virtual std::vector<DateChatLogIdxPair> getDateIdxs(const QDate& startDate,
size_t maxDates) const = 0;
virtual std::size_t size() const = 0;
signals:
void itemUpdated(ChatLogIdx idx);
};

View File

@ -289,11 +289,6 @@ std::vector<IChatLog::DateChatLogIdxPair> SessionChatLog::getDateIdxs(const QDat
return ret;
}
std::size_t SessionChatLog::size() const
{
return items.size();
}
void SessionChatLog::insertMessageAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName,
ChatLogMessage message)
{

View File

@ -45,7 +45,6 @@ public:
ChatLogIdx getFirstIdx() const override;
ChatLogIdx getNextIdx() const override;
std::vector<DateChatLogIdxPair> getDateIdxs(const QDate& startDate, size_t maxDates) const override;
std::size_t size() const override;
void insertMessageAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName, ChatLogMessage message);
void insertFileAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName, ChatLogFile file);

View File

@ -681,16 +681,16 @@ void GenericChatForm::loadHistory(const QDateTime &time, const LoadHistoryDialog
void GenericChatForm::loadHistoryTo(const QDateTime &time)
{
chatWidget->setScroll(false);
auto end = ChatLogIdx(0);
auto end = chatLog.getFirstIdx();
if (time.isNull()) {
end = messages.begin()->first;
} else {
end = firstItemAfterDate(time.date(), chatLog);
}
auto begin = ChatLogIdx(0);
if (end.get() > DEF_NUM_MSG_TO_LOAD) {
begin = ChatLogIdx(end.get() - DEF_NUM_MSG_TO_LOAD);
auto begin = chatLog.getFirstIdx();
if (end - begin > DEF_NUM_MSG_TO_LOAD) {
begin = end - DEF_NUM_MSG_TO_LOAD;
}
if (begin != end) {
@ -712,27 +712,24 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time)
bool GenericChatForm::loadHistoryFrom(const QDateTime &time)
{
chatWidget->setScroll(false);
auto begin = ChatLogIdx(0);
auto begin = chatLog.getFirstIdx();
if (time.isNull()) {
begin = messages.rbegin()->first;
} else {
begin = firstItemAfterDate(time.date(), chatLog);
}
int add = DEF_NUM_MSG_TO_LOAD;
if (begin.get() + DEF_NUM_MSG_TO_LOAD > chatLog.getNextIdx().get()) {
add = chatLog.getNextIdx().get() - begin.get();
}
const auto end = chatLog.getNextIdx() < begin + DEF_NUM_MSG_TO_LOAD
? chatLog.getNextIdx()
: begin + DEF_NUM_MSG_TO_LOAD;
// The chatLog.getNextIdx() is usually 1 more than the idx on last "messages" item
// so if we have nothing to load, "add" is equal 1
if (add <= 1) {
if (end - begin <= 1) {
chatWidget->setScroll(true);
return false;
}
auto end = ChatLogIdx(begin.get() + add);
renderMessages(begin, end);
return true;
@ -986,7 +983,7 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch
return;
}
if (chatLog.getNextIdx().get() == messages.rbegin()->first.get() + 1) {
if (chatLog.getNextIdx() == messages.rbegin()->first + 1) {
disableSearchText();
} else {
goToCurrentDate();
@ -1057,41 +1054,41 @@ void GenericChatForm::handleSearchResult(SearchResult result, SearchDirection di
auto endRenderedIdx = messages.rbegin()->first;
if (direction == SearchDirection::Up) {
if (searchIdx.get() < firstRenderedIdx.get()) {
if (searchIdx.get() > DEF_NUM_MSG_TO_LOAD / 2) {
firstRenderedIdx = ChatLogIdx(searchIdx.get() - DEF_NUM_MSG_TO_LOAD / 2);
if (searchIdx < firstRenderedIdx) {
if (searchIdx - chatLog.getFirstIdx() > DEF_NUM_MSG_TO_LOAD / 2) {
firstRenderedIdx = searchIdx - DEF_NUM_MSG_TO_LOAD / 2;
} else {
firstRenderedIdx = ChatLogIdx(0);
firstRenderedIdx = chatLog.getFirstIdx();
}
}
if (endRenderedIdx.get() - firstRenderedIdx.get() > DEF_NUM_MSG_TO_LOAD) {
endRenderedIdx = ChatLogIdx(firstRenderedIdx.get() + DEF_NUM_MSG_TO_LOAD);
if (endRenderedIdx - firstRenderedIdx > DEF_NUM_MSG_TO_LOAD) {
endRenderedIdx = firstRenderedIdx + DEF_NUM_MSG_TO_LOAD;
}
} else {
if (searchIdx.get() < firstRenderedIdx.get()) {
if (searchIdx < firstRenderedIdx) {
firstRenderedIdx = searchIdx;
}
if (firstRenderedIdx == searchIdx || searchIdx.get() > endRenderedIdx.get()) {
if (searchIdx.get() + DEF_NUM_MSG_TO_LOAD > chatLog.getNextIdx().get()) {
if (firstRenderedIdx == searchIdx || searchIdx > endRenderedIdx) {
if (searchIdx + DEF_NUM_MSG_TO_LOAD > chatLog.getNextIdx()) {
endRenderedIdx = chatLog.getNextIdx();
} else {
endRenderedIdx = ChatLogIdx(searchIdx.get() + DEF_NUM_MSG_TO_LOAD);
endRenderedIdx = searchIdx + DEF_NUM_MSG_TO_LOAD;
}
}
if (endRenderedIdx.get() - firstRenderedIdx.get() > DEF_NUM_MSG_TO_LOAD) {
if (endRenderedIdx.get() > DEF_NUM_MSG_TO_LOAD) {
firstRenderedIdx = ChatLogIdx(endRenderedIdx.get() - DEF_NUM_MSG_TO_LOAD);
if (endRenderedIdx - firstRenderedIdx > DEF_NUM_MSG_TO_LOAD) {
if (endRenderedIdx - chatLog.getFirstIdx() > DEF_NUM_MSG_TO_LOAD) {
firstRenderedIdx = endRenderedIdx - DEF_NUM_MSG_TO_LOAD;
} else {
firstRenderedIdx = ChatLogIdx(0);
firstRenderedIdx = chatLog.getFirstIdx();
}
}
}
if (!messages.empty() && (firstRenderedIdx.get() < messages.begin()->first.get()
|| endRenderedIdx.get() > messages.rbegin()->first.get())) {
if (!messages.empty() && (firstRenderedIdx < messages.begin()->first
|| endRenderedIdx > messages.rbegin()->first)) {
chatWidget->clear();
messages.clear();
@ -1166,8 +1163,9 @@ void GenericChatForm::goToCurrentDate()
{
chatWidget->clear();
messages.clear();
auto end = ChatLogIdx(chatLog.size());
auto begin = end.get() > DEF_NUM_MSG_TO_LOAD ? ChatLogIdx(end.get() - DEF_NUM_MSG_TO_LOAD) : ChatLogIdx(0);
auto end = chatLog.getNextIdx();
auto numMessages = std::min(DEF_NUM_MSG_TO_LOAD, chatLog.getNextIdx() - chatLog.getFirstIdx());
auto begin = end - numMessages;
renderMessages(begin, end);
}