harbour-fernschreiber/src/chatmodel.h

97 lines
3.9 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;
Q_INVOKABLE void clear();
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-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);
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 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();
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-08-22 18:30:02 +03:00
};
#endif // CHATMODEL_H