QTfrontend/model/playerslistmodel.cpp
author sheepluva
Mon, 05 Aug 2019 00:20:45 +0200
changeset 15295 f382ec6dba11
parent 12897 fc47fc4af6bd
permissions -rw-r--r--
In hindsight my emscripten-ifdef (70d416a8f63f) is nonsense. As fpcrtl_glShaderSource() would not be defined and lead to compiling issues. So either it's 3 ifdefs (in pas2cRedo, pas2cSystem and misc.c), in order to toggle between fpcrtl_ and the native function, or alternatively have no ifdef for it at all. I'm going with none at all, which means emscripten will compile with the original (const) function prototype, being wrapped by the fpcrtl_ function, same as non-emscripten builds.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     1
#include <QModelIndexList>
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     2
#include <QModelIndex>
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     3
#include <QPainter>
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     4
#include <QFile>
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     5
#include <QTextStream>
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
     6
#include <QDebug>
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     7
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     8
#include "playerslistmodel.h"
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     9
#include "hwconsts.h"
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    10
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    11
PlayersListModel::PlayersListModel(QObject *parent) :
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    12
    QAbstractListModel(parent)
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    13
{
9503
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
    14
    m_fontInRoom = QFont();
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
    15
    m_fontInRoom.setItalic(true);
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    16
}
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    17
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    18
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    19
int PlayersListModel::rowCount(const QModelIndex &parent) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    20
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    21
    if(parent.isValid())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    22
        return 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    23
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    24
        return m_data.size();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    25
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    26
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    27
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    28
QVariant PlayersListModel::data(const QModelIndex &index, int role) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    29
{
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
    30
    if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    31
        return QVariant(QVariant::Invalid);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    32
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    33
    return m_data.at(index.row()).value(role);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    34
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    35
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    36
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    37
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    38
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    39
    if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    40
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    41
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    42
    m_data[index.row()].insert(role, value);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    43
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    44
    emit dataChanged(index, index);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    45
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    46
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    47
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    48
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    49
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    50
bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    51
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    52
    return insertRows(row, 1, parent);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    53
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    54
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    55
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    56
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    57
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    58
    if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    59
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    60
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    61
    beginInsertRows(parent, row, row + count - 1);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    62
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    63
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    64
        m_data.insert(row, DataEntry());
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    65
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    66
    endInsertRows();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    67
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    68
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    69
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    70
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    71
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    72
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    73
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    74
    if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    75
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    76
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    77
    beginRemoveRows(parent, row, row + count - 1);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    78
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    79
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    80
        m_data.removeAt(row);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    81
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    82
    endRemoveRows();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    83
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    84
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    85
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    86
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    87
QModelIndex PlayersListModel::nicknameIndex(const QString & nickname)
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    88
{
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    89
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    90
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    91
    if(mil.size() > 0)
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    92
        return mil[0];
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    93
    else
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    94
        return QModelIndex();
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    95
}
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    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
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    98
{
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    99
    insertRow(rowCount());
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
   100
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   101
    QModelIndex mi = index(rowCount() - 1);
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   102
    setData(mi, nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   103
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   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
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
   107
}
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   108
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   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
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   117
    QModelIndex mi = nicknameIndex(nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   118
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   119
    if(mi.isValid())
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   120
        removeRow(mi.row());
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   121
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   122
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   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
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   125
{
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   126
    QModelIndex mi = nicknameIndex(nickname);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   127
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   128
    if(mi.isValid())
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   129
    {
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   130
        setData(mi, true, RoomFilterRole);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   131
        updateIcon(mi);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   132
        updateSortData(mi);
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   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
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   136
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   137
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   138
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   139
void PlayersListModel::playerLeftRoom(const QString & nickname)
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   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
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   143
    QModelIndex mi = nicknameIndex(nickname);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   144
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   145
    if(mi.isValid())
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   146
    {
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   147
        setData(mi, false, RoomFilterRole);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   148
        setData(mi, false, RoomAdmin);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   149
        setData(mi, false, Ready);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   150
        setData(mi, false, InGame);
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   151
        updateIcon(mi);
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   152
    }
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   153
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   154
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   155
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   156
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   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
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   177
    QModelIndex mi = nicknameIndex(nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   178
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   179
    if(mi.isValid())
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   180
    {
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   181
        setData(mi, isSet, flagType);
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   182
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   183
        if(flagType == Friend || flagType == ServerAdmin
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   184
                || flagType == Ignore || flagType == RoomAdmin)
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   185
            updateSortData(mi);
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   186
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   187
        updateIcon(mi);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   188
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   189
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   190
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   191
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   192
bool PlayersListModel::isFlagSet(const QString & nickname, StateFlag flagType)
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   193
{
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   194
    QModelIndex mi = nicknameIndex(nickname);
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   195
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   196
    if(mi.isValid())
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
   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
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   202
    else
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   203
        return false;
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   204
}
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   205
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   206
void PlayersListModel::resetRoomFlags()
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   207
{
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   208
    for(int i = rowCount() - 1; i >= 0; --i)
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   209
    {
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   210
        QModelIndex mi = index(i);
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   211
7833
20e3bd929f07 This should cover all entries from issue 438
unc0rr
parents: 7765
diff changeset
   212
        if(mi.data(RoomFilterRole).toBool())
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   213
        {
7833
20e3bd929f07 This should cover all entries from issue 438
unc0rr
parents: 7765
diff changeset
   214
            setData(mi, false, RoomFilterRole);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   215
            setData(mi, false, RoomAdmin);
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   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
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   218
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   219
            updateSortData(mi);
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   220
            updateIcon(mi);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   221
        }
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   222
    }
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   223
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   224
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   225
void PlayersListModel::updateIcon(const QModelIndex & index)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   226
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   227
    quint32 iconNum = 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   228
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   229
    QList<bool> flags;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   230
    flags
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   231
        << index.data(Ready).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   232
        << index.data(ServerAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   233
        << index.data(RoomAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   234
        << index.data(Registered).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   235
        << index.data(Friend).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   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
20e3bd929f07 This should cover all entries from issue 438
unc0rr
parents: 7765
diff changeset
   238
        << index.data(RoomFilterRole).toBool()
9503
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   239
        << index.data(InRoom).toBool()
10753
e56db5d988ef fix issue with contributor hat being displayed for wrong players
sheepluva
parents: 9727
diff changeset
   240
        << index.data(Contributor).toBool()
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   241
        ;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   242
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   243
    for(int i = flags.size() - 1; i >= 0; --i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   244
        if(flags[i])
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   245
            iconNum |= 1 << i;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   246
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   247
    if(m_icons().contains(iconNum))
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   248
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   249
        setData(index, m_icons().value(iconNum), Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   250
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   251
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   252
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   253
        QPixmap result(24, 16);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   254
        result.fill(Qt::transparent);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   255
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   256
        QPainter painter(&result);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   257
7833
20e3bd929f07 This should cover all entries from issue 438
unc0rr
parents: 7765
diff changeset
   258
        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
   259
        {
7765
1e162c1d6dc7 'In game' client flag, both server and frontend support
unc0rr
parents: 7764
diff changeset
   260
            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
   261
            {
7765
1e162c1d6dc7 'In game' client flag, both server and frontend support
unc0rr
parents: 7764
diff changeset
   262
                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
   263
            }
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   264
            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
   265
            {
7765
1e162c1d6dc7 'In game' client flag, both server and frontend support
unc0rr
parents: 7764
diff changeset
   266
                if(index.data(Ready).toBool())
1e162c1d6dc7 'In game' client flag, both server and frontend support
unc0rr
parents: 7764
diff changeset
   267
                    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
   268
                else
1e162c1d6dc7 'In game' client flag, both server and frontend support
unc0rr
parents: 7764
diff changeset
   269
                    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
   270
            }
9503
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   271
        } else
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   272
        { // we're in lobby
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   273
            if(!index.data(InRoom).toBool())
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   274
                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
   275
        }
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   276
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   277
        QString mainIconName(":/res/chat/");
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   278
9503
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   279
        if(index.data(ServerAdmin).toBool())
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   280
            mainIconName += "serveradmin";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   281
        else
9503
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   282
        {
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   283
            if(index.data(RoomAdmin).toBool())
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   284
                mainIconName += "roomadmin";
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   285
            else
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   286
                mainIconName += "hedgehog";
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   287
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   288
            if(index.data(Contributor).toBool())
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   289
                mainIconName += "contributor";
8f9b04138456 Icons for 'i' and 'c' flags
unc0rr
parents: 8891
diff changeset
   290
        }
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   291
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   292
        if(!index.data(Registered).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   293
            mainIconName += "_gray";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   294
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   295
        painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   296
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   297
        if(index.data(Ignore).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   298
            painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   299
        else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   300
        if(index.data(Friend).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   301
            painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   302
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   303
        painter.end();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   304
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   305
        QIcon icon(result);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   306
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   307
        setData(index, icon, Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   308
        m_icons().insert(iconNum, icon);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   309
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   310
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   311
    if(index.data(Ignore).toBool())
12897
fc47fc4af6bd Finish porting. Seems to work, but no thorough testing has been performed
unc0rr
parents: 10753
diff changeset
   312
        setData(index, QColor(Qt::gray), Qt::ForegroundRole);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   313
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   314
    if(index.data(Friend).toBool())
12897
fc47fc4af6bd Finish porting. Seems to work, but no thorough testing has been performed
unc0rr
parents: 10753
diff changeset
   315
        setData(index, QColor(Qt::green), Qt::ForegroundRole);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   316
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   317
        setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   318
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   319
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   320
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   321
QHash<quint32, QIcon> & PlayersListModel::m_icons()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   322
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   323
    static QHash<quint32, QIcon> iconsCache;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   324
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   325
    return iconsCache;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   326
}
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   327
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   328
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   329
void PlayersListModel::updateSortData(const QModelIndex & index)
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   330
{
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   331
    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
   332
            // room admins go first, then server admins, then friends
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   333
            .arg(1 - index.data(RoomAdmin).toInt())
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   334
            .arg(1 - index.data(ServerAdmin).toInt())
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   335
            .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
   336
            // ignored at bottom
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   337
            .arg(index.data(Ignore).toInt())
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   338
            // 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
   339
            // 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
   340
            .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
   341
            // sort ignoring case
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   342
            .arg(index.data(Qt::DisplayRole).toString().toLower())
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   343
            ;
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   344
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   345
    setData(index, result, SortRole);
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   346
}
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   347
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   348
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   349
void PlayersListModel::setNickname(const QString &nickname)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   350
{
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   351
    m_nickname = nickname;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   352
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   353
    loadSet(m_friendsSet, "friends");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   354
    loadSet(m_ignoredSet, "ignore");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   355
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   356
    for(int i = rowCount() - 1; i >= 0; --i)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   357
        checkFriendIgnore(index(i));
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   358
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   359
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   360
bool PlayersListModel::isFriend(const QString & nickname)
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   361
{
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   362
    return m_friendsSet.contains(nickname.toLower());
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
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   365
bool PlayersListModel::isIgnored(const QString & nickname)
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   366
{
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   367
    return m_ignoredSet.contains(nickname.toLower());
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   368
}
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   369
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   370
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   371
{
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   372
    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
   373
    setData(mi, isIgnored(mi.data().toString()), Ignore);
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   374
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   375
    updateIcon(mi);
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   376
    updateSortData(mi);
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   377
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   378
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   379
void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   380
{
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   381
    set.clear();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   382
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   383
    QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   384
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   385
    QFile txt(fileName);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   386
    if(!txt.open(QIODevice::ReadOnly))
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   387
        return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   388
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   389
    QTextStream stream(&txt);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   390
    stream.setCodec("UTF-8");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   391
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   392
    while(!stream.atEnd())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   393
    {
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   394
        QString str = stream.readLine();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   395
        if(str.startsWith(";") || str.isEmpty())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   396
            continue;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   397
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   398
        set.insert(str.trimmed());
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   399
    }
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   400
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   401
    txt.close();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   402
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   403
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   404
void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   405
{
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   406
    qDebug("saving set");
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   407
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   408
    QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   409
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   410
    QFile txt(fileName);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   411
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   412
    // list empty? => rather have no file for the list than an empty one
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   413
    if (set.isEmpty())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   414
    {
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   415
        if (txt.exists())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   416
        {
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   417
            // try to remove file, if successful we're done here.
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   418
            if (txt.remove())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   419
                return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   420
        }
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   421
        else
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   422
            // there is no file
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   423
            return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   424
    }
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   425
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   426
    if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate))
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   427
        return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   428
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   429
    QTextStream stream(&txt);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   430
    stream.setCodec("UTF-8");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   431
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   432
    stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   433
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   434
    foreach(const QString & nick, set.values())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   435
        stream << nick << endl;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   436
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   437
    txt.close();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   438
}