1 /***************************************************************************
2 * Copyright (C) 2005-2015 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()),
59 // ====================
61 // ====================
63 QString IrcUser::hostmask() const
65 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
69 QDateTime IrcUser::idleTime()
71 if (QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
72 _idleTime = QDateTime();
77 QStringList IrcUser::channels() const
81 foreach(channel, _channels) {
82 chanList << channel->name();
88 void IrcUser::setCodecForEncoding(const QString &name)
90 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
94 void IrcUser::setCodecForEncoding(QTextCodec *codec)
96 _codecForEncoding = codec;
100 void IrcUser::setCodecForDecoding(const QString &name)
102 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
106 void IrcUser::setCodecForDecoding(QTextCodec *codec)
108 _codecForDecoding = codec;
112 QString IrcUser::decodeString(const QByteArray &text) const
114 if (!codecForDecoding()) return network()->decodeString(text);
115 return ::decodeString(text, codecForDecoding());
119 QByteArray IrcUser::encodeString(const QString &string) const
121 if (codecForEncoding()) {
122 return codecForEncoding()->fromUnicode(string);
124 return network()->encodeString(string);
128 // ====================
130 // ====================
131 void IrcUser::setUser(const QString &user)
133 if (!user.isEmpty() && _user != user) {
140 void IrcUser::setRealName(const QString &realName)
142 if (!realName.isEmpty() && _realName != realName) {
143 _realName = realName;
149 void IrcUser::setAway(const bool &away)
159 void IrcUser::setAwayMessage(const QString &awayMessage)
161 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
162 _awayMessage = awayMessage;
163 SYNC(ARG(awayMessage))
168 void IrcUser::setIdleTime(const QDateTime &idleTime)
170 if (idleTime.isValid() && _idleTime != idleTime) {
171 _idleTime = idleTime;
172 _idleTimeSet = QDateTime::currentDateTime();
178 void IrcUser::setLoginTime(const QDateTime &loginTime)
180 if (loginTime.isValid() && _loginTime != loginTime) {
181 _loginTime = loginTime;
187 void IrcUser::setServer(const QString &server)
189 if (!server.isEmpty() && _server != server) {
196 void IrcUser::setIrcOperator(const QString &ircOperator)
198 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
199 _ircOperator = ircOperator;
200 SYNC(ARG(ircOperator))
205 void IrcUser::setLastAwayMessage(const int &lastAwayMessage)
207 if (lastAwayMessage > _lastAwayMessage) {
208 _lastAwayMessage = lastAwayMessage;
209 SYNC(ARG(lastAwayMessage))
214 void IrcUser::setHost(const QString &host)
216 if (!host.isEmpty() && _host != host) {
223 void IrcUser::setNick(const QString &nick)
225 if (!nick.isEmpty() && nick != _nick) {
234 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
236 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
237 _whoisServiceReply = whoisServiceReply;
238 SYNC(ARG(whoisServiceReply))
243 void IrcUser::setSuserHost(const QString &suserHost)
245 if (!suserHost.isEmpty() && suserHost != _suserHost) {
246 _suserHost = suserHost;
252 void IrcUser::setEncrypted(bool encrypted)
254 _encrypted = encrypted;
255 emit encryptedSet(encrypted);
260 void IrcUser::updateObjectName()
262 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
266 void IrcUser::updateHostmask(const QString &mask)
268 if (mask == hostmask())
271 QString user = userFromMask(mask);
272 QString host = hostFromMask(mask);
278 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
281 if (!_channels.contains(channel)) {
282 _channels.insert(channel);
283 if (!skip_channel_join)
284 channel->joinIrcUser(this);
289 void IrcUser::joinChannel(const QString &channelname)
291 joinChannel(network()->newIrcChannel(channelname));
295 void IrcUser::partChannel(IrcChannel *channel)
297 if (_channels.contains(channel)) {
298 _channels.remove(channel);
299 disconnect(channel, 0, this, 0);
301 QString channelName = channel->name();
302 SYNC_OTHER(partChannel, ARG(channelName))
303 if (_channels.isEmpty() && !network()->isMe(this))
309 void IrcUser::partChannel(const QString &channelname)
311 IrcChannel *channel = network()->ircChannel(channelname);
313 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
316 partChannel(channel);
323 QList<IrcChannel *> channels = _channels.toList();
325 foreach(IrcChannel *channel, channels) {
326 disconnect(channel, 0, this, 0);
329 network()->removeIrcUser(this);
335 void IrcUser::channelDestroyed()
338 IrcChannel *channel = static_cast<IrcChannel *>(sender());
339 if (_channels.contains(channel)) {
340 _channels.remove(channel);
341 if (_channels.isEmpty() && !network()->isMe(this))
347 void IrcUser::setUserModes(const QString &modes)
351 emit userModesSet(modes);
355 void IrcUser::addUserModes(const QString &modes)
360 for (int i = 0; i < modes.count(); i++) {
361 if (!_userModes.contains(modes[i]))
362 _userModes += modes[i];
366 emit userModesAdded(modes);
370 void IrcUser::removeUserModes(const QString &modes)
375 for (int i = 0; i < modes.count(); i++) {
376 _userModes.remove(modes[i]);
379 emit userModesRemoved(modes);
383 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
385 _lastActivity[buffer] = time;
386 emit lastChannelActivityUpdated(buffer, time);
390 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
392 _lastSpokenTo[buffer] = time;
393 emit lastSpokenToUpdated(buffer, time);