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