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/>. *
* *
********************************************************************************************/
// connect(&timer,&QTimer::timeout,this,&ChannelModel::sortChanged);
int ChannelModel::rowCount( const QModelIndex &parent ) const
Q_UNUSED( parent )
int count = channelList.count();
QVariant ChannelModel::data( const QModelIndex &index, int role ) const
if ( channelList.isEmpty() || row > channelList.count() ) {
auto currentChannel = channelList.at( row );
if ( role == name ) {
if ( role == roomId ) {
if ( role == lastMessageText ) {
auto channel = channelList.at( row );
if ( message.isNull() ) {
Armin Felder
committed
if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) {
return tr( QByteArrayLiteral( "file upload" ) );
if ( role == channelType ) {
if ( role == unreadMessages ) {
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 ) {
if ( role == blocked ) {
return currentChannel->getBlocked();
}
if ( role == username ) {
return currentChannel->getUsername();
}
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 );
Armin Felder
committed
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 );
return false;
}
}
QHash<int, QByteArray> ChannelModel::roleNames() const
{
QHash<int, QByteArray> roles;
Armin Felder
committed
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" );
void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp )
Q_UNUSED( timestamp )
for ( const auto ¤t : channelList ) {
if ( current->getRoomId() == id ) {
//emit dataChanged(index(pos),index(pos),{lastMessageText});
}
sortChanged();
}
void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
Q_UNUSED( number )
qDebug() << "unread signal catched";
for ( const auto ¤t : channelList ) {
if ( current->getRoomId() == id ) {
if ( found ) {
qDebug() << "channel found";
emit dataChanged( index( pos ), index( pos ), {unreadMessages} );
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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 )
Armin Felder
committed
{
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();
Armin Felder
committed
}
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() ) {
} else if ( youngest2.isNull() && !youngest1.isNull() ) {
void ChannelModel::clear()
{
channelList.clear();