1 /***************************************************************************
2 * Copyright (C) 2005-2013 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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()),
58 // ====================
60 // ====================
62 QString IrcUser::hostmask() const
64 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
68 QDateTime IrcUser::idleTime()
70 if (QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
71 _idleTime = QDateTime();
76 QStringList IrcUser::channels() const
80 foreach(channel, _channels) {
81 chanList << channel->name();
87 void IrcUser::setCodecForEncoding(const QString &name)
89 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
93 void IrcUser::setCodecForEncoding(QTextCodec *codec)
95 _codecForEncoding = codec;
99 void IrcUser::setCodecForDecoding(const QString &name)
101 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
105 void IrcUser::setCodecForDecoding(QTextCodec *codec)
107 _codecForDecoding = codec;
111 QString IrcUser::decodeString(const QByteArray &text) const
113 if (!codecForDecoding()) return network()->decodeString(text);
114 return ::decodeString(text, codecForDecoding());
118 QByteArray IrcUser::encodeString(const QString &string) const
120 if (codecForEncoding()) {
121 return codecForEncoding()->fromUnicode(string);
123 return network()->encodeString(string);
127 // ====================
129 // ====================
130 void IrcUser::setUser(const QString &user)
132 if (!user.isEmpty() && _user != user) {
139 void IrcUser::setRealName(const QString &realName)
141 if (!realName.isEmpty() && _realName != realName) {
142 _realName = realName;
148 void IrcUser::setAway(const bool &away)
158 void IrcUser::setAwayMessage(const QString &awayMessage)
160 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
161 _awayMessage = awayMessage;
162 SYNC(ARG(awayMessage))
167 void IrcUser::setIdleTime(const QDateTime &idleTime)
169 if (idleTime.isValid() && _idleTime != idleTime) {
170 _idleTime = idleTime;
171 _idleTimeSet = QDateTime::currentDateTime();
177 void IrcUser::setLoginTime(const QDateTime &loginTime)
179 if (loginTime.isValid() && _loginTime != loginTime) {
180 _loginTime = loginTime;
186 void IrcUser::setServer(const QString &server)
188 if (!server.isEmpty() && _server != server) {
195 void IrcUser::setIrcOperator(const QString &ircOperator)
197 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
198 _ircOperator = ircOperator;
199 SYNC(ARG(ircOperator))
204 void IrcUser::setLastAwayMessage(const int &lastAwayMessage)
206 if (lastAwayMessage > _lastAwayMessage) {
207 _lastAwayMessage = lastAwayMessage;
208 SYNC(ARG(lastAwayMessage))
213 void IrcUser::setHost(const QString &host)
215 if (!host.isEmpty() && _host != host) {
222 void IrcUser::setNick(const QString &nick)
224 if (!nick.isEmpty() && nick != _nick) {
233 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
235 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
236 _whoisServiceReply = whoisServiceReply;
237 SYNC(ARG(whoisServiceReply))
242 void IrcUser::setSuserHost(const QString &suserHost)
244 if (!suserHost.isEmpty() && suserHost != _suserHost) {
245 _suserHost = suserHost;
251 void IrcUser::updateObjectName()
253 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
257 void IrcUser::updateHostmask(const QString &mask)
259 if (mask == hostmask())
262 QString user = userFromMask(mask);
263 QString host = hostFromMask(mask);
269 void IrcUser::joinChannel(IrcChannel *channel)
272 if (!_channels.contains(channel)) {
273 _channels.insert(channel);
274 channel->joinIrcUser(this);
279 void IrcUser::joinChannel(const QString &channelname)
281 joinChannel(network()->newIrcChannel(channelname));
285 void IrcUser::partChannel(IrcChannel *channel)
287 if (_channels.contains(channel)) {
288 _channels.remove(channel);
289 disconnect(channel, 0, this, 0);
291 QString channelName = channel->name();
292 SYNC_OTHER(partChannel, ARG(channelName))
293 if (_channels.isEmpty() && !network()->isMe(this))
299 void IrcUser::partChannel(const QString &channelname)
301 IrcChannel *channel = network()->ircChannel(channelname);
303 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
306 partChannel(channel);
313 QList<IrcChannel *> channels = _channels.toList();
315 foreach(IrcChannel *channel, channels) {
316 disconnect(channel, 0, this, 0);
319 network()->removeIrcUser(this);
325 void IrcUser::channelDestroyed()
328 IrcChannel *channel = static_cast<IrcChannel *>(sender());
329 if (_channels.contains(channel)) {
330 _channels.remove(channel);
331 if (_channels.isEmpty() && !network()->isMe(this))
337 void IrcUser::setUserModes(const QString &modes)
341 emit userModesSet(modes);
345 void IrcUser::addUserModes(const QString &modes)
350 for (int i = 0; i < modes.count(); i++) {
351 if (!_userModes.contains(modes[i]))
352 _userModes += modes[i];
356 emit userModesAdded(modes);
360 void IrcUser::removeUserModes(const QString &modes)
365 for (int i = 0; i < modes.count(); i++) {
366 _userModes.remove(modes[i]);
369 emit userModesRemoved(modes);
373 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
375 _lastActivity[buffer] = time;
376 emit lastChannelActivityUpdated(buffer, time);
380 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
382 _lastSpokenTo[buffer] = time;
383 emit lastSpokenToUpdated(buffer, time);