ircuserlist.cpp
1 //------------------------------------------------------------------------------
2 // ircuserlist.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircuserlist.h"
24 #include "irc/ircuserinfo.h"
25 
26 IRCUserList::~IRCUserList()
27 {
28  for (int i = 0; i < usersArray.size(); ++i)
29  {
30  delete usersArray[i];
31  }
32 }
33 
35 {
36  int index = this->indexOfName(userInfo.cleanNickname());
37  if (index >= 0)
38  {
39  *usersArray[index] = userInfo;
40  return false;
41  }
42  else
43  {
44  IRCUserInfo* pUserInfo = new IRCUserInfo(userInfo);
45  usersArray.append(pUserInfo);
46  return true;
47  }
48 }
49 
50 bool IRCUserList::changeNick(const QString& oldNickname, const QString& newNickname)
51 {
52  const IRCUserInfo* pExistingInfo = user(oldNickname);
53  if (pExistingInfo == NULL)
54  {
55  return false;
56  }
57 
58  IRCUserInfo user = *pExistingInfo;
59  user.setPrefixedNickname(newNickname);
60  removeNameFromCachedList(oldNickname);
62  return true;
63 }
64 
65 bool IRCUserList::hasUser(const QString& nickname) const
66 {
67  return (indexOfName(nickname) != -1);
68 }
69 
70 int IRCUserList::indexOfName(const QString& nickname) const
71 {
72  for (int i = 0; i < usersArray.size(); ++i)
73  {
74  const IRCUserInfo& storedUser = *usersArray[i];
75  if (storedUser.isSameNickname(nickname))
76  {
77  return i;
78  }
79  }
80 
81  return -1;
82 }
83 
84 bool IRCUserList::removeNameFromCachedList(const QString& nickname)
85 {
86  int index = indexOfName(nickname);
87  if (index < 0)
88  {
89  return false;
90  }
91 
92  delete usersArray[index];
93  usersArray.remove(index);
94  return true;
95 }
96 
97 void IRCUserList::setUserModes(const QString& nickname, const QList<char> &modes)
98 {
99  int index = this->indexOfName(nickname);
100  if (index >= 0)
101  {
102  IRCUserInfo& userInfo = *usersArray[index];
103  userInfo.setModes(modes);
104  }
105 }
106 
107 QStringList IRCUserList::toStringList() const
108 {
109  QStringList nicksList;
110  for (int i = 0; i < usersArray.size(); ++i)
111  {
112  nicksList << usersArray[i]->prefixedName();
113  }
114 
115  return nicksList;
116 }
117 
118 const IRCUserInfo* IRCUserList::user(const QString& nickname) const
119 {
120  int index = this->indexOfName(nickname);
121  if (index < 0)
122  {
123  return NULL;
124  }
125 
126  return usersArray[index];
127 }
128 
129 IRCUserInfo IRCUserList::userCopy(const QString& nickname) const
130 {
131  const IRCUserInfo* pUserInfo = user(nickname);
132  if (pUserInfo == NULL)
133  {
134  return IRCUserInfo();
135  }
136  return *pUserInfo;
137 }
QString cleanNickname() const
Returns nickname with no prefixes, contrary to the prefixedName() .
Definition: ircuserinfo.cpp:50
bool appendNameToCachedList(const IRCUserInfo &userInfo)
Appends a single name to the users array.
Definition: ircuserlist.cpp:34
bool isSameNickname(const IRCUserInfo &otherUser) const
Check if this user and user specified as parameter are the same user.
Definition: ircuserinfo.cpp:79
bool changeNick(const QString &oldNickname, const QString &newNickname)
Changes a nickname while preserving user flags.
Definition: ircuserlist.cpp:50
IRCUserInfo userCopy(const QString &nickname) const
Gets a copy of the IRCUserInfo for user with given name.
bool removeNameFromCachedList(const QString &nickname)
Removes a name from the sortecd cachedNames list.
Definition: ircuserlist.cpp:84
Holds information flags about given nickname.
Definition: ircuserinfo.h:35
QStringList toStringList() const
Returns all prefixed nicknames in a string list.