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 //qDebug() << nick() << "destroyed.";
55 // ====================
57 // ====================
59 QString IrcUser::user() const {
63 QString IrcUser::host() const {
67 QString IrcUser::nick() const {
71 QString IrcUser::realName() const {
75 QString IrcUser::hostmask() const {
76 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
79 bool IrcUser::isAway() const {
83 QString IrcUser::awayMessage() const {
87 QString IrcUser::server() const {
91 QDateTime IrcUser::idleTime() const {
95 QString IrcUser::ircOperator() const {
99 int IrcUser::lastAwayMessage() const {
100 return _lastAwayMessage;
103 QString IrcUser::userModes() const {
107 QStringList IrcUser::channels() const {
108 QStringList chanList;
110 foreach(channel, _channels) {
111 chanList << channel->name();
116 Network* IrcUser::network() const {
120 QTextCodec *IrcUser::codecForEncoding() const {
121 return _codecForEncoding;
124 void IrcUser::setCodecForEncoding(const QString &name) {
125 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
128 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
129 _codecForEncoding = codec;
132 QTextCodec *IrcUser::codecForDecoding() const {
133 return _codecForDecoding;
136 void IrcUser::setCodecForDecoding(const QString &name) {
137 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
140 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
141 _codecForDecoding = codec;
144 QString IrcUser::decodeString(const QByteArray &text) const {
145 if(!codecForDecoding()) return network()->decodeString(text);
146 return ::decodeString(text, codecForDecoding());
149 QByteArray IrcUser::encodeString(const QString string) const {
150 if(codecForEncoding()) {
151 return codecForEncoding()->fromUnicode(string);
153 return network()->encodeString(string);
156 // ====================
158 // ====================
159 void IrcUser::setUser(const QString &user) {
160 if(!user.isEmpty() && _user != user) {
166 void IrcUser::setRealName(const QString &realName) {
167 if (!realName.isEmpty() && _realName != realName) {
168 _realName = realName;
169 emit realNameSet(realName);
173 void IrcUser::setAway(const bool &away) {
180 void IrcUser::setAwayMessage(const QString &awayMessage) {
181 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
182 _awayMessage = awayMessage;
183 emit awayMessageSet(awayMessage);
187 void IrcUser::setIdleTime(const QDateTime &idleTime) {
188 if(idleTime.isValid() && _idleTime != idleTime) {
189 _idleTime = idleTime;
190 emit idleTimeSet(idleTime);
194 void IrcUser::setServer(const QString &server) {
195 if(!server.isEmpty() && _server != server) {
197 emit serverSet(server);
201 void IrcUser::setIrcOperator(const QString &ircOperator) {
202 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
203 _ircOperator = ircOperator;
204 emit ircOperatorSet(ircOperator);
208 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
209 if(lastAwayMessage > _lastAwayMessage) {
210 _lastAwayMessage = lastAwayMessage;
211 emit lastAwayMessageSet(lastAwayMessage);
215 void IrcUser::setHost(const QString &host) {
216 if(!host.isEmpty() && _host != host) {
222 void IrcUser::setNick(const QString &nick) {
223 if(!nick.isEmpty() && nick != _nick) {
230 void IrcUser::updateObjectName() {
231 QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
232 QString oldName = objectName();
233 if(oldName != newName) {
234 setObjectName(newName);
235 emit renameObject(oldName, newName);
239 void IrcUser::updateHostmask(const QString &mask) {
240 if(mask == hostmask())
243 QString user = userFromMask(mask);
244 QString host = hostFromMask(mask);
249 void IrcUser::joinChannel(IrcChannel *channel) {
251 if(!_channels.contains(channel)) {
252 _channels.insert(channel);
254 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
255 emit channelJoined(channel->name());
259 void IrcUser::joinChannel(const QString &channelname) {
260 joinChannel(network()->newIrcChannel(channelname));
263 void IrcUser::partChannel(IrcChannel *channel) {
264 if(_channels.contains(channel)) {
265 _channels.remove(channel);
266 disconnect(channel, 0, this, 0);
268 emit channelParted(channel->name());
272 void IrcUser::partChannel(const QString &channelname) {
273 IrcChannel *channel = network()->ircChannel(channelname);
275 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
277 partChannel(channel);
281 void IrcUser::channelDestroyed() {
283 IrcChannel *channel = static_cast<IrcChannel*>(sender());
285 if(_channels.contains(channel)) {
286 _channels.remove(channel);
287 disconnect(channel, 0, this, 0);
291 void IrcUser::setUserModes(const QString &modes) {
293 emit userModesSet(modes);
296 void IrcUser::addUserMode(const QString &mode) {
297 if(!_userModes.contains(mode)) {
299 emit userModeAdded(mode);
303 void IrcUser::removeUserMode(const QString &mode) {
304 if(_userModes.contains(mode)) {
305 _userModes.remove(mode);
306 emit userModeRemoved(mode);
310 void IrcUser::initSetChannels(const QStringList channels) {
311 foreach(QString channel, channels) {
312 joinChannel(channel);