Skip to content
Snippets Groups Projects
messagemodel.h 4.11 KiB
Newer Older
/********************************************************************************************
 *                                                                                          *
 * Copyright (C) 2017 Armin Felder, Dennis Beier                                            *
 * This file is part of RocketChatMobileEngine <https://git.fairkom.net/chat/fairchat>.     *
 *                                                                                          *
 * RocketChatMobileEngine 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.                                                      *
 *                                                                                          *
 * RocketChatMobileEngine 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 RocketChatMobileEngine. If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                                          *
 ********************************************************************************************/


Armin Felder's avatar
Armin Felder committed
#ifndef MESSAGEMODEL_H
#define MESSAGEMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QString>
#include <QMap>

#include "repos/entities/rocketchatchannel.h"
#include "container/sortedvector.h"
class RocketChatChannel;

class MessagesModel : public QAbstractListModel
{
        Q_OBJECT
        Q_PROPERTY( QString currentChannel READ getCurrent WRITE setCurrent NOTIFY currentChannelChanged )

Armin Felder's avatar
Armin Felder committed
        enum class MessageRoles : int {
            text = Qt::UserRole + 1,
            type,
            linkurl,
            author,
            date,
            time,
            ownMessage,
            width,
            height,
            formattedDate,
            formattedTime,
            messageType,
            authorId,
            id,
            replyMessage,
            replyAutor,
            replyDate,
Armin Felder's avatar
Armin Felder committed
            replyUrl,
            avatarImg
        };
        MessagesModel();
        QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
        QString getCurrent() const;
        void setCurrent( const QString &value );
        Q_INVOKABLE int rowCount( const QModelIndex &parent = QModelIndex() ) const;
Armin Felder's avatar
Armin Felder committed

Armin Felder's avatar
Armin Felder committed
        Q_INVOKABLE int getPositionOfMessage( const QString &pId );
Armin Felder's avatar
Armin Felder committed

        void onCurrentChannelChanged();
        bool addChannel( const QSharedPointer<RocketChatChannel> & );
    public slots:
        void addBlockedUser( const QString &pUserId );

    protected:
        QString current;
        QSet<QString> duplicateCheck;

        QHash<int, QByteArray> roleNames() const;
        bool buffered = false;
        int mInsertCount = 0;
        void onNewMessage( const QSharedPointer<RocketChatChannel> &channel, qint64 pMessageId );
        SortedVector<RocketChatMessage> messagesOfCurrentChannel;
        void onBeginInsertList( const QString &pChannelId );
        void onEndInsertList( const QString &pChannelId );
        QMap<QString, QSharedPointer<RocketChatChannel>> mChannelMap;
        QMutex mutex;
        QSet<QString> mBlockedUsers;
        void onMessageDeleted(const QString channelId, const QString messageId);
Armin Felder's avatar
Armin Felder committed
        void onDataChanged( const QString &id, const QString &property );
    signals:
        void currentChannelChanged( void );
        void countChanged();
    public slots:
        void addChannelSlot( const QSharedPointer<RocketChatChannel> & );
Armin Felder's avatar
Armin Felder committed
};

#endif // MESSAGEMODEL_H