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