-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrostermodel.cpp
276 lines (243 loc) · 8.18 KB
/
rostermodel.cpp
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
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Hanghish
Copyright (C) 2015 Daniele Rogora
This file is part of Hangish.
Hangish 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.
Hangish 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 Nome-Programma. If not, see <http://www.gnu.org/licenses/>
*/
#include "rostermodel.h"
RosterModel::RosterModel(QObject *parent) :
QAbstractListModel(parent)
{
}
bool RosterModel::findIndex(QString convId, int &idx)
{
int i = 0;
bool found = false;
foreach (ConvAbstract *ca, conversations) {
if (ca->convId==convId) {
found = true;
break;
}
i++;
}
idx = i;
return found;
}
QHash<int, QByteArray> RosterModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
roles.insert(ConvIdRole, QByteArray("id"));
roles.insert(NameRole, QByteArray("name"));
roles.insert(PartnumRole, QByteArray("participantsNum"));
roles.insert(UnreadRole, QByteArray("unread"));
roles.insert(ImageRole, "imagePath");
roles.insert(NotificationLevelRole, "notifLevel");
return roles;
}
int RosterModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return conversations.size();
}
QVariant RosterModel::data(const QModelIndex &index, int role) const
{
//Reverse the order so that the most recent conv is displayed on top
ConvAbstract * conv = conversations.at(conversations.size() - index.row() - 1);
if (role == NameRole)
return QVariant::fromValue(conv->name);
else if (role == PartnumRole)
return QVariant::fromValue(conv->participantsNum);
else if (role == ConvIdRole)
return QVariant::fromValue(conv->convId);
else if (role == UnreadRole)
return QVariant::fromValue(conv->unread);
else if (role == ImageRole) {
if (conv->imagePaths.size()) { // TODO once we should support multiple images
return conv->imagePaths;
}
else {
return "";
}
}
else if (role == NotificationLevelRole) {
return QVariant::fromValue(conv->notificationLevel);
}
return QVariant();
}
void RosterModel::setMySelf(User pmyself)
{
myself = pmyself;
}
void RosterModel::addConversationAbstract(Conversation pConv)
{
//Simply skip archived conversations, or left group conversations, for now, but only for the UI; they are correctly parsed
if (pConv.view == ARCHIVED_VIEW || pConv.status == LEFT)
return;
QString name = "";
QStringList imagePaths;
foreach (Participant p, pConv.participants) {
if (p.user.chat_id!=myself.chat_id) {
if (!p.user.photo.isEmpty()) {
QString image = p.user.photo;
if (!image.startsWith("https:")) image.prepend("https:");
imagePaths << image;
}
else {
imagePaths << "qrc:///icons/unknown.png";
}
}
}
if (pConv.name.size() > 0) {
name = pConv.name;
} else {
foreach (Participant p, pConv.participants) {
if (p.user.chat_id!=myself.chat_id) {
if ((pConv.participants.last().user.chat_id != p.user.chat_id && pConv.participants.last().user.chat_id != myself.chat_id) ||
((pConv.participants.last().user.chat_id == myself.chat_id) && pConv.participants[pConv.participants.size() - 2].user.chat_id != p.user.chat_id)
)
name += QString(p.user.display_name + ", ");
else
name += QString(p.user.display_name + " ");
}
}
}
beginInsertRows(QModelIndex(), rowCount(), rowCount());
conversations.append(new ConvAbstract(pConv.id, name, imagePaths, pConv.participants.size(), pConv.unread, pConv.notifLevel));
endInsertRows();
}
bool RosterModel::conversationExists(QString convId)
{
foreach (ConvAbstract *c, conversations) {
if (c->convId == convId)
return true;
}
return false;
}
void RosterModel::addUnreadMsg(QString convId)
{
int i;
bool found = findIndex(convId, i);
if (found) {
conversations[i]->unread += 1;
//put the referenced conversation on top of the list
if (i!=conversations.length()-1) {
beginResetModel();
conversations.move(i,conversations.length()-1);
endResetModel();
}
else {
//No need to reset model
QModelIndex r1 = index(conversations.length() - 1 - i);
emit dataChanged(r1, r1);
}
}
}
bool RosterModel::hasUnreadMessages(QString convId)
{
foreach (ConvAbstract *c, conversations) {
if (c->convId == convId)
{
return (c->unread > 0);
}
}
return false;
}
void RosterModel::putOnTop(QString convId)
{
int i;
bool found = findIndex(convId, i);
if (found && i!=conversations.length()-1) {
beginResetModel();
qDebug() << "Moving";
conversations.move(i,conversations.length()-1);
qDebug() << "Moved";
endResetModel();
}
}
void RosterModel::setReadConv(QString convId)
{
int i;
bool found = findIndex(convId, i);
if (found) {
conversations[i]->unread = 0;
QModelIndex r1 = index(conversations.length() - 1 - i);
emit dataChanged(r1, r1);
}
}
QString RosterModel::getConversationName(QString convId) {
foreach (ConvAbstract *ca, conversations) {
if (ca->convId==convId)
return ca->name;
}
return "Conv not found";
}
void RosterModel::renameConversation(QString convId, QString newName)
{
int i;
bool found = findIndex(convId, i);
if (found) {
qDebug() << "Found";
if (newName.size() > 0) {
conversations[i]->name = newName;
} else {
conversations[i]->name = "No name - restart hangish to get participants";
/* TODO: retrieve contacts names to restore previous name
foreach (Participant p, conversations[i]->participants) {
if (p.user.chat_id!=myself.chat_id) {
if (conversations[i]->participants.last().user.chat_id != p.user.chat_id && conversations[i]->participants.last().user.chat_id != myself.chat_id)
conversations[i]-> += QString(p.user.display_name + ", ");
else
conversations[i]-> += QString(p.user.display_name + " ");
}
}
*/
}
//put the referenced conversation on top of the list
if (i!=conversations.length()-1) {
beginResetModel();
conversations.move(i,conversations.length()-1);
endResetModel();
}
else {
//No need to reset model
QModelIndex r1 = index(conversations.length() - 1 - i);
emit dataChanged(r1, r1);
}
}
}
void RosterModel::deleteConversation(QString convId)
{
qDebug() << "Del conv from list " << convId;
int i;
bool found = findIndex(convId, i);
if (found) {
qDebug() << "Found";
beginRemoveRows(QModelIndex(), i, i);
conversations.removeAt(i);
endRemoveRows();
//TODO: check why this is the only (wrong) way to make it work!
for (int i=0; i<conversations.size(); i++) {
QModelIndex r1 = index(i);
emit dataChanged(r1, r1);
}
}
}
void RosterModel::updateNotificationLevel(QString convId, int newLevel)
{
int i;
bool found = findIndex(convId, i);
if (found && conversations[i]->notificationLevel != newLevel) {
qDebug() << "Found, updating";
conversations[i]->notificationLevel = newLevel;
QModelIndex r1 = index(conversations.length() - 1 - i);
emit dataChanged(r1, r1);
}
}