Newer
Older
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "messagemodel.h"
MessagesModel::MessagesModel()
{
}
QVariant MessagesModel::data( const QModelIndex &index, int role ) const
{
int row = index.row();
QSharedPointer<RocketChatMessage> messageAtIndex = messagesOfCurrentChannel.at( row );
if ( role == MessageRoles::type ) {
return messageAtIndex->getMessageType();
}
if ( role == MessageRoles::text ) {
return messageAtIndex->getMessageString();
}
if ( role == MessageRoles::linkurl ) {
if ( !messageAtIndex->getAttachments().empty() ) {
return messageAtIndex->getAttachments().first()->getUrl();
} else {
qWarning() << "linkurl not found";
}
}
if ( role == author ) {
return messageAtIndex->getAuthor();
}
if ( role == date ) {
return messageAtIndex->getFormattedDate();
}
if ( role == time ) {
return messageAtIndex->getFormattedTime();
}
if ( role == ownMessage ) {
return messageAtIndex->getOwnMessage();
}
if ( role == width ) {
if ( !messageAtIndex->getAttachments().empty() ) {
return messageAtIndex->getAttachments().first()->getWidth();
} else {
qWarning() << "height not found";
}
}
if ( role == height ) {
if ( !messageAtIndex->getAttachments().empty() ) {
return messageAtIndex->getAttachments().first()->getHeight();
} else {
qWarning() << "height not found";
}
}
if(role == formattedDate)
return messageAtIndex->getFormattedDate();
if(role == formattedTime)
return messageAtIndex->getFormattedTime();
if(role == messageType)
return messageAtIndex->getMessageType();
qWarning() << "MessagesModel data not found for row";
return QVariant();
}
QString MessagesModel::getCurrent() const
{
return current;
}
void MessagesModel::setCurrent( const QString &value )
{
QMutexLocker(&this->mutex);
current = value;
beginResetModel();
if ( mChannelMap.contains( value ) ) {
messagesOfCurrentChannel = mChannelMap[value]->getMessageRepo()->timestampIndex().values().toVector();
duplicateCheck.clear();
for ( auto currentMessage : messagesOfCurrentChannel ) {
duplicateCheck.insert( currentMessage->getId() );
}
} else {
duplicateCheck.clear();
messagesOfCurrentChannel.clear();
}
endResetModel();
emit countChanged();
}
int MessagesModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent );
qDebug() << "number of messages in model" << messagesOfCurrentChannel.count();
int count = 0;
if ( messagesOfCurrentChannel.isEmpty() ) {
return 0;
}
count = messagesOfCurrentChannel.count();
return count;
}
bool MessagesModel::addChannel(QSharedPointer<RocketChatChannel> channel)
{
QString id = channel->getRoomId();
if(mChannelMap.contains(id))
return false;
mChannelMap.insert(id, channel);
connect(channel.data(),&RocketChatChannel::messageAdded,this,[=](QString id, qint64 timestamp){
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
onNewMessage(channel,timestamp);
});
return true;
}
QHash<int, QByteArray> MessagesModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[text] = "msg";
roles[author] = "author";
roles[linkurl] = "linkurl";
roles[type] = "type";
roles[date] = "date";
roles[time] = "time";
roles[ownMessage] = "ownMessage";
roles[height] = "height";
roles[width ] = "width";
roles[formattedDate ] = "formattedDate";
roles[formattedTime] = "formattedTime";
roles[messageType] = "messageType";
return roles;
}
void MessagesModel::onNewMessage( QSharedPointer<RocketChatChannel> channel, qint64 timestamp )
{
QMutexLocker(&this->mutex);
if(timestamp == 0){
setCurrent(current);
}
if ( channel->getRoomId() == current ) {
auto message = channel->getMessageRepo()->timestampIndex()[timestamp];
if(message.isNull())
return;
int row = messagesOfCurrentChannel.findPosition(message);
if ( !duplicateCheck.contains( message->getId() ) ) {
beginInsertRows( QModelIndex(), row, row );
messagesOfCurrentChannel.insert( row, message );
mInsertCount++;
endInsertRows();
duplicateCheck.insert( message->getId() );
}
if ( !buffered ) {
mInsertCount = 0;
emit countChanged();
}
}
}
void MessagesModel::onBeginInsertList( QString pChannelId )
{
if ( pChannelId == current ) {
buffered = true;
mInsertCount = 0;
}
}
void MessagesModel::onEndInsertList( QString pChannelId )
{
if ( pChannelId == current ) {
buffered = false;
if ( mInsertCount ) {
mInsertCount = 0;
emit countChanged();
}
}
}
void MessagesModel::addChannelSlot(QSharedPointer<RocketChatChannel> channel)
{
addChannel(channel);
}