channelmodel.cpp 12.3 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
#include "channelmodel.h"

24
int ChannelModel::rowCount( const QModelIndex &parent ) const
armin's avatar
fixes  
armin committed
25
{
26
27
    Q_UNUSED( parent )
    int count = channelList.count();
28
    return count;
armin's avatar
fixes  
armin committed
29
30
}

31
QVariant ChannelModel::data( const QModelIndex &index, int role ) const
armin's avatar
fixes  
armin committed
32
33
{
    int row = index.row();
34
35

    if ( channelList.isEmpty() || row > channelList.count() ) {
armin's avatar
fixes  
armin committed
36
37
        return QVariant();
    }
38
39
40

    auto currentChannel = channelList.at( row );

armin's avatar
cleanup    
armin committed
41
    auto roleType = static_cast<ChannelRoles>( role );
42

armin's avatar
cleanup    
armin committed
43
44
45
    switch ( roleType ) {
        case ChannelRoles::name:
            return currentChannel->getName();
46

armin's avatar
cleanup    
armin committed
47
48
        case ChannelRoles::roomId:
            return currentChannel->getRoomId();
49

armin's avatar
cleanup    
armin committed
50
51
52
        case ChannelRoles::lastMessageText: {
                auto channel = channelList.at( row );
                auto message = channel->getYoungestMessage();
53

armin's avatar
cleanup    
armin committed
54
55
56
                if ( message.isNull() ) {
                    return "";
                }
57

armin's avatar
cleanup    
armin committed
58
59
60
                if ( message->getMessageType() == QStringLiteral( "file" ) || message->getMessageType() == QStringLiteral( "image" ) || message->getMessageType() == QStringLiteral( "audio" ) ) {
                    return tr( QByteArrayLiteral( "file upload" ) );
                }
61
62
63
                auto messageText =message->getMessageString();
                messageText = messageText.replace("<img height='20' width='20'","<img height='10' width='10'");
                return messageText;
armin's avatar
cleanup    
armin committed
64
            }
65

armin's avatar
cleanup    
armin committed
66
67
        case ChannelRoles::channelType:
            return currentChannel->getType();
68

armin's avatar
cleanup    
armin committed
69
70
        case ChannelRoles::unreadMessages:
            return currentChannel->getUnreadMessages();
71

armin's avatar
cleanup    
armin committed
72
73
        case ChannelRoles::readonly:
            return currentChannel->getReadOnly() || currentChannel->getArchived() || currentChannel->getBlocked();
74

armin's avatar
cleanup    
armin committed
75
76
        case ChannelRoles::ownerId:
            return currentChannel->getOwnerId();
77

armin's avatar
cleanup    
armin committed
78
79
        case ChannelRoles::archived:
            return currentChannel->getArchived();
80

armin's avatar
cleanup    
armin committed
81
82
        case ChannelRoles::blocked:
            return currentChannel->getBlocked();
armin's avatar
armin committed
83

armin's avatar
cleanup    
armin committed
84
85
        case ChannelRoles::username:
            return currentChannel->getUsername();
armin's avatar
armin committed
86

armin's avatar
cleanup    
armin committed
87
88
89
        case ChannelRoles::userStatus: {
                int status = 0;
                auto partner = currentChannel->getChatPartner();
armin's avatar
armin committed
90

armin's avatar
cleanup    
armin committed
91
92
93
94
95
                if ( !partner.isNull() ) {
                    switch ( partner->getStatus() ) {
                        case RocketChatUser::status::ONLINE :
                            status = 0;
                            break;
armin's avatar
armin committed
96

armin's avatar
cleanup    
armin committed
97
98
99
                        case RocketChatUser::status::AWAY :
                            status = 1;
                            break;
armin's avatar
armin committed
100

armin's avatar
cleanup    
armin committed
101
102
103
                        case RocketChatUser::status::OFFLINE:
                            status = 2;
                            break;
armin's avatar
armin committed
104

armin's avatar
cleanup    
armin committed
105
106
107
108
                        case RocketChatUser::status::BUSY:
                            status = 3;
                            break;
                    }
armin's avatar
armin committed
109

armin's avatar
cleanup    
armin committed
110
111
112
113
                }

                return status;
            }
armin's avatar
armin committed
114

armin's avatar
cleanup    
armin committed
115
116
        case ChannelRoles::deleted:
            return currentChannel->getDeleted();
armin's avatar
armin committed
117

armin's avatar
cleanup    
armin committed
118
119
        case ChannelRoles::ownerName:
            return currentChannel->getOwnerName();
armin's avatar
armin committed
120
121
122
123
124

        case ChannelRoles::avatarImg: {
                auto avatarImg = currentChannel->getAvatarImg();

                if ( !avatarImg.isNull() ) {
Armin Felder's avatar
Armin Felder committed
125
                    auto path = Utils::getPathPrefix() + avatarImg->getFilePath();
armin's avatar
armin committed
126
127
128
129
130
131
132
133
134
135
136

                    if ( path.endsWith( "svg" ) ) {
                        return "qrc:res/user-identity.svg";
                    } else {
                        return path;
                    }
                } else {
                    return "";
                }
            }

armin's avatar
armin committed
137
138
    }

armin's avatar
armin committed
139
    return QVariant();
armin's avatar
armin committed
140

armin's avatar
fixes  
armin committed
141
142
}

143
bool ChannelModel::addChannel( const QSharedPointer<RocketChatChannel> &channel )
armin's avatar
fixes  
armin committed
144
{
145
    if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) {
Armin Felder's avatar
Armin Felder committed
146
        //connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
147
        connect( channel.data(), &RocketChatChannel::unreadMessagesChanged, this, &ChannelModel::onUnreadMessageChanged, Qt::UniqueConnection );
armin's avatar
armin committed
148
        connect( channel.data(), &RocketChatChannel::dataChanged, this, &ChannelModel::onDataChanged, Qt::UniqueConnection );
149
        connect( channel.data(), &RocketChatChannel::channelDeleted, this, &ChannelModel::onDeleted, Qt::UniqueConnection );
Armin Felder's avatar
Armin Felder committed
150
151
        connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged);

152
153
        duplicateCheck.insert( channel->getRoomId() );
        int pos = channelList.findPosition( channel );
armin's avatar
sd    
armin committed
154
155
156
        if(pos <0){
            pos = 0;
        }
armin's avatar
armin committed
157
        beginInsertRows( QModelIndex(), pos, pos );
158
        channelList.insertSort( channel );
armin's avatar
armin committed
159
        endInsertRows();
armin's avatar
fixes  
armin committed
160
161
162
        sortChanged();
        return true;
    }
armin's avatar
as    
armin committed
163
164
165

    return false;

armin's avatar
fixes  
armin committed
166
167
}

Armin Felder's avatar
Armin Felder committed
168
169
170
171
172
bool ChannelModel::addChannelsSlot(const QVector<QSharedPointer<RocketChatChannel> > &pChannels)
{
    beginResetModel();
    for(const auto &channel: pChannels){
        if ( !channel.isNull() && channel->getRoomId() != "" && !duplicateCheck.contains( channel->getRoomId() ) ) {
Armin Felder's avatar
Armin Felder committed
173
            //connect( channel.data(), &RocketChatChannel::messageAdded, this, &ChannelModel::onNewerMessage, Qt::UniqueConnection );
Armin Felder's avatar
Armin Felder committed
174
175
176
            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 );
Armin Felder's avatar
Armin Felder committed
177
            connect(channel.data(), &RocketChatChannel::updatedChanged, this, &ChannelModel::onChannelOrderChanged);
Armin Felder's avatar
Armin Felder committed
178
179
180
181
182
183
184
185
            duplicateCheck.insert( channel->getRoomId() );
            channelList.insertSort( channel );
        }
    }
    endResetModel();
    return true;
}

armin's avatar
fixes  
armin committed
186
187
188
QHash<int, QByteArray> ChannelModel::roleNames() const
{
    QHash<int, QByteArray> roles;
armin's avatar
cleanup    
armin committed
189
190
191
192
193
194
195
196
197
198
199
200
201
    roles[static_cast<int>( ChannelRoles::name )] = QByteArrayLiteral( "name" );
    roles[static_cast<int>( ChannelRoles::roomId )] =  QByteArrayLiteral( "roomId" );
    roles[static_cast<int>( ChannelRoles::unreadMessages )] = QByteArrayLiteral( "unreadMessages" );
    roles[static_cast<int>( ChannelRoles::channelType )] = QByteArrayLiteral( "type" );
    roles[static_cast<int>( ChannelRoles::lastMessageText )] = QByteArrayLiteral( "lastMessageText" );
    roles[static_cast<int>( ChannelRoles::readonly )] = QByteArrayLiteral( "readonly" );
    roles[static_cast<int>( ChannelRoles::archived )] = QByteArrayLiteral( "archived" );
    roles[static_cast<int>( ChannelRoles::blocked )] = QByteArrayLiteral( "blocked" );
    roles[static_cast<int>( ChannelRoles::deleted )] = QByteArrayLiteral( "deleted" );
    roles[static_cast<int>( ChannelRoles::ownerId )] = QByteArrayLiteral( "ownerId" );
    roles[static_cast<int>( ChannelRoles::ownerName )] = QByteArrayLiteral( "ownerName" );
    roles[static_cast<int>( ChannelRoles::username )] = QByteArrayLiteral( "username" );
    roles[static_cast<int>( ChannelRoles::userStatus )] = QByteArrayLiteral( "userStatus" );
armin's avatar
armin committed
202
    roles[static_cast<int>( ChannelRoles::avatarImg )] = QByteArrayLiteral( "avatarImg" );
203

armin's avatar
fixes  
armin committed
204
205
206
    return roles;
}

207
void ChannelModel::onNewerMessage( const QString &id, qint64 timestamp )
armin's avatar
fixes  
armin committed
208
{
209
    Q_UNUSED( timestamp )
armin's avatar
fixes  
armin committed
210
211
    int pos = 0;
    bool found = false;
212
213
214

    for ( const auto &current : channelList ) {
        if ( current->getRoomId() == id ) {
armin's avatar
fixes  
armin committed
215
216
217
            found = true;
            break;
        }
218

armin's avatar
fixes  
armin committed
219
220
        pos++;
    }
221
222

    if ( found ) {
armin's avatar
fixes  
armin committed
223
224
225
        //emit dataChanged(index(pos),index(pos),{lastMessageText});
    }

Armin Felder's avatar
Armin Felder committed
226
   // sortChanged();
armin's avatar
fixes  
armin committed
227
228
}

229
void ChannelModel::onUnreadMessageChanged( const QString &id, int number )
armin's avatar
fixes  
armin committed
230
{
231
    Q_UNUSED( number )
armin's avatar
as    
armin committed
232
    qDebug() << QStringLiteral( "unread signal catched" );
armin's avatar
fixes  
armin committed
233
234
    int pos = 0;
    bool found = false;
armin's avatar
armin committed
235

236
237
    for ( const auto &current : channelList ) {
        if ( current->getRoomId() == id ) {
armin's avatar
fixes  
armin committed
238
239
240
            found = true;
            break;
        }
241

armin's avatar
fixes  
armin committed
242
243
        pos++;
    }
armin's avatar
armin committed
244

245
246
    if ( found ) {
        qDebug() << "channel found";
armin's avatar
cleanup    
armin committed
247
        emit dataChanged( index( pos ), index( pos ), {static_cast<int>( ChannelRoles::unreadMessages )} );
armin's avatar
fixes  
armin committed
248
249
250
251
        emit unreadMessagesChanged();
    }
}

armin's avatar
armin committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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();

armin's avatar
as    
armin committed
269
270
271
        for ( auto it = roles.begin(); it != roles.end(); it++ ) {
            if ( roles[it.key()] == property ) {
                emit dataChanged( index( pos ), index( pos ), {it.key()} );
armin's avatar
armin committed
272
273
274
275
276
            }
        }
    }
}

armin's avatar
armin committed
277
void ChannelModel::onDeleted( const QString &pId, bool deleted )
278
{
armin's avatar
as    
armin committed
279
    Q_UNUSED( deleted )
armin's avatar
armin committed
280
    int idx = -1;
armin's avatar
armin committed
281

armin's avatar
armin committed
282
283
284
285
    for ( int i = 0; i < channelList.length(); i++ ) {
        if ( channelList.at( i )->getRoomId() == pId ) {
            idx = i;
            break;
armin's avatar
armin committed
286
        }
armin's avatar
armin committed
287
    }
armin's avatar
armin committed
288

armin's avatar
armin committed
289
290
291
292
    if ( idx != -1 ) {
        beginRemoveRows( QModelIndex(), idx, idx );
        channelList.remove( idx );
        endRemoveRows();
armin's avatar
armin committed
293
    }
armin's avatar
armin committed
294

295
296
}

Armin Felder's avatar
Armin Felder committed
297
298
299
300
301
302
303
void ChannelModel::onChannelOrderChanged(const QString &pId, qint64 pTimestamp)
{
    beginResetModel();
    channelList.reOrder();
    endResetModel();
}

304
void ChannelModel::addChannelSlot( const QSharedPointer<RocketChatChannel> &channel )
armin's avatar
fixes  
armin committed
305
{
306
    addChannel( channel );
armin's avatar
fixes  
armin committed
307
308
309
310
311
312
313
}

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

314
void ChannelModel::setType( const QString &value )
armin's avatar
fixes  
armin committed
315
316
317
318
319
320
{
    type = value;
}

void ChannelModel::sortChanged()
{
Armin Felder's avatar
fix    
Armin Felder committed
321

armin's avatar
fixes  
armin committed
322
    beginResetModel();
Armin Felder's avatar
fix    
Armin Felder committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    std::sort( channelList.begin(), channelList.end(), []( const QSharedPointer<RocketChatChannel> &channel1, const 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;
        }

        if ( youngest1.isNull() && !youngest2.isNull() ) {
            return false;
        }

        if ( youngest2.isNull() && !youngest1.isNull() ) {
            return true;
        }

        return false;

    } );
armin's avatar
fixes  
armin committed
344
345
346
    endResetModel();
    emit countChanged();
}
347
348
349

void ChannelModel::clear()
{
Dennis Beier's avatar
Dennis Beier committed
350
    beginResetModel();
351
    channelList.clear();
Dennis Beier's avatar
Dennis Beier committed
352
353
    duplicateCheck.clear();
    endResetModel();
354
}