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()),
53 // ====================
55 // ====================
57 QString IrcUser::user() const {
61 QString IrcUser::host() const {
65 QString IrcUser::nick() const {
69 QString IrcUser::realName() const {
73 QString IrcUser::hostmask() const {
74 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
77 bool IrcUser::isAway() const {
81 QString IrcUser::awayMessage() const {
85 QString IrcUser::server() const {
89 QDateTime IrcUser::idleTime() const {
93 QString IrcUser::ircOperator() const {
97 int IrcUser::lastAwayMessage() const {
98 return _lastAwayMessage;
101 QString IrcUser::userModes() const {
105 QStringList IrcUser::channels() const {
106 QStringList chanList;
108 foreach(channel, _channels) {
109 chanList << channel->name();
114 Network* IrcUser::network() const {
118 QTextCodec *IrcUser::codecForEncoding() const {
119 return _codecForEncoding;
122 void IrcUser::setCodecForEncoding(const QString &name) {
123 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
126 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
127 _codecForEncoding = codec;
130 QTextCodec *IrcUser::codecForDecoding() const {
131 return _codecForDecoding;
134 void IrcUser::setCodecForDecoding(const QString &name) {
135 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
138 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
139 _codecForDecoding = codec;
142 QString IrcUser::decodeString(const QByteArray &text) const {
143 if(!codecForDecoding()) return network()->decodeString(text);
144 return ::decodeString(text, codecForDecoding());
147 QByteArray IrcUser::encodeString(const QString &string) const {
148 if(codecForEncoding()) {
149 return codecForEncoding()->fromUnicode(string);
151 return network()->encodeString(string);
154 // ====================
156 // ====================
157 void IrcUser::setUser(const QString &user) {
158 if(!user.isEmpty() && _user != user) {
164 void IrcUser::setRealName(const QString &realName) {
165 if (!realName.isEmpty() && _realName != realName) {
166 _realName = realName;
167 emit realNameSet(realName);
171 void IrcUser::setAway(const bool &away) {
178 void IrcUser::setAwayMessage(const QString &awayMessage) {
179 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
180 _awayMessage = awayMessage;
181 emit awayMessageSet(awayMessage);
185 void IrcUser::setIdleTime(const QDateTime &idleTime) {
186 if(idleTime.isValid() && _idleTime != idleTime) {
187 _idleTime = idleTime;
188 emit idleTimeSet(idleTime);
192 void IrcUser::setServer(const QString &server) {
193 if(!server.isEmpty() && _server != server) {
195 emit serverSet(server);
199 void IrcUser::setIrcOperator(const QString &ircOperator) {
200 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
201 _ircOperator = ircOperator;
202 emit ircOperatorSet(ircOperator);
206 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
207 if(lastAwayMessage > _lastAwayMessage) {
208 _lastAwayMessage = lastAwayMessage;
209 emit lastAwayMessageSet(lastAwayMessage);
213 void IrcUser::setHost(const QString &host) {
214 if(!host.isEmpty() && _host != host) {
220 void IrcUser::setNick(const QString &nick) {
221 if(!nick.isEmpty() && nick != _nick) {
228 void IrcUser::updateObjectName() {
229 QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
230 QString oldName = objectName();
231 if(oldName != newName) {
232 setObjectName(newName);
233 emit renameObject(oldName, newName);
237 void IrcUser::updateHostmask(const QString &mask) {
238 if(mask == hostmask())
241 QString user = userFromMask(mask);
242 QString host = hostFromMask(mask);
247 void IrcUser::joinChannel(IrcChannel *channel) {
249 if(!_channels.contains(channel)) {
250 _channels.insert(channel);
252 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
253 emit channelJoined(channel->name());
257 void IrcUser::joinChannel(const QString &channelname) {
258 joinChannel(network()->newIrcChannel(channelname));
261 void IrcUser::partChannel(IrcChannel *channel) {
262 if(_channels.contains(channel)) {
263 _channels.remove(channel);
264 disconnect(channel, 0, this, 0);
266 emit channelParted(channel->name());
270 void IrcUser::partChannel(const QString &channelname) {
271 IrcChannel *channel = network()->ircChannel(channelname);
273 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
275 partChannel(channel);
279 void IrcUser::channelDestroyed() {
281 IrcChannel *channel = static_cast<IrcChannel*>(sender());
282 if(_channels.contains(channel)) {
283 _channels.remove(channel);
287 void IrcUser::setUserModes(const QString &modes) {
289 emit userModesSet(modes);
292 void IrcUser::addUserMode(const QString &mode) {
293 if(!_userModes.contains(mode)) {
295 emit userModeAdded(mode);
299 void IrcUser::removeUserMode(const QString &mode) {
300 if(_userModes.contains(mode)) {
301 _userModes.remove(mode);
302 emit userModeRemoved(mode);
306 void IrcUser::initSetChannels(const QStringList channels) {
307 foreach(QString channel, channels) {
308 joinChannel(channel);