1 /***************************************************************************
2 * Copyright (C) 2005-09 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 INIT_SYNCABLE_OBJECT(IrcUser)
32 IrcUser::IrcUser(const QString &hostmask, Network *network) : SyncableObject(network),
34 _nick(nickFromMask(hostmask)),
35 _user(userFromMask(hostmask)),
36 _host(hostFromMask(hostmask)),
41 // _idleTime(QDateTime::currentDateTime()),
55 // ====================
57 // ====================
59 QString IrcUser::hostmask() const {
60 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
63 QDateTime IrcUser::idleTime() {
64 if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
65 _idleTime = QDateTime();
69 QStringList IrcUser::channels() const {
72 foreach(channel, _channels) {
73 chanList << channel->name();
79 void IrcUser::setCodecForEncoding(const QString &name) {
80 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
83 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
84 _codecForEncoding = codec;
87 void IrcUser::setCodecForDecoding(const QString &name) {
88 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
91 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
92 _codecForDecoding = codec;
95 QString IrcUser::decodeString(const QByteArray &text) const {
96 if(!codecForDecoding()) return network()->decodeString(text);
97 return ::decodeString(text, codecForDecoding());
100 QByteArray IrcUser::encodeString(const QString &string) const {
101 if(codecForEncoding()) {
102 return codecForEncoding()->fromUnicode(string);
104 return network()->encodeString(string);
107 // ====================
109 // ====================
110 void IrcUser::setUser(const QString &user) {
111 if(!user.isEmpty() && _user != user) {
117 void IrcUser::setRealName(const QString &realName) {
118 if (!realName.isEmpty() && _realName != realName) {
119 _realName = realName;
124 void IrcUser::setAway(const bool &away) {
132 void IrcUser::setAwayMessage(const QString &awayMessage) {
133 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
134 _awayMessage = awayMessage;
135 SYNC(ARG(awayMessage))
139 void IrcUser::setIdleTime(const QDateTime &idleTime) {
140 if(idleTime.isValid() && _idleTime != idleTime) {
141 _idleTime = idleTime;
142 _idleTimeSet = QDateTime::currentDateTime();
147 void IrcUser::setLoginTime(const QDateTime &loginTime) {
148 if(loginTime.isValid() && _loginTime != loginTime) {
149 _loginTime = loginTime;
154 void IrcUser::setServer(const QString &server) {
155 if(!server.isEmpty() && _server != server) {
161 void IrcUser::setIrcOperator(const QString &ircOperator) {
162 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
163 _ircOperator = ircOperator;
164 SYNC(ARG(ircOperator))
168 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
169 if(lastAwayMessage > _lastAwayMessage) {
170 _lastAwayMessage = lastAwayMessage;
171 SYNC(ARG(lastAwayMessage))
175 void IrcUser::setHost(const QString &host) {
176 if(!host.isEmpty() && _host != host) {
182 void IrcUser::setNick(const QString &nick) {
183 if(!nick.isEmpty() && nick != _nick) {
184 qDebug() << "setNick" << this << _nick << nick;
192 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
193 if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
194 _whoisServiceReply = whoisServiceReply;
195 SYNC(ARG(whoisServiceReply))
199 void IrcUser::setSuserHost(const QString &suserHost) {
200 if(!suserHost.isEmpty() && suserHost != _suserHost) {
201 _suserHost = suserHost;
206 void IrcUser::updateObjectName() {
207 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
210 void IrcUser::updateHostmask(const QString &mask) {
211 if(mask == hostmask())
214 QString user = userFromMask(mask);
215 QString host = hostFromMask(mask);
220 void IrcUser::joinChannel(IrcChannel *channel) {
222 if(!_channels.contains(channel)) {
223 _channels.insert(channel);
224 channel->joinIrcUser(this);
228 void IrcUser::joinChannel(const QString &channelname) {
229 joinChannel(network()->newIrcChannel(channelname));
232 void IrcUser::partChannel(IrcChannel *channel) {
233 if(_channels.contains(channel)) {
234 _channels.remove(channel);
235 disconnect(channel, 0, this, 0);
237 QString channelName = channel->name();
238 SYNC_OTHER(partChannel, ARG(channelName))
239 if(_channels.isEmpty() && !network()->isMe(this))
244 void IrcUser::partChannel(const QString &channelname) {
245 IrcChannel *channel = network()->ircChannel(channelname);
247 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
249 partChannel(channel);
253 void IrcUser::quit() {
254 QList<IrcChannel *> channels = _channels.toList();
256 foreach(IrcChannel *channel, channels) {
257 disconnect(channel, 0, this, 0);
260 network()->removeIrcUser(this);
265 void IrcUser::channelDestroyed() {
267 IrcChannel *channel = static_cast<IrcChannel*>(sender());
268 if(_channels.contains(channel)) {
269 _channels.remove(channel);
270 if(_channels.isEmpty() && !network()->isMe(this))
275 void IrcUser::setUserModes(const QString &modes) {
278 emit userModesSet(modes);
281 void IrcUser::addUserModes(const QString &modes) {
285 for(int i = 0; i < modes.count(); i++) {
286 if(!_userModes.contains(modes[i]))
287 _userModes += modes[i];
291 emit userModesAdded(modes);
294 void IrcUser::removeUserModes(const QString &modes) {
298 for(int i = 0; i < modes.count(); i++) {
299 _userModes.remove(modes[i]);
302 emit userModesRemoved(modes);
305 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time) {
306 _lastActivity[buffer] = time;
307 emit lastChannelActivityUpdated(buffer, time);
310 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time) {
311 _lastSpokenTo[buffer] = time;
312 emit lastSpokenToUpdated(buffer, time);