#include "channelmodel.h" ChannelModel::ChannelModel() { // connect(&timer,&QTimer::timeout,this,&ChannelModel::sortChanged); } int ChannelModel::rowCount(const QModelIndex &parent) const { return channelList.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 ""; return message->getMessageString(); } if(role == channelType){ return currentChannel->getType(); } if(role == unreadMessages){ return currentChannel->getUnreadMessages(); } return QVariant(); } bool ChannelModel::addChannel(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); 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] = "name"; roles[roomId] = "roomId"; roles[unreadMessages] = "unreadMessages"; roles[channelType] = "type"; roles[lastMessageText] = "lastMessageText"; return roles; } void ChannelModel::onNewerMessage(QString id, qint64 timestamp) { int pos = 0; bool found = false; for(QSharedPointer<RocketChatChannel> current : channelList){ if(current->getRoomId() == id ){ found = true; break; } pos++; } if(found){ //emit dataChanged(index(pos),index(pos),{lastMessageText}); } sortChanged(); } void ChannelModel::onUnreadMessageChanged(QString id, int number) { int pos = 0; bool found = false; for(QSharedPointer<RocketChatChannel> current : channelList){ if(current->getRoomId() == id ){ found = true; break; } pos++; } if(found){ emit dataChanged(index(pos),index(pos),{unreadMessages}); emit unreadMessagesChanged(); } } void ChannelModel::addChannelSlot(QSharedPointer<RocketChatChannel> channel) { addChannel(channel); } QString ChannelModel::getType() const { return type; } void ChannelModel::setType(const QString &value) { type = value; } void ChannelModel::sortChanged() { qDebug() << "sort changed"; 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()){ qDebug()<<channel1->getName()<<" ts "<<youngest1->getTimestamp(); qDebug()<<channel2->getName()<<" ts "<<youngest2->getTimestamp(); auto timestamp1 = youngest1->getTimestamp(); auto timestamp2 = youngest2->getTimestamp(); return timestamp2 < timestamp1; }else if(youngest1.isNull() && !youngest2.isNull()){ qDebug()<<channel2->getName()<<" ts "<<youngest2->getTimestamp(); return false; }else if(youngest2.isNull() && !youngest1.isNull()){ qDebug()<<channel1->getName()<<" ts "<<youngest1->getTimestamp(); return true; }else{ return false; } }); endResetModel(); emit countChanged(); }