/
NeoChapay
/
telegraf
Обзор
Документация
Войти
/
NeoChapay
/
telegraf
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/chatlistmodel.cpp
831 строка
34 KB
mbarashkov
Исправил некоторые предупреждения
11 янв 2025, 13:43
11 янв 2025, 13:43
a0bb378
Код
Авторство
О чём код?
#include "chatlistmodel.h" #include "telegrafutils.h" #include <QListIterator> #define DEBUG_MODULE ChatListModel #include "debuglog.h" ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper, AppSettings *appSettings) : showHiddenChats(false) { this->tdLibWrapper = tdLibWrapper; this->appSettings = appSettings; connect(tdLibWrapper, SIGNAL(newChatDiscovered(QString,QVariantMap)), this, SLOT(handleChatDiscovered(QString,QVariantMap))); connect(tdLibWrapper, SIGNAL(chatLastMessageUpdated(QString,QString,QVariantMap)), this, SLOT(handleChatLastMessageUpdated(QString,QString,QVariantMap))); connect(tdLibWrapper, SIGNAL(chatOrderUpdated(QString,QString,QString)), this, SLOT(handleChatOrderUpdated(QString,QString,QString))); connect(tdLibWrapper, SIGNAL(chatReadInboxUpdated(QString,QString,int)), this, SLOT(handleChatReadInboxUpdated(QString,QString,int))); connect(tdLibWrapper, SIGNAL(chatReadOutboxUpdated(QString,QString)), this, SLOT(handleChatReadOutboxUpdated(QString,QString))); connect(tdLibWrapper, SIGNAL(chatPhotoUpdated(qlonglong,QVariantMap)), this, SLOT(handleChatPhotoUpdated(qlonglong,QVariantMap))); connect(tdLibWrapper, SIGNAL(chatPinnedMessageUpdated(qlonglong,qlonglong)), this, SLOT(handleChatPinnedMessageUpdated(qlonglong,qlonglong))); connect(tdLibWrapper, SIGNAL(messageSendSucceeded(qlonglong,qlonglong,QVariantMap)), this, SLOT(handleMessageSendSucceeded(qlonglong,qlonglong,QVariantMap))); connect(tdLibWrapper, SIGNAL(chatNotificationSettingsUpdated(QString,QVariantMap)), this, SLOT(handleChatNotificationSettingsUpdated(QString,QVariantMap))); connect(tdLibWrapper, SIGNAL(superGroupUpdated(qlonglong)), this, SLOT(handleGroupUpdated(qlonglong))); connect(tdLibWrapper, SIGNAL(basicGroupUpdated(qlonglong)), this, SLOT(handleGroupUpdated(qlonglong))); connect(tdLibWrapper, SIGNAL(secretChatUpdated(qlonglong,QVariantMap)), this, SLOT(handleSecretChatUpdated(qlonglong,QVariantMap))); connect(tdLibWrapper, SIGNAL(secretChatReceived(qlonglong,QVariantMap)), this, SLOT(handleSecretChatUpdated(qlonglong,QVariantMap))); connect(tdLibWrapper, SIGNAL(chatTitleUpdated(QString,QString)), this, SLOT(handleChatTitleUpdated(QString,QString))); connect(tdLibWrapper, SIGNAL(chatIsMarkedAsUnreadUpdated(qlonglong,bool)), this, SLOT(handleChatIsMarkedAsUnreadUpdated(qlonglong,bool))); connect(tdLibWrapper, SIGNAL(chatPinnedUpdated(qlonglong,bool,QString)), this, SLOT(handleChatPinnedUpdated(qlonglong,bool,QString))); connect(tdLibWrapper, SIGNAL(chatDraftMessageUpdated(qlonglong,QVariantMap,QString)), this, SLOT(handleChatDraftMessageUpdated(qlonglong,QVariantMap,QString))); connect(tdLibWrapper, SIGNAL(chatUnreadMentionCountUpdated(qlonglong,int)), this, SLOT(handleChatUnreadMentionCountUpdated(qlonglong,int))); connect(tdLibWrapper, SIGNAL(chatUnreadReactionCountUpdated(qlonglong,int)), this, SLOT(handleChatUnreadReactionCountUpdated(qlonglong,int))); connect(tdLibWrapper, SIGNAL(chatAvailableReactionsUpdated(qlonglong,QVariantMap)), this, SLOT(handleChatAvailableReactionsUpdated(qlonglong,QVariantMap))); connect(tdLibWrapper, SIGNAL(chatFolders(QVariantList,qlonglong)), this, SLOT(handleChatFolders(QVariantList,qlonglong))); connect(tdLibWrapper, SIGNAL(chatFolder(QVariantMap)), this, SLOT(handleChatFolderInformation(QVariantMap))); connect(tdLibWrapper, SIGNAL(chatInArchive(qlonglong)), this, SLOT(handleUpdateChatInArchive(qlonglong))); // Don't start the timer until we have at least one chat relativeTimeRefreshTimer = new QTimer(this); relativeTimeRefreshTimer->setSingleShot(false); relativeTimeRefreshTimer->setInterval(30000); connect(relativeTimeRefreshTimer, SIGNAL(timeout()), SLOT(handleRelativeTimeRefreshTimer())); connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SIGNAL(countChanged())); connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SIGNAL(countChanged())); connect(this, SIGNAL(modelReset()), SIGNAL(countChanged())); requestSortTimer = new QTimer(this); requestSortTimer->setSingleShot(true); requestSortTimer->setInterval(300); connect(requestSortTimer, SIGNAL(timeout()), SLOT(handleRequestSortTimer())); chatInsertionLessThanBufferTimer = new QTimer(this); chatInsertionLessThanBufferTimer->setSingleShot(true); chatInsertionLessThanBufferTimer->setInterval(1000); connect(chatInsertionLessThanBufferTimer, SIGNAL(timeout()), SLOT(handleChatInsertionLessThanBufferTimer())); } ChatListModel::~ChatListModel() { LOG("Destroying myself..."); qDeleteAll(chatList); qDeleteAll(hiddenChats); } ChatListModel* ChatListModel::clone() { ChatListModel* res = new ChatListModel(tdLibWrapper, appSettings); res->relativeTimeRefreshTimer->stop(); for(int i = 0; i < chatList.count(); i++) { res->chatList.append(chatList.at(i)->clone()); } return res; } void ChatListModel::reset() { chatList.clear(); hiddenChats.clear(); } QHash<int,QByteArray> ChatListModel::roleNames() const { QHash<int,QByteArray> roles; roles.insert(ChatListModel::RoleDisplay, "display"); roles.insert(ChatListModel::RoleChatId, "chat_id"); roles.insert(ChatListModel::RoleOrder, "order"); roles.insert(ChatListModel::RoleChatType, "chat_type"); roles.insert(ChatListModel::RoleGroupId, "group_id"); roles.insert(ChatListModel::RoleTitle, "title"); roles.insert(ChatListModel::RolePhotoSmall, "photo_small"); roles.insert(ChatListModel::RoleUnreadCount, "unread_count"); roles.insert(ChatListModel::RoleUnreadMentionCount, "unread_mention_count"); roles.insert(ChatListModel::RoleUnreadReactionCount, "unread_reaction_count"); roles.insert(ChatListModel::RoleAvailableReactions, "available_reactions"); roles.insert(ChatListModel::RoleLastReadInboxMessageId, "last_read_inbox_message_id"); roles.insert(ChatListModel::RoleLastMessageSenderId, "last_message_sender_id"); roles.insert(ChatListModel::RoleLastMessageDate, "last_message_date"); roles.insert(ChatListModel::RoleLastMessageText, "last_message_text"); roles.insert(ChatListModel::RoleLastMessageStatus, "last_message_status"); roles.insert(ChatListModel::RoleChatMemberStatus, "chat_member_status"); roles.insert(ChatListModel::RoleSecretChatState, "secret_chat_state"); roles.insert(ChatListModel::RoleIsVerified, "is_verified"); roles.insert(ChatListModel::RoleIsChannel, "is_channel"); roles.insert(ChatListModel::RoleIsGroup, "is_group"); roles.insert(ChatListModel::RoleIsBasicGroup, "is_basic_group"); roles.insert(ChatListModel::RoleIsSuperGroup, "is_super_group"); roles.insert(ChatListModel::RoleIsPrivateChat, "is_private_chat"); roles.insert(ChatListModel::RoleIsContact, "is_contact"); roles.insert(ChatListModel::RoleIsBot, "is_bot"); roles.insert(ChatListModel::RoleIsMarkedAsUnread, "is_marked_as_unread"); roles.insert(ChatListModel::RoleIsPinned, "is_pinned"); roles.insert(ChatListModel::RoleIsArchived, "is_archived"); roles.insert(ChatListModel::RoleIsMuted, "is_muted"); roles.insert(ChatListModel::RoleFilter, "filter"); roles.insert(ChatListModel::RoleDraftMessageDate, "draft_message_date"); roles.insert(ChatListModel::RoleDraftMessageText, "draft_message_text"); roles.insert(ChatListModel::RoleChatFoldersList, "chat_folder"); roles.insert(ChatListModel::RoleMainChatPositionId, "main_chats_folder_position"); return roles; } int ChatListModel::rowCount(const QModelIndex &) const { return chatList.size(); } QVariant ChatListModel::data(const QModelIndex &index, int role) const { const int row = index.row(); if (row >= 0 && row < chatList.size()) { const ChatData *data = chatList.at(row); switch ((ChatListModel::Role)role) { case ChatListModel::RoleDisplay: return data->chatData; case ChatListModel::RoleOrder: return data->order; case ChatListModel::RoleChatId: return data->chatId; case ChatListModel::RoleChatType: return data->chatType; case ChatListModel::RoleGroupId: return data->groupId; case ChatListModel::RoleTitle: return data->title(); case ChatListModel::RolePhotoSmall: return data->photoSmall(); case ChatListModel::RoleUnreadCount: return data->unreadCount(); case ChatListModel::RoleUnreadMentionCount: return data->unreadMentionCount(); case ChatListModel::RoleAvailableReactions: return data->availableReactions(); case ChatListModel::RoleUnreadReactionCount: return data->unreadReactionCount(); case ChatListModel::RoleLastReadInboxMessageId: return data->lastReadInboxMessageId(); case ChatListModel::RoleLastMessageSenderId: return data->senderUserId(); case ChatListModel::RoleLastMessageText: return data->senderMessageText(); case ChatListModel::RoleLastMessageDate: return data->senderMessageDate(); case ChatListModel::RoleLastMessageStatus: return data->senderMessageStatus(); case ChatListModel::RoleChatMemberStatus: return data->memberStatus; case ChatListModel::RoleSecretChatState: return data->secretChatState; case ChatListModel::RoleIsVerified: return data->verified; case ChatListModel::RoleIsChannel: return data->isChannel(); case ChatListModel::RoleIsGroup: return data->isGroup(); case ChatListModel::RoleIsBasicGroup: return data->isBasicGroup(); case ChatListModel::RoleIsSuperGroup: return data->isSuperGroup(); case ChatListModel::RoleIsPrivateChat: return data->isPrivate(); case ChatListModel::RoleIsContact: return data->isContact(); case ChatListModel::RoleIsBot: return data->isBot(); case ChatListModel::RoleIsMarkedAsUnread: return data->isMarkedAsUnread(); case ChatListModel::RoleIsPinned: return data->isPinned(); case ChatListModel::RoleIsArchived: return data->archived; case ChatListModel::RoleIsMuted: return data->isMute(); case ChatListModel::RoleFilter: return data->title() + " " + data->senderMessageText(); case ChatListModel::RoleDraftMessageText: return data->draftMessageText(); case ChatListModel::RoleDraftMessageDate: return data->draftMessageDate(); case ChatListModel::RoleChatFoldersList: return data->chatId/*this->getChatFolders()*/; case ChatListModel::RoleMainChatPositionId: return mainAllChatFolderPosition; case ChatListModel::RoleArchiveChatPositionId: return getArchiveChatListId(); } } return QVariant(); } QVariantMap ChatListModel::get(int row) { QHash<int,QByteArray> names = roleNames(); QHashIterator<int, QByteArray> i(names); QVariantMap res; QModelIndex idx = index(row, 0); while (i.hasNext()) { i.next(); QVariant data = idx.data(i.key()); res[i.value()] = data; } return res; } QVariantMap ChatListModel::getById(qlonglong chatId) { if (chatIndexMap.contains(chatId)) { return chatList.value(chatIndexMap.value(chatId))->chatData; } return QVariantMap(); } void ChatListModel::enableRefreshTimer() { // Start timestamp refresh timer if not yet active (usually when the first visible chat is discovered) if (!relativeTimeRefreshTimer->isActive()) { LOG("Enabling refresh timer"); relativeTimeRefreshTimer->start(); } } void ChatListModel::calculateUnreadState() { if (this->appSettings->onlineOnlyMode()) { LOG("Online-only mode: Calculating unread state on my own..."); int unreadMessages = 0; int unreadChats = 0; QListIterator<ChatData*> chatIterator(this->chatList); while (chatIterator.hasNext()) { ChatData *currentChat = chatIterator.next(); int unreadCount = currentChat->unreadCount(); if (unreadCount > 0) { unreadChats++; unreadMessages += unreadCount; } } LOG("Online-only mode: New unread state:" << unreadMessages << unreadChats); emit unreadStateChanged(unreadMessages, unreadChats); } } void ChatListModel::addChatsFromBuffer() { if(chatInsertionLessThanBufferTimer->isActive()) { chatInsertionLessThanBufferTimer->stop(); } auto bufferSize = addedChatBuffer.size(); auto currentSize = chatList.size(); beginInsertRows(QModelIndex(), currentSize, currentSize + bufferSize - 1); for(const auto bufferedChat: std::as_const(addedChatBuffer)) { chatList.append(bufferedChat); chatIndexMap.insert(bufferedChat->chatId, chatList.size() - 1); } endInsertRows(); addedChatBuffer.clear(); if(!sortRequestedOnce) { lastChatCount = chatList.size(); requestSortTimer->start(); } } void ChatListModel::addVisibleChat(ChatData *chat) { if (this->tdLibWrapper->getJoinChatRequested()) { this->tdLibWrapper->registerJoinChat(); emit chatJoined(chat->chatId, chat->chatData.value("title").toString()); } addedChatBuffer.append(chat); if(addedChatBuffer.size() < chatBufferSize) { chatInsertionLessThanBufferTimer->start(); return; } addChatsFromBuffer(); } ChatListModel::ChatData* ChatListModel::getChatData(int row) { return chatList.at(row); } void ChatListModel::updateChatVisibility(const TDLibWrapper::Group *group) { //LOG("Updating chat visibility" << (group ? qPrintable(QString::number(group->groupId)) : "")); // See if any group has been removed from from view for (int i = 0; i < chatList.size(); i++) { ChatData *chat = chatList.at(i); const QVector<int> changedRoles(chat->updateGroup(group)); if (chat->isHidden() && !showHiddenChats) { LOG("Hiding chat" << chat->chatId << "at" << i); beginRemoveRows(QModelIndex(), i, i); chatList.removeAt(i); // Update damaged part of the map const int n = chatList.size(); for (int pos = i; pos < n; pos++) { chatIndexMap.insert(chatList.at(pos)->chatId, pos); } i--; hiddenChats.insert(chat->chatId, chat); endRemoveRows(); } else if (!changedRoles.isEmpty()) { const QModelIndex modelIndex(index(i)); emit dataChanged(modelIndex, modelIndex, changedRoles); } } // And see if any group been added to the view const QList<ChatData*> hiddenChatList = hiddenChats.values(); const int n = hiddenChatList.size(); for (int j = 0; j < n; j++) { ChatData *chat = hiddenChatList.at(j); chat->updateGroup(group); if (!chat->isHidden() || showHiddenChats) { hiddenChats.remove(chat->chatId); addVisibleChat(chat); } } } void ChatListModel::updateSecretChatVisibility(const QVariantMap secretChatDetails) { LOG("Updating secret chat visibility" << secretChatDetails.value(ID).toString()); // See if any secret chat has been closed for (int i = 0; i < chatList.size(); i++) { ChatData *chat = chatList.at(i); if (chat->chatType != TDLibWrapper::ChatTypeSecret) { continue; } if (chat->chatData.value(TYPE).toMap().value(SECRET_CHAT_ID).toLongLong() != secretChatDetails.value(ID).toLongLong()) { continue; } const QVector<int> changedRoles(chat->updateSecretChat(secretChatDetails)); if (chat->isHidden() && !showHiddenChats) { LOG("Hiding chat" << chat->chatId << "at" << i); beginRemoveRows(QModelIndex(), i, i); chatList.removeAt(i); // Update damaged part of the map const int n = chatList.size(); for (int pos = i; pos < n; pos++) { chatIndexMap.insert(chatList.at(pos)->chatId, pos); } i--; hiddenChats.insert(chat->chatId, chat); endRemoveRows(); } else if (!changedRoles.isEmpty()) { const QModelIndex modelIndex(index(i)); emit dataChanged(modelIndex, modelIndex, changedRoles); } } } bool ChatListModel::showAllChats() const { return showHiddenChats; } void ChatListModel::setShowAllChats(bool showAll) { if (showHiddenChats != showAll) { showHiddenChats = showAll; updateChatVisibility(Q_NULLPTR); emit showAllChatsChanged(); } } void ChatListModel::handleChatDiscovered(const QString &, const QVariantMap &chatToBeAdded) { LOG("New chat discovered"); ChatData *chat = new ChatData(tdLibWrapper, chatToBeAdded); const TDLibWrapper::Group *group = tdLibWrapper->getGroup(chat->groupId); if (group) { chat->updateGroup(group); } if (chat->chatType == TDLibWrapper::ChatTypeSecret) { QVariantMap secretChatDetails = tdLibWrapper->getSecretChatFromCache(chatToBeAdded.value(TYPE).toMap().value(SECRET_CHAT_ID).toLongLong()); if (!secretChatDetails.isEmpty()) { chat->updateSecretChat(secretChatDetails); } } if (chat->isHidden() && !showHiddenChats) { LOG("Hidden chat" << chat->chatId); hiddenChats.insert(chat->chatId, chat); } else { LOG("Visible chat" << chat->chatId); addVisibleChat(chat); } } void ChatListModel::handleChatLastMessageUpdated(const QString &id, const QString &order, const QVariantMap &lastMessage) { bool ok; const qlonglong chatId = id.toLongLong(&ok); if (ok) { if (chatIndexMap.contains(chatId)) { int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, chat->updateLastMessage(lastMessage)); emit chatChanged(chatId); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating last message for hidden chat" << chatId << "new order" << order); chat->setOrder(order); chat->chatData.insert(LAST_MESSAGE, lastMessage); // A chat can become visible (e.g. when a known contact joins Telegram) // When the private chat is discovered it doesn't have any messages, now it could be there... if (!chat->isHidden() || showHiddenChats) { hiddenChats.remove(chatId); addVisibleChat(chat); } } } } } void ChatListModel::handleChatOrderUpdated(const QString &id, const QString &order, const QString &chatListName) { bool ok; const qlonglong chatId = id.toLongLong(&ok); if (ok) { if (chatIndexMap.contains(chatId)) { LOG("Updating chat order of" << chatId << "to" << order); int chatIndex = chatIndexMap.value(chatId); if (chatList.at(chatIndex)->setOrder(order)) { //updateChatOrder(chatIndex); if (chatListName.contains("chatListArchive")) { chatList.at(chatIndex)->archived = true; } } } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating order of hidden chat" << chatId << "to" << order); chat->setOrder(order); if (chatListName.contains("chatListArchive")) { chat->archived = true; } } } } } void ChatListModel::handleChatReadInboxUpdated(const QString &id, const QString &lastReadInboxMessageId, int unreadCount) { bool ok; const qlonglong chatId = id.toLongLong(&ok); if (ok) { const qlonglong messageId = lastReadInboxMessageId.toLongLong(); if (chatIndexMap.contains(chatId)) { LOG("Updating chat unread count for" << chatId << "unread messages" << unreadCount << ", last read message ID: " << lastReadInboxMessageId); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleDisplay); if (chat->updateUnreadCount(unreadCount)) { changedRoles.append(ChatListModel::RoleUnreadCount); } if (chat->updateLastReadInboxMessageId(messageId)) { changedRoles.append(ChatListModel::RoleLastReadInboxMessageId); } const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); this->calculateUnreadState(); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating unread count for hidden chat" << chatId << "unread messages" << unreadCount << ", last read message ID: " << lastReadInboxMessageId); chat->updateUnreadCount(unreadCount); chat->updateLastReadInboxMessageId(messageId); } } } } void ChatListModel::handleChatReadOutboxUpdated(const QString &id, const QString &lastReadOutboxMessageId) { bool ok; const qlonglong chatId = id.toLongLong(&ok); if (ok) { if (chatIndexMap.contains(chatId)) { LOG("Updating last read message for" << chatId << "last ID" << lastReadOutboxMessageId); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(LAST_READ_OUTBOX_MESSAGE_ID, lastReadOutboxMessageId); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { chat->chatData.insert(LAST_READ_OUTBOX_MESSAGE_ID, lastReadOutboxMessageId); } } } } void ChatListModel::handleChatPhotoUpdated(qlonglong chatId, const QVariantMap &photo) { if (chatIndexMap.contains(chatId)) { LOG("Updating chat photo for" << chatId); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(PHOTO, photo); QVector<int> changedRoles; changedRoles.append(ChatListModel::RolePhotoSmall); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating photo for hidden chat" << chatId); chat->chatData.insert(PHOTO, photo); } } } void ChatListModel::handleChatPinnedMessageUpdated(qlonglong chatId, qlonglong pinnedMessageId) { if (chatIndexMap.contains(chatId)) { LOG("Updating pinned message for" << chatId); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(PINNED_MESSAGE_ID, pinnedMessageId); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating pinned message for hidden chat" << chatId); chat->chatData.insert(PINNED_MESSAGE_ID, pinnedMessageId); } } } void ChatListModel::handleMessageSendSucceeded(qlonglong, qlonglong, const QVariantMap &message) { bool ok; const qlonglong chatId(message.value(CHAT_ID).toLongLong(&ok)); if (ok) { if (chatIndexMap.contains(chatId)) { const int chatIndex = chatIndexMap.value(chatId); LOG("Updating last message for chat" << chatId << "at index" << chatIndex << ", as message was sent, old ID:" << oldMessageId << ", new ID:" << messageId); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, chatList.at(chatIndex)->updateLastMessage(message)); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating last message for hidden chat" << chatId << ", as message was sent, old ID:" << oldMessageId << ", new ID:" << messageId); chat->chatData.insert(LAST_MESSAGE, message); } } } } void ChatListModel::handleChatNotificationSettingsUpdated(const QString &id, const QVariantMap &chatNotificationSettings) { bool ok; const qlonglong chatId = id.toLongLong(&ok); if (ok) { if (chatIndexMap.contains(chatId)) { const int chatIndex = chatIndexMap.value(chatId); LOG("Updating notification settings for chat" << chatId << "at index" << chatIndex); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(NOTIFICATION_SETTINGS, chatNotificationSettings); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { chat->chatData.insert(NOTIFICATION_SETTINGS, chatNotificationSettings); } } } } void ChatListModel::handleGroupUpdated(qlonglong groupId) { updateChatVisibility(tdLibWrapper->getGroup(groupId)); } void ChatListModel::handleSecretChatUpdated(qlonglong, const QVariantMap &secretChat) { updateSecretChatVisibility(secretChat); } void ChatListModel::handleChatTitleUpdated(const QString &chatId, const QString &title) { qlonglong chatIdLongLong = chatId.toLongLong(); if (chatIndexMap.contains(chatIdLongLong)) { LOG("Updating title for" << chatId); const int chatIndex = chatIndexMap.value(chatIdLongLong); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(TITLE, title); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleTitle); changedRoles.append(ChatListModel::RoleFilter); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId.toLongLong()); if (chat) { LOG("Updating title for hidden chat" << chatId); chat->chatData.insert(TITLE, title); } } } void ChatListModel::handleChatPinnedUpdated(qlonglong chatId, bool chatIsPinned, const QString &chatListName) { if (chatIndexMap.contains(chatId)) { LOG("Updating chat is pinned for" << chatId << chatIsPinned); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); if (chatListName.contains("chatListArchive")) { chatList.at(chatIndex)->archived = true; } chat->chatData.insert(IS_PINNED, chatIsPinned); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleIsPinned); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating chat is pinned for hidden chat" << chatId); chat->chatData.insert(IS_PINNED, chatIsPinned); if (chatListName.contains("chatListArchive")) { chat->archived = true; } } } } void ChatListModel::handleChatIsMarkedAsUnreadUpdated(qlonglong chatId, bool chatIsMarkedAsUnread) { if (chatIndexMap.contains(chatId)) { LOG("Updating chat is marked as unread for" << chatId << chatIsMarkedAsUnread); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(IS_MARKED_AS_UNREAD, chatIsMarkedAsUnread); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleIsMarkedAsUnread); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating chat is marked as unread for hidden chat" << chatId); chat->chatData.insert(IS_MARKED_AS_UNREAD, chatIsMarkedAsUnread); } } } void ChatListModel::handleChatDraftMessageUpdated(qlonglong chatId, const QVariantMap &draftMessage, const QString &order) { LOG("Updating draft message for" << chatId); if (chatIndexMap.contains(chatId)) { const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(DRAFT_MESSAGE, draftMessage); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleDraftMessageDate); changedRoles.append(ChatListModel::RoleDraftMessageText); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); if (chat->setOrder(order)) { //updateChatOrder(chatIndex); } } } void ChatListModel::handleChatUnreadMentionCountUpdated(qlonglong chatId, int unreadMentionCount) { if (chatIndexMap.contains(chatId)) { LOG("Updating mention count for" << chatId << unreadMentionCount); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(UNREAD_MENTION_COUNT, unreadMentionCount); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleUnreadMentionCount); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating mention count for hidden chat" << chatId << unreadMentionCount); chat->chatData.insert(UNREAD_MENTION_COUNT, unreadMentionCount); } } } void ChatListModel::handleChatUnreadReactionCountUpdated(qlonglong chatId, int unreadReactionCount) { if (chatIndexMap.contains(chatId)) { LOG("Updating reaction count for" << chatId << unreadReactionCount); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(UNREAD_REACTION_COUNT, unreadReactionCount); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleUnreadReactionCount); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating reaction count for hidden chat" << chatId << unreadReactionCount); chat->chatData.insert(UNREAD_REACTION_COUNT, unreadReactionCount); } } } void ChatListModel::handleChatAvailableReactionsUpdated(qlonglong chatId, const QVariantMap availableReactions) { if (chatIndexMap.contains(chatId)) { LOG("Updating available reaction type for" << chatId << availableReactions); const int chatIndex = chatIndexMap.value(chatId); ChatData *chat = chatList.at(chatIndex); chat->chatData.insert(AVAILABLE_REACTIONS, availableReactions); QVector<int> changedRoles; changedRoles.append(ChatListModel::RoleAvailableReactions); const QModelIndex modelIndex(index(chatIndex)); emit dataChanged(modelIndex, modelIndex, changedRoles); } else { ChatData *chat = hiddenChats.value(chatId); if (chat) { LOG("Updating available reaction type for hidden chat" << chatId << availableReactions); chat->chatData.insert(AVAILABLE_REACTIONS, availableReactions); } } } void ChatListModel::handleRelativeTimeRefreshTimer() { LOG("Refreshing timestamps"); QVector<int> roles; roles.append(ChatListModel::RoleLastMessageDate); roles.append(ChatListModel::RoleLastMessageStatus); emit dataChanged(index(0), index(chatList.size() - 1), roles); } void ChatListModel::handleChatInsertionLessThanBufferTimer() { addChatsFromBuffer(); } void ChatListModel::handleRequestSortTimer() { if(chatList.size() - lastChatCount < chatBufferSize) { sortRequestedOnce = true; emit requestSort(); } } void ChatListModel::handleChatFolders(const QVariantList &foldersInformation, qlonglong mainChatlistPosition) { LOG("Updating available chat Folders" << foldersInformation << "with main Chatlist position" << mainChatlistPosition); chatFolders.clear(); int positionIndex = 0; mainAllChatFolderPosition = mainChatlistPosition; for (const auto&folder: foldersInformation) { chatFolders.append(folder.toMap()); ++positionIndex; } QVariantMap map; map["id"] = QString::number(mainAllChatFolderPosition); map["title"] = tr("All Chats"); chatFolders.insert(mainAllChatFolderPosition, map); auto archivePosition = chatFolders.size(); map["id"] = QString::number(archivePosition); map["title"] = tr("Archive"); chatFolders.insert(archivePosition, map); emit chatFoldersChanged(chatFolders); } void ChatListModel::handleUpdateChatInArchive(const qlonglong chatID) { if (!hiddenChats.contains(chatID)) { auto index = chatIndexMap.value(chatID); if (index < chatList.size()) { chatList.at(chatIndexMap.value(chatID))->archived = true; } } else { hiddenChats.value(chatID)->archived = true; //qDebug() << chatID << " chat is hidden"; } } qlonglong ChatListModel::getSelectedFolderId(QString selectedFolder) const { for (const auto& folder: chatFolders) { if (folder.toMap()["title"].toString().contains(selectedFolder)) { return folder.toMap()["id"].toLongLong(); } } return -1; } void ChatListModel::handleChatFolderInformation(const QVariantMap &chatFolderInformation) { QString title = chatFolderInformation.value("title").toString(); if (chatFolderList.contains(title)) { auto it = chatFolderList.find(title); if (it != chatFolderList.end()) chatFolderList.erase(it); } chatFolderList.insert(title, chatFolderInformation); emit chatFolderInformationChanged(chatFolderList); } QVariantList ChatListModel::getChatFolders() const { return chatFolders; } QList<qlonglong> ChatListModel::getChatFoldersListConditionIDs(QString selectedFolder, QString condition) const { QList<qlonglong> ret_value; if (chatFolderList.contains(selectedFolder)) { auto list = chatFolderList.value(selectedFolder).toMap().value(condition).toList(); for (const auto &id: list){ ret_value.push_back(id.toLongLong()); } } return ret_value; } bool ChatListModel::isChatFolderCondition(QString selectedFolder, QString condition) const { if (chatFolderList.contains(selectedFolder)) { auto map = chatFolderList.value(selectedFolder).toMap(); return map.value(condition).toBool(); } return false; };