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