harbour-fernschreiber/src/chatmodel.h

104 lines
4.4 KiB
C
Raw Normal View History

2020-09-02 23:49:15 +03:00
/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
2020-09-02 23:49:15 +03:00
This file is part of Fernschreiber.
Fernschreiber is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Fernschreiber is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
2020-08-22 18:30:02 +03:00
#ifndef CHATMODEL_H
#define CHATMODEL_H
#include <QAbstractListModel>
#include "tdlibwrapper.h"
class ChatModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(qlonglong chatId READ getChatId NOTIFY chatIdChanged)
2020-11-15 07:11:10 +03:00
Q_PROPERTY(QVariantMap smallPhoto READ smallPhoto NOTIFY smallPhotoChanged)
2020-08-22 18:30:02 +03:00
public:
ChatModel(TDLibWrapper *tdLibWrapper);
~ChatModel() override;
virtual QHash<int,QByteArray> roleNames() const override;
2020-08-22 18:30:02 +03:00
virtual int rowCount(const QModelIndex&) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
2020-12-27 02:16:25 +03:00
Q_INVOKABLE void clear(bool contentOnly = false);
Q_INVOKABLE void initialize(const QVariantMap &chatInformation);
2020-08-26 23:52:06 +03:00
Q_INVOKABLE void triggerLoadMoreHistory();
Q_INVOKABLE void triggerLoadMoreFuture();
Q_INVOKABLE QVariantMap getChatInformation();
2020-11-15 07:11:10 +03:00
Q_INVOKABLE QVariantMap getMessage(int index);
Q_INVOKABLE int getLastReadMessageIndex();
2020-12-27 02:16:25 +03:00
Q_INVOKABLE void setSearchQuery(const QString newSearchQuery);
2020-11-15 07:11:10 +03:00
QVariantMap smallPhoto() const;
qlonglong getChatId() const;
2020-08-22 22:43:20 +03:00
signals:
2020-11-15 07:11:10 +03:00
void messagesReceived(int modelIndex, int lastReadSentIndex, int totalCount);
void messagesIncrementalUpdate(int modelIndex, int lastReadSentIndex);
2020-09-22 21:32:35 +03:00
void newMessageReceived(const QVariantMap &message);
2020-11-15 07:11:10 +03:00
void unreadCountUpdated(int unreadCount, const QString &lastReadInboxMessageId);
void lastReadSentMessageUpdated(int lastReadSentIndex);
void notificationSettingsUpdated();
2020-11-15 07:11:10 +03:00
void messageUpdated(int modelIndex);
void smallPhotoChanged();
void chatIdChanged();
void pinnedMessageChanged();
2020-08-22 22:43:20 +03:00
private slots:
2020-11-15 07:11:10 +03:00
void handleMessagesReceived(const QVariantList &messages, int totalCount);
void handleNewMessageReceived(qlonglong chatId, const QVariantMap &message);
void handleMessageReceived(qlonglong chatId, qlonglong messageId, const QVariantMap &message);
2020-11-15 07:11:10 +03:00
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount);
2020-08-30 20:04:16 +03:00
void handleChatReadOutboxUpdated(const QString &chatId, const QString &lastReadOutboxMessageId);
void handleMessageSendSucceeded(qlonglong messageId, qlonglong oldMessageId, const QVariantMap &message);
void handleChatNotificationSettingsUpdated(const QString &chatId, const QVariantMap &chatNotificationSettings);
2020-11-15 07:11:10 +03:00
void handleChatPhotoUpdated(qlonglong chatId, const QVariantMap &photo);
void handleChatPinnedMessageUpdated(qlonglong chatId, qlonglong pinnedMessageId);
void handleMessageContentUpdated(qlonglong chatId, qlonglong messageId, const QVariantMap &newContent);
void handleMessageEditedUpdated(qlonglong chatId, qlonglong messageId, const QVariantMap &replyMarkup);
void handleMessageInteractionInfoUpdated(qlonglong chatId, qlonglong messageId, const QVariantMap &updatedInfo);
void handleMessagesDeleted(qlonglong chatId, const QList<qlonglong> &messageIds);
private:
class MessageData;
void removeRange(int firstDeleted, int lastDeleted);
void insertMessages(const QList<MessageData*> newMessages);
void appendMessages(const QList<MessageData*> newMessages);
void prependMessages(const QList<MessageData*> newMessages);
QVariantMap enhanceMessage(const QVariantMap &message);
int calculateLastKnownMessageId();
int calculateLastReadSentMessageId();
int calculateScrollPosition(int listInboxPosition);
bool isMostRecentMessageLoaded();
2020-08-22 22:43:20 +03:00
2020-08-22 18:30:02 +03:00
private:
TDLibWrapper *tdLibWrapper;
QList<MessageData*> messages;
QHash<qlonglong,int> messageIndexMap;
QVariantMap chatInformation;
2020-11-15 07:11:10 +03:00
qlonglong chatId;
2020-08-22 22:43:20 +03:00
bool inReload;
2020-08-26 23:52:06 +03:00
bool inIncrementalUpdate;
2020-12-27 02:16:25 +03:00
bool searchModeActive;
QString searchQuery;
2020-08-22 18:30:02 +03:00
};
#endif // CHATMODEL_H