qmlfrontend/players_model.cpp
author Wuzzy <Wuzzy2@mail.ru>
Sat, 15 Jun 2019 12:09:12 +0200
changeset 15149 59c595191eba
parent 15052 773beead236f
permissions -rw-r--r--
Change max hedgehog estimate for drawn map sizes 1-3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
     1
#include <QDebug>
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
     2
#include <QFile>
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
     3
#include <QModelIndex>
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     4
#include <QModelIndexList>
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     5
#include <QPainter>
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     6
#include <QTextStream>
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     7
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
     8
#include "players_model.h"
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     9
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    10
PlayersListModel::PlayersListModel(QObject *parent)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    11
    : QAbstractListModel(parent) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    12
  m_fontInRoom = QFont();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    13
  m_fontInRoom.setItalic(true);
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    14
}
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    15
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    16
int PlayersListModel::rowCount(const QModelIndex &parent) const {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    17
  if (parent.isValid())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    18
    return 0;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    19
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    20
    return m_data.size();
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    21
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    22
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    23
QVariant PlayersListModel::data(const QModelIndex &index, int role) const {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    24
  if (!index.isValid() || index.row() < 0 || index.row() >= rowCount() ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    25
      index.column() != 0)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    26
    return QVariant(QVariant::Invalid);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    27
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    28
  return m_data.at(index.row()).value(role);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    29
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    30
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    31
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    32
                               int role) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    33
  if (!index.isValid() || index.row() < 0 || index.row() >= rowCount() ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    34
      index.column() != 0)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    35
    return false;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    36
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    37
  m_data[index.row()].insert(role, value);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    38
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    39
  emit dataChanged(index, index);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    40
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    41
  return true;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    42
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    43
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    44
bool PlayersListModel::insertRows(int row, int count,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    45
                                  const QModelIndex &parent) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    46
  if (parent.isValid() || row > rowCount() || row < 0 || count < 1)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    47
    return false;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    48
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    49
  beginInsertRows(parent, row, row + count - 1);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    50
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    51
  for (int i = 0; i < count; ++i) m_data.insert(row, DataEntry());
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    52
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    53
  endInsertRows();
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    54
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    55
  return true;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    56
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    57
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    58
bool PlayersListModel::removeRows(int row, int count,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    59
                                  const QModelIndex &parent) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    60
  if (parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    61
    return false;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    62
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    63
  beginRemoveRows(parent, row, row + count - 1);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    64
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    65
  for (int i = 0; i < count; ++i) m_data.removeAt(row);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    66
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    67
  endRemoveRows();
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    68
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    69
  return true;
7725
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
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    72
QModelIndex PlayersListModel::nicknameIndex(const QString &nickname) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    73
  QModelIndexList mil =
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    74
      match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    75
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    76
  if (mil.size() > 0)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    77
    return mil[0];
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    78
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    79
    return QModelIndex();
9727
e89ed65f62da small cleanup etc
sheepluva
parents: 9725
diff changeset
    80
}
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    81
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    82
void PlayersListModel::addPlayer(const QString &nickname, bool notify) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    83
  insertRow(rowCount());
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    84
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    85
  QModelIndex mi = index(rowCount() - 1);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    86
  setData(mi, nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    87
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    88
  checkFriendIgnore(mi);
8891
bf67b4d7d7b4 - Better fix to friends joins/quits highlighting problem
unc0rr
parents: 7842
diff changeset
    89
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    90
  emit nickAddedLobby(nickname, notify);
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    91
}
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    92
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    93
void PlayersListModel::removePlayer(const QString &nickname,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    94
                                    const QString &msg) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    95
  if (msg.isEmpty())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    96
    emit nickRemovedLobby(nickname, QString());
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    97
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
    98
    emit nickRemovedLobby(nickname, msg);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    99
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   100
  QModelIndex mi = nicknameIndex(nickname);
8891
bf67b4d7d7b4 - Better fix to friends joins/quits highlighting problem
unc0rr
parents: 7842
diff changeset
   101
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   102
  if (mi.isValid()) removeRow(mi.row());
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   103
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   104
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   105
void PlayersListModel::playerJoinedRoom(const QString &nickname, bool notify) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   106
  QModelIndex mi = nicknameIndex(nickname);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   107
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   108
  if (mi.isValid()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   109
    setData(mi, true, RoomFilterRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   110
    updateIcon(mi);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   111
    updateSortData(mi);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   112
  }
8891
bf67b4d7d7b4 - Better fix to friends joins/quits highlighting problem
unc0rr
parents: 7842
diff changeset
   113
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   114
  emit nickAdded(nickname, notify);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   115
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   116
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   117
void PlayersListModel::playerLeftRoom(const QString &nickname) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   118
  emit nickRemoved(nickname);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   119
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   120
  QModelIndex mi = nicknameIndex(nickname);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   121
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   122
  if (mi.isValid()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   123
    setData(mi, false, RoomFilterRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   124
    setData(mi, false, RoomAdmin);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   125
    setData(mi, false, Ready);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   126
    setData(mi, false, InGame);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   127
    updateIcon(mi);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   128
  }
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   129
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   130
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   131
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   132
                               bool isSet) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   133
  if (flagType == Friend) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   134
    if (isSet)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   135
      m_friendsSet.insert(nickname.toLower());
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   136
    else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   137
      m_friendsSet.remove(nickname.toLower());
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   138
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   139
    // FIXME: set proper file name
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   140
    // saveSet(m_friendsSet, "friends");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   141
  } else if (flagType == Ignore) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   142
    if (isSet)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   143
      m_ignoredSet.insert(nickname.toLower());
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   144
    else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   145
      m_ignoredSet.remove(nickname.toLower());
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   146
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   147
    // FIXME: set proper file name
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   148
    // saveSet(m_ignoredSet, "ignore");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   149
  }
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   150
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   151
  QModelIndex mi = nicknameIndex(nickname);
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   152
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   153
  if (mi.isValid()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   154
    setData(mi, isSet, flagType);
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   155
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   156
    if (flagType == Friend || flagType == ServerAdmin || flagType == Ignore ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   157
        flagType == RoomAdmin)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   158
      updateSortData(mi);
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   159
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   160
    updateIcon(mi);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   161
  }
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   162
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   163
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   164
bool PlayersListModel::isFlagSet(const QString &nickname, StateFlag flagType) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   165
  QModelIndex mi = nicknameIndex(nickname);
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   166
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   167
  if (mi.isValid())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   168
    return mi.data(flagType).toBool();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   169
  else if (flagType == Friend)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   170
    return isFriend(nickname);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   171
  else if (flagType == Ignore)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   172
    return isIgnored(nickname);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   173
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   174
    return false;
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   175
}
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   176
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   177
void PlayersListModel::resetRoomFlags() {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   178
  for (int i = rowCount() - 1; i >= 0; --i) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   179
    QModelIndex mi = index(i);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   180
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   181
    if (mi.data(RoomFilterRole).toBool()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   182
      setData(mi, false, RoomFilterRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   183
      setData(mi, false, RoomAdmin);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   184
      setData(mi, false, Ready);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   185
      setData(mi, false, InGame);
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   186
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   187
      updateSortData(mi);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   188
      updateIcon(mi);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   189
    }
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   190
  }
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   191
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   192
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   193
void PlayersListModel::updateIcon(const QModelIndex &index) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   194
  quint32 iconNum = 0;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   195
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   196
  QList<bool> flags;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   197
  flags << index.data(Ready).toBool() << index.data(ServerAdmin).toBool()
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   198
        << index.data(RoomAdmin).toBool() << index.data(Registered).toBool()
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   199
        << index.data(Friend).toBool() << index.data(Ignore).toBool()
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   200
        << index.data(InGame).toBool() << index.data(RoomFilterRole).toBool()
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   201
        << index.data(InRoom).toBool() << index.data(Contributor).toBool();
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   202
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   203
  for (int i = flags.size() - 1; i >= 0; --i)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   204
    if (flags[i]) iconNum |= 1 << i;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   205
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   206
  if (m_icons().contains(iconNum)) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   207
    setData(index, m_icons().value(iconNum), Qt::DecorationRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   208
  } else {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   209
    QPixmap result(24, 16);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   210
    result.fill(Qt::transparent);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   211
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   212
    QPainter painter(&result);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   213
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   214
    if (index.data(RoomFilterRole).toBool()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   215
      if (index.data(InGame).toBool()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   216
        painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/ingame.png"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   217
      } else {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   218
        if (index.data(Ready).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   219
          painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   220
        else
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   221
          painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp_off.png"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   222
      }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   223
    } else {  // we're in lobby
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   224
      if (!index.data(InRoom).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   225
        painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/Flake.png"));
7725
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
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   228
    QString mainIconName(":/res/chat/");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   229
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   230
    if (index.data(ServerAdmin).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   231
      mainIconName += "serveradmin";
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   232
    else {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   233
      if (index.data(RoomAdmin).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   234
        mainIconName += "roomadmin";
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   235
      else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   236
        mainIconName += "hedgehog";
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   237
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   238
      if (index.data(Contributor).toBool()) mainIconName += "contributor";
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   239
    }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   240
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   241
    if (!index.data(Registered).toBool()) mainIconName += "_gray";
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   242
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   243
    painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   244
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   245
    if (index.data(Ignore).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   246
      painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   247
    else if (index.data(Friend).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   248
      painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   249
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   250
    painter.end();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   251
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   252
    QIcon icon(result);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   253
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   254
    setData(index, icon, Qt::DecorationRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   255
    m_icons().insert(iconNum, icon);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   256
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   257
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   258
  if (index.data(Ignore).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   259
    setData(index, QColor(Qt::gray), Qt::ForegroundRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   260
  else if (index.data(Friend).toBool())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   261
    setData(index, QColor(Qt::green), Qt::ForegroundRole);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   262
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   263
    setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   264
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   265
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   266
QHash<quint32, QIcon> &PlayersListModel::m_icons() {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   267
  static QHash<quint32, QIcon> iconsCache;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   268
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   269
  return iconsCache;
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   270
}
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   271
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   272
void PlayersListModel::updateSortData(const QModelIndex &index) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   273
  QString result =
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   274
      QString("%1%2%3%4%5%6")
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   275
          // room admins go first, then server admins, then friends
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   276
          .arg(1 - index.data(RoomAdmin).toInt())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   277
          .arg(1 - index.data(ServerAdmin).toInt())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   278
          .arg(1 - index.data(Friend).toInt())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   279
          // ignored at bottom
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   280
          .arg(index.data(Ignore).toInt())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   281
          // keep nicknames starting from non-letter character at bottom within
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   282
          // group assume there are no empty nicks in list
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   283
          .arg(index.data(Qt::DisplayRole).toString().at(0).isLetter() ? 0 : 1)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   284
          // sort ignoring case
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   285
          .arg(index.data(Qt::DisplayRole).toString().toLower());
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   286
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   287
  setData(index, result, SortRole);
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   288
}
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   289
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   290
void PlayersListModel::setNickname(const QString &nickname) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   291
  m_nickname = nickname;
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   292
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   293
  // FIXME: set proper file names
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   294
  // loadSet(m_friendsSet, "friends");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   295
  // loadSet(m_ignoredSet, "ignore");
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   296
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   297
  for (int i = rowCount() - 1; i >= 0; --i) checkFriendIgnore(index(i));
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   298
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   299
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   300
bool PlayersListModel::isFriend(const QString &nickname) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   301
  return m_friendsSet.contains(nickname.toLower());
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   302
}
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   303
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   304
bool PlayersListModel::isIgnored(const QString &nickname) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   305
  return m_ignoredSet.contains(nickname.toLower());
9725
68b5d87cfdb0 regression fix: reallow offline players to be added to friendslist/ignore list
sheepluva
parents: 9503
diff changeset
   306
}
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   307
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   308
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   309
  setData(mi, isFriend(mi.data().toString()), Friend);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   310
  setData(mi, isIgnored(mi.data().toString()), Ignore);
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   311
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   312
  updateIcon(mi);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   313
  updateSortData(mi);
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   314
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   315
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   316
void PlayersListModel::loadSet(QSet<QString> &set, const QString &fileName) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   317
  set.clear();
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   318
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   319
  QFile txt(fileName);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   320
  if (!txt.open(QIODevice::ReadOnly)) return;
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   321
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   322
  QTextStream stream(&txt);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   323
  stream.setCodec("UTF-8");
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   324
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   325
  while (!stream.atEnd()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   326
    QString str = stream.readLine();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   327
    if (str.startsWith(";") || str.isEmpty()) continue;
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   328
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   329
    set.insert(str.trimmed());
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   330
  }
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   331
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   332
  txt.close();
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   333
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   334
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   335
void PlayersListModel::saveSet(const QSet<QString> &set,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   336
                               const QString &fileName) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   337
  qDebug("saving set");
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   338
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   339
  QFile txt(fileName);
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   340
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   341
  // list empty? => rather have no file for the list than an empty one
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   342
  if (set.isEmpty()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   343
    if (txt.exists()) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   344
      // try to remove file, if successful we're done here.
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   345
      if (txt.remove()) return;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   346
    } else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   347
      // there is no file
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   348
      return;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   349
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   350
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   351
  if (!txt.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   352
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   353
  QTextStream stream(&txt);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   354
  stream.setCodec("UTF-8");
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   355
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   356
  stream << "; this list is used by Hedgewars - do not edit it unless you know "
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   357
            "what you're doing!"
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   358
         << endl;
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   359
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   360
  foreach (const QString &nick, set.values())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   361
    stream << nick << endl;
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   362
15052
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12902
diff changeset
   363
  txt.close();
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   364
}