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()),
54 // ====================
56 // ====================
58 QString IrcUser::user() const {
62 QString IrcUser::host() const {
66 QString IrcUser::nick() const {
70 QString IrcUser::realName() const {
74 QString IrcUser::hostmask() const {
75 return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
78 bool IrcUser::isAway() const {
82 QString IrcUser::awayMessage() const {
86 QString IrcUser::server() const {
90 QDateTime IrcUser::idleTime() const {
94 QString IrcUser::ircOperator() const {
98 int IrcUser::lastAwayMessage() const {
99 return _lastAwayMessage;
102 QString IrcUser::userModes() const {
106 QStringList IrcUser::channels() const {
107 QStringList chanList;
109 foreach(channel, _channels) {
110 chanList << channel->name();
115 Network* IrcUser::network() const {
119 QTextCodec *IrcUser::codecForEncoding() const {
120 return _codecForEncoding;
123 void IrcUser::setCodecForEncoding(const QString &name) {
124 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
127 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
128 _codecForEncoding = codec;
131 QTextCodec *IrcUser::codecForDecoding() const {
132 return _codecForDecoding;
135 void IrcUser::setCodecForDecoding(const QString &name) {
136 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
139 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
140 _codecForDecoding = codec;
143 QString IrcUser::decodeString(const QByteArray &text) const {
144 if(!codecForDecoding()) return network()->decodeString(text);
145 return ::decodeString(text, codecForDecoding());
148 QByteArray IrcUser::encodeString(const QString string) const {
149 if(codecForEncoding()) {
150 return codecForEncoding()->fromUnicode(string);
152 return network()->encodeString(string);
155 // ====================
157 // ====================
158 void IrcUser::setUser(const QString &user) {
159 if(!user.isEmpty() && _user != user) {
165 void IrcUser::setRealName(const QString &realName) {
166 if (!realName.isEmpty() && _realName != realName) {
167 _realName = realName;
168 emit realNameSet(realName);
172 void IrcUser::setAway(const bool &away) {
179 void IrcUser::setAwayMessage(const QString &awayMessage) {
180 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
181 _awayMessage = awayMessage;
182 emit awayMessageSet(awayMessage);
186 void IrcUser::setIdleTime(const QDateTime &idleTime) {
187 if(idleTime.isValid() && _idleTime != idleTime) {
188 _idleTime = idleTime;
189 emit idleTimeSet(idleTime);
193 void IrcUser::setServer(const QString &server) {
194 if(!server.isEmpty() && _server != server) {
196 emit serverSet(server);
200 void IrcUser::setIrcOperator(const QString &ircOperator) {
201 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
202 _ircOperator = ircOperator;
203 emit ircOperatorSet(ircOperator);
207 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
208 if(lastAwayMessage > _lastAwayMessage) {
209 _lastAwayMessage = lastAwayMessage;
210 emit lastAwayMessageSet(lastAwayMessage);
214 void IrcUser::setHost(const QString &host) {
215 if(!host.isEmpty() && _host != host) {
221 void IrcUser::setNick(const QString &nick) {
222 if(!nick.isEmpty() && nick != _nick) {
229 void IrcUser::updateObjectName() {
230 QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
231 QString oldName = objectName();
232 if(oldName != newName) {
233 setObjectName(newName);
234 emit renameObject(oldName, newName);
238 void IrcUser::updateHostmask(const QString &mask) {
239 if(mask == hostmask())
242 QString user = userFromMask(mask);
243 QString host = hostFromMask(mask);
248 void IrcUser::joinChannel(IrcChannel *channel) {
250 if(!_channels.contains(channel)) {
251 _channels.insert(channel);
253 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
254 emit channelJoined(channel->name());
258 void IrcUser::joinChannel(const QString &channelname) {
259 joinChannel(network()->newIrcChannel(channelname));
262 void IrcUser::partChannel(IrcChannel *channel) {
263 if(_channels.contains(channel)) {
264 _channels.remove(channel);
265 disconnect(channel, 0, this, 0);
267 emit channelParted(channel->name());
271 void IrcUser::partChannel(const QString &channelname) {
272 IrcChannel *channel = network()->ircChannel(channelname);
274 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
276 partChannel(channel);
280 void IrcUser::channelDestroyed() {
282 IrcChannel *channel = static_cast<IrcChannel*>(sender());
283 if(_channels.contains(channel)) {
284 _channels.remove(channel);
288 void IrcUser::setUserModes(const QString &modes) {
290 emit userModesSet(modes);
293 void IrcUser::addUserMode(const QString &mode) {
294 if(!_userModes.contains(mode)) {
296 emit userModeAdded(mode);
300 void IrcUser::removeUserMode(const QString &mode) {
301 if(_userModes.contains(mode)) {
302 _userModes.remove(mode);
303 emit userModeRemoved(mode);
307 void IrcUser::initSetChannels(const QStringList channels) {
308 foreach(QString channel, channels) {
309 joinChannel(channel);