harbour-fernschreiber/src/chatmodel.h
Slava Monich 0cb19d833c Message API usage cleanup
1. Pass chat_id where appropriate
2. Pass message_id and chat_id (which are numbers) as numbers
3. Use pre-initialized QStrings more often
4. Don't pass numbers by const reference, it doesn't make sense
5. Removed some redundant const modifiers
2020-12-30 18:20:50 +02:00

101 lines
4.2 KiB
C++

/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
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/>.
*/
#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)
Q_PROPERTY(QVariantMap smallPhoto READ smallPhoto NOTIFY smallPhotoChanged)
public:
ChatModel(TDLibWrapper *tdLibWrapper);
~ChatModel() override;
virtual QHash<int,QByteArray> roleNames() const override;
virtual int rowCount(const QModelIndex&) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
Q_INVOKABLE void clear(bool contentOnly = false);
Q_INVOKABLE void initialize(const QVariantMap &chatInformation);
Q_INVOKABLE void triggerLoadMoreHistory();
Q_INVOKABLE void triggerLoadMoreFuture();
Q_INVOKABLE QVariantMap getChatInformation();
Q_INVOKABLE QVariantMap getMessage(int index);
Q_INVOKABLE int getLastReadMessageIndex();
Q_INVOKABLE void setSearchQuery(const QString newSearchQuery);
QVariantMap smallPhoto() const;
qlonglong getChatId() const;
signals:
void messagesReceived(int modelIndex, int lastReadSentIndex, int totalCount);
void messagesIncrementalUpdate(int modelIndex, int lastReadSentIndex);
void newMessageReceived(const QVariantMap &message);
void unreadCountUpdated(int unreadCount, const QString &lastReadInboxMessageId);
void lastReadSentMessageUpdated(int lastReadSentIndex);
void notificationSettingsUpdated();
void messageUpdated(int modelIndex);
void smallPhotoChanged();
void chatIdChanged();
void pinnedMessageChanged();
private slots:
void handleMessagesReceived(const QVariantList &messages, int totalCount);
void handleNewMessageReceived(qlonglong chatId, const QVariantMap &message);
void handleMessageReceived(qlonglong chatId, qlonglong messageId, const QVariantMap &message);
void handleChatReadInboxUpdated(const QString &chatId, const QString &lastReadInboxMessageId, int unreadCount);
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);
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 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();
private:
TDLibWrapper *tdLibWrapper;
QList<MessageData*> messages;
QHash<qlonglong,int> messageIndexMap;
QVariantMap chatInformation;
qlonglong chatId;
bool inReload;
bool inIncrementalUpdate;
bool searchModeActive;
QString searchQuery;
};
#endif // CHATMODEL_H