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