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