1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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 ***************************************************************************/
24 #include "networkinfo.h"
25 #include "signalproxy.h"
26 #include "ircchannel.h"
31 IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
32 : QObject(networkinfo),
34 _nick(nickFromMask(hostmask)),
35 _user(userFromMask(hostmask)),
36 _host(hostFromMask(hostmask)),
37 networkInfo(networkinfo),
45 //qDebug() << nick() << "destroyed.";
48 // ====================
50 // ====================
51 bool IrcUser::initialized() const {
55 QString IrcUser::user() const {
59 QString IrcUser::host() const {
63 QString IrcUser::nick() const {
67 QString IrcUser::hostmask() const {
68 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
71 QString IrcUser::userModes() const {
75 QStringList IrcUser::channels() const {
78 foreach(channel, _channels) {
79 chanList << channel->name();
84 QTextCodec *IrcUser::codecForEncoding() const {
85 return _codecForEncoding;
88 void IrcUser::setCodecForEncoding(const QString &name) {
89 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
92 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
93 _codecForEncoding = codec;
96 QTextCodec *IrcUser::codecForDecoding() const {
97 return _codecForDecoding;
100 void IrcUser::setCodecForDecoding(const QString &name) {
101 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
104 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
105 _codecForDecoding = codec;
108 QString IrcUser::decodeString(const QByteArray &text) const {
109 if(!codecForDecoding()) return networkInfo->decodeString(text);
110 return ::decodeString(text, codecForDecoding());
113 QByteArray IrcUser::encodeString(const QString string) const {
114 if(codecForEncoding()) {
115 return codecForEncoding()->fromUnicode(string);
117 return networkInfo->encodeString(string);
120 // ====================
122 // ====================
123 void IrcUser::setUser(const QString &user) {
124 if(!user.isEmpty() && _user != user) {
130 void IrcUser::setHost(const QString &host) {
131 if(!host.isEmpty() && _host != host) {
137 void IrcUser::setNick(const QString &nick) {
138 if(!nick.isEmpty() && nick != _nick) {
139 QString oldnick(_nick);
146 void IrcUser::updateObjectName() {
147 QString oldName(objectName());
148 setObjectName(QString::number(networkInfo->networkId()) + "/" + _nick);
149 if(!oldName.isEmpty()) {
150 emit renameObject(oldName, objectName());
155 void IrcUser::updateHostmask(const QString &mask) {
156 if(mask == hostmask())
159 QString user = userFromMask(mask);
160 QString host = hostFromMask(mask);
165 void IrcUser::joinChannel(IrcChannel *channel) {
167 if(!_channels.contains(channel)) {
169 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
170 _channels.insert(channel);
171 emit channelJoined(channel->name());
175 void IrcUser::joinChannel(const QString &channelname) {
176 joinChannel(networkInfo->newIrcChannel(channelname));
179 void IrcUser::partChannel(IrcChannel *channel) {
180 if(_channels.contains(channel)) {
181 _channels.remove(channel);
182 disconnect(channel, 0, this, 0);
184 emit channelParted(channel->name());
188 void IrcUser::partChannel(const QString &channelname) {
189 IrcChannel *channel = networkInfo->ircChannel(channelname);
191 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
193 partChannel(channel);
197 void IrcUser::channelDestroyed() {
199 IrcChannel *channel = static_cast<IrcChannel*>(sender());
201 if(_channels.contains(channel)) {
202 _channels.remove(channel);
203 disconnect(channel, 0, this, 0);
207 void IrcUser::setUserModes(const QString &modes) {
209 emit userModesSet(modes);
212 void IrcUser::addUserMode(const QString &mode) {
213 if(!_userModes.contains(mode)) {
215 emit userModeAdded(mode);
219 void IrcUser::removeUserMode(const QString &mode) {
220 if(_userModes.contains(mode)) {
221 _userModes.remove(mode);
222 emit userModeRemoved(mode);
226 void IrcUser::initSetChannels(const QStringList channels) {
227 foreach(QString channel, channels) {
228 joinChannel(channel);
232 void IrcUser::setInitialized() {