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 ***************************************************************************/
24 #include "signalproxy.h"
25 #include "ircchannel.h"
30 IrcUser::IrcUser(const QString &hostmask, Network *network)
31 : SyncableObject(network),
33 _nick(network->encodeServerString(nickFromMask(hostmask))),
34 _user(network->encodeServerString(userFromMask(hostmask))),
35 _host(network->encodeServerString(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()) {
111 QByteArray newUser = network()->encodeServerString(user);
112 if(newUser != _user) {
119 void IrcUser::setRealName(const QString &realName) {
120 if(!realName.isEmpty()) {
121 QByteArray newRealName = network()->encodeServerString(realName);
122 if(newRealName != _realName) {
123 _realName = newRealName;
124 emit realNameSet(realName);
129 void IrcUser::setAway(const bool &away) {
136 void IrcUser::setAwayMessage(const QString &awayMessage) {
137 if(!awayMessage.isEmpty()) {
138 QByteArray newAwayMessage = network()->encodeServerString(awayMessage);
139 if(newAwayMessage != _awayMessage) {
140 _awayMessage = newAwayMessage;
141 emit awayMessageSet(awayMessage);
146 void IrcUser::setIdleTime(const QDateTime &idleTime) {
147 if(idleTime.isValid() && _idleTime != idleTime) {
148 _idleTime = idleTime;
149 _idleTimeSet = QDateTime::currentDateTime();
150 emit idleTimeSet(idleTime);
154 void IrcUser::setLoginTime(const QDateTime &loginTime) {
155 if(loginTime.isValid() && _loginTime != loginTime) {
156 _loginTime = loginTime;
157 emit loginTimeSet(loginTime);
161 void IrcUser::setServer(const QString &server) {
162 if(!server.isEmpty() && _server != server) {
164 emit serverSet(server);
168 void IrcUser::setIrcOperator(const QString &ircOperator) {
169 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
170 _ircOperator = ircOperator;
171 emit ircOperatorSet(ircOperator);
175 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
176 if(lastAwayMessage > _lastAwayMessage) {
177 _lastAwayMessage = lastAwayMessage;
178 emit lastAwayMessageSet(lastAwayMessage);
182 void IrcUser::setHost(const QString &host) {
183 if(!host.isEmpty()) {
184 QByteArray newHost = network()->encodeServerString(host);
185 if(newHost != _host) {
192 void IrcUser::setNick(const QString &nick) {
193 if(!nick.isEmpty()) {
194 QByteArray newNick = network()->encodeServerString(nick);
195 if(newNick != _nick) {
203 void IrcUser::setWhoisServiceReply(const QString &whoisServiceReply) {
204 if(!whoisServiceReply.isEmpty() && whoisServiceReply != _whoisServiceReply) {
205 _whoisServiceReply = whoisServiceReply;
206 emit whoisServiceReplySet(whoisServiceReply);
210 void IrcUser::setSuserHost(const QString &suserHost) {
211 if(!suserHost.isEmpty() && suserHost != _suserHost) {
212 _suserHost = suserHost;
213 emit suserHostSet(suserHost);
217 void IrcUser::updateObjectName() {
218 renameObject(QString::number(network()->networkId().toInt()) + "/" + nick());
221 void IrcUser::updateHostmask(const QString &mask) {
222 if(mask == hostmask())
225 QString user = userFromMask(mask);
226 QString host = hostFromMask(mask);
231 void IrcUser::joinChannel(IrcChannel *channel) {
233 if(!_channels.contains(channel)) {
234 _channels.insert(channel);
235 channel->joinIrcUsers(this);
236 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
240 void IrcUser::joinChannel(const QString &channelname) {
241 joinChannel(network()->newIrcChannel(channelname));
244 void IrcUser::partChannel(IrcChannel *channel) {
245 if(_channels.contains(channel)) {
246 _channels.remove(channel);
247 disconnect(channel, 0, this, 0);
249 emit channelParted(channel->name());
250 if(_channels.isEmpty() && !network()->isMe(this))
255 void IrcUser::partChannel(const QString &channelname) {
256 IrcChannel *channel = network()->ircChannel(channelname);
258 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
260 partChannel(channel);
264 void IrcUser::channelDestroyed() {
266 IrcChannel *channel = static_cast<IrcChannel*>(sender());
267 if(_channels.contains(channel)) {
268 _channels.remove(channel);
269 if(_channels.isEmpty())
274 void IrcUser::setUserModes(const QString &modes) {
276 emit userModesSet(modes);
279 void IrcUser::addUserModes(const QString &modes) {
283 for(int i = 0; i < modes.count(); i++) {
284 if(!_userModes.contains(modes[i]))
285 _userModes += modes[i];
288 emit userModesAdded(modes);
291 void IrcUser::removeUserModes(const QString &modes) {
295 for(int i = 0; i < modes.count(); i++) {
296 _userModes.remove(modes[i]);
298 emit userModesRemoved(modes);