227 static QHash<quint32, QIcon> iconsCache; |
230 static QHash<quint32, QIcon> iconsCache; |
228 |
231 |
229 return iconsCache; |
232 return iconsCache; |
230 } |
233 } |
231 |
234 |
|
235 |
232 void PlayersListModel::updateSortData(const QModelIndex & index) |
236 void PlayersListModel::updateSortData(const QModelIndex & index) |
233 { |
237 { |
234 QString result = QString("%1%2%3%4%5") |
238 QString result = QString("%1%2%3%4%5") |
235 .arg(1 - index.data(RoomAdmin).toInt()) |
239 .arg(1 - index.data(RoomAdmin).toInt()) |
236 .arg(1 - index.data(ServerAdmin).toInt()) |
240 .arg(1 - index.data(ServerAdmin).toInt()) |
239 .arg(index.data(Qt::DisplayRole).toString().toLower()) |
243 .arg(index.data(Qt::DisplayRole).toString().toLower()) |
240 ; |
244 ; |
241 |
245 |
242 setData(index, result, SortRole); |
246 setData(index, result, SortRole); |
243 } |
247 } |
|
248 |
|
249 |
|
250 void PlayersListModel::setNickname(const QString &nickname) |
|
251 { |
|
252 m_nickname = nickname; |
|
253 |
|
254 loadSet(m_friendsSet, "friends"); |
|
255 loadSet(m_ignoredSet, "ignore"); |
|
256 |
|
257 for(int i = rowCount() - 1; i >= 0; --i) |
|
258 checkFriendIgnore(index(i)); |
|
259 } |
|
260 |
|
261 |
|
262 void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) |
|
263 { |
|
264 setData(mi, m_friendsSet.contains(mi.data().toString().toLower()), Friend); |
|
265 setData(mi, m_ignoredSet.contains(mi.data().toString().toLower()), Ignore); |
|
266 |
|
267 updateIcon(mi); |
|
268 } |
|
269 |
|
270 void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix) |
|
271 { |
|
272 set.clear(); |
|
273 |
|
274 QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
|
275 |
|
276 QFile txt(fileName); |
|
277 if(!txt.open(QIODevice::ReadOnly)) |
|
278 return; |
|
279 |
|
280 QTextStream stream(&txt); |
|
281 stream.setCodec("UTF-8"); |
|
282 |
|
283 while(!stream.atEnd()) |
|
284 { |
|
285 QString str = stream.readLine(); |
|
286 if(str.startsWith(";") || str.isEmpty()) |
|
287 continue; |
|
288 |
|
289 set.insert(str.trimmed()); |
|
290 } |
|
291 |
|
292 txt.close(); |
|
293 } |
|
294 |
|
295 void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix) |
|
296 { |
|
297 QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix); |
|
298 |
|
299 QFile txt(fileName); |
|
300 |
|
301 // list empty? => rather have no file for the list than an empty one |
|
302 if (set.isEmpty()) |
|
303 { |
|
304 if (txt.exists()) |
|
305 { |
|
306 // try to remove file, if successful we're done here. |
|
307 if (txt.remove()) |
|
308 return; |
|
309 } |
|
310 else |
|
311 // there is no file |
|
312 return; |
|
313 } |
|
314 |
|
315 if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) |
|
316 return; |
|
317 |
|
318 QTextStream stream(&txt); |
|
319 stream.setCodec("UTF-8"); |
|
320 |
|
321 stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl; |
|
322 |
|
323 foreach(const QString & nick, set.values()) |
|
324 stream << nick << endl; |
|
325 |
|
326 txt.close(); |
|
327 } |