QTfrontend/model/playerslistmodel.cpp
changeset 7732 fad3408fdcc1
parent 7731 262228c64f15
child 7737 ff63da8a3202
equal deleted inserted replaced
7731:262228c64f15 7732:fad3408fdcc1
     1 #include <QModelIndexList>
     1 #include <QModelIndexList>
     2 #include <QModelIndex>
     2 #include <QModelIndex>
     3 #include <QPainter>
     3 #include <QPainter>
       
     4 #include <QFile>
       
     5 #include <QTextStream>
     4 #include <QDebug>
     6 #include <QDebug>
     5 
     7 
     6 #include "playerslistmodel.h"
     8 #include "playerslistmodel.h"
       
     9 #include "hwconsts.h"
     7 
    10 
     8 PlayersListModel::PlayersListModel(QObject *parent) :
    11 PlayersListModel::PlayersListModel(QObject *parent) :
     9     QAbstractListModel(parent)
    12     QAbstractListModel(parent)
    10 {
    13 {
    11 
    14 
    87 
    90 
    88     QModelIndex mi = index(rowCount() - 1);
    91     QModelIndex mi = index(rowCount() - 1);
    89     setData(mi, nickname);
    92     setData(mi, nickname);
    90 
    93 
    91     updateSortData(mi);
    94     updateSortData(mi);
    92     updateIcon(mi);
    95     checkFriendIgnore(mi);
    93 }
    96 }
    94 
    97 
    95 
    98 
    96 void PlayersListModel::removePlayer(const QString & nickname)
    99 void PlayersListModel::removePlayer(const QString & nickname)
    97 {
   100 {
   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 }