channelmodel.cpp 8.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/********************************************************************************************
 *                                                                                          *
 * 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/>.           *
 *                                                                                          *
 ********************************************************************************************/


armin's avatar
fixes  
armin committed
22
23
24
25
#include "channelmodel.h"

ChannelModel::ChannelModel()
{
26
    // connect(&timer,&QTimer::timeout,this,&ChannelModel::sortChanged);
armin's avatar
fixes  
armin committed
27
28
}

29
int ChannelModel::rowCount( const QModelIndex &parent ) const
armin's avatar
fixes  
armin committed
30
{
31
32
    Q_UNUSED( parent )
    int count = channelList.count();
33
    return count;
armin's avatar
fixes  
armin committed
34
35
}

36
QVariant ChannelModel::data( const QModelIndex &index, int role ) const
armin's avatar
fixes  
armin committed
37
38
{
    int row = index.row();
39
40

    if ( channelList.isEmpty() || row > channelList.count() ) {
armin's avatar
fixes  
armin committed
41
42
        return QVariant();
    }
43
44
45
46

    auto currentChannel = channelList.at( row );

    if ( role == name ) {
armin's avatar
fixes  
armin committed
47
48
        return currentChannel->getName();
    }
49
50

    if ( role == roomId ) {
armin's avatar
fixes  
armin committed
51
52
        return currentChannel->getRoomId();
    }
53
54
55

    if ( role == lastMessageText ) {
        auto channel = channelList.at( row );
armin's avatar
fixes  
armin committed
56
        auto message = channel->getYoungestMessage();
57
58

        if ( message.isNull() ) {
armin's avatar
fixes  
armin committed
59
            return "";
60
61
        }

62
63
        if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) {
            return tr( QByteArrayLiteral( "file upload" ) );
64
65
        }

armin's avatar
fixes  
armin committed
66
67
        return message->getMessageString();
    }
68
69

    if ( role == channelType ) {
armin's avatar
fixes  
armin committed
70
71
        return currentChannel->getType();
    }
72
73

    if ( role == unreadMessages ) {
armin's avatar
fixes  
armin committed
74
75
        return currentChannel->getUnreadMessages();
    }
76
77

    if ( role == readonly ) {
armin's avatar
armin committed
78
        return currentChannel->getReadOnly() || currentChannel->getArchived() || currentChannel->getBlocked();
armin's avatar
armin committed
79
    }
80
81

    if ( role == archived ) {
armin's avatar
armin committed
82
83
        return currentChannel->getArchived();
    }
84

armin's avatar
armin committed
85
86
87
88
89
90
91
    if ( role == blocked ) {
        return currentChannel->getBlocked();
    }

    {
        return QVariant();
    }
armin's avatar
fixes  
armin committed
92
93
}

94
bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel )
armin's avatar
fixes  
armin committed
95
{
96
97
98
    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 );
armin's avatar
armin committed
99
        connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection );
100
        connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
101
102
103
104
        duplicateCheck.insert( channel->getRoomId() );
        int pos = channelList.findPosition( channel );
        emit beginInsertRows( QModelIndex(), pos, pos );
        channelList.insertSort( channel );
armin's avatar
fixes  
armin committed
105
106
107
        emit endInsertRows();
        sortChanged();
        return true;
108
    } else {
armin's avatar
fixes  
armin committed
109
110
111
112
113
114
115
        return false;
    }
}

QHash<int, QByteArray> ChannelModel::roleNames() const
{
    QHash<int, QByteArray> roles;
116
117
118
119
120
121
122
123
124
    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" );
armin's avatar
fixes  
armin committed
125
126
127
    return roles;
}

128
void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp )
armin's avatar
fixes  
armin committed
129
{
130
    Q_UNUSED( timestamp )
armin's avatar
fixes  
armin committed
131
132
    int pos = 0;
    bool found = false;
133
134
135

    for ( const auto &current : channelList ) {
        if ( current->getRoomId() == id ) {
armin's avatar
fixes  
armin committed
136
137
138
            found = true;
            break;
        }
139

armin's avatar
fixes  
armin committed
140
141
        pos++;
    }
142
143

    if ( found ) {
armin's avatar
fixes  
armin committed
144
145
146
147
148
149
        //emit dataChanged(index(pos),index(pos),{lastMessageText});
    }

    sortChanged();
}

150
void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
armin's avatar
fixes  
armin committed
151
{
152
153
    Q_UNUSED( number )
    qDebug() << "unread signal catched";
armin's avatar
fixes  
armin committed
154
155
    int pos = 0;
    bool found = false;
armin's avatar
armin committed
156

157
158
    for ( const auto &current : channelList ) {
        if ( current->getRoomId() == id ) {
armin's avatar
fixes  
armin committed
159
160
161
            found = true;
            break;
        }
162

armin's avatar
fixes  
armin committed
163
164
        pos++;
    }
armin's avatar
armin committed
165

166
167
168
    if ( found ) {
        qDebug() << "channel found";
        emit dataChanged( index( pos ), index( pos ), {unreadMessages} );
armin's avatar
fixes  
armin committed
169
170
171
172
        emit unreadMessagesChanged();
    }
}

armin's avatar
armin committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
void ChannelModel::onDataChanged( const QString &id, const QString &property )
{
    int pos = 0;
    bool found = false;

    for ( const auto &current : 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} );
            }
        }
    }
}

armin's avatar
armin committed
199
void ChannelModel::onDeleted( const QString &pId, bool deleted )
200
{
armin's avatar
armin committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    if ( channelList.contains( pId ) ) {
        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();
        }
    }
217
218
}

219
void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel )
armin's avatar
fixes  
armin committed
220
{
221
    addChannel( channel );
armin's avatar
fixes  
armin committed
222
223
224
225
226
227
228
}

QString ChannelModel::getType() const
{
    return type;
}

229
void ChannelModel::setType( const QString &value )
armin's avatar
fixes  
armin committed
230
231
232
233
234
235
236
237
{
    type = value;
}

void ChannelModel::sortChanged()
{

    beginResetModel();
238
    std::sort( channelList.begin(), channelList.end(), []( QSharedPointer<RocketChatChannel> channel1, QSharedPointer<RocketChatChannel> channel2 ) {
armin's avatar
fixes  
armin committed
239
240
        auto youngest1 = channel1->getYoungestMessage();
        auto youngest2 = channel2->getYoungestMessage();
241
242

        if ( !youngest1.isNull() && !youngest2.isNull() ) {
armin's avatar
fixes  
armin committed
243
244
245
            auto timestamp1 = youngest1->getTimestamp();
            auto timestamp2 = youngest2->getTimestamp();
            return timestamp2 < timestamp1;
246
        } else if ( youngest1.isNull() && !youngest2.isNull() ) {
armin's avatar
fixes  
armin committed
247
            return false;
248
        } else if ( youngest2.isNull() && !youngest1.isNull() ) {
armin's avatar
fixes  
armin committed
249
            return true;
250
        } else {
armin's avatar
fixes  
armin committed
251
252
            return false;
        }
253
    } );
armin's avatar
fixes  
armin committed
254
255
256
    endResetModel();
    emit countChanged();
}
257
258
259

void ChannelModel::clear()
{
Dennis Beier's avatar
Dennis Beier committed
260
    beginResetModel();
261
    channelList.clear();
Dennis Beier's avatar
Dennis Beier committed
262
263
    duplicateCheck.clear();
    endResetModel();
264
}