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 "irc/ircuserinfo.h"
24 #include "ircuserlist.h"
25 
26 IRCUserList::~IRCUserList()
27 {
28  qDeleteAll(usersArray);
29 }
30 
32 {
33  int index = this->indexOfName(userInfo.cleanNickname());
34  if (index >= 0)
35  {
36  *usersArray[index] = userInfo;
37  return false;
38  }
39  else
40  {
41  auto pUserInfo = new IRCUserInfo(userInfo);
42  usersArray.append(pUserInfo);
43  return true;
44  }
45 }
46 
47 bool IRCUserList::changeNick(const QString &oldNickname, const QString &newNickname)
48 {
49  const IRCUserInfo *pExistingInfo = user(oldNickname);
50  if (pExistingInfo == nullptr)
51  return false;
52 
53  IRCUserInfo user = *pExistingInfo;
54  user.setPrefixedNickname(newNickname);
55  removeNameFromCachedList(oldNickname);
57  return true;
58 }
59 
60 bool IRCUserList::hasUser(const QString &nickname) const
61 {
62  return indexOfName(nickname) != -1;
63 }
64 
65 int IRCUserList::indexOfName(const QString &nickname) const
66 {
67  for (int i = 0; i < usersArray.size(); ++i)
68  {
69  const IRCUserInfo &storedUser = *usersArray[i];
70  if (storedUser.isSameNickname(nickname))
71  return i;
72  }
73 
74  return -1;
75 }
76 
77 bool IRCUserList::removeNameFromCachedList(const QString &nickname)
78 {
79  int index = indexOfName(nickname);
80  if (index < 0)
81  return false;
82 
83  delete usersArray[index];
84  usersArray.remove(index);
85  return true;
86 }
87 
88 void IRCUserList::setUserModes(const QString &nickname, const QList<char> &modes)
89 {
90  int index = this->indexOfName(nickname);
91  if (index >= 0)
92  {
93  IRCUserInfo &userInfo = *usersArray[index];
94  userInfo.setModes(modes);
95  }
96 }
97 
98 QStringList IRCUserList::toStringList() const
99 {
100  QStringList nicksList;
101  for (auto *i : usersArray)
102  nicksList << i->prefixedName();
103 
104  return nicksList;
105 }
106 
107 const IRCUserInfo *IRCUserList::user(const QString &nickname) const
108 {
109  int index = this->indexOfName(nickname);
110  if (index < 0)
111  return nullptr;
112 
113  return usersArray[index];
114 }
115 
116 IRCUserInfo IRCUserList::userCopy(const QString &nickname) const
117 {
118  const IRCUserInfo *pUserInfo = user(nickname);
119  if (pUserInfo == nullptr)
120  return IRCUserInfo();
121  return *pUserInfo;
122 }