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)),
41 _idleTime(QDateTime::currentDateTime()),
51 //qDebug() << nick() << "destroyed.";
54 // ====================
56 // ====================
58 QString IrcUser::user() const {
62 QString IrcUser::host() const {
66 QString IrcUser::nick() const {
70 QString IrcUser::realName() const {
74 QString IrcUser::hostmask() const {
75 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
78 bool IrcUser::isAway() const {
82 QString IrcUser::awayMessage() const {
86 QString IrcUser::server() const {
90 QDateTime IrcUser::idleTime() const {
94 QString IrcUser::ircOperator() const {
98 QString IrcUser::userModes() const {
102 QStringList IrcUser::channels() const {
103 QStringList chanList;
105 foreach(channel, _channels) {
106 chanList << channel->name();
111 Network* IrcUser::network() const {
115 QTextCodec *IrcUser::codecForEncoding() const {
116 return _codecForEncoding;
119 void IrcUser::setCodecForEncoding(const QString &name) {
120 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
123 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
124 _codecForEncoding = codec;
127 QTextCodec *IrcUser::codecForDecoding() const {
128 return _codecForDecoding;
131 void IrcUser::setCodecForDecoding(const QString &name) {
132 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
135 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
136 _codecForDecoding = codec;
139 QString IrcUser::decodeString(const QByteArray &text) const {
140 if(!codecForDecoding()) return network()->decodeString(text);
141 return ::decodeString(text, codecForDecoding());
144 QByteArray IrcUser::encodeString(const QString string) const {
145 if(codecForEncoding()) {
146 return codecForEncoding()->fromUnicode(string);
148 return network()->encodeString(string);
151 // ====================
153 // ====================
154 void IrcUser::setUser(const QString &user) {
155 if(!user.isEmpty() && _user != user) {
161 void IrcUser::setRealName(const QString &realName) {
162 if (!realName.isEmpty() && _realName != realName) {
163 _realName = realName;
164 emit realNameSet(realName);
168 void IrcUser::setAway(const bool &away) {
175 void IrcUser::setAwayMessage(const QString &awayMessage) {
176 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
177 _awayMessage = awayMessage;
178 emit awayMessageSet(awayMessage);
182 void IrcUser::setIdleTime(const QDateTime &idleTime) {
183 if(idleTime.isValid() && _idleTime != idleTime) {
184 _idleTime = idleTime;
185 emit idleTimeSet(idleTime);
189 void IrcUser::setServer(const QString &server) {
190 if(!server.isEmpty() && _server != server) {
192 emit serverSet(server);
196 void IrcUser::setIrcOperator(const QString &ircOperator) {
197 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
198 _ircOperator = ircOperator;
199 emit ircOperatorSet(ircOperator);
203 void IrcUser::setHost(const QString &host) {
204 if(!host.isEmpty() && _host != host) {
210 void IrcUser::setNick(const QString &nick) {
211 if(!nick.isEmpty() && nick != _nick) {
218 void IrcUser::updateObjectName() {
219 QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
220 QString oldName = objectName();
221 if(oldName != newName) {
222 setObjectName(newName);
223 emit renameObject(oldName, newName);
227 void IrcUser::updateHostmask(const QString &mask) {
228 if(mask == hostmask())
231 QString user = userFromMask(mask);
232 QString host = hostFromMask(mask);
237 void IrcUser::joinChannel(IrcChannel *channel) {
239 if(!_channels.contains(channel)) {
240 _channels.insert(channel);
242 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
243 emit channelJoined(channel->name());
247 void IrcUser::joinChannel(const QString &channelname) {
248 joinChannel(network()->newIrcChannel(channelname));
251 void IrcUser::partChannel(IrcChannel *channel) {
252 if(_channels.contains(channel)) {
253 _channels.remove(channel);
254 disconnect(channel, 0, this, 0);
256 emit channelParted(channel->name());
260 void IrcUser::partChannel(const QString &channelname) {
261 IrcChannel *channel = network()->ircChannel(channelname);
263 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
265 partChannel(channel);
269 void IrcUser::channelDestroyed() {
271 IrcChannel *channel = static_cast<IrcChannel*>(sender());
273 if(_channels.contains(channel)) {
274 _channels.remove(channel);
275 disconnect(channel, 0, this, 0);
279 void IrcUser::setUserModes(const QString &modes) {
281 emit userModesSet(modes);
284 void IrcUser::addUserMode(const QString &mode) {
285 if(!_userModes.contains(mode)) {
287 emit userModeAdded(mode);
291 void IrcUser::removeUserMode(const QString &mode) {
292 if(_userModes.contains(mode)) {
293 _userModes.remove(mode);
294 emit userModeRemoved(mode);
298 void IrcUser::initSetChannels(const QStringList channels) {
299 foreach(QString channel, channels) {
300 joinChannel(channel);