replaced Client::fakeInput() with Client::userInpt() (now static but no longer a...
[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
52 QString IrcUser::user() const {
53   return _user;
54 }
55
56 QString IrcUser::host() const {
57   return _host;
58 }
59
60 QString IrcUser::nick() const {
61   return _nick;
62 }
63
64 QString IrcUser::hostmask() const {
65   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
66 }
67
68 QString IrcUser::userModes() const {
69   return _userModes;
70 }
71
72 QStringList IrcUser::channels() const {
73   QStringList chanList;
74   IrcChannel *channel;
75   foreach(channel, _channels) {
76     chanList << channel->name();
77   }
78   return chanList;
79 }
80
81 Network* IrcUser::network() const {
82   return _network;
83 }
84
85 QTextCodec *IrcUser::codecForEncoding() const {
86   return _codecForEncoding;
87 }
88
89 void IrcUser::setCodecForEncoding(const QString &name) {
90   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
91 }
92
93 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
94   _codecForEncoding = codec;
95 }
96
97 QTextCodec *IrcUser::codecForDecoding() const {
98   return _codecForDecoding;
99 }
100
101 void IrcUser::setCodecForDecoding(const QString &name) {
102   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
103 }
104
105 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
106   _codecForDecoding = codec;
107 }
108
109 QString IrcUser::decodeString(const QByteArray &text) const {
110   if(!codecForDecoding()) return network()->decodeString(text);
111   return ::decodeString(text, codecForDecoding());
112 }
113
114 QByteArray IrcUser::encodeString(const QString string) const {
115   if(codecForEncoding()) {
116     return codecForEncoding()->fromUnicode(string);
117   }
118   return network()->encodeString(string);
119 }
120
121 // ====================
122 //  PUBLIC SLOTS:
123 // ====================
124 void IrcUser::setUser(const QString &user) {
125   if(!user.isEmpty() && _user != user) {
126     _user = user;
127     emit userSet(user);
128   }
129 }
130
131 void IrcUser::setHost(const QString &host) {
132   if(!host.isEmpty() && _host != host) {
133     _host = host;
134     emit hostSet(host);
135   }
136 }
137
138 void IrcUser::setNick(const QString &nick) {
139   if(!nick.isEmpty() && nick != _nick) {
140     _nick = nick;
141     updateObjectName();
142     emit nickSet(nick);
143   }
144 }
145
146 void IrcUser::updateObjectName() {
147   QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
148   QString oldName = objectName();
149   if(oldName != newName) {
150     setObjectName(newName);
151     emit renameObject(oldName, newName);
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     _channels.insert(channel);
169     channel->join(this);
170     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
171     emit channelJoined(channel->name());
172   }
173 }
174
175 void IrcUser::joinChannel(const QString &channelname) {
176   joinChannel(network()->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 = network()->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