ircuserprefix.cpp
1 //------------------------------------------------------------------------------
2 // ircuserprefix.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircuserprefix.h"
24 
25 #include <QMap>
26 
27 class IRCModePrefix
28 {
29  public:
30  char mode;
31  char prefix;
32 
33  IRCModePrefix(char mode, char prefix)
34  {
35  this->mode = mode;
36  this->prefix = prefix;
37  }
38 };
39 
40 
41 DClass<IRCUserPrefix>
42 {
43  public:
44  QList<IRCModePrefix> map;
45 };
46 
47 DPointered(IRCUserPrefix)
48 
50 {
51 }
52 
53 IRCUserPrefix::IRCUserPrefix(const IRCUserPrefix &other)
54 {
55  d = other.d;
56 }
57 
58 IRCUserPrefix::~IRCUserPrefix()
59 {
60 }
61 
62 IRCUserPrefix &IRCUserPrefix::operator=(const IRCUserPrefix &other)
63 {
64  d = other.d;
65  return *this;
66 }
67 
68 void IRCUserPrefix::assignPrefix(char mode, char prefix)
69 {
70  d->map << IRCModePrefix(mode, prefix);
71 }
72 
73 QString IRCUserPrefix::cleanNickname(const QString &nickname) const
74 {
75  if (!nickname.isEmpty())
76  {
77  if (hasPrefix(nickname[0].toLatin1()))
78  {
79  return nickname.mid(1);
80  }
81  }
82  return nickname;
83 }
84 
85 int IRCUserPrefix::compare(char mode1, char mode2) const
86 {
87  if (mode1 == mode2)
88  {
89  return 0;
90  }
91  foreach (const IRCModePrefix &candidate, d->map)
92  {
93  if (candidate.mode == mode1)
94  {
95  return -1;
96  }
97  else if (candidate.mode == mode2)
98  {
99  return 1;
100  }
101  }
102  // Neither was found so we treat them as equal.
103  return 0;
104 }
105 
106 bool IRCUserPrefix::hasMode(char mode) const
107 {
108  return prefixForMode(mode) != 0;
109 }
110 
111 bool IRCUserPrefix::hasPrefix(char prefix) const
112 {
113  return modeForPrefix(prefix) != 0;
114 }
115 
117 {
118  IRCUserPrefix obj;
119  obj.assignPrefix('o', '@');
120  obj.assignPrefix('h', '%');
121  obj.assignPrefix('v', '+');
122  return obj;
123 }
124 
125 char IRCUserPrefix::modeFromNickname(const QString &nickname) const
126 {
127  if (!nickname.isEmpty())
128  {
129  if (hasPrefix(nickname[0].toLatin1()))
130  {
131  return modeForPrefix(nickname[0].toLatin1());
132  }
133  }
134  return 0;
135 }
136 
137 char IRCUserPrefix::modeForPrefix(char prefix) const
138 {
139  foreach (const IRCModePrefix &candidate, d->map)
140  {
141  if (candidate.prefix == prefix)
142  {
143  return candidate.mode;
144  }
145  }
146  return 0;
147 }
148 
149 char IRCUserPrefix::prefixForMode(char mode) const
150 {
151  foreach (const IRCModePrefix &candidate, d->map)
152  {
153  if (candidate.mode == mode)
154  {
155  return candidate.prefix;
156  }
157  }
158  return 0;
159 }
160 
161 char IRCUserPrefix::topMostMode(const QList<char> &candidates) const
162 {
163  char highest = 0;
164  foreach (char candidate, candidates)
165  {
166  if (compare(highest, candidate) > 0)
167  {
168  highest = candidate;
169  }
170  }
171  return highest;
172 }
char topMostMode(const QList< char > &candidates) const
Out of list of possible modes picks mode with highest priority.
static IRCUserPrefix mkDefault()
User modes: op, half-op and voice.
One-to-one association of visible prefixes to user mode.
Definition: ircuserprefix.h:37