Close notifications on other connected clients as well
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 INIT_SYNCABLE_OBJECT(IrcUser)
32 IrcUser::IrcUser(const QString &hostmask, Network *network) : 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     _whoisServiceReply(),
45     _network(network),
46     _codecForEncoding(0),
47     _codecForDecoding(0)
48 {
49   updateObjectName();
50 }
51
52 IrcUser::~IrcUser() {
53 }
54
55 // ====================
56 //  PUBLIC:
57 // ====================
58
59 QString IrcUser::hostmask() const {
60   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
61 }
62
63 QDateTime IrcUser::idleTime() {
64   if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
65     _idleTime = QDateTime();
66   return _idleTime;
67 }
68
69 QStringList IrcUser::channels() const {
70   QStringList chanList;
71   IrcChannel *channel;
72   foreach(channel, _channels) {
73     chanList << channel->name();
74   }
75   return chanList;
76 }
77
78
79 void IrcUser::setCodecForEncoding(const QString &name) {
80   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
81 }
82
83 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
84   _codecForEncoding = codec;
85 }
86
87 void IrcUser::setCodecForDecoding(const QString &name) {
88   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
89 }
90
91 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
92   _codecForDecoding = codec;
93 }
94
95 QString IrcUser::decodeString(const QByteArray &text) const {
96   if(!codecForDecoding()) return network()->decodeString(text);
97   return ::decodeString(text, codecForDecoding());
98 }
99
100 QByteArray IrcUser::encodeString(const QString &string) const {
101   if(codecForEncoding()) {
102     return codecForEncoding()->fromUnicode(string);
103   }
104   return network()->encodeString(string);
105 }
106
107 // ====================
108 //  PUBLIC SLOTS:
109 // ====================
110 void IrcUser::setUser(const QString &user) {
111   if(!user.isEmpty() && _user != user) {
112     _user = user;
113     SYNC(ARG(user));
114   }
115 }
116
117 void IrcUser::setRealName(const QString &realName) {
118   if (!realName.isEmpty() && _realName != realName) {
119     _realName = realName;
120     SYNC(ARG(realName))
121   }
122 }
123
124 void IrcUser::setAway(const bool &away) {
125   if(away != _away) {
126     _away = away;
127     SYNC(ARG(away))
128     emit awaySet(away);
129   }
130 }
131
132 void IrcUser::setAwayMessage(const QString &awayMessage) {
133   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
134     _awayMessage = awayMessage;
135     SYNC(ARG(awayMessage))
136   }
137 }
138
139 void IrcUser::setIdleTime(const QDateTime &idleTime) {
140   if(idleTime.isValid() && _idleTime != idleTime) {
141     _idleTime = idleTime;
142     _idleTimeSet = QDateTime::currentDateTime();
143     SYNC(ARG(idleTime))
144   }
145 }
146
147 void IrcUser::setLoginTime(const QDateTime &loginTime) {
148   if(loginTime.isValid() && _loginTime != loginTime) {
149     _loginTime = loginTime;
150     SYNC(ARG(loginTime))
151   }
152 }
153
154 void IrcUser::setServer(const QString &server) {
155   if(!server.isEmpty() && _server != server) {
156     _server = server;
157     SYNC(ARG(server))
158   }
159 }
160
161 void IrcUser::setIrcOperator(const QString &ircOperator) {
162   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
163     _ircOperator = ircOperator;
164     SYNC(ARG(ircOperator))
165   }
166 }
167
168 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
169   if(lastAwayMessage > _lastAwayMessage) {
170     _lastAwayMessage = lastAwayMessage;
171     SYNC(ARG(lastAwayMessage))
172   }
173 }
174
175 void IrcUser::setHost(const QString &host) {
176   if(!host.isEmpty() && _host != host) {
177     _host = host;
178     SYNC(ARG(host))
179   }
180 }
181
182 void IrcUser::setNick(const QString &nick) {
183   if(!nick.isEmpty() && nick != _nick) {
184     _nick = nick;
185     updateObjectName();
186     SYNC(ARG(nick))
187     emit nickSet(nick);
188   }
189 }
190
191 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
192   if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
193     _whoisServiceReply = whoisServiceReply;
194     SYNC(ARG(whoisServiceReply))
195   }
196 }
197
198 void IrcUser::setSuserHost(const QString &suserHost) {
199   if(!suserHost.isEmpty() && suserHost != _suserHost) {
200     _suserHost = suserHost;
201     SYNC(ARG(suserHost))
202   }
203 }
204
205 void IrcUser::updateObjectName() {
206   renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
207 }
208
209 void IrcUser::updateHostmask(const QString &mask) {
210   if(mask == hostmask())
211     return;
212
213   QString user = userFromMask(mask);
214   QString host = hostFromMask(mask);
215   setUser(user);
216   setHost(host);
217 }
218
219 void IrcUser::joinChannel(IrcChannel *channel) {
220   Q_ASSERT(channel);
221   if(!_channels.contains(channel)) {
222     _channels.insert(channel);
223     channel->joinIrcUser(this);
224   }
225 }
226
227 void IrcUser::joinChannel(const QString &channelname) {
228   joinChannel(network()->newIrcChannel(channelname));
229 }
230
231 void IrcUser::partChannel(IrcChannel *channel) {
232   if(_channels.contains(channel)) {
233     _channels.remove(channel);
234     disconnect(channel, 0, this, 0);
235     channel->part(this);
236     QString channelName = channel->name();
237     SYNC_OTHER(partChannel, ARG(channelName))
238     if(_channels.isEmpty() && !network()->isMe(this))
239       quit();
240   }
241 }
242
243 void IrcUser::partChannel(const QString &channelname) {
244   IrcChannel *channel = network()->ircChannel(channelname);
245   if(channel == 0) {
246     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
247   } else {
248     partChannel(channel);
249   }
250 }
251
252 void IrcUser::quit() {
253   QList<IrcChannel *> channels = _channels.toList();
254   _channels.clear();
255   foreach(IrcChannel *channel, channels) {
256     disconnect(channel, 0, this, 0);
257     channel->part(this);
258   }
259   network()->removeIrcUser(this);
260   SYNC(NO_ARG)
261   emit quited();
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() && !network()->isMe(this))
270       quit();
271   }
272 }
273
274 void IrcUser::setUserModes(const QString &modes) {
275   _userModes = modes;
276   SYNC(ARG(modes))
277   emit userModesSet(modes);
278 }
279
280 void IrcUser::addUserModes(const QString &modes) {
281   if(modes.isEmpty())
282     return;
283
284   for(int i = 0; i < modes.count(); i++) {
285     if(!_userModes.contains(modes[i]))
286       _userModes += modes[i];
287   }
288
289   SYNC(ARG(modes))
290   emit userModesAdded(modes);
291 }
292
293 void IrcUser::removeUserModes(const QString &modes) {
294   if(modes.isEmpty())
295     return;
296
297   for(int i = 0; i < modes.count(); i++) {
298     _userModes.remove(modes[i]);
299   }
300   SYNC(ARG(modes))
301   emit userModesRemoved(modes);
302 }
303
304 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time) {
305   _lastActivity[buffer] = time;
306   emit lastChannelActivityUpdated(buffer, time);
307 }
308
309 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time) {
310   _lastSpokenTo[buffer] = time;
311   emit lastSpokenToUpdated(buffer, time);
312 }