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)),
45 //qDebug() << nick() << "destroyed.";
48 // ====================
50 // ====================
52 QString IrcUser::user() const {
56 QString IrcUser::host() const {
60 QString IrcUser::nick() const {
64 QString IrcUser::hostmask() const {
65 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
68 QString IrcUser::userModes() const {
72 QStringList IrcUser::channels() const {
75 foreach(channel, _channels) {
76 chanList << channel->name();
81 Network* IrcUser::network() const {
85 QTextCodec *IrcUser::codecForEncoding() const {
86 return _codecForEncoding;
89 void IrcUser::setCodecForEncoding(const QString &name) {
90 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
93 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
94 _codecForEncoding = codec;
97 QTextCodec *IrcUser::codecForDecoding() const {
98 return _codecForDecoding;
101 void IrcUser::setCodecForDecoding(const QString &name) {
102 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
105 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
106 _codecForDecoding = codec;
109 QString IrcUser::decodeString(const QByteArray &text) const {
110 if(!codecForDecoding()) return network()->decodeString(text);
111 return ::decodeString(text, codecForDecoding());
114 QByteArray IrcUser::encodeString(const QString string) const {
115 if(codecForEncoding()) {
116 return codecForEncoding()->fromUnicode(string);
118 return network()->encodeString(string);
121 // ====================
123 // ====================
124 void IrcUser::setUser(const QString &user) {
125 if(!user.isEmpty() && _user != user) {
131 void IrcUser::setHost(const QString &host) {
132 if(!host.isEmpty() && _host != host) {
138 void IrcUser::setNick(const QString &nick) {
139 if(!nick.isEmpty() && nick != _nick) {
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);
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)) {
168 _channels.insert(channel);
170 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
171 emit channelJoined(channel->name());
175 void IrcUser::joinChannel(const QString &channelname) {
176 joinChannel(network()->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 = network()->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);