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  return nickname.mid(1);
79  }
80  return nickname;
81 }
82 
83 int IRCUserPrefix::compare(char mode1, char mode2) const
84 {
85  if (mode1 == mode2)
86  return 0;
87  for (const IRCModePrefix &candidate : d->map)
88  {
89  if (candidate.mode == mode1)
90  return -1;
91  else if (candidate.mode == mode2)
92  return 1;
93  }
94  // Neither was found so we treat them as equal.
95  return 0;
96 }
97 
98 bool IRCUserPrefix::hasMode(char mode) const
99 {
100  return prefixForMode(mode) != 0;
101 }
102 
103 bool IRCUserPrefix::hasPrefix(char prefix) const
104 {
105  return modeForPrefix(prefix) != 0;
106 }
107 
109 {
110  IRCUserPrefix obj;
111  obj.assignPrefix('o', '@');
112  obj.assignPrefix('h', '%');
113  obj.assignPrefix('v', '+');
114  return obj;
115 }
116 
117 char IRCUserPrefix::modeFromNickname(const QString &nickname) const
118 {
119  if (!nickname.isEmpty())
120  {
121  if (hasPrefix(nickname[0].toLatin1()))
122  return modeForPrefix(nickname[0].toLatin1());
123  }
124  return 0;
125 }
126 
127 char IRCUserPrefix::modeForPrefix(char prefix) const
128 {
129  for (const IRCModePrefix &candidate : d->map)
130  {
131  if (candidate.prefix == prefix)
132  return candidate.mode;
133  }
134  return 0;
135 }
136 
137 char IRCUserPrefix::prefixForMode(char mode) const
138 {
139  for (const IRCModePrefix &candidate : d->map)
140  {
141  if (candidate.mode == mode)
142  return candidate.prefix;
143  }
144  return 0;
145 }
146 
147 char IRCUserPrefix::topMostMode(const QList<char> &candidates) const
148 {
149  char highest = 0;
150  for (char candidate : candidates)
151  {
152  if (compare(highest, candidate) > 0)
153  highest = candidate;
154  }
155  return highest;
156 }
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