Skip to content
Snippets Groups Projects
messagemodel.cpp 4.27 KiB
Newer Older
Armin Felder's avatar
Armin Felder committed
#include "messagemodel.h"

MessageModel::MessageModel()
{

}

ChannelRepository *MessageModel::getRepo() const
{
    return repo;
}

void MessageModel::setRepo( ChannelRepository *value )
{
    repo = value;
    connect( repo, &ChannelRepository::newMessage, this, &MessageModel::onNewMessage );
    connect( repo, &ChannelRepository::beginInsertList, this, &MessageModel::onBeginInsertList );
    connect( repo, &ChannelRepository::endInsertList, this, &MessageModel::onEndInsertList );
}

QVariant MessageModel::data( const QModelIndex &index, int role ) const
{
    int row = index.row();

    auto messageAtIndex =  messagesOfCurrentChannel.at( row );

    if ( role == MessageRoles::type ) {
        return messageAtIndex->getMessageType();
    }

    if ( role == MessageRoles::text ) {
        return messageAtIndex->getMessageString();
    }

    if ( role == MessageRoles::linkurl ) {
        if ( !messageAtIndex->getAttachments().empty() ) {
            return messageAtIndex->getAttachments().first()->getUrl();
        } else {
            qWarning() << "linkurl not found";
        }
    }

    if ( role == author ) {
        return messageAtIndex->getAuthor();
    }

    if ( role == date ) {
        return messageAtIndex->getFormattedDate();
    }

    if ( role == time ) {
        return messageAtIndex->getFormattedTime();
    }

    if ( role == ownMessage ) {
        return messageAtIndex->getOwnMessage();
    }

    if ( role == width ) {
        if ( !messageAtIndex->getAttachments().empty() ) {
            return messageAtIndex->getAttachments().first()->getWidth();
        } else {
            qWarning() << "height not found";
        }
    }

    if ( role == height ) {
        if ( !messageAtIndex->getAttachments().empty() ) {
            return messageAtIndex->getAttachments().first()->getHeight();
        } else {
            qWarning() << "height not found";
        }
    }

    qWarning() << "messageModel data not found for row";
    return QVariant();
}

QString MessageModel::getCurrent() const
{
    return current;
}

void MessageModel::setCurrent( const QString &value )
{
    current = value;

    if ( repo->contains( value ) ) {
        messagesOfCurrentChannel = repo->get( value )->getMessageRepo()->order();
        duplicateCheck.clear();

        for ( auto currentMessage : messagesOfCurrentChannel ) {
            duplicateCheck.insert( currentMessage->getId() );
        }

        emit countChanged();
    } else {
        duplicateCheck.clear();
        messagesOfCurrentChannel.clear();
        emit countChanged();
    }
}

int MessageModel::rowCount( const QModelIndex &parent ) const
{
    Q_UNUSED( parent );
    qDebug() << "number of messages in model" << messagesOfCurrentChannel.count();

    if ( !repo ) {
        return 0;
    }

    int count = 0;

    if ( messagesOfCurrentChannel.isEmpty() ) {
        return 0;
    }

    count = messagesOfCurrentChannel.count();

    return count;
}

QHash<int, QByteArray> MessageModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[text] = "text";
    roles[author] = "author";
    roles[linkurl] = "linkurl";
    roles[type] = "type";
    roles[date] = "date";
    roles[time] = "time";
    roles[ownMessage] = "ownMessage";
    roles[height] = "height";
    roles[width ] = "width";
    return roles;
}

void MessageModel::onNewMessage( QString channelId, int row, QString pMessageId )
{

    if ( channelId == current ) {
        auto channel = repo->get( channelId );
        auto message = channel->getMessageRepo()->get( pMessageId );

        if ( !duplicateCheck.contains( message->getId() ) ) {
            beginInsertRows( QModelIndex(), row, row );
            messagesOfCurrentChannel.insert( row, message );
            mInsertCount++;
            endInsertRows();
            duplicateCheck.insert( message->getId() );
        }

        if ( !buffered ) {
            mInsertCount = 0;
            emit countChanged();
        }
    }
}

void MessageModel::onBeginInsertList( QString pChannelId )
{
    if ( pChannelId == current ) {
        buffered = true;
        mInsertCount = 0;
    }
}

void MessageModel::onEndInsertList( QString pChannelId )
{
    if ( pChannelId == current ) {
        buffered = false;

        if ( mInsertCount ) {
            mInsertCount = 0;
            emit countChanged();
        }
    }
}