fix timestamp in Russian day change message
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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
53 IrcUser::~IrcUser()
54 {
55 }
56
57
58 // ====================
59 //  PUBLIC:
60 // ====================
61
62 QString IrcUser::hostmask() const
63 {
64     return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
65 }
66
67
68 QDateTime IrcUser::idleTime()
69 {
70     if (QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
71         _idleTime = QDateTime();
72     return _idleTime;
73 }
74
75
76 QStringList IrcUser::channels() const
77 {
78     QStringList chanList;
79     IrcChannel *channel;
80     foreach(channel, _channels) {
81         chanList << channel->name();
82     }
83     return chanList;
84 }
85
86
87 void IrcUser::setCodecForEncoding(const QString &name)
88 {
89     setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
90 }
91
92
93 void IrcUser::setCodecForEncoding(QTextCodec *codec)
94 {
95     _codecForEncoding = codec;
96 }
97
98
99 void IrcUser::setCodecForDecoding(const QString &name)
100 {
101     setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
102 }
103
104
105 void IrcUser::setCodecForDecoding(QTextCodec *codec)
106 {
107     _codecForDecoding = codec;
108 }
109
110
111 QString IrcUser::decodeString(const QByteArray &text) const
112 {
113     if (!codecForDecoding()) return network()->decodeString(text);
114     return ::decodeString(text, codecForDecoding());
115 }
116
117
118 QByteArray IrcUser::encodeString(const QString &string) const
119 {
120     if (codecForEncoding()) {
121         return codecForEncoding()->fromUnicode(string);
122     }
123     return network()->encodeString(string);
124 }
125
126
127 // ====================
128 //  PUBLIC SLOTS:
129 // ====================
130 void IrcUser::setUser(const QString &user)
131 {
132     if (!user.isEmpty() && _user != user) {
133         _user = user;
134         SYNC(ARG(user));
135     }
136 }
137
138
139 void IrcUser::setRealName(const QString &realName)
140 {
141     if (!realName.isEmpty() && _realName != realName) {
142         _realName = realName;
143         SYNC(ARG(realName))
144     }
145 }
146
147
148 void IrcUser::setAway(const bool &away)
149 {
150     if (away != _away) {
151         _away = away;
152         SYNC(ARG(away))
153         emit awaySet(away);
154     }
155 }
156
157
158 void IrcUser::setAwayMessage(const QString &awayMessage)
159 {
160     if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
161         _awayMessage = awayMessage;
162         SYNC(ARG(awayMessage))
163     }
164 }
165
166
167 void IrcUser::setIdleTime(const QDateTime &idleTime)
168 {
169     if (idleTime.isValid() && _idleTime != idleTime) {
170         _idleTime = idleTime;
171         _idleTimeSet = QDateTime::currentDateTime();
172         SYNC(ARG(idleTime))
173     }
174 }
175
176
177 void IrcUser::setLoginTime(const QDateTime &loginTime)
178 {
179     if (loginTime.isValid() && _loginTime != loginTime) {
180         _loginTime = loginTime;
181         SYNC(ARG(loginTime))
182     }
183 }
184
185
186 void IrcUser::setServer(const QString &server)
187 {
188     if (!server.isEmpty() && _server != server) {
189         _server = server;
190         SYNC(ARG(server))
191     }
192 }
193
194
195 void IrcUser::setIrcOperator(const QString &ircOperator)
196 {
197     if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
198         _ircOperator = ircOperator;
199         SYNC(ARG(ircOperator))
200     }
201 }
202
203
204 void IrcUser::setLastAwayMessage(const int &lastAwayMessage)
205 {
206     if (lastAwayMessage > _lastAwayMessage) {
207         _lastAwayMessage = lastAwayMessage;
208         SYNC(ARG(lastAwayMessage))
209     }
210 }
211
212
213 void IrcUser::setHost(const QString &host)
214 {
215     if (!host.isEmpty() && _host != host) {
216         _host = host;
217         SYNC(ARG(host))
218     }
219 }
220
221
222 void IrcUser::setNick(const QString &nick)
223 {
224     if (!nick.isEmpty() && nick != _nick) {
225         _nick = nick;
226         updateObjectName();
227         SYNC(ARG(nick))
228         emit nickSet(nick);
229     }
230 }
231
232
233 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
234 {
235     if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
236         _whoisServiceReply = whoisServiceReply;
237         SYNC(ARG(whoisServiceReply))
238     }
239 }
240
241
242 void IrcUser::setSuserHost(const QString &suserHost)
243 {
244     if (!suserHost.isEmpty() && suserHost != _suserHost) {
245         _suserHost = suserHost;
246         SYNC(ARG(suserHost))
247     }
248 }
249
250
251 void IrcUser::updateObjectName()
252 {
253     renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
254 }
255
256
257 void IrcUser::updateHostmask(const QString &mask)
258 {
259     if (mask == hostmask())
260         return;
261
262     QString user = userFromMask(mask);
263     QString host = hostFromMask(mask);
264     setUser(user);
265     setHost(host);
266 }
267
268
269 void IrcUser::joinChannel(IrcChannel *channel)
270 {
271     Q_ASSERT(channel);
272     if (!_channels.contains(channel)) {
273         _channels.insert(channel);
274         channel->joinIrcUser(this);
275     }
276 }
277
278
279 void IrcUser::joinChannel(const QString &channelname)
280 {
281     joinChannel(network()->newIrcChannel(channelname));
282 }
283
284
285 void IrcUser::partChannel(IrcChannel *channel)
286 {
287     if (_channels.contains(channel)) {
288         _channels.remove(channel);
289         disconnect(channel, 0, this, 0);
290         channel->part(this);
291         QString channelName = channel->name();
292         SYNC_OTHER(partChannel, ARG(channelName))
293         if (_channels.isEmpty() && !network()->isMe(this))
294             quit();
295     }
296 }
297
298
299 void IrcUser::partChannel(const QString &channelname)
300 {
301     IrcChannel *channel = network()->ircChannel(channelname);
302     if (channel == 0) {
303         qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
304     }
305     else {
306         partChannel(channel);
307     }
308 }
309
310
311 void IrcUser::quit()
312 {
313     QList<IrcChannel *> channels = _channels.toList();
314     _channels.clear();
315     foreach(IrcChannel *channel, channels) {
316         disconnect(channel, 0, this, 0);
317         channel->part(this);
318     }
319     network()->removeIrcUser(this);
320     SYNC(NO_ARG)
321     emit quited();
322 }
323
324
325 void IrcUser::channelDestroyed()
326 {
327     // private slot!
328     IrcChannel *channel = static_cast<IrcChannel *>(sender());
329     if (_channels.contains(channel)) {
330         _channels.remove(channel);
331         if (_channels.isEmpty() && !network()->isMe(this))
332             quit();
333     }
334 }
335
336
337 void IrcUser::setUserModes(const QString &modes)
338 {
339     _userModes = modes;
340     SYNC(ARG(modes))
341     emit userModesSet(modes);
342 }
343
344
345 void IrcUser::addUserModes(const QString &modes)
346 {
347     if (modes.isEmpty())
348         return;
349
350     for (int i = 0; i < modes.count(); i++) {
351         if (!_userModes.contains(modes[i]))
352             _userModes += modes[i];
353     }
354
355     SYNC(ARG(modes))
356     emit userModesAdded(modes);
357 }
358
359
360 void IrcUser::removeUserModes(const QString &modes)
361 {
362     if (modes.isEmpty())
363         return;
364
365     for (int i = 0; i < modes.count(); i++) {
366         _userModes.remove(modes[i]);
367     }
368     SYNC(ARG(modes))
369     emit userModesRemoved(modes);
370 }
371
372
373 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
374 {
375     _lastActivity[buffer] = time;
376     emit lastChannelActivityUpdated(buffer, time);
377 }
378
379
380 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
381 {
382     _lastSpokenTo[buffer] = time;
383     emit lastSpokenToUpdated(buffer, time);
384 }