/******************************************************************************************** * * * 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/>. * * * ********************************************************************************************/ #include "channelmodel.h" ChannelModel::ChannelModel() { // connect(&timer,&QTimer::timeout,this,&ChannelModel::sortChanged); } int ChannelModel::rowCount( const QModelIndex &parent ) const { Q_UNUSED( parent ) int count = channelList.count(); return count; } QVariant ChannelModel::data( const QModelIndex &index, int role ) const { int row = index.row(); if ( channelList.isEmpty() || row > channelList.count() ) { return QVariant(); } auto currentChannel = channelList.at( row ); if ( role == name ) { return currentChannel->getName(); } if ( role == roomId ) { return currentChannel->getRoomId(); } if ( role == lastMessageText ) { auto channel = channelList.at( row ); auto message = channel->getYoungestMessage(); if ( message.isNull() ) { return ""; } if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) { return tr( QByteArrayLiteral( "file upload" ) ); } return message->getMessageString(); } if ( role == channelType ) { return currentChannel->getType(); } if ( role == unreadMessages ) { return currentChannel->getUnreadMessages(); } if ( role == readonly ) { return currentChannel->getReadOnly() || currentChannel->getArchived() || currentChannel->getBlocked(); } if ( role == ownerId ) { return currentChannel->getOwnerName(); } if ( role == ownerId ) { return currentChannel->getOwnerId(); } if ( role == archived ) { return currentChannel->getArchived(); } if ( role == blocked ) { return currentChannel->getBlocked(); } if ( role == username ) { return currentChannel->getUsername(); } return QVariant(); } bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel ) { if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) { connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection ); connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection ); duplicateCheck.insert( channel->getRoomId() ); int pos = channelList.findPosition( channel ); emit beginInsertRows( QModelIndex(), pos, pos ); channelList.insertSort( channel ); emit endInsertRows(); sortChanged(); return true; } else { return false; } } QHash<int, QByteArray> ChannelModel::roleNames() const { QHash<int, QByteArray> roles; roles[name] = QByteArrayLiteral( "name" ); roles[roomId] = QByteArrayLiteral( "roomId" ); roles[unreadMessages] = QByteArrayLiteral( "unreadMessages" ); roles[channelType] = QByteArrayLiteral( "type" ); roles[lastMessageText] = QByteArrayLiteral( "lastMessageText" ); roles[readonly] = QByteArrayLiteral( "readonly" ); roles[archived] = QByteArrayLiteral( "archived" ); roles[blocked] = QByteArrayLiteral( "blocked" ); roles[deleted] = QByteArrayLiteral( "deleted" ); roles[ownerId] = QByteArrayLiteral( "ownerId" ); roles[ownerName] = QByteArrayLiteral( "ownerName" ); roles[username] = QByteArrayLiteral( "username" ); return roles; } void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp ) { Q_UNUSED( timestamp ) int pos = 0; bool found = false; for ( const auto ¤t : channelList ) { if ( current->getRoomId() == id ) { found = true; break; } pos++; } if ( found ) { //emit dataChanged(index(pos),index(pos),{lastMessageText}); } sortChanged(); } void ChannelModel::onUnreadMessageChanged( const QString &id, int number ) { Q_UNUSED( number ) qDebug() << "unread signal catched"; int pos = 0; bool found = false; for ( const auto ¤t : channelList ) { if ( current->getRoomId() == id ) { found = true; break; } pos++; } if ( found ) { qDebug() << "channel found"; emit dataChanged( index( pos ), index( pos ), {unreadMessages} ); emit unreadMessagesChanged(); } } void ChannelModel::onDataChanged( const QString &id, const QString &property ) { int pos = 0; bool found = false; for ( const auto ¤t : channelList ) { if ( current->getRoomId() == id ) { found = true; break; } pos++; } if ( found ) { auto roles = roleNames(); auto roleKeys = roles.keys(); for ( auto roleKey : roleKeys ) { if ( roles[roleKey] == property ) { emit dataChanged( index( pos ), index( pos ), {roleKey} ); } } } } void ChannelModel::onDeleted( const QString &pId, bool deleted ) { int idx = -1; for ( int i = 0; i < channelList.length(); i++ ) { if ( channelList.at( i )->getRoomId() == pId ) { idx = i; break; } } if ( idx != -1 ) { beginRemoveRows( QModelIndex(), idx, idx ); channelList.remove( idx ); endRemoveRows(); } } void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel ) { addChannel( channel ); } QString ChannelModel::getType() const { return type; } void ChannelModel::setType( const QString &value ) { type = value; } void ChannelModel::sortChanged() { beginResetModel(); std::sort( channelList.begin(), channelList.end(), []( QSharedPointer<RocketChatChannel> channel1, QSharedPointer<RocketChatChannel> channel2 ) { auto youngest1 = channel1->getYoungestMessage(); auto youngest2 = channel2->getYoungestMessage(); if ( !youngest1.isNull() && !youngest2.isNull() ) { auto timestamp1 = youngest1->getTimestamp(); auto timestamp2 = youngest2->getTimestamp(); return timestamp2 < timestamp1; } else if ( youngest1.isNull() && !youngest2.isNull() ) { return false; } else if ( youngest2.isNull() && !youngest1.isNull() ) { return true; } else { return false; } } ); endResetModel(); emit countChanged(); } void ChannelModel::clear() { beginResetModel(); channelList.clear(); duplicateCheck.clear(); endResetModel(); }