f6818b9a2b52cc43981cc2df90b326a9f4c767a6
[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 }
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 Network* IrcUser::network() const {
85   return _network;
86 }
87
88 QTextCodec *IrcUser::codecForEncoding() const {
89   return _codecForEncoding;
90 }
91
92 void IrcUser::setCodecForEncoding(const QString &name) {
93   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
94 }
95
96 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
97   _codecForEncoding = codec;
98 }
99
100 QTextCodec *IrcUser::codecForDecoding() const {
101   return _codecForDecoding;
102 }
103
104 void IrcUser::setCodecForDecoding(const QString &name) {
105   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
106 }
107
108 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
109   _codecForDecoding = codec;
110 }
111
112 QString IrcUser::decodeString(const QByteArray &text) const {
113   if(!codecForDecoding()) return network()->decodeString(text);
114   return ::decodeString(text, codecForDecoding());
115 }
116
117 QByteArray IrcUser::encodeString(const QString string) const {
118   if(codecForEncoding()) {
119     return codecForEncoding()->fromUnicode(string);
120   }
121   return network()->encodeString(string);
122 }
123
124 // ====================
125 //  PUBLIC SLOTS:
126 // ====================
127 void IrcUser::setUser(const QString &user) {
128   if(!user.isEmpty() && _user != user) {
129     _user = user;
130     emit userSet(user);
131   }
132 }
133
134 void IrcUser::setHost(const QString &host) {
135   if(!host.isEmpty() && _host != host) {
136     _host = host;
137     emit hostSet(host);
138   }
139 }
140
141 void IrcUser::setNick(const QString &nick) {
142   if(!nick.isEmpty() && nick != _nick) {
143     _nick = nick;
144     updateObjectName();
145     emit nickSet(nick);
146   }
147 }
148
149 void IrcUser::updateObjectName() {
150   QString newName = QString::number(network()->networkId()) + "/" + _nick;
151   QString oldName = objectName();
152   if(oldName != newName) {
153     setObjectName(newName);
154     emit renameObject(oldName, newName);
155   }
156 }
157
158 void IrcUser::updateHostmask(const QString &mask) {
159   if(mask == hostmask())
160     return;
161
162   QString user = userFromMask(mask);
163   QString host = hostFromMask(mask);
164   setUser(user);
165   setHost(host);
166 }
167
168 void IrcUser::joinChannel(IrcChannel *channel) {
169   Q_ASSERT(channel);
170   if(!_channels.contains(channel)) {
171     _channels.insert(channel);
172     channel->join(this);
173     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
174     emit channelJoined(channel->name());
175   }
176 }
177
178 void IrcUser::joinChannel(const QString &channelname) {
179   joinChannel(network()->newIrcChannel(channelname));
180 }
181
182 void IrcUser::partChannel(IrcChannel *channel) {
183   if(_channels.contains(channel)) {
184     _channels.remove(channel);
185     disconnect(channel, 0, this, 0);
186     channel->part(this);
187     emit channelParted(channel->name());
188   }
189 }
190
191 void IrcUser::partChannel(const QString &channelname) {
192   IrcChannel *channel = network()->ircChannel(channelname);
193   if(channel == 0) {
194     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
195   } else {
196     partChannel(channel);
197   }
198 }
199
200 void IrcUser::channelDestroyed() {
201   // private slot!
202   IrcChannel *channel = static_cast<IrcChannel*>(sender());
203   Q_ASSERT(channel);
204   if(_channels.contains(channel)) {
205     _channels.remove(channel);
206     disconnect(channel, 0, this, 0);
207   }
208 }
209
210 void IrcUser::setUserModes(const QString &modes) {
211   _userModes = modes;
212   emit userModesSet(modes);
213 }
214
215 void IrcUser::addUserMode(const QString &mode) {
216   if(!_userModes.contains(mode)) {
217     _userModes += mode;
218     emit userModeAdded(mode);
219   }
220 }
221
222 void IrcUser::removeUserMode(const QString &mode) {
223   if(_userModes.contains(mode)) {
224     _userModes.remove(mode);
225     emit userModeRemoved(mode);
226   }
227 }
228
229 void IrcUser::initSetChannels(const QStringList channels) {
230   foreach(QString channel, channels) {
231     joinChannel(channel);
232   }
233 }
234
235 void IrcUser::setInitialized() {
236   _initialized = true;
237   emit initDone();
238 }
239