urls are now properly identified if the topic is scrolled
[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 "signalproxy.h"
25 #include "ircchannel.h"
26
27 #include <QTextCodec>
28 #include <QDebug>
29
30 IrcUser::IrcUser(const QString &hostmask, Network *network)
31   : SyncableObject(network),
32     _initialized(false),
33     _nick(network->encodeServerString(nickFromMask(hostmask))),
34     _user(network->encodeServerString(userFromMask(hostmask))),
35     _host(network->encodeServerString(hostFromMask(hostmask))),
36     _realName(),
37     _awayMessage(),
38     _away(false),
39     _server(),
40     // _idleTime(QDateTime::currentDateTime()),
41     _ircOperator(),
42     _lastAwayMessage(0),
43     _whoisServiceReply(),
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::hostmask() const {
59   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
60 }
61
62 QDateTime IrcUser::idleTime() {
63   if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
64     _idleTime = QDateTime();
65   return _idleTime;
66 }
67
68 QStringList IrcUser::channels() const {
69   QStringList chanList;
70   IrcChannel *channel;
71   foreach(channel, _channels) {
72     chanList << channel->name();
73   }
74   return chanList;
75 }
76
77
78 void IrcUser::setCodecForEncoding(const QString &name) {
79   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
80 }
81
82 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
83   _codecForEncoding = codec;
84 }
85
86 void IrcUser::setCodecForDecoding(const QString &name) {
87   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
88 }
89
90 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
91   _codecForDecoding = codec;
92 }
93
94 QString IrcUser::decodeString(const QByteArray &text) const {
95   if(!codecForDecoding()) return network()->decodeString(text);
96   return ::decodeString(text, codecForDecoding());
97 }
98
99 QByteArray IrcUser::encodeString(const QString &string) const {
100   if(codecForEncoding()) {
101     return codecForEncoding()->fromUnicode(string);
102   }
103   return network()->encodeString(string);
104 }
105
106 // ====================
107 //  PUBLIC SLOTS:
108 // ====================
109 void IrcUser::setUser(const QString &user) {
110   if(!user.isEmpty()) {
111     QByteArray newUser = network()->encodeServerString(user);
112     if(newUser != _user) {
113       _user = newUser;
114       emit userSet(user);
115     }
116   }
117 }
118
119 void IrcUser::setRealName(const QString &realName) {
120   if(!realName.isEmpty()) {
121     QByteArray newRealName = network()->encodeServerString(realName);
122     if(newRealName != _realName) {
123       _realName = newRealName;
124       emit realNameSet(realName);
125     }
126   }
127 }
128
129 void IrcUser::setAway(const bool &away) {
130   if(away != _away) {
131     _away = away;
132     emit awaySet(away);
133   }
134 }
135
136 void IrcUser::setAwayMessage(const QString &awayMessage) {
137   if(!awayMessage.isEmpty()) {
138     QByteArray newAwayMessage = network()->encodeServerString(awayMessage);
139     if(newAwayMessage != _awayMessage) {
140       _awayMessage = newAwayMessage;
141       emit awayMessageSet(awayMessage);
142     }
143   }
144 }
145
146 void IrcUser::setIdleTime(const QDateTime &idleTime) {
147   if(idleTime.isValid() && _idleTime != idleTime) {
148     _idleTime = idleTime;
149     _idleTimeSet = QDateTime::currentDateTime();
150     emit idleTimeSet(idleTime);
151   }
152 }
153
154 void IrcUser::setLoginTime(const QDateTime &loginTime) {
155   if(loginTime.isValid() && _loginTime != loginTime) {
156     _loginTime = loginTime;
157     emit loginTimeSet(loginTime);
158   }
159 }
160
161 void IrcUser::setServer(const QString &server) {
162   if(!server.isEmpty() && _server != server) {
163     _server = server;
164     emit serverSet(server);
165   }
166 }
167
168 void IrcUser::setIrcOperator(const QString &ircOperator) {
169   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
170     _ircOperator = ircOperator;
171     emit ircOperatorSet(ircOperator);
172   }
173 }
174
175 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
176   if(lastAwayMessage > _lastAwayMessage) {
177     _lastAwayMessage = lastAwayMessage;
178     emit lastAwayMessageSet(lastAwayMessage);
179   }
180 }
181
182 void IrcUser::setHost(const QString &host) {
183   if(!host.isEmpty()) {
184     QByteArray newHost = network()->encodeServerString(host);
185     if(newHost != _host) {
186       _host = newHost;
187       emit hostSet(host);
188     }
189   }
190 }
191
192 void IrcUser::setNick(const QString &nick) {
193   if(!nick.isEmpty()) {
194     QByteArray newNick = network()->encodeServerString(nick);
195     if(newNick != _nick) {
196       _nick = newNick;
197       updateObjectName();
198       emit nickSet(nick);
199     }
200   }
201 }
202
203 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
204   if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
205     _whoisServiceReply = whoisServiceReply;
206     emit whoisServiceReplySet(whoisServiceReply);
207   }
208 }
209
210 void IrcUser::setSuserHost(const QString &suserHost) {
211   if(!suserHost.isEmpty() && suserHost != _suserHost) {
212     _suserHost = suserHost;
213     emit suserHostSet(suserHost);
214   }
215 }
216
217 void IrcUser::updateObjectName() {
218   renameObject(QString::number(network()->networkId().toInt()) + "/" + nick());
219 }
220
221 void IrcUser::updateHostmask(const QString &mask) {
222   if(mask == hostmask())
223     return;
224
225   QString user = userFromMask(mask);
226   QString host = hostFromMask(mask);
227   setUser(user);
228   setHost(host);
229 }
230
231 void IrcUser::joinChannel(IrcChannel *channel) {
232   Q_ASSERT(channel);
233   if(!_channels.contains(channel)) {
234     _channels.insert(channel);
235     channel->joinIrcUsers(this);
236     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
237   }
238 }
239
240 void IrcUser::joinChannel(const QString &channelname) {
241   joinChannel(network()->newIrcChannel(channelname));
242 }
243
244 void IrcUser::partChannel(IrcChannel *channel) {
245   if(_channels.contains(channel)) {
246     _channels.remove(channel);
247     disconnect(channel, 0, this, 0);
248     channel->part(this);
249     emit channelParted(channel->name());
250     if(_channels.isEmpty() && !network()->isMe(this))
251       deleteLater();
252   }
253 }
254
255 void IrcUser::partChannel(const QString &channelname) {
256   IrcChannel *channel = network()->ircChannel(channelname);
257   if(channel == 0) {
258     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
259   } else {
260     partChannel(channel);
261   }
262 }
263
264 void IrcUser::channelDestroyed() {
265   // private slot!
266   IrcChannel *channel = static_cast<IrcChannel*>(sender());
267   if(_channels.contains(channel)) {
268     _channels.remove(channel);
269     if(_channels.isEmpty())
270       deleteLater();
271   }
272 }
273
274 void IrcUser::setUserModes(const QString &modes) {
275   _userModes = modes;
276   emit userModesSet(modes);
277 }
278
279 void IrcUser::addUserModes(const QString &modes) {
280   if(modes.isEmpty())
281     return;
282
283   for(int i = 0; i < modes.count(); i++) {
284     if(!_userModes.contains(modes[i]))
285       _userModes += modes[i];
286   }
287
288   emit userModesAdded(modes);
289 }
290
291 void IrcUser::removeUserModes(const QString &modes) {
292   if(modes.isEmpty())
293     return;
294
295   for(int i = 0; i < modes.count(); i++) {
296     _userModes.remove(modes[i]);
297   }
298   emit userModesRemoved(modes);
299 }