Testing the new SyncObjects concept - looking good so far
[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   }
129 }
130
131 void IrcUser::setAwayMessage(const QString &awayMessage) {
132   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
133     _awayMessage = awayMessage;
134     SYNC(ARG(awayMessage))
135   }
136 }
137
138 void IrcUser::setIdleTime(const QDateTime &idleTime) {
139   if(idleTime.isValid() && _idleTime != idleTime) {
140     _idleTime = idleTime;
141     _idleTimeSet = QDateTime::currentDateTime();
142     SYNC(ARG(idleTime))
143   }
144 }
145
146 void IrcUser::setLoginTime(const QDateTime &loginTime) {
147   if(loginTime.isValid() && _loginTime != loginTime) {
148     _loginTime = loginTime;
149     SYNC(ARG(loginTime))
150   }
151 }
152
153 void IrcUser::setServer(const QString &server) {
154   if(!server.isEmpty() && _server != server) {
155     _server = server;
156     SYNC(ARG(server))
157   }
158 }
159
160 void IrcUser::setIrcOperator(const QString &ircOperator) {
161   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
162     _ircOperator = ircOperator;
163     SYNC(ARG(ircOperator))
164   }
165 }
166
167 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
168   if(lastAwayMessage > _lastAwayMessage) {
169     _lastAwayMessage = lastAwayMessage;
170     SYNC(ARG(lastAwayMessage))
171   }
172 }
173
174 void IrcUser::setHost(const QString &host) {
175   if(!host.isEmpty() && _host != host) {
176     _host = host;
177     SYNC(ARG(host))
178   }
179 }
180
181 void IrcUser::setNick(const QString &nick) {
182   if(!nick.isEmpty() && nick != _nick) {
183     _nick = nick;
184     updateObjectName();
185     SYNC(ARG(nick))
186   }
187 }
188
189 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
190   if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
191     _whoisServiceReply = whoisServiceReply;
192     SYNC(ARG(whoisServiceReply))
193   }
194 }
195
196 void IrcUser::setSuserHost(const QString &suserHost) {
197   if(!suserHost.isEmpty() && suserHost != _suserHost) {
198     _suserHost = suserHost;
199     SYNC(ARG(suserHost))
200   }
201 }
202
203 void IrcUser::updateObjectName() {
204   renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
205 }
206
207 void IrcUser::updateHostmask(const QString &mask) {
208   if(mask == hostmask())
209     return;
210
211   QString user = userFromMask(mask);
212   QString host = hostFromMask(mask);
213   setUser(user);
214   setHost(host);
215 }
216
217 void IrcUser::joinChannel(IrcChannel *channel) {
218   Q_ASSERT(channel);
219   if(!_channels.contains(channel)) {
220     _channels.insert(channel);
221     channel->joinIrcUsers(this);
222   }
223 }
224
225 void IrcUser::joinChannel(const QString &channelname) {
226   joinChannel(network()->newIrcChannel(channelname));
227 }
228
229 void IrcUser::partChannel(IrcChannel *channel) {
230   if(_channels.contains(channel)) {
231     _channels.remove(channel);
232     disconnect(channel, 0, this, 0);
233     channel->part(this);
234     emit channelParted(channel->name());
235     if(_channels.isEmpty() && !network()->isMe(this))
236       quit();
237   }
238 }
239
240 void IrcUser::partChannel(const QString &channelname) {
241   IrcChannel *channel = network()->ircChannel(channelname);
242   if(channel == 0) {
243     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
244   } else {
245     partChannel(channel);
246   }
247 }
248
249 void IrcUser::quit() {
250   QList<IrcChannel *> channels = _channels.toList();
251   _channels.clear();
252   foreach(IrcChannel *channel, channels) {
253     disconnect(channel, 0, this, 0);
254     channel->part(this);
255   }
256   network()->removeIrcUser(this);
257   emit quited();
258 }
259
260 void IrcUser::channelDestroyed() {
261   // private slot!
262   IrcChannel *channel = static_cast<IrcChannel*>(sender());
263   if(_channels.contains(channel)) {
264     _channels.remove(channel);
265     if(_channels.isEmpty() && !network()->isMe(this))
266       quit();
267   }
268 }
269
270 void IrcUser::setUserModes(const QString &modes) {
271   _userModes = modes;
272   SYNC(ARG(modes))
273 }
274
275 void IrcUser::addUserModes(const QString &modes) {
276   if(modes.isEmpty())
277     return;
278
279   for(int i = 0; i < modes.count(); i++) {
280     if(!_userModes.contains(modes[i]))
281       _userModes += modes[i];
282   }
283
284   SYNC(ARG(modes))
285 }
286
287 void IrcUser::removeUserModes(const QString &modes) {
288   if(modes.isEmpty())
289     return;
290
291   for(int i = 0; i < modes.count(); i++) {
292     _userModes.remove(modes[i]);
293   }
294   SYNC(ARG(modes))
295 }
296
297 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time) {
298   _lastActivity[buffer] = time;
299   emit lastChannelActivityUpdated(buffer, time);
300 }
301
302 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time) {
303   _lastSpokenTo[buffer] = time;
304   emit lastSpokenToUpdated(buffer, time);
305 }