harbour-fernschreiber/src/chatmodel.cpp

147 lines
4.6 KiB
C++
Raw Normal View History

2020-08-22 18:30:02 +03:00
#include "chatmodel.h"
2020-08-22 22:43:20 +03:00
#include <QListIterator>
2020-08-22 18:30:02 +03:00
ChatModel::ChatModel(TDLibWrapper *tdLibWrapper)
{
this->tdLibWrapper = tdLibWrapper;
2020-08-22 22:43:20 +03:00
this->inReload = false;
2020-08-26 23:52:06 +03:00
this->inIncrementalUpdate = false;
2020-08-22 22:43:20 +03:00
connect(this->tdLibWrapper, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList)));
2020-08-23 00:49:02 +03:00
connect(this->tdLibWrapper, SIGNAL(newMessageReceived(QString, QVariantMap)), this, SLOT(handleNewMessageReceived(QString, QVariantMap)));
2020-08-22 18:30:02 +03:00
}
ChatModel::~ChatModel()
{
}
int ChatModel::rowCount(const QModelIndex &) const
{
return messages.size();
}
QVariant ChatModel::data(const QModelIndex &index, int role) const
{
if(index.isValid() && role == Qt::DisplayRole) {
return QVariant(messages.value(index.row()));
}
return QVariant();
}
bool ChatModel::insertRows(int row, int count, const QModelIndex &parent)
{
qDebug() << "[ChatModel] Inserting at " << row << ", row count: " << count;
beginInsertRows(parent, row, row + count - 1);
2020-08-22 22:43:20 +03:00
for (int i = 0; i < count; i++) {
this->messages.insert(row + i, this->messagesToBeAdded.at(i));
}
2020-08-22 18:30:02 +03:00
this->messageIndexMap.clear();
for (int i = 0; i < this->messages.size(); i++) {
this->messageIndexMap.insert(this->messages.at(i).toMap().value("id").toString(), i);
}
endInsertRows();
return true;
}
2020-08-22 22:43:20 +03:00
void ChatModel::initialize(const QString &chatId)
{
this->chatId = chatId;
this->messages.clear();
this->messageIndexMap.clear();
this->messagesToBeAdded.clear();
}
2020-08-26 23:52:06 +03:00
void ChatModel::triggerLoadMoreHistory()
{
qDebug() << "[ChatModel] Trigger loading older history...";
if (!this->inIncrementalUpdate) {
this->inIncrementalUpdate = true;
this->tdLibWrapper->getChatHistory(this->chatId, this->messages.first().toMap().value("id").toLongLong());
}
}
2020-08-22 22:43:20 +03:00
bool compareMessages(const QVariant &message1, const QVariant &message2)
{
QVariantMap messageMap1 = message1.toMap();
QVariantMap messageMap2 = message2.toMap();
if (messageMap1.value("id").toLongLong() < messageMap2.value("id").toLongLong()) {
return true;
} else {
return false;
}
}
void ChatModel::handleMessagesReceived(const QVariantList &messages)
{
qDebug() << "[ChatModel] Receiving new messages :)" << messages.size();
if (messages.size() == 0) {
emit noMessagesAvailable();
return;
}
2020-08-23 00:49:02 +03:00
this->messagesMutex.lock();
2020-08-22 22:43:20 +03:00
this->messagesToBeAdded.clear();
QListIterator<QVariant> messagesIterator(messages);
while (messagesIterator.hasNext()) {
QVariantMap currentMessage = messagesIterator.next().toMap();
if (currentMessage.value("chat_id").toString() == this->chatId) {
this->messagesToBeAdded.append(currentMessage);
}
}
std::sort(this->messagesToBeAdded.begin(), this->messagesToBeAdded.end(), compareMessages);
2020-08-23 00:49:02 +03:00
this->insertMessages();
this->messagesMutex.unlock();
// First call only returns a few messages, we need to get a little more than that...
if (this->messagesToBeAdded.size() < 10 && !this->inReload) {
2020-08-23 00:49:02 +03:00
qDebug() << "[ChatModel] Only one message received in first call, loading more...";
this->inReload = true;
this->tdLibWrapper->getChatHistory(this->chatId, this->messagesToBeAdded.first().toMap().value("id").toLongLong());
} else {
qDebug() << "[ChatModel] Messages loaded, notifying chat UI...";
this->inReload = false;
2020-08-26 23:52:06 +03:00
if (this->inIncrementalUpdate) {
this->inIncrementalUpdate = false;
emit messagesIncrementalUpdate();
} else {
emit messagesReceived();
}
2020-08-23 00:49:02 +03:00
}
}
void ChatModel::handleNewMessageReceived(const QString &chatId, const QVariantMap &message)
{
if (chatId == this->chatId) {
qDebug() << "[ChatModel] New message received for this chat";
this->messagesMutex.lock();
this->messagesToBeAdded.clear();
this->messagesToBeAdded.append(message);
this->insertMessages();
this->messagesMutex.unlock();
emit newMessageReceived();
}
}
void ChatModel::insertMessages()
{
2020-08-22 22:43:20 +03:00
if (this->messages.isEmpty()) {
beginResetModel();
this->messages.append(this->messagesToBeAdded);
endResetModel();
} else {
// There is only an append or a prepend, tertium non datur! (probably ;))
if (this->messages.last().toMap().value("id").toLongLong() < this->messagesToBeAdded.first().toMap().value("id").toLongLong()) {
// Append
this->insertRows(rowCount(QModelIndex()), this->messagesToBeAdded.size());
} else {
// Prepend
this->insertRows(0, this->messagesToBeAdded.size());
}
}
}