1 /***************************************************************************
2 * Copyright (C) 2005-2018 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()),
51 _lastAwayMessage.setTimeSpec(Qt::UTC);
52 _lastAwayMessage.setMSecsSinceEpoch(0);
61 // ====================
63 // ====================
65 QString IrcUser::hostmask() const
67 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
71 QDateTime IrcUser::idleTime()
73 if ((QDateTime::currentDateTime().toMSecsSinceEpoch() - _idleTimeSet.toMSecsSinceEpoch())
75 // 20 * 60 * 1000 = 1200000
76 // 20 minutes have elapsed, clear the known idle time as it's likely inaccurate by now
77 _idleTime = QDateTime();
83 QStringList IrcUser::channels() const
87 foreach(channel, _channels) {
88 chanList << channel->name();
94 void IrcUser::setCodecForEncoding(const QString &name)
96 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
100 void IrcUser::setCodecForEncoding(QTextCodec *codec)
102 _codecForEncoding = codec;
106 void IrcUser::setCodecForDecoding(const QString &name)
108 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
112 void IrcUser::setCodecForDecoding(QTextCodec *codec)
114 _codecForDecoding = codec;
118 QString IrcUser::decodeString(const QByteArray &text) const
120 if (!codecForDecoding()) return network()->decodeString(text);
121 return ::decodeString(text, codecForDecoding());
125 QByteArray IrcUser::encodeString(const QString &string) const
127 if (codecForEncoding()) {
128 return codecForEncoding()->fromUnicode(string);
130 return network()->encodeString(string);
134 // ====================
136 // ====================
137 void IrcUser::setUser(const QString &user)
139 if (!user.isEmpty() && _user != user) {
146 void IrcUser::setRealName(const QString &realName)
148 if (!realName.isEmpty() && _realName != realName) {
149 _realName = realName;
155 void IrcUser::setAccount(const QString &account)
157 if (_account != account) {
164 void IrcUser::setAway(const bool &away)
174 void IrcUser::setAwayMessage(const QString &awayMessage)
176 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
177 _awayMessage = awayMessage;
178 SYNC(ARG(awayMessage))
183 void IrcUser::setIdleTime(const QDateTime &idleTime)
185 if (idleTime.isValid() && _idleTime != idleTime) {
186 _idleTime = idleTime;
187 _idleTimeSet = QDateTime::currentDateTime();
193 void IrcUser::setLoginTime(const QDateTime &loginTime)
195 if (loginTime.isValid() && _loginTime != loginTime) {
196 _loginTime = loginTime;
202 void IrcUser::setServer(const QString &server)
204 if (!server.isEmpty() && _server != server) {
211 void IrcUser::setIrcOperator(const QString &ircOperator)
213 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
214 _ircOperator = ircOperator;
215 SYNC(ARG(ircOperator))
220 void IrcUser::setLastAwayMessage(const QDateTime &lastAwayMessage)
222 if (lastAwayMessage > _lastAwayMessage) {
223 _lastAwayMessage = lastAwayMessage;
224 SYNC(ARG(lastAwayMessage))
229 void IrcUser::setHost(const QString &host)
231 if (!host.isEmpty() && _host != host) {
238 void IrcUser::setNick(const QString &nick)
240 if (!nick.isEmpty() && nick != _nick) {
249 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
251 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
252 _whoisServiceReply = whoisServiceReply;
253 SYNC(ARG(whoisServiceReply))
258 void IrcUser::setSuserHost(const QString &suserHost)
260 if (!suserHost.isEmpty() && suserHost != _suserHost) {
261 _suserHost = suserHost;
267 void IrcUser::setEncrypted(bool encrypted)
269 _encrypted = encrypted;
270 emit encryptedSet(encrypted);
275 void IrcUser::updateObjectName()
277 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
281 void IrcUser::updateHostmask(const QString &mask)
283 if (mask == hostmask())
286 QString user = userFromMask(mask);
287 QString host = hostFromMask(mask);
293 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
296 if (!_channels.contains(channel)) {
297 _channels.insert(channel);
298 if (!skip_channel_join)
299 channel->joinIrcUser(this);
304 void IrcUser::joinChannel(const QString &channelname)
306 joinChannel(network()->newIrcChannel(channelname));
310 void IrcUser::partChannel(IrcChannel *channel)
312 if (_channels.contains(channel)) {
313 _channels.remove(channel);
314 disconnect(channel, 0, this, 0);
316 QString channelName = channel->name();
317 SYNC_OTHER(partChannel, ARG(channelName))
318 if (_channels.isEmpty() && !network()->isMe(this))
324 void IrcUser::partChannel(const QString &channelname)
326 IrcChannel *channel = network()->ircChannel(channelname);
328 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
331 partChannel(channel);
338 QList<IrcChannel *> channels = _channels.toList();
340 foreach(IrcChannel *channel, channels) {
341 disconnect(channel, 0, this, 0);
344 network()->removeIrcUser(this);
350 void IrcUser::channelDestroyed()
353 IrcChannel *channel = static_cast<IrcChannel *>(sender());
354 if (_channels.contains(channel)) {
355 _channels.remove(channel);
356 if (_channels.isEmpty() && !network()->isMe(this))
362 void IrcUser::setUserModes(const QString &modes)
364 if (_userModes != modes) {
367 emit userModesSet(modes);
372 void IrcUser::addUserModes(const QString &modes)
377 // Don't needlessly sync when no changes are made
378 bool changesMade = false;
379 for (int i = 0; i < modes.count(); i++) {
380 if (!_userModes.contains(modes[i])) {
381 _userModes += modes[i];
388 emit userModesAdded(modes);
393 void IrcUser::removeUserModes(const QString &modes)
398 for (int i = 0; i < modes.count(); i++) {
399 _userModes.remove(modes[i]);
402 emit userModesRemoved(modes);
406 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
408 _lastActivity[buffer] = time;
409 emit lastChannelActivityUpdated(buffer, time);
413 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
415 _lastSpokenTo[buffer] = time;
416 emit lastSpokenToUpdated(buffer, time);