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 ***************************************************************************/
24 #include "networkinfo.h"
25 #include "signalproxy.h"
26 #include "ircchannel.h"
31 IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
32 : SyncableObject(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) {
145 void IrcUser::updateObjectName() {
146 QString newName = QString::number(networkInfo->networkId()) + "/" + _nick;
147 QString oldName = objectName();
148 if(oldName != newName) {
149 setObjectName(newName);
150 emit renameObject(oldName, newName);
154 void IrcUser::updateHostmask(const QString &mask) {
155 if(mask == hostmask())
158 QString user = userFromMask(mask);
159 QString host = hostFromMask(mask);
164 void IrcUser::joinChannel(IrcChannel *channel) {
166 if(!_channels.contains(channel)) {
167 _channels.insert(channel);
169 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
170 emit channelJoined(channel->name());
174 void IrcUser::joinChannel(const QString &channelname) {
175 joinChannel(networkInfo->newIrcChannel(channelname));
178 void IrcUser::partChannel(IrcChannel *channel) {
179 if(_channels.contains(channel)) {
180 _channels.remove(channel);
181 disconnect(channel, 0, this, 0);
183 emit channelParted(channel->name());
187 void IrcUser::partChannel(const QString &channelname) {
188 IrcChannel *channel = networkInfo->ircChannel(channelname);
190 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
192 partChannel(channel);
196 void IrcUser::channelDestroyed() {
198 IrcChannel *channel = static_cast<IrcChannel*>(sender());
200 if(_channels.contains(channel)) {
201 _channels.remove(channel);
202 disconnect(channel, 0, this, 0);
206 void IrcUser::setUserModes(const QString &modes) {
208 emit userModesSet(modes);
211 void IrcUser::addUserMode(const QString &mode) {
212 if(!_userModes.contains(mode)) {
214 emit userModeAdded(mode);
218 void IrcUser::removeUserMode(const QString &mode) {
219 if(_userModes.contains(mode)) {
220 _userModes.remove(mode);
221 emit userModeRemoved(mode);
225 void IrcUser::initSetChannels(const QStringList channels) {
226 foreach(QString channel, channels) {
227 joinChannel(channel);
231 void IrcUser::setInitialized() {