fixed a bug in IrcChannel. Upgrade is strongly advised! Distclean as usual.
[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)
32   : SyncableObject(network),
33     _initialized(false),
34     _nick(nickFromMask(hostmask)),
35     _user(userFromMask(hostmask)),
36     _host(hostFromMask(hostmask)),
37     _realName(),
38     _awayMessage(),
39     _away(false),
40     _server(),
41     _idleTime(QDateTime::currentDateTime()),
42     _ircOperator(),
43     _lastAwayMessage(0),
44     _network(network),
45     _codecForEncoding(0),
46     _codecForDecoding(0)
47 {
48   updateObjectName();
49 }
50
51 IrcUser::~IrcUser() {
52   QList<IrcChannel *> channels = _channels.toList();
53   foreach(IrcChannel *channel, channels) {
54     partChannel(channel);
55   }
56 }
57
58 // ====================
59 //  PUBLIC:
60 // ====================
61
62 QString IrcUser::user() const {
63   return _user;
64 }
65
66 QString IrcUser::host() const {
67   return _host;
68 }
69
70 QString IrcUser::nick() const {
71   return _nick;
72 }
73
74 QString IrcUser::realName() const {
75   return _realName;
76 }
77
78 QString IrcUser::hostmask() const {
79   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
80 }
81
82 bool IrcUser::isAway() const {
83   return _away;
84 }
85
86 QString IrcUser::awayMessage() const {
87   return _awayMessage;
88 }
89
90 QString IrcUser::server() const {
91   return _server;
92 }
93
94 QDateTime IrcUser::idleTime() const {
95   return _idleTime;
96 }
97
98 QString IrcUser::ircOperator() const {
99   return _ircOperator;
100 }
101
102 int IrcUser::lastAwayMessage() const {
103   return _lastAwayMessage;
104 }
105
106 QString IrcUser::userModes() const {
107   return _userModes;
108 }
109
110 QStringList IrcUser::channels() const {
111   QStringList chanList;
112   IrcChannel *channel;
113   foreach(channel, _channels) {
114     chanList << channel->name();
115   }
116   return chanList;
117 }
118
119 Network* IrcUser::network() const {
120   return _network;
121 }
122
123 QTextCodec *IrcUser::codecForEncoding() const {
124   return _codecForEncoding;
125 }
126
127 void IrcUser::setCodecForEncoding(const QString &name) {
128   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
129 }
130
131 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
132   _codecForEncoding = codec;
133 }
134
135 QTextCodec *IrcUser::codecForDecoding() const {
136   return _codecForDecoding;
137 }
138
139 void IrcUser::setCodecForDecoding(const QString &name) {
140   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
141 }
142
143 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
144   _codecForDecoding = codec;
145 }
146
147 QString IrcUser::decodeString(const QByteArray &text) const {
148   if(!codecForDecoding()) return network()->decodeString(text);
149   return ::decodeString(text, codecForDecoding());
150 }
151
152 QByteArray IrcUser::encodeString(const QString string) const {
153   if(codecForEncoding()) {
154     return codecForEncoding()->fromUnicode(string);
155   }
156   return network()->encodeString(string);
157 }
158
159 // ====================
160 //  PUBLIC SLOTS:
161 // ====================
162 void IrcUser::setUser(const QString &user) {
163   if(!user.isEmpty() && _user != user) {
164     _user = user;
165     emit userSet(user);
166   }
167 }
168
169 void IrcUser::setRealName(const QString &realName) {
170   if (!realName.isEmpty() && _realName != realName) {
171     _realName = realName;
172     emit realNameSet(realName);
173   }
174 }
175
176 void IrcUser::setAway(const bool &away) {
177   if(away != _away) {
178     _away = away;
179     emit awaySet(away);
180   }
181 }
182
183 void IrcUser::setAwayMessage(const QString &awayMessage) {
184   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
185     _awayMessage = awayMessage;
186     emit awayMessageSet(awayMessage);
187   }
188 }
189
190 void IrcUser::setIdleTime(const QDateTime &idleTime) {
191   if(idleTime.isValid() && _idleTime != idleTime) {
192     _idleTime = idleTime;
193     emit idleTimeSet(idleTime);
194   }
195 }
196
197 void IrcUser::setServer(const QString &server) {
198   if(!server.isEmpty() && _server != server) {
199     _server = server;
200     emit serverSet(server);
201   }
202 }
203
204 void IrcUser::setIrcOperator(const QString &ircOperator) {
205   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
206     _ircOperator = ircOperator;
207     emit ircOperatorSet(ircOperator);
208   }
209 }
210
211 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
212   if(lastAwayMessage > _lastAwayMessage) {
213     _lastAwayMessage = lastAwayMessage;
214     emit lastAwayMessageSet(lastAwayMessage);
215   }
216 }
217
218 void IrcUser::setHost(const QString &host) {
219   if(!host.isEmpty() && _host != host) {
220     _host = host;
221     emit hostSet(host);
222   }
223 }
224
225 void IrcUser::setNick(const QString &nick) {
226   if(!nick.isEmpty() && nick != _nick) {
227     _nick = nick;
228     updateObjectName();
229     emit nickSet(nick);
230   }
231 }
232
233 void IrcUser::updateObjectName() {
234   QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
235   QString oldName = objectName();
236   if(oldName != newName) {
237     setObjectName(newName);
238     emit renameObject(oldName, newName);
239   }
240 }
241
242 void IrcUser::updateHostmask(const QString &mask) {
243   if(mask == hostmask())
244     return;
245
246   QString user = userFromMask(mask);
247   QString host = hostFromMask(mask);
248   setUser(user);
249   setHost(host);
250 }
251
252 void IrcUser::joinChannel(IrcChannel *channel) {
253   Q_ASSERT(channel);
254   if(!_channels.contains(channel)) {
255     _channels.insert(channel);
256     channel->join(this);
257     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
258     emit channelJoined(channel->name());
259   }
260 }
261
262 void IrcUser::joinChannel(const QString &channelname) {
263   joinChannel(network()->newIrcChannel(channelname));
264 }
265
266 void IrcUser::partChannel(IrcChannel *channel) {
267   if(_channels.contains(channel)) {
268     _channels.remove(channel);
269     disconnect(channel, 0, this, 0);
270     channel->part(this);
271     emit channelParted(channel->name());
272   }
273 }
274
275 void IrcUser::partChannel(const QString &channelname) {
276   IrcChannel *channel = network()->ircChannel(channelname);
277   if(channel == 0) {
278     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
279   } else {
280     partChannel(channel);
281   }
282 }
283
284 void IrcUser::channelDestroyed() {
285   // private slot!
286   IrcChannel *channel = static_cast<IrcChannel*>(sender());
287   Q_ASSERT(channel);
288   if(_channels.contains(channel)) {
289     _channels.remove(channel);
290     disconnect(channel, 0, this, 0);
291   }
292 }
293
294 void IrcUser::setUserModes(const QString &modes) {
295   _userModes = modes;
296   emit userModesSet(modes);
297 }
298
299 void IrcUser::addUserMode(const QString &mode) {
300   if(!_userModes.contains(mode)) {
301     _userModes += mode;
302     emit userModeAdded(mode);
303   }
304 }
305
306 void IrcUser::removeUserMode(const QString &mode) {
307   if(_userModes.contains(mode)) {
308     _userModes.remove(mode);
309     emit userModeRemoved(mode);
310   }
311 }
312
313 void IrcUser::initSetChannels(const QStringList channels) {
314   foreach(QString channel, channels) {
315     joinChannel(channel);
316   }
317 }
318