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; class ChatLineContent;
struct ToxFile; 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 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) void ChatHistory::onFileUpdated(const ToxPk& sender, const ToxFile& file)
{ {
if (canUseHistory()) { if (canUseHistory()) {

View File

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

View File

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

View File

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

View File

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