4c12580d5f3b4702b79670042cc0daf913188e6e
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "ircuser.h"
22 #include "util.h"
23
24 #include "network.h"
25 #include "signalproxy.h"
26 #include "ircchannel.h"
27
28 #include <QTextCodec>
29 #include <QDebug>
30
31 IrcUser::IrcUser(const QString &hostmask, Network *network) : SyncableObject(network),
32     _initialized(false),
33     _nick(nickFromMask(hostmask)),
34     _user(userFromMask(hostmask)),
35     _host(hostFromMask(hostmask)),
36     _realName(),
37     _awayMessage(),
38     _away(false),
39     _server(),
40     _idleTime(QDateTime::currentDateTime()),
41     _ircOperator(),
42     _lastAwayMessage(0),
43     _network(network),
44     _codecForEncoding(0),
45     _codecForDecoding(0)
46 {
47   updateObjectName();
48 }
49
50 IrcUser::~IrcUser() {
51 }
52
53 // ====================
54 //  PUBLIC:
55 // ====================
56
57 QString IrcUser::user() const {
58   return _user;
59 }
60
61 QString IrcUser::host() const {
62   return _host;
63 }
64
65 QString IrcUser::nick() const {
66   return _nick;
67 }
68
69 QString IrcUser::realName() const {
70   return _realName;
71 }
72
73 QString IrcUser::hostmask() const {
74   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
75 }
76
77 bool IrcUser::isAway() const {
78   return _away;
79 }
80
81 QString IrcUser::awayMessage() const {
82   return _awayMessage;
83 }
84
85 QString IrcUser::server() const {
86   return _server;
87 }
88
89 QDateTime IrcUser::idleTime() const {
90   return _idleTime;
91 }
92
93 QString IrcUser::ircOperator() const {
94   return _ircOperator;
95 }
96
97 int IrcUser::lastAwayMessage() const {
98   return _lastAwayMessage;
99 }
100
101 QString IrcUser::userModes() const {
102   return _userModes;
103 }
104
105 QStringList IrcUser::channels() const {
106   QStringList chanList;
107   IrcChannel *channel;
108   foreach(channel, _channels) {
109     chanList << channel->name();
110   }
111   return chanList;
112 }
113
114 Network* IrcUser::network() const {
115   return _network;
116 }
117
118 QTextCodec *IrcUser::codecForEncoding() const {
119   return _codecForEncoding;
120 }
121
122 void IrcUser::setCodecForEncoding(const QString &name) {
123   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
124 }
125
126 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
127   _codecForEncoding = codec;
128 }
129
130 QTextCodec *IrcUser::codecForDecoding() const {
131   return _codecForDecoding;
132 }
133
134 void IrcUser::setCodecForDecoding(const QString &name) {
135   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
136 }
137
138 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
139   _codecForDecoding = codec;
140 }
141
142 QString IrcUser::decodeString(const QByteArray &text) const {
143   if(!codecForDecoding()) return network()->decodeString(text);
144   return ::decodeString(text, codecForDecoding());
145 }
146
147 QByteArray IrcUser::encodeString(const QString &string) const {
148   if(codecForEncoding()) {
149     return codecForEncoding()->fromUnicode(string);
150   }
151   return network()->encodeString(string);
152 }
153
154 // ====================
155 //  PUBLIC SLOTS:
156 // ====================
157 void IrcUser::setUser(const QString &user) {
158   if(!user.isEmpty() && _user != user) {
159     _user = user;
160     emit userSet(user);
161   }
162 }
163
164 void IrcUser::setRealName(const QString &realName) {
165   if (!realName.isEmpty() && _realName != realName) {
166     _realName = realName;
167     emit realNameSet(realName);
168   }
169 }
170
171 void IrcUser::setAway(const bool &away) {
172   if(away != _away) {
173     _away = away;
174     emit awaySet(away);
175   }
176 }
177
178 void IrcUser::setAwayMessage(const QString &awayMessage) {
179   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
180     _awayMessage = awayMessage;
181     emit awayMessageSet(awayMessage);
182   }
183 }
184
185 void IrcUser::setIdleTime(const QDateTime &idleTime) {
186   if(idleTime.isValid() && _idleTime != idleTime) {
187     _idleTime = idleTime;
188     emit idleTimeSet(idleTime);
189   }
190 }
191
192 void IrcUser::setServer(const QString &server) {
193   if(!server.isEmpty() && _server != server) {
194     _server = server;
195     emit serverSet(server);
196   }
197 }
198
199 void IrcUser::setIrcOperator(const QString &ircOperator) {
200   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
201     _ircOperator = ircOperator;
202     emit ircOperatorSet(ircOperator);
203   }
204 }
205
206 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
207   if(lastAwayMessage > _lastAwayMessage) {
208     _lastAwayMessage = lastAwayMessage;
209     emit lastAwayMessageSet(lastAwayMessage);
210   }
211 }
212
213 void IrcUser::setHost(const QString &host) {
214   if(!host.isEmpty() && _host != host) {
215     _host = host;
216     emit hostSet(host);
217   }
218 }
219
220 void IrcUser::setNick(const QString &nick) {
221   if(!nick.isEmpty() && nick != _nick) {
222     _nick = nick;
223     updateObjectName();
224     emit nickSet(nick);
225   }
226 }
227
228 void IrcUser::updateObjectName() {
229   renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
230 }
231
232 void IrcUser::updateHostmask(const QString &mask) {
233   if(mask == hostmask())
234     return;
235
236   QString user = userFromMask(mask);
237   QString host = hostFromMask(mask);
238   setUser(user);
239   setHost(host);
240 }
241
242 void IrcUser::joinChannel(IrcChannel *channel) {
243   Q_ASSERT(channel);
244   if(!_channels.contains(channel)) {
245     _channels.insert(channel);
246     channel->joinIrcUsers(this);
247     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
248   }
249 }
250
251 void IrcUser::joinChannel(const QString &channelname) {
252   joinChannel(network()->newIrcChannel(channelname));
253 }
254
255 void IrcUser::partChannel(IrcChannel *channel) {
256   if(_channels.contains(channel)) {
257     _channels.remove(channel);
258     disconnect(channel, 0, this, 0);
259     channel->part(this);
260     emit channelParted(channel->name());
261     if(_channels.isEmpty() && network()->isMe(this))
262       deleteLater();
263   }
264 }
265
266 void IrcUser::partChannel(const QString &channelname) {
267   IrcChannel *channel = network()->ircChannel(channelname);
268   if(channel == 0) {
269     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
270   } else {
271     partChannel(channel);
272   }
273 }
274
275 void IrcUser::channelDestroyed() {
276   // private slot!
277   IrcChannel *channel = static_cast<IrcChannel*>(sender());
278   if(_channels.contains(channel)) {
279     _channels.remove(channel);
280     if(_channels.isEmpty())
281       deleteLater();
282   }
283 }
284
285 void IrcUser::setUserModes(const QString &modes) {
286   _userModes = modes;
287   emit userModesSet(modes);
288 }
289
290 void IrcUser::addUserMode(const QString &mode) {
291   if(!_userModes.contains(mode)) {
292     _userModes += mode;
293     emit userModeAdded(mode);
294   }
295 }
296
297 void IrcUser::removeUserMode(const QString &mode) {
298   if(_userModes.contains(mode)) {
299     _userModes.remove(mode);
300     emit userModeRemoved(mode);
301   }
302 }
303
304 void IrcUser::initSetChannels(const QStringList channels) {
305   foreach(QString channel, channels) {
306     joinChannel(channel);
307   }
308 }
309