1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include "signalproxy.h"
26 #include "ircchannel.h"
31 IrcUser::IrcUser(const QString &hostmask, Network *network) : SyncableObject(network),
33 _nick(nickFromMask(hostmask)),
34 _user(userFromMask(hostmask)),
35 _host(hostFromMask(hostmask)),
40 // _idleTime(QDateTime::currentDateTime()),
54 // ====================
56 // ====================
58 QString IrcUser::hostmask() const {
59 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
62 QDateTime IrcUser::idleTime() {
63 if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
64 _idleTime = QDateTime();
68 QStringList IrcUser::channels() const {
71 foreach(channel, _channels) {
72 chanList << channel->name();
78 void IrcUser::setCodecForEncoding(const QString &name) {
79 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
82 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
83 _codecForEncoding = codec;
86 void IrcUser::setCodecForDecoding(const QString &name) {
87 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
90 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
91 _codecForDecoding = codec;
94 QString IrcUser::decodeString(const QByteArray &text) const {
95 if(!codecForDecoding()) return network()->decodeString(text);
96 return ::decodeString(text, codecForDecoding());
99 QByteArray IrcUser::encodeString(const QString &string) const {
100 if(codecForEncoding()) {
101 return codecForEncoding()->fromUnicode(string);
103 return network()->encodeString(string);
106 // ====================
108 // ====================
109 void IrcUser::setUser(const QString &user) {
110 if(!user.isEmpty() && _user != user) {
116 void IrcUser::setRealName(const QString &realName) {
117 if (!realName.isEmpty() && _realName != realName) {
118 _realName = realName;
119 emit realNameSet(realName);
123 void IrcUser::setAway(const bool &away) {
130 void IrcUser::setAwayMessage(const QString &awayMessage) {
131 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
132 _awayMessage = awayMessage;
133 emit awayMessageSet(awayMessage);
137 void IrcUser::setIdleTime(const QDateTime &idleTime) {
138 if(idleTime.isValid() && _idleTime != idleTime) {
139 _idleTime = idleTime;
140 _idleTimeSet = QDateTime::currentDateTime();
141 emit idleTimeSet(idleTime);
145 void IrcUser::setLoginTime(const QDateTime &loginTime) {
146 if(loginTime.isValid() && _loginTime != loginTime) {
147 _loginTime = loginTime;
148 emit loginTimeSet(loginTime);
152 void IrcUser::setServer(const QString &server) {
153 if(!server.isEmpty() && _server != server) {
155 emit serverSet(server);
159 void IrcUser::setIrcOperator(const QString &ircOperator) {
160 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
161 _ircOperator = ircOperator;
162 emit ircOperatorSet(ircOperator);
166 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
167 if(lastAwayMessage > _lastAwayMessage) {
168 _lastAwayMessage = lastAwayMessage;
169 emit lastAwayMessageSet(lastAwayMessage);
173 void IrcUser::setHost(const QString &host) {
174 if(!host.isEmpty() && _host != host) {
180 void IrcUser::setNick(const QString &nick) {
181 if(!nick.isEmpty() && nick != _nick) {
188 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
189 if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
190 _whoisServiceReply = whoisServiceReply;
191 emit whoisServiceReplySet(whoisServiceReply);
195 void IrcUser::setSuserHost(const QString &suserHost) {
196 if(!suserHost.isEmpty() && suserHost != _suserHost) {
197 _suserHost = suserHost;
198 emit suserHostSet(suserHost);
202 void IrcUser::updateObjectName() {
203 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
206 void IrcUser::updateHostmask(const QString &mask) {
207 if(mask == hostmask())
210 QString user = userFromMask(mask);
211 QString host = hostFromMask(mask);
216 void IrcUser::joinChannel(IrcChannel *channel) {
218 if(!_channels.contains(channel)) {
219 _channels.insert(channel);
220 channel->joinIrcUsers(this);
221 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
225 void IrcUser::joinChannel(const QString &channelname) {
226 joinChannel(network()->newIrcChannel(channelname));
229 void IrcUser::partChannel(IrcChannel *channel) {
230 if(_channels.contains(channel)) {
231 _channels.remove(channel);
232 disconnect(channel, 0, this, 0);
234 emit channelParted(channel->name());
235 if(_channels.isEmpty() && network()->isMe(this))
240 void IrcUser::partChannel(const QString &channelname) {
241 IrcChannel *channel = network()->ircChannel(channelname);
243 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
245 partChannel(channel);
249 void IrcUser::channelDestroyed() {
251 IrcChannel *channel = static_cast<IrcChannel*>(sender());
252 if(_channels.contains(channel)) {
253 _channels.remove(channel);
254 if(_channels.isEmpty())
259 void IrcUser::setUserModes(const QString &modes) {
261 emit userModesSet(modes);
264 void IrcUser::addUserModes(const QString &modes) {
268 for(int i = 0; i < modes.count(); i++) {
269 if(!_userModes.contains(modes[i]))
270 _userModes += modes[i];
273 emit userModesAdded(modes);
276 void IrcUser::removeUserModes(const QString &modes) {
280 for(int i = 0; i < modes.count(); i++) {
281 _userModes.remove(modes[i]);
283 emit userModesRemoved(modes);