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