My X-Mas present to you: partially working encodings! \o/
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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 "networkinfo.h"
25 #include "signalproxy.h"
26 #include "ircchannel.h"
27
28 #include <QTextCodec>
29 #include <QDebug>
30
31 IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
32   : QObject(networkinfo),
33     _initialized(false),
34     _nick(nickFromMask(hostmask)),
35     _user(userFromMask(hostmask)),
36     _host(hostFromMask(hostmask)),
37     networkInfo(networkinfo),
38     _codecForEncoding(0),
39     _codecForDecoding(0)
40 {
41   updateObjectName();
42 }
43
44 IrcUser::~IrcUser() {
45   //qDebug() << nick() << "destroyed.";
46 }
47
48 // ====================
49 //  PUBLIC:
50 // ====================
51 bool IrcUser::initialized() const {
52   return _initialized;
53 }
54
55 QString IrcUser::user() const {
56   return _user;
57 }
58
59 QString IrcUser::host() const {
60   return _host;
61 }
62
63 QString IrcUser::nick() const {
64   return _nick;
65 }
66
67 QString IrcUser::hostmask() const {
68   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
69 }
70
71 QString IrcUser::userModes() const {
72   return _userModes;
73 }
74
75 QStringList IrcUser::channels() const {
76   QStringList chanList;
77   IrcChannel *channel;
78   foreach(channel, _channels) {
79     chanList << channel->name();
80   }
81   return chanList;
82 }
83
84 QTextCodec *IrcUser::codecForEncoding() const {
85   return _codecForEncoding;
86 }
87
88 void IrcUser::setCodecForEncoding(const QString &name) {
89   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
90 }
91
92 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
93   _codecForEncoding = codec;
94 }
95
96 QTextCodec *IrcUser::codecForDecoding() const {
97   return _codecForDecoding;
98 }
99
100 void IrcUser::setCodecForDecoding(const QString &name) {
101   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
102 }
103
104 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
105   _codecForDecoding = codec;
106 }
107
108 QString IrcUser::decodeString(const QByteArray &text) const {
109   if(!codecForDecoding()) return networkInfo->decodeString(text);
110   return ::decodeString(text, codecForDecoding());
111 }
112
113 QByteArray IrcUser::encodeString(const QString string) const {
114   if(codecForEncoding()) {
115     return codecForEncoding()->fromUnicode(string);
116   }
117   return networkInfo->encodeString(string);
118 }
119
120 // ====================
121 //  PUBLIC SLOTS:
122 // ====================
123 void IrcUser::setUser(const QString &user) {
124   if(!user.isEmpty() && _user != user) {
125     _user = user;
126     emit userSet(user);
127   }
128 }
129
130 void IrcUser::setHost(const QString &host) {
131   if(!host.isEmpty() && _host != host) {
132     _host = host;
133     emit hostSet(host);
134   }
135 }
136
137 void IrcUser::setNick(const QString &nick) {
138   if(!nick.isEmpty() && nick != _nick) {
139     QString oldnick(_nick);
140     _nick = nick;
141     updateObjectName();
142     emit nickSet(nick);
143   }
144 }
145
146 void IrcUser::updateObjectName() {
147   QString oldName(objectName());
148   setObjectName(QString::number(networkInfo->networkId()) + "/" + _nick);
149   if(!oldName.isEmpty()) {
150     emit renameObject(oldName, objectName());
151   }
152 }
153
154
155 void IrcUser::updateHostmask(const QString &mask) {
156   if(mask == hostmask())
157     return;
158
159   QString user = userFromMask(mask);
160   QString host = hostFromMask(mask);
161   setUser(user);
162   setHost(host);
163 }
164
165 void IrcUser::joinChannel(IrcChannel *channel) {
166   Q_ASSERT(channel);
167   if(!_channels.contains(channel)) {
168     channel->join(this);
169     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
170     _channels.insert(channel);
171     emit channelJoined(channel->name());
172   }
173 }
174
175 void IrcUser::joinChannel(const QString &channelname) {
176   joinChannel(networkInfo->newIrcChannel(channelname));
177 }
178
179 void IrcUser::partChannel(IrcChannel *channel) {
180   if(_channels.contains(channel)) {
181     _channels.remove(channel);
182     disconnect(channel, 0, this, 0);
183     channel->part(this);
184     emit channelParted(channel->name());
185   }
186 }
187
188 void IrcUser::partChannel(const QString &channelname) {
189   IrcChannel *channel = networkInfo->ircChannel(channelname);
190   if(channel == 0) {
191     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
192   } else {
193     partChannel(channel);
194   }
195 }
196
197 void IrcUser::channelDestroyed() {
198   // private slot!
199   IrcChannel *channel = static_cast<IrcChannel*>(sender());
200   Q_ASSERT(channel);
201   if(_channels.contains(channel)) {
202     _channels.remove(channel);
203     disconnect(channel, 0, this, 0);
204   }
205 }
206
207 void IrcUser::setUserModes(const QString &modes) {
208   _userModes = modes;
209   emit userModesSet(modes);
210 }
211
212 void IrcUser::addUserMode(const QString &mode) {
213   if(!_userModes.contains(mode)) {
214     _userModes += mode;
215     emit userModeAdded(mode);
216   }
217 }
218
219 void IrcUser::removeUserMode(const QString &mode) {
220   if(_userModes.contains(mode)) {
221     _userModes.remove(mode);
222     emit userModeRemoved(mode);
223   }
224 }
225
226 void IrcUser::initSetChannels(const QStringList channels) {
227   foreach(QString channel, channels) {
228     joinChannel(channel);
229   }
230 }
231
232 void IrcUser::setInitialized() {
233   _initialized = true;
234   emit initDone();
235 }
236