# HG changeset patch # User unc0rr # Date 1349728697 -14400 # Node ID fad3408fdcc1209592b0d901e4de94976fe3f6d9 # Parent 262228c64f15aa849ff9fc75791cfa7e682ed2b3 Load friends/ignored nicks from file diff -r 262228c64f15 -r fad3408fdcc1 QTfrontend/model/playerslistmodel.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 #include #include +#include +#include #include #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 & 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 & 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(); +} diff -r 262228c64f15 -r fad3408fdcc1 QTfrontend/model/playerslistmodel.h --- 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 #include #include +#include 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 & m_icons(); typedef QHash DataEntry; QList m_data; + QSet m_friendsSet, m_ignoredSet; + QString m_nickname; + void updateIcon(const QModelIndex & index); void updateSortData(const QModelIndex & index); + void loadSet(QSet & set, const QString & suffix); + void saveSet(const QSet & set, const QString & suffix); + void checkFriendIgnore(const QModelIndex & mi); }; #endif // PLAYERSLISTMODEL_H diff -r 262228c64f15 -r fad3408fdcc1 QTfrontend/net/newnetclient.cpp --- 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(); }