author | unc0rr |
Mon, 14 Dec 2015 00:24:03 +0300 | |
branch | qmlfrontend |
changeset 11459 | 3c5d99013baf |
parent 10753 | e56db5d988ef |
child 12902 | fc47fc4af6bd |
permissions | -rw-r--r-- |
7725 | 1 |
#include <QModelIndexList> |
2 |
#include <QModelIndex> |
|
3 |
#include <QPainter> |
|
7732 | 4 |
#include <QFile> |
5 |
#include <QTextStream> |
|
7727 | 6 |
#include <QDebug> |
7725 | 7 |
|
7723 | 8 |
#include "playerslistmodel.h" |
7732 | 9 |
#include "hwconsts.h" |
7723 | 10 |
|
11 |
PlayersListModel::PlayersListModel(QObject *parent) : |
|
7725 | 12 |
QAbstractListModel(parent) |
7723 | 13 |
{ |
9503 | 14 |
m_fontInRoom = QFont(); |
15 |
m_fontInRoom.setItalic(true); |
|
7723 | 16 |
} |
17 |
||
7725 | 18 |
|
19 |
int PlayersListModel::rowCount(const QModelIndex &parent) const |
|
20 |
{ |
|
21 |
if(parent.isValid()) |
|
22 |
return 0; |
|
23 |
else |
|
24 |
return m_data.size(); |
|
25 |
} |
|
26 |
||
27 |
||
28 |
QVariant PlayersListModel::data(const QModelIndex &index, int role) const |
|
29 |
{ |
|
7727 | 30 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0) |
7725 | 31 |
return QVariant(QVariant::Invalid); |
32 |
||
33 |
return m_data.at(index.row()).value(role); |
|
34 |
} |
|
35 |
||
36 |
||
37 |
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role) |
|
38 |
{ |
|
39 |
if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0) |
|
40 |
return false; |
|
41 |
||
42 |
m_data[index.row()].insert(role, value); |
|
43 |
||
44 |
emit dataChanged(index, index); |
|
45 |
||
46 |
return true; |
|
47 |
} |
|
48 |
||
49 |
||
50 |
bool PlayersListModel::insertRow(int row, const QModelIndex &parent) |
|
51 |
{ |
|
52 |
return insertRows(row, 1, parent); |
|
53 |
} |
|
54 |
||
55 |
||
56 |
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent) |
|
57 |
{ |
|
58 |
if(parent.isValid() || row > rowCount() || row < 0 || count < 1) |
|
59 |
return false; |
|
60 |
||
61 |
beginInsertRows(parent, row, row + count - 1); |
|
62 |
||
63 |
for(int i = 0; i < count; ++i) |
|
64 |
m_data.insert(row, DataEntry()); |
|
65 |
||
66 |
endInsertRows(); |
|
67 |
||
68 |
return true; |
|
69 |
} |
|
70 |
||
71 |
||
72 |
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent) |
|
73 |
{ |
|
74 |
if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1) |
|
75 |
return false; |
|
76 |
||
77 |
beginRemoveRows(parent, row, row + count - 1); |
|
78 |
||
79 |
for(int i = 0; i < count; ++i) |
|
80 |
m_data.removeAt(row); |
|
81 |
||
82 |
endRemoveRows(); |
|
83 |
||
84 |
return true; |
|
85 |
} |
|
86 |
||
9727 | 87 |
QModelIndex PlayersListModel::nicknameIndex(const QString & nickname) |
88 |
{ |
|
89 |
QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly); |
|
90 |
||
91 |
if(mil.size() > 0) |
|
92 |
return mil[0]; |
|
93 |
else |
|
94 |
return QModelIndex(); |
|
95 |
} |
|
7725 | 96 |
|
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
97 |
void PlayersListModel::addPlayer(const QString & nickname, bool notify) |
7723 | 98 |
{ |
7725 | 99 |
insertRow(rowCount()); |
7723 | 100 |
|
7728 | 101 |
QModelIndex mi = index(rowCount() - 1); |
102 |
setData(mi, nickname); |
|
7725 | 103 |
|
7732 | 104 |
checkFriendIgnore(mi); |
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
105 |
|
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
106 |
emit nickAddedLobby(nickname, notify); |
7723 | 107 |
} |
7725 | 108 |
|
109 |
||
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
110 |
void PlayersListModel::removePlayer(const QString & nickname, const QString &msg) |
7725 | 111 |
{ |
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
112 |
if(msg.isEmpty()) |
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
113 |
emit nickRemovedLobby(nickname); |
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
114 |
else |
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
115 |
emit nickRemovedLobby(nickname, msg); |
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
116 |
|
9727 | 117 |
QModelIndex mi = nicknameIndex(nickname); |
7725 | 118 |
|
9727 | 119 |
if(mi.isValid()) |
120 |
removeRow(mi.row()); |
|
7725 | 121 |
} |
122 |
||
123 |
||
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
124 |
void PlayersListModel::playerJoinedRoom(const QString & nickname, bool notify) |
7731 | 125 |
{ |
9727 | 126 |
QModelIndex mi = nicknameIndex(nickname); |
7731 | 127 |
|
9727 | 128 |
if(mi.isValid()) |
7764 | 129 |
{ |
9727 | 130 |
setData(mi, true, RoomFilterRole); |
131 |
updateIcon(mi); |
|
132 |
updateSortData(mi); |
|
7764 | 133 |
} |
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
134 |
|
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
135 |
emit nickAdded(nickname, notify); |
7731 | 136 |
} |
137 |
||
138 |
||
139 |
void PlayersListModel::playerLeftRoom(const QString & nickname) |
|
140 |
{ |
|
8891
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
141 |
emit nickRemoved(nickname); |
bf67b4d7d7b4
- Better fix to friends joins/quits highlighting problem
unc0rr
parents:
7842
diff
changeset
|
142 |
|
9727 | 143 |
QModelIndex mi = nicknameIndex(nickname); |
7731 | 144 |
|
9727 | 145 |
if(mi.isValid()) |
7764 | 146 |
{ |
9727 | 147 |
setData(mi, false, RoomFilterRole); |
148 |
setData(mi, false, RoomAdmin); |
|
149 |
setData(mi, false, Ready); |
|
150 |
setData(mi, false, InGame); |
|
151 |
updateIcon(mi); |
|
7764 | 152 |
} |
7731 | 153 |
} |
154 |
||
155 |
||
7725 | 156 |
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet) |
157 |
{ |
|
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
158 |
if(flagType == Friend) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
159 |
{ |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
160 |
if(isSet) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
161 |
m_friendsSet.insert(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
162 |
else |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
163 |
m_friendsSet.remove(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
164 |
|
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
165 |
saveSet(m_friendsSet, "friends"); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
166 |
} |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
167 |
else if(flagType == Ignore) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
168 |
{ |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
169 |
if(isSet) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
170 |
m_ignoredSet.insert(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
171 |
else |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
172 |
m_ignoredSet.remove(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
173 |
|
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
174 |
saveSet(m_ignoredSet, "ignore"); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
175 |
} |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
176 |
|
9727 | 177 |
QModelIndex mi = nicknameIndex(nickname); |
7725 | 178 |
|
9727 | 179 |
if(mi.isValid()) |
7725 | 180 |
{ |
9727 | 181 |
setData(mi, isSet, flagType); |
7728 | 182 |
|
183 |
if(flagType == Friend || flagType == ServerAdmin |
|
184 |
|| flagType == Ignore || flagType == RoomAdmin) |
|
9727 | 185 |
updateSortData(mi); |
7728 | 186 |
|
9727 | 187 |
updateIcon(mi); |
7725 | 188 |
} |
189 |
} |
|
190 |
||
191 |
||
7737 | 192 |
bool PlayersListModel::isFlagSet(const QString & nickname, StateFlag flagType) |
193 |
{ |
|
9727 | 194 |
QModelIndex mi = nicknameIndex(nickname); |
7737 | 195 |
|
9727 | 196 |
if(mi.isValid()) |
197 |
return mi.data(flagType).toBool(); |
|
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
198 |
else if(flagType == Friend) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
199 |
return isFriend(nickname); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
200 |
else if(flagType == Ignore) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
201 |
return isIgnored(nickname); |
7737 | 202 |
else |
203 |
return false; |
|
204 |
} |
|
205 |
||
7731 | 206 |
void PlayersListModel::resetRoomFlags() |
207 |
{ |
|
208 |
for(int i = rowCount() - 1; i >= 0; --i) |
|
209 |
{ |
|
210 |
QModelIndex mi = index(i); |
|
211 |
||
7833 | 212 |
if(mi.data(RoomFilterRole).toBool()) |
7731 | 213 |
{ |
7833 | 214 |
setData(mi, false, RoomFilterRole); |
7731 | 215 |
setData(mi, false, RoomAdmin); |
216 |
setData(mi, false, Ready); |
|
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
217 |
setData(mi, false, InGame); |
7737 | 218 |
|
219 |
updateSortData(mi); |
|
220 |
updateIcon(mi); |
|
7731 | 221 |
} |
222 |
} |
|
223 |
} |
|
224 |
||
7725 | 225 |
void PlayersListModel::updateIcon(const QModelIndex & index) |
226 |
{ |
|
227 |
quint32 iconNum = 0; |
|
228 |
||
229 |
QList<bool> flags; |
|
230 |
flags |
|
231 |
<< index.data(Ready).toBool() |
|
232 |
<< index.data(ServerAdmin).toBool() |
|
233 |
<< index.data(RoomAdmin).toBool() |
|
234 |
<< index.data(Registered).toBool() |
|
235 |
<< index.data(Friend).toBool() |
|
236 |
<< index.data(Ignore).toBool() |
|
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
237 |
<< index.data(InGame).toBool() |
7833 | 238 |
<< index.data(RoomFilterRole).toBool() |
9503 | 239 |
<< index.data(InRoom).toBool() |
10753
e56db5d988ef
fix issue with contributor hat being displayed for wrong players
sheepluva
parents:
9727
diff
changeset
|
240 |
<< index.data(Contributor).toBool() |
7725 | 241 |
; |
242 |
||
243 |
for(int i = flags.size() - 1; i >= 0; --i) |
|
244 |
if(flags[i]) |
|
245 |
iconNum |= 1 << i; |
|
246 |
||
247 |
if(m_icons().contains(iconNum)) |
|
248 |
{ |
|
249 |
setData(index, m_icons().value(iconNum), Qt::DecorationRole); |
|
250 |
} |
|
251 |
else |
|
252 |
{ |
|
253 |
QPixmap result(24, 16); |
|
254 |
result.fill(Qt::transparent); |
|
255 |
||
256 |
QPainter painter(&result); |
|
257 |
||
7833 | 258 |
if(index.data(RoomFilterRole).toBool()) |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
259 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
260 |
if(index.data(InGame).toBool()) |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
261 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
262 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/ingame.png")); |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
263 |
} |
7764 | 264 |
else |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
265 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
266 |
if(index.data(Ready).toBool()) |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
267 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png")); |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
268 |
else |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
269 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp_off.png")); |
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
270 |
} |
9503 | 271 |
} else |
272 |
{ // we're in lobby |
|
273 |
if(!index.data(InRoom).toBool()) |
|
274 |
painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/Flake.png")); |
|
7842
d1c0e4341165
- Better fix than the one from rc7f5c3bd7f8c. Now just scrolling back till net/nettype/main page. Not tested aswell.
unc0rr
parents:
7833
diff
changeset
|
275 |
} |
7725 | 276 |
|
277 |
QString mainIconName(":/res/chat/"); |
|
278 |
||
9503 | 279 |
if(index.data(ServerAdmin).toBool()) |
7725 | 280 |
mainIconName += "serveradmin"; |
281 |
else |
|
9503 | 282 |
{ |
283 |
if(index.data(RoomAdmin).toBool()) |
|
284 |
mainIconName += "roomadmin"; |
|
285 |
else |
|
286 |
mainIconName += "hedgehog"; |
|
287 |
||
288 |
if(index.data(Contributor).toBool()) |
|
289 |
mainIconName += "contributor"; |
|
290 |
} |
|
7725 | 291 |
|
292 |
if(!index.data(Registered).toBool()) |
|
293 |
mainIconName += "_gray"; |
|
294 |
||
295 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png")); |
|
296 |
||
297 |
if(index.data(Ignore).toBool()) |
|
298 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png")); |
|
299 |
else |
|
300 |
if(index.data(Friend).toBool()) |
|
301 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png")); |
|
302 |
||
303 |
painter.end(); |
|
304 |
||
305 |
QIcon icon(result); |
|
306 |
||
307 |
setData(index, icon, Qt::DecorationRole); |
|
308 |
m_icons().insert(iconNum, icon); |
|
309 |
} |
|
310 |
||
311 |
if(index.data(Ignore).toBool()) |
|
312 |
setData(index, Qt::gray, Qt::ForegroundRole); |
|
313 |
else |
|
314 |
if(index.data(Friend).toBool()) |
|
315 |
setData(index, Qt::green, Qt::ForegroundRole); |
|
316 |
else |
|
317 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole); |
|
318 |
} |
|
319 |
||
320 |
||
321 |
QHash<quint32, QIcon> & PlayersListModel::m_icons() |
|
322 |
{ |
|
323 |
static QHash<quint32, QIcon> iconsCache; |
|
324 |
||
325 |
return iconsCache; |
|
326 |
} |
|
7728 | 327 |
|
7732 | 328 |
|
7728 | 329 |
void PlayersListModel::updateSortData(const QModelIndex & index) |
330 |
{ |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
331 |
QString result = QString("%1%2%3%4%5%6") |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
332 |
// room admins go first, then server admins, then friends |
7728 | 333 |
.arg(1 - index.data(RoomAdmin).toInt()) |
334 |
.arg(1 - index.data(ServerAdmin).toInt()) |
|
335 |
.arg(1 - index.data(Friend).toInt()) |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
336 |
// ignored at bottom |
7728 | 337 |
.arg(index.data(Ignore).toInt()) |
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
338 |
// keep nicknames starting from non-letter character at bottom within group |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
339 |
// assume there are no empty nicks in list |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
340 |
.arg(index.data(Qt::DisplayRole).toString().at(0).isLetter() ? 0 : 1) |
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
341 |
// sort ignoring case |
7728 | 342 |
.arg(index.data(Qt::DisplayRole).toString().toLower()) |
343 |
; |
|
344 |
||
345 |
setData(index, result, SortRole); |
|
346 |
} |
|
7732 | 347 |
|
348 |
||
349 |
void PlayersListModel::setNickname(const QString &nickname) |
|
350 |
{ |
|
351 |
m_nickname = nickname; |
|
352 |
||
353 |
loadSet(m_friendsSet, "friends"); |
|
354 |
loadSet(m_ignoredSet, "ignore"); |
|
355 |
||
356 |
for(int i = rowCount() - 1; i >= 0; --i) |
|
357 |
checkFriendIgnore(index(i)); |
|
358 |
} |
|
359 |
||
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
360 |
bool PlayersListModel::isFriend(const QString & nickname) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
361 |
{ |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
362 |
return m_friendsSet.contains(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
363 |
} |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
364 |
|
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
365 |
bool PlayersListModel::isIgnored(const QString & nickname) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
366 |
{ |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
367 |
return m_ignoredSet.contains(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
368 |
} |
7732 | 369 |
|
370 |
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) |
|
371 |
{ |
|
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
372 |
setData(mi, isFriend(mi.data().toString()), Friend); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
373 |
setData(mi, isIgnored(mi.data().toString()), Ignore); |
7732 | 374 |
|
375 |
updateIcon(mi); |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
376 |
updateSortData(mi); |
7732 | 377 |
} |
378 |
||
379 |
void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix) |
|
380 |
{ |
|
381 |
set.clear(); |
|
382 |
||
383 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
|
384 |
||
385 |
QFile txt(fileName); |
|
386 |
if(!txt.open(QIODevice::ReadOnly)) |
|
387 |
return; |
|
388 |
||
389 |
QTextStream stream(&txt); |
|
390 |
stream.setCodec("UTF-8"); |
|
391 |
||
392 |
while(!stream.atEnd()) |
|
393 |
{ |
|
394 |
QString str = stream.readLine(); |
|
395 |
if(str.startsWith(";") || str.isEmpty()) |
|
396 |
continue; |
|
397 |
||
398 |
set.insert(str.trimmed()); |
|
399 |
} |
|
400 |
||
401 |
txt.close(); |
|
402 |
} |
|
403 |
||
404 |
void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix) |
|
405 |
{ |
|
7737 | 406 |
qDebug("saving set"); |
407 |
||
7732 | 408 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
409 |
||
410 |
QFile txt(fileName); |
|
411 |
||
412 |
// list empty? => rather have no file for the list than an empty one |
|
413 |
if (set.isEmpty()) |
|
414 |
{ |
|
415 |
if (txt.exists()) |
|
416 |
{ |
|
417 |
// try to remove file, if successful we're done here. |
|
418 |
if (txt.remove()) |
|
419 |
return; |
|
420 |
} |
|
421 |
else |
|
422 |
// there is no file |
|
423 |
return; |
|
424 |
} |
|
425 |
||
426 |
if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
|
427 |
return; |
|
428 |
||
429 |
QTextStream stream(&txt); |
|
430 |
stream.setCodec("UTF-8"); |
|
431 |
||
432 |
stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl; |
|
433 |
||
434 |
foreach(const QString & nick, set.values()) |
|
435 |
stream << nick << endl; |
|
436 |
||
437 |
txt.close(); |
|
438 |
} |