1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
25 #include "signalproxy.h"
26 #include "ircchannel.h"
31 IrcUser::IrcUser(const QString &hostmask, Network *network)
32 : SyncableObject(network),
34 _nick(nickFromMask(hostmask)),
35 _user(userFromMask(hostmask)),
36 _host(hostFromMask(hostmask)),
46 //qDebug() << nick() << "destroyed.";
49 // ====================
51 // ====================
53 QString IrcUser::user() const {
57 QString IrcUser::host() const {
61 QString IrcUser::nick() const {
65 QString IrcUser::realName() const {
69 QString IrcUser::hostmask() const {
70 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
73 bool IrcUser::isAway() const {
77 QString IrcUser::awayMessage() const {
81 QString IrcUser::userModes() const {
85 QStringList IrcUser::channels() const {
88 foreach(channel, _channels) {
89 chanList << channel->name();
94 Network* IrcUser::network() const {
98 QTextCodec *IrcUser::codecForEncoding() const {
99 return _codecForEncoding;
102 void IrcUser::setCodecForEncoding(const QString &name) {
103 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
106 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
107 _codecForEncoding = codec;
110 QTextCodec *IrcUser::codecForDecoding() const {
111 return _codecForDecoding;
114 void IrcUser::setCodecForDecoding(const QString &name) {
115 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
118 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
119 _codecForDecoding = codec;
122 QString IrcUser::decodeString(const QByteArray &text) const {
123 if(!codecForDecoding()) return network()->decodeString(text);
124 return ::decodeString(text, codecForDecoding());
127 QByteArray IrcUser::encodeString(const QString string) const {
128 if(codecForEncoding()) {
129 return codecForEncoding()->fromUnicode(string);
131 return network()->encodeString(string);
134 // ====================
136 // ====================
137 void IrcUser::setUser(const QString &user) {
138 if(!user.isEmpty() && _user != user) {
144 void IrcUser::setRealName(const QString &realName) {
145 if (!realName.isEmpty() && _realName != realName) {
146 _realName = realName;
147 emit realNameSet(realName);
151 void IrcUser::setAway(const bool &away) {
158 void IrcUser::setAwayMessage(const QString &awayMessage) {
159 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
160 _awayMessage = awayMessage;
161 emit awayMessageSet(awayMessage);
166 void IrcUser::setHost(const QString &host) {
167 if(!host.isEmpty() && _host != host) {
173 void IrcUser::setNick(const QString &nick) {
174 if(!nick.isEmpty() && nick != _nick) {
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);
190 void IrcUser::updateHostmask(const QString &mask) {
191 if(mask == hostmask())
194 QString user = userFromMask(mask);
195 QString host = hostFromMask(mask);
200 void IrcUser::joinChannel(IrcChannel *channel) {
202 if(!_channels.contains(channel)) {
203 _channels.insert(channel);
205 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
206 emit channelJoined(channel->name());
210 void IrcUser::joinChannel(const QString &channelname) {
211 joinChannel(network()->newIrcChannel(channelname));
214 void IrcUser::partChannel(IrcChannel *channel) {
215 if(_channels.contains(channel)) {
216 _channels.remove(channel);
217 disconnect(channel, 0, this, 0);
219 emit channelParted(channel->name());
223 void IrcUser::partChannel(const QString &channelname) {
224 IrcChannel *channel = network()->ircChannel(channelname);
226 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
228 partChannel(channel);
232 void IrcUser::channelDestroyed() {
234 IrcChannel *channel = static_cast<IrcChannel*>(sender());
236 if(_channels.contains(channel)) {
237 _channels.remove(channel);
238 disconnect(channel, 0, this, 0);
242 void IrcUser::setUserModes(const QString &modes) {
244 emit userModesSet(modes);
247 void IrcUser::addUserMode(const QString &mode) {
248 if(!_userModes.contains(mode)) {
250 emit userModeAdded(mode);
254 void IrcUser::removeUserMode(const QString &mode) {
255 if(_userModes.contains(mode)) {
256 _userModes.remove(mode);
257 emit userModeRemoved(mode);
261 void IrcUser::initSetChannels(const QStringList channels) {
262 foreach(QString channel, channels) {
263 joinChannel(channel);