1 /***************************************************************************
2 * Copyright (C) 2005-2016 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::setAccount(const QString &account)
151 if (_account != account) {
158 void IrcUser::setAway(const bool &away)
168 void IrcUser::setAwayMessage(const QString &awayMessage)
170 if (!awayMessage.isEmpty() && _awayMessage != awayMessage) {
171 _awayMessage = awayMessage;
172 SYNC(ARG(awayMessage))
177 void IrcUser::setIdleTime(const QDateTime &idleTime)
179 if (idleTime.isValid() && _idleTime != idleTime) {
180 _idleTime = idleTime;
181 _idleTimeSet = QDateTime::currentDateTime();
187 void IrcUser::setLoginTime(const QDateTime &loginTime)
189 if (loginTime.isValid() && _loginTime != loginTime) {
190 _loginTime = loginTime;
196 void IrcUser::setServer(const QString &server)
198 if (!server.isEmpty() && _server != server) {
205 void IrcUser::setIrcOperator(const QString &ircOperator)
207 if (!ircOperator.isEmpty() && _ircOperator != ircOperator) {
208 _ircOperator = ircOperator;
209 SYNC(ARG(ircOperator))
214 void IrcUser::setLastAwayMessage(const int &lastAwayMessage)
216 if (lastAwayMessage > _lastAwayMessage) {
217 _lastAwayMessage = lastAwayMessage;
218 SYNC(ARG(lastAwayMessage))
223 void IrcUser::setHost(const QString &host)
225 if (!host.isEmpty() && _host != host) {
232 void IrcUser::setNick(const QString &nick)
234 if (!nick.isEmpty() && nick != _nick) {
243 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply)
245 if (!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
246 _whoisServiceReply = whoisServiceReply;
247 SYNC(ARG(whoisServiceReply))
252 void IrcUser::setSuserHost(const QString &suserHost)
254 if (!suserHost.isEmpty() && suserHost != _suserHost) {
255 _suserHost = suserHost;
261 void IrcUser::setEncrypted(bool encrypted)
263 _encrypted = encrypted;
264 emit encryptedSet(encrypted);
269 void IrcUser::updateObjectName()
271 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
275 void IrcUser::updateHostmask(const QString &mask)
277 if (mask == hostmask())
280 QString user = userFromMask(mask);
281 QString host = hostFromMask(mask);
287 void IrcUser::joinChannel(IrcChannel *channel, bool skip_channel_join)
290 if (!_channels.contains(channel)) {
291 _channels.insert(channel);
292 if (!skip_channel_join)
293 channel->joinIrcUser(this);
298 void IrcUser::joinChannel(const QString &channelname)
300 joinChannel(network()->newIrcChannel(channelname));
304 void IrcUser::partChannel(IrcChannel *channel)
306 if (_channels.contains(channel)) {
307 _channels.remove(channel);
308 disconnect(channel, 0, this, 0);
310 QString channelName = channel->name();
311 SYNC_OTHER(partChannel, ARG(channelName))
312 if (_channels.isEmpty() && !network()->isMe(this))
318 void IrcUser::partChannel(const QString &channelname)
320 IrcChannel *channel = network()->ircChannel(channelname);
322 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
325 partChannel(channel);
332 QList<IrcChannel *> channels = _channels.toList();
334 foreach(IrcChannel *channel, channels) {
335 disconnect(channel, 0, this, 0);
338 network()->removeIrcUser(this);
344 void IrcUser::channelDestroyed()
347 IrcChannel *channel = static_cast<IrcChannel *>(sender());
348 if (_channels.contains(channel)) {
349 _channels.remove(channel);
350 if (_channels.isEmpty() && !network()->isMe(this))
356 void IrcUser::setUserModes(const QString &modes)
358 if (_userModes != modes) {
361 emit userModesSet(modes);
366 void IrcUser::addUserModes(const QString &modes)
371 // Don't needlessly sync when no changes are made
372 bool changesMade = false;
373 for (int i = 0; i < modes.count(); i++) {
374 if (!_userModes.contains(modes[i])) {
375 _userModes += modes[i];
382 emit userModesAdded(modes);
387 void IrcUser::removeUserModes(const QString &modes)
392 for (int i = 0; i < modes.count(); i++) {
393 _userModes.remove(modes[i]);
396 emit userModesRemoved(modes);
400 void IrcUser::setLastChannelActivity(BufferId buffer, const QDateTime &time)
402 _lastActivity[buffer] = time;
403 emit lastChannelActivityUpdated(buffer, time);
407 void IrcUser::setLastSpokenTo(BufferId buffer, const QDateTime &time)
409 _lastSpokenTo[buffer] = time;
410 emit lastSpokenToUpdated(buffer, time);