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()),
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().toMSecsSinceEpoch() - _idleTimeSet.toMSecsSinceEpoch())
73 // 20 * 60 * 1000 = 1200000
74 // 20 minutes have elapsed, clear the known idle time as it's likely inaccurate by now
75 _idleTime = QDateTime();
81 QStringList IrcUser::channels() const
85 foreach(channel, _channels) {
86 chanList << channel->name();
92 void IrcUser::setCodecForEncoding(const QString &name)
94 setCodecForEncoding(QTextCodec::codecForName(name.toLatin1()));
98 void IrcUser::setCodecForEncoding(QTextCodec *codec)
100 _codecForEncoding = codec;
104 void IrcUser::setCodecForDecoding(const QString &name)
106 setCodecForDecoding(QTextCodec::codecForName(name.toLatin1()));
110 void IrcUser::setCodecForDecoding(QTextCodec *codec)
112 _codecForDecoding = codec;
116 QString IrcUser::decodeString(const QByteArray &text) const
118 if (!codecForDecoding()) return network()->decodeString(text);
119 return ::decodeString(text, codecForDecoding());
123 QByteArray IrcUser::encodeString(const QString &string) const
125 if (codecForEncoding()) {
126 return codecForEncoding()->fromUnicode(string);
128 return network()->encodeString(string);
132 // ====================
134 // ====================
135 void IrcUser::setUser(const QString &user)
137 if (!user.isEmpty() && _user != user) {
144 void IrcUser::setRealName(const QString &realName)
146 if (!realName.isEmpty() && _realName != realName) {
147 _realName = realName;
153 void IrcUser::setAccount(const QString &account)
155 if (_account != account) {
162 void IrcUser::setAway(const bool &away)
172 void IrcUser::setAwayMessage(const QString &awayMessage)
174 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
175 _awayMessage = awayMessage;
176 SYNC(ARG(awayMessage))
181 void IrcUser::setIdleTime(const QDateTime &idleTime)
183 if (idleTime.isValid() && _idleTime != idleTime) {
184 _idleTime = idleTime;
185 _idleTimeSet = QDateTime::currentDateTime();
191 void IrcUser::setLoginTime(const QDateTime &loginTime)
193 if (loginTime.isValid() && _loginTime != loginTime) {
194 _loginTime = loginTime;
200 void IrcUser::setServer(const QString &server)
202 if (!server.isEmpty() && _server != server) {
209 void IrcUser::setIrcOperator(const QString &ircOperator)
211 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
212 _ircOperator = ircOperator;
213 SYNC(ARG(ircOperator))
218 void IrcUser::setLastAwayMessage(const int &lastAwayMessage)
220 if (lastAwayMessage > _lastAwayMessage) {
221 _lastAwayMessage = lastAwayMessage;
222 SYNC(ARG(lastAwayMessage))
227 void IrcUser::setHost(const QString &host)
229 if (!host.isEmpty() && _host != host) {
236 void IrcUser::setNick(const QString &nick)
238 if (!nick.isEmpty() && nick != _nick) {
247 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
249 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
250 _whoisServiceReply = whoisServiceReply;
251 SYNC(ARG(whoisServiceReply))
256 void IrcUser::setSuserHost(const QString &suserHost)
258 if (!suserHost.isEmpty() && suserHost != _suserHost) {
259 _suserHost = suserHost;
265 void IrcUser::setEncrypted(bool encrypted)
267 _encrypted = encrypted;
268 emit encryptedSet(encrypted);
273 void IrcUser::updateObjectName()
275 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
279 void IrcUser::updateHostmask(const QString &mask)
281 if (mask == hostmask())
284 QString user = userFromMask(mask);
285 QString host = hostFromMask(mask);
291 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
294 if (!_channels.contains(channel)) {
295 _channels.insert(channel);
296 if (!skip_channel_join)
297 channel->joinIrcUser(this);
302 void IrcUser::joinChannel(const QString &channelname)
304 joinChannel(network()->newIrcChannel(channelname));
308 void IrcUser::partChannel(IrcChannel *channel)
310 if (_channels.contains(channel)) {
311 _channels.remove(channel);
312 disconnect(channel, 0, this, 0);
314 QString channelName = channel->name();
315 SYNC_OTHER(partChannel, ARG(channelName))
316 if (_channels.isEmpty() && !network()->isMe(this))
322 void IrcUser::partChannel(const QString &channelname)
324 IrcChannel *channel = network()->ircChannel(channelname);
326 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
329 partChannel(channel);
336 QList<IrcChannel *> channels = _channels.toList();
338 foreach(IrcChannel *channel, channels) {
339 disconnect(channel, 0, this, 0);
342 network()->removeIrcUser(this);
348 void IrcUser::channelDestroyed()
351 IrcChannel *channel = static_cast<IrcChannel *>(sender());
352 if (_channels.contains(channel)) {
353 _channels.remove(channel);
354 if (_channels.isEmpty() && !network()->isMe(this))
360 void IrcUser::setUserModes(const QString &modes)
362 if (_userModes != modes) {
365 emit userModesSet(modes);
370 void IrcUser::addUserModes(const QString &modes)
375 // Don't needlessly sync when no changes are made
376 bool changesMade = false;
377 for (int i = 0; i < modes.count(); i++) {
378 if (!_userModes.contains(modes[i])) {
379 _userModes += modes[i];
386 emit userModesAdded(modes);
391 void IrcUser::removeUserModes(const QString &modes)
396 for (int i = 0; i < modes.count(); i++) {
397 _userModes.remove(modes[i]);
400 emit userModesRemoved(modes);
404 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
406 _lastActivity[buffer] = time;
407 emit lastChannelActivityUpdated(buffer, time);
411 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
413 _lastSpokenTo[buffer] = time;
414 emit lastSpokenToUpdated(buffer, time);