author | sheepluva |
Wed, 18 Jun 2014 01:37:28 +0200 | |
changeset 10324 | 5d90d8ca9657 |
parent 9727 | e89ed65f62da |
child 10753 | e56db5d988ef |
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() |
7725 | 240 |
; |
241 |
||
242 |
for(int i = flags.size() - 1; i >= 0; --i) |
|
243 |
if(flags[i]) |
|
244 |
iconNum |= 1 << i; |
|
245 |
||
246 |
if(m_icons().contains(iconNum)) |
|
247 |
{ |
|
248 |
setData(index, m_icons().value(iconNum), Qt::DecorationRole); |
|
249 |
} |
|
250 |
else |
|
251 |
{ |
|
252 |
QPixmap result(24, 16); |
|
253 |
result.fill(Qt::transparent); |
|
254 |
||
255 |
QPainter painter(&result); |
|
256 |
||
7833 | 257 |
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
|
258 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
259 |
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
|
260 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
261 |
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
|
262 |
} |
7764 | 263 |
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
|
264 |
{ |
7765
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
265 |
if(index.data(Ready).toBool()) |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
266 |
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
|
267 |
else |
1e162c1d6dc7
'In game' client flag, both server and frontend support
unc0rr
parents:
7764
diff
changeset
|
268 |
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
|
269 |
} |
9503 | 270 |
} else |
271 |
{ // we're in lobby |
|
272 |
if(!index.data(InRoom).toBool()) |
|
273 |
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
|
274 |
} |
7725 | 275 |
|
276 |
QString mainIconName(":/res/chat/"); |
|
277 |
||
9503 | 278 |
if(index.data(ServerAdmin).toBool()) |
7725 | 279 |
mainIconName += "serveradmin"; |
280 |
else |
|
9503 | 281 |
{ |
282 |
if(index.data(RoomAdmin).toBool()) |
|
283 |
mainIconName += "roomadmin"; |
|
284 |
else |
|
285 |
mainIconName += "hedgehog"; |
|
286 |
||
287 |
if(index.data(Contributor).toBool()) |
|
288 |
mainIconName += "contributor"; |
|
289 |
} |
|
7725 | 290 |
|
291 |
if(!index.data(Registered).toBool()) |
|
292 |
mainIconName += "_gray"; |
|
293 |
||
294 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png")); |
|
295 |
||
296 |
if(index.data(Ignore).toBool()) |
|
297 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png")); |
|
298 |
else |
|
299 |
if(index.data(Friend).toBool()) |
|
300 |
painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png")); |
|
301 |
||
302 |
painter.end(); |
|
303 |
||
304 |
QIcon icon(result); |
|
305 |
||
306 |
setData(index, icon, Qt::DecorationRole); |
|
307 |
m_icons().insert(iconNum, icon); |
|
308 |
} |
|
309 |
||
310 |
if(index.data(Ignore).toBool()) |
|
311 |
setData(index, Qt::gray, Qt::ForegroundRole); |
|
312 |
else |
|
313 |
if(index.data(Friend).toBool()) |
|
314 |
setData(index, Qt::green, Qt::ForegroundRole); |
|
315 |
else |
|
316 |
setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole); |
|
317 |
} |
|
318 |
||
319 |
||
320 |
QHash<quint32, QIcon> & PlayersListModel::m_icons() |
|
321 |
{ |
|
322 |
static QHash<quint32, QIcon> iconsCache; |
|
323 |
||
324 |
return iconsCache; |
|
325 |
} |
|
7728 | 326 |
|
7732 | 327 |
|
7728 | 328 |
void PlayersListModel::updateSortData(const QModelIndex & index) |
329 |
{ |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
330 |
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
|
331 |
// room admins go first, then server admins, then friends |
7728 | 332 |
.arg(1 - index.data(RoomAdmin).toInt()) |
333 |
.arg(1 - index.data(ServerAdmin).toInt()) |
|
334 |
.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
|
335 |
// ignored at bottom |
7728 | 336 |
.arg(index.data(Ignore).toInt()) |
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
337 |
// 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
|
338 |
// 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
|
339 |
.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
|
340 |
// sort ignoring case |
7728 | 341 |
.arg(index.data(Qt::DisplayRole).toString().toLower()) |
342 |
; |
|
343 |
||
344 |
setData(index, result, SortRole); |
|
345 |
} |
|
7732 | 346 |
|
347 |
||
348 |
void PlayersListModel::setNickname(const QString &nickname) |
|
349 |
{ |
|
350 |
m_nickname = nickname; |
|
351 |
||
352 |
loadSet(m_friendsSet, "friends"); |
|
353 |
loadSet(m_ignoredSet, "ignore"); |
|
354 |
||
355 |
for(int i = rowCount() - 1; i >= 0; --i) |
|
356 |
checkFriendIgnore(index(i)); |
|
357 |
} |
|
358 |
||
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
359 |
bool PlayersListModel::isFriend(const QString & nickname) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
360 |
{ |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
361 |
return m_friendsSet.contains(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
362 |
} |
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 |
bool PlayersListModel::isIgnored(const QString & nickname) |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
365 |
{ |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
366 |
return m_ignoredSet.contains(nickname.toLower()); |
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
367 |
} |
7732 | 368 |
|
369 |
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) |
|
370 |
{ |
|
9725
68b5d87cfdb0
regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents:
9503
diff
changeset
|
371 |
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
|
372 |
setData(mi, isIgnored(mi.data().toString()), Ignore); |
7732 | 373 |
|
374 |
updateIcon(mi); |
|
7744
75e1d0c0ba72
- Nicks starting from not-letter char go to bottom of the list
unc0rr
parents:
7737
diff
changeset
|
375 |
updateSortData(mi); |
7732 | 376 |
} |
377 |
||
378 |
void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix) |
|
379 |
{ |
|
380 |
set.clear(); |
|
381 |
||
382 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
|
383 |
||
384 |
QFile txt(fileName); |
|
385 |
if(!txt.open(QIODevice::ReadOnly)) |
|
386 |
return; |
|
387 |
||
388 |
QTextStream stream(&txt); |
|
389 |
stream.setCodec("UTF-8"); |
|
390 |
||
391 |
while(!stream.atEnd()) |
|
392 |
{ |
|
393 |
QString str = stream.readLine(); |
|
394 |
if(str.startsWith(";") || str.isEmpty()) |
|
395 |
continue; |
|
396 |
||
397 |
set.insert(str.trimmed()); |
|
398 |
} |
|
399 |
||
400 |
txt.close(); |
|
401 |
} |
|
402 |
||
403 |
void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix) |
|
404 |
{ |
|
7737 | 405 |
qDebug("saving set"); |
406 |
||
7732 | 407 |
QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
408 |
||
409 |
QFile txt(fileName); |
|
410 |
||
411 |
// list empty? => rather have no file for the list than an empty one |
|
412 |
if (set.isEmpty()) |
|
413 |
{ |
|
414 |
if (txt.exists()) |
|
415 |
{ |
|
416 |
// try to remove file, if successful we're done here. |
|
417 |
if (txt.remove()) |
|
418 |
return; |
|
419 |
} |
|
420 |
else |
|
421 |
// there is no file |
|
422 |
return; |
|
423 |
} |
|
424 |
||
425 |
if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
|
426 |
return; |
|
427 |
||
428 |
QTextStream stream(&txt); |
|
429 |
stream.setCodec("UTF-8"); |
|
430 |
||
431 |
stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl; |
|
432 |
||
433 |
foreach(const QString & nick, set.values()) |
|
434 |
stream << nick << endl; |
|
435 |
||
436 |
txt.close(); |
|
437 |
} |