summaryrefslogtreecommitdiffstats
path: root/konversation/src/channelnick.cpp
blob: 8640acdab670447c42f463738540790517a5b0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

/*
  begin:     Wed Aug 04 2004
  copyright: (C) 2002,2003,2004 by Dario Abatianni
  email:     eisfuchs@tigress.com
*/

/* An instance of ChannelNick is made for each nick in each channel.
   So for a person in multiple channels, they will have one NickInfo, and multiple ChannelNicks.
   It contains a pointer to the NickInfo, and the mode of that person in the channel.
*/

#include "channelnick.h"
#include "channel.h"
#include "server.h"

#include <klocale.h>


ChannelNick::ChannelNick(const NickInfoPtr& nickInfo, const bool& isop, const bool& isadmin,
			 const bool& isowner, const bool& ishalfop, const bool& hasvoice)
: KShared()
{
    this->nickInfo = nickInfo;
    this->isop = isop;
    this->isadmin = isadmin;
    this->isowner = isowner;
    this->ishalfop = ishalfop;
    this->hasvoice = hasvoice;
    m_timeStamp = 0;
    m_recentActivity = 0;
}

ChannelNick::~ChannelNick()
{
}

bool ChannelNick::isOp() const
{
  return isop;
}

bool ChannelNick::isAdmin() const
{
  return isadmin;
}

bool ChannelNick::isOwner() const
{
  return isowner;
}

bool ChannelNick::isHalfOp() const
{
  return ishalfop;
}

bool ChannelNick::hasVoice() const
{
  return hasvoice;
}

bool ChannelNick::isAnyTypeOfOp() const
{
  return isop || isadmin || isowner || ishalfop;
}

NickInfoPtr ChannelNick::getNickInfo() const
{
  return nickInfo;
}

/** @param mode 'v' to set voice, 'a' to set admin, 'h' to set halfop, 'o' to set op.
 *  @param state what to set the mode to.
 */
bool ChannelNick::setMode(char mode, bool state)
{
    switch (mode)
    {
        case 'q':
            return setOwner(state);
        case 'a':
            return setAdmin(state);
        case 'o':
            return setOp(state);
        case 'h':
            return setHalfOp(state);
        case 'v':
            return setVoice(state);
        default:
            kdDebug() << "Mode '" << mode << "' not recognised in setModeForChannelNick";
            return false;
    }
}

/** Used still for passing modes from inputfilter to Server.  Should be removed.
 */
bool ChannelNick::setMode(int mode)
{
    bool voice = mode%2;
    mode >>= 1;
    bool halfop = mode %2;
    mode >>= 1;
    bool op = mode %2;
    mode >>= 1;
    bool owner = mode %2;
    mode >>= 1;
    bool admin = mode %2;
    return setMode(admin, owner, op, halfop, voice);
}

bool ChannelNick::setMode(bool admin,bool owner,bool op,bool halfop,bool voice)
{
    if(isadmin==admin && isowner==owner && isop==op && ishalfop==halfop && hasvoice==voice)
        return false;
    isadmin=admin;
    isowner=owner;
    isop=op;
    ishalfop=halfop;
    hasvoice=voice;
    nickInfo->getServer()->emitChannelNickChanged(this);
    emit channelNickChanged();
    return true;
}

/** set the voice for the nick, and update
 * @returns Whether it needed to be changed.  False for no change.
 */
bool ChannelNick::setVoice(bool state)
{
    if(hasvoice==state) return false;
    hasvoice=state;
    nickInfo->getServer()->emitChannelNickChanged(this);
    emit channelNickChanged();
    return true;
}

bool ChannelNick::setOwner(bool state)
{
    if(isowner==state) return false;
    isowner=state;
    nickInfo->getServer()->emitChannelNickChanged(this);
    emit channelNickChanged();
    return true;
}

bool ChannelNick::setAdmin(bool state)
{
    if(isadmin==state) return false;
    isadmin=state;
    nickInfo->getServer()->emitChannelNickChanged(this);
    emit channelNickChanged();
    return true;
}

bool ChannelNick::setHalfOp(bool state)
{
    if(ishalfop==state) return false;
    ishalfop=state;
    nickInfo->getServer()->emitChannelNickChanged(this);
    emit channelNickChanged();
    return true;
}

bool ChannelNick::setOp(bool state)
{
    if(isop==state) return false;
    isop=state;
    nickInfo->getServer()->emitChannelNickChanged(this);
    emit channelNickChanged();
    return true;
}

//Purely provided for convience because they are used so often.
//Just calls nickInfo->getNickname() etc
QString ChannelNick::getNickname() const
{
    if ( this )
        return nickInfo->getNickname();
    else
        return QString();
}

QString ChannelNick::getHostmask() const
{
    if ( this )
        return nickInfo->getHostmask();
    else
        return QString();
}

QString ChannelNick::tooltip()
{
    //  if(addressee.isEmpty()) return QString();
    KABC::Addressee addressee = nickInfo->getAddressee();
    QString strTooltip;
    QTextStream tooltip( &strTooltip, IO_WriteOnly );

    tooltip << "<qt>";

    tooltip << "<table cellspacing=\"0\" cellpadding=\"0\">";

    nickInfo->tooltipTableData(tooltip);

    QStringList modes;
    if(isOp()) modes << i18n("Operator");
    if(isAdmin()) modes << i18n("Admin");
    if(isOwner()) modes << i18n("Owner");
    if(isHalfOp()) modes << i18n("Half-operator");
    if(hasVoice()) modes << i18n("Has voice");
    //Don't show anything if the user is just a normal user
    //if(modes.empty()) modes << i18n("A normal user");
    if(!modes.empty())
    {
        tooltip << "<tr><td><b>" << i18n("Mode") << ": </b></td><td>" << modes.join(", ") << "</td></tr>";
    }
    tooltip << "</table></qt>";
    //kdDebug() << strTooltip << endl;
    //if(!dirty) return QString();
    return strTooltip;
}

QString ChannelNick::loweredNickname() const
{
    return nickInfo->loweredNickname();
}

uint ChannelNick::timeStamp() const
{
  return m_timeStamp;
}

uint ChannelNick::recentActivity() const
{
    return m_recentActivity;
}

void ChannelNick::moreActive()
{
    m_recentActivity++;
}

void ChannelNick::lessActive()
{
    m_recentActivity--;
}

void ChannelNick::setTimeStamp(uint stamp)
{
  m_timeStamp = stamp;
}

#include "channelnick.moc"