Load friends/ignored nicks from file
authorunc0rr
Tue, 09 Oct 2012 00:38:17 +0400
changeset 7732 fad3408fdcc1
parent 7731 262228c64f15
child 7733 a1476c09403f
Load friends/ignored nicks from file
QTfrontend/model/playerslistmodel.cpp
QTfrontend/model/playerslistmodel.h
QTfrontend/net/newnetclient.cpp
--- a/QTfrontend/model/playerslistmodel.cpp	Mon Oct 08 23:57:17 2012 +0400
+++ b/QTfrontend/model/playerslistmodel.cpp	Tue Oct 09 00:38:17 2012 +0400
@@ -1,9 +1,12 @@
 #include <QModelIndexList>
 #include <QModelIndex>
 #include <QPainter>
+#include <QFile>
+#include <QTextStream>
 #include <QDebug>
 
 #include "playerslistmodel.h"
+#include "hwconsts.h"
 
 PlayersListModel::PlayersListModel(QObject *parent) :
     QAbstractListModel(parent)
@@ -89,7 +92,7 @@
     setData(mi, nickname);
 
     updateSortData(mi);
-    updateIcon(mi);
+    checkFriendIgnore(mi);
 }
 
 
@@ -229,6 +232,7 @@
     return iconsCache;
 }
 
+
 void PlayersListModel::updateSortData(const QModelIndex & index)
 {
     QString result = QString("%1%2%3%4%5")
@@ -241,3 +245,83 @@
 
     setData(index, result, SortRole);
 }
+
+
+void PlayersListModel::setNickname(const QString &nickname)
+{
+    m_nickname = nickname;
+
+    loadSet(m_friendsSet, "friends");
+    loadSet(m_ignoredSet, "ignore");
+
+    for(int i = rowCount() - 1; i >= 0; --i)
+        checkFriendIgnore(index(i));
+}
+
+
+void PlayersListModel::checkFriendIgnore(const QModelIndex &mi)
+{
+    setData(mi, m_friendsSet.contains(mi.data().toString().toLower()), Friend);
+    setData(mi, m_ignoredSet.contains(mi.data().toString().toLower()), Ignore);
+
+    updateIcon(mi);
+}
+
+void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix)
+{
+    set.clear();
+
+    QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
+
+    QFile txt(fileName);
+    if(!txt.open(QIODevice::ReadOnly))
+        return;
+
+    QTextStream stream(&txt);
+    stream.setCodec("UTF-8");
+
+    while(!stream.atEnd())
+    {
+        QString str = stream.readLine();
+        if(str.startsWith(";") || str.isEmpty())
+            continue;
+
+        set.insert(str.trimmed());
+    }
+
+    txt.close();
+}
+
+void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix)
+{
+    QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
+
+    QFile txt(fileName);
+
+    // list empty? => rather have no file for the list than an empty one
+    if (set.isEmpty())
+    {
+        if (txt.exists())
+        {
+            // try to remove file, if successful we're done here.
+            if (txt.remove())
+                return;
+        }
+        else
+            // there is no file
+            return;
+    }
+
+    if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate))
+        return;
+
+    QTextStream stream(&txt);
+    stream.setCodec("UTF-8");
+
+    stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl;
+
+    foreach(const QString & nick, set.values())
+        stream << nick << endl;
+
+    txt.close();
+}
--- a/QTfrontend/model/playerslistmodel.h	Mon Oct 08 23:57:17 2012 +0400
+++ b/QTfrontend/model/playerslistmodel.h	Tue Oct 09 00:38:17 2012 +0400
@@ -5,6 +5,7 @@
 #include <QHash>
 #include <QIcon>
 #include <QModelIndex>
+#include <QSet>
 
 class PlayersListModel : public QAbstractListModel
 {
@@ -43,13 +44,20 @@
     void playerLeftRoom(const QString & nickname);
     void setFlag(const QString & nickname, StateFlag flagType, bool isSet);
     void resetRoomFlags();
+    void setNickname(const QString & nickname);
 
 private:
     QHash<quint32, QIcon> & m_icons();
     typedef QHash<int, QVariant> DataEntry;
     QList<DataEntry> m_data;
+    QSet<QString> m_friendsSet, m_ignoredSet;
+    QString m_nickname;
+
     void updateIcon(const QModelIndex & index);
     void updateSortData(const QModelIndex & index);
+    void loadSet(QSet<QString> & set, const QString & suffix);
+    void saveSet(const QSet<QString> & set, const QString & suffix);
+    void checkFriendIgnore(const QModelIndex & mi);
 };
 
 #endif // PLAYERSLISTMODEL_H
--- a/QTfrontend/net/newnetclient.cpp	Mon Oct 08 23:57:17 2012 +0400
+++ b/QTfrontend/net/newnetclient.cpp	Tue Oct 09 00:38:17 2012 +0400
@@ -243,6 +243,7 @@
     if (lst[0] == "NICK")
     {
         mynick = lst[1];
+        m_playersModel->setNickname(mynick);
         return ;
     }
 
@@ -446,6 +447,8 @@
         netClientState = InLobby;
         askRoomsList();
         emit LeftRoom(tr("You got kicked"));
+        m_playersModel->resetRoomFlags();
+
         return;
     }
 
@@ -462,6 +465,7 @@
             if (lst[i] == mynick)
             {
                 netClientState = InLobby;
+                m_playersModel->resetRoomFlags();
                 RawSendNet(QString("LIST"));
                 emit connected();
             }
@@ -644,6 +648,7 @@
         {
             netClientState = InLobby;
             askRoomsList();
+            m_playersModel->resetRoomFlags();
             emit LeftRoom(tr("Room destroyed"));
             return;
         }
@@ -906,6 +911,7 @@
 void HWNewNet::partRoom()
 {
     netClientState = InLobby;
+    m_playersModel->resetRoomFlags();
     RawSendNet(QString("PART"));
     askRoomsList();
 }