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)
32 : SyncableObject(network),
34 _nick(nickFromMask(hostmask)),
35 _user(userFromMask(hostmask)),
36 _host(hostFromMask(hostmask)),
41 _idleTime(QDateTime::currentDateTime()),
52 QList<IrcChannel *> channels = _channels.toList();
53 foreach(IrcChannel *channel, channels) {
58 // ====================
60 // ====================
62 QString IrcUser::user() const {
66 QString IrcUser::host() const {
70 QString IrcUser::nick() const {
74 QString IrcUser::realName() const {
78 QString IrcUser::hostmask() const {
79 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
82 bool IrcUser::isAway() const {
86 QString IrcUser::awayMessage() const {
90 QString IrcUser::server() const {
94 QDateTime IrcUser::idleTime() const {
98 QString IrcUser::ircOperator() const {
102 int IrcUser::lastAwayMessage() const {
103 return _lastAwayMessage;
106 QString IrcUser::userModes() const {
110 QStringList IrcUser::channels() const {
111 QStringList chanList;
113 foreach(channel, _channels) {
114 chanList << channel->name();
119 Network* IrcUser::network() const {
123 QTextCodec *IrcUser::codecForEncoding() const {
124 return _codecForEncoding;
127 void IrcUser::setCodecForEncoding(const QString &name) {
128 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
131 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
132 _codecForEncoding = codec;
135 QTextCodec *IrcUser::codecForDecoding() const {
136 return _codecForDecoding;
139 void IrcUser::setCodecForDecoding(const QString &name) {
140 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
143 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
144 _codecForDecoding = codec;
147 QString IrcUser::decodeString(const QByteArray &text) const {
148 if(!codecForDecoding()) return network()->decodeString(text);
149 return ::decodeString(text, codecForDecoding());
152 QByteArray IrcUser::encodeString(const QString string) const {
153 if(codecForEncoding()) {
154 return codecForEncoding()->fromUnicode(string);
156 return network()->encodeString(string);
159 // ====================
161 // ====================
162 void IrcUser::setUser(const QString &user) {
163 if(!user.isEmpty() && _user != user) {
169 void IrcUser::setRealName(const QString &realName) {
170 if (!realName.isEmpty() && _realName != realName) {
171 _realName = realName;
172 emit realNameSet(realName);
176 void IrcUser::setAway(const bool &away) {
183 void IrcUser::setAwayMessage(const QString &awayMessage) {
184 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
185 _awayMessage = awayMessage;
186 emit awayMessageSet(awayMessage);
190 void IrcUser::setIdleTime(const QDateTime &idleTime) {
191 if(idleTime.isValid() && _idleTime != idleTime) {
192 _idleTime = idleTime;
193 emit idleTimeSet(idleTime);
197 void IrcUser::setServer(const QString &server) {
198 if(!server.isEmpty() && _server != server) {
200 emit serverSet(server);
204 void IrcUser::setIrcOperator(const QString &ircOperator) {
205 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
206 _ircOperator = ircOperator;
207 emit ircOperatorSet(ircOperator);
211 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
212 if(lastAwayMessage > _lastAwayMessage) {
213 _lastAwayMessage = lastAwayMessage;
214 emit lastAwayMessageSet(lastAwayMessage);
218 void IrcUser::setHost(const QString &host) {
219 if(!host.isEmpty() && _host != host) {
225 void IrcUser::setNick(const QString &nick) {
226 if(!nick.isEmpty() && nick != _nick) {
233 void IrcUser::updateObjectName() {
234 QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
235 QString oldName = objectName();
236 if(oldName != newName) {
237 setObjectName(newName);
238 emit renameObject(oldName, newName);
242 void IrcUser::updateHostmask(const QString &mask) {
243 if(mask == hostmask())
246 QString user = userFromMask(mask);
247 QString host = hostFromMask(mask);
252 void IrcUser::joinChannel(IrcChannel *channel) {
254 if(!_channels.contains(channel)) {
255 _channels.insert(channel);
257 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
258 emit channelJoined(channel->name());
262 void IrcUser::joinChannel(const QString &channelname) {
263 joinChannel(network()->newIrcChannel(channelname));
266 void IrcUser::partChannel(IrcChannel *channel) {
267 if(_channels.contains(channel)) {
268 _channels.remove(channel);
269 disconnect(channel, 0, this, 0);
271 emit channelParted(channel->name());
275 void IrcUser::partChannel(const QString &channelname) {
276 IrcChannel *channel = network()->ircChannel(channelname);
278 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
280 partChannel(channel);
284 void IrcUser::channelDestroyed() {
286 IrcChannel *channel = static_cast<IrcChannel*>(sender());
288 if(_channels.contains(channel)) {
289 _channels.remove(channel);
290 disconnect(channel, 0, this, 0);
294 void IrcUser::setUserModes(const QString &modes) {
296 emit userModesSet(modes);
299 void IrcUser::addUserMode(const QString &mode) {
300 if(!_userModes.contains(mode)) {
302 emit userModeAdded(mode);
306 void IrcUser::removeUserMode(const QString &mode) {
307 if(_userModes.contains(mode)) {
308 _userModes.remove(mode);
309 emit userModeRemoved(mode);
313 void IrcUser::initSetChannels(const QStringList channels) {
314 foreach(QString channel, channels) {
315 joinChannel(channel);