channelmodel.cpp 4.7 KB
Newer Older
armin's avatar
fixes  
armin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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 "";
armin's avatar
armin committed
31
32
        if(message->getMessageType() == "file" || message->getMessageType() == "image" || message->getMessageType() == "audio")
            return tr("file upload");
armin's avatar
fixes  
armin committed
33
34
35
36
37
38
39
40
        return message->getMessageString();
    }
    if(role == channelType){
        return currentChannel->getType();
    }
    if(role == unreadMessages){
        return currentChannel->getUnreadMessages();
    }
armin's avatar
armin committed
41
42
43
44
45
46
    if(role == readonly){
        return currentChannel->getReadOnly() || currentChannel->getArchived();
    }
    if(role == archived){
        return currentChannel->getArchived();
    }
armin's avatar
fixes  
armin committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    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";
armin's avatar
armin committed
76
77
    roles[readonly] = "readonly";
    roles[archived] = "archived";
armin's avatar
fixes  
armin committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    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)
{
armin's avatar
armin committed
101
    qDebug()<<"unread signal catched";
armin's avatar
fixes  
armin committed
102
103
    int pos = 0;
    bool found = false;
armin's avatar
armin committed
104

armin's avatar
fixes  
armin committed
105
106
107
108
109
110
111
    for(QSharedPointer<RocketChatChannel> current : channelList){
        if(current->getRoomId() == id ){
            found = true;
            break;
        }
        pos++;
    }
armin's avatar
armin committed
112

armin's avatar
fixes  
armin committed
113
    if(found){
armin's avatar
armin committed
114
        qDebug()<<"channel found";
armin's avatar
fixes  
armin committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        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();
}