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() {
90 if(QDateTime::currentDateTime().toTime_t() - _idleTimeSet.toTime_t() > 1200)
91 _idleTime = QDateTime();
95 QDateTime IrcUser::loginTime() const {
99 QString IrcUser::ircOperator() const {
103 int IrcUser::lastAwayMessage() const {
104 return _lastAwayMessage;
107 QString IrcUser::userModes() const {
111 QStringList IrcUser::channels() const {
112 QStringList chanList;
114 foreach(channel, _channels) {
115 chanList << channel->name();
120 Network* IrcUser::network() const {
124 QTextCodec *IrcUser::codecForEncoding() const {
125 return _codecForEncoding;
128 void IrcUser::setCodecForEncoding(const QString &name) {
129 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
132 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
133 _codecForEncoding = codec;
136 QTextCodec *IrcUser::codecForDecoding() const {
137 return _codecForDecoding;
140 void IrcUser::setCodecForDecoding(const QString &name) {
141 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
144 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
145 _codecForDecoding = codec;
148 QString IrcUser::decodeString(const QByteArray &text) const {
149 if(!codecForDecoding()) return network()->decodeString(text);
150 return ::decodeString(text, codecForDecoding());
153 QByteArray IrcUser::encodeString(const QString &string) const {
154 if(codecForEncoding()) {
155 return codecForEncoding()->fromUnicode(string);
157 return network()->encodeString(string);
160 // ====================
162 // ====================
163 void IrcUser::setUser(const QString &user) {
164 if(!user.isEmpty() && _user != user) {
170 void IrcUser::setRealName(const QString &realName) {
171 if (!realName.isEmpty() && _realName != realName) {
172 _realName = realName;
173 emit realNameSet(realName);
177 void IrcUser::setAway(const bool &away) {
184 void IrcUser::setAwayMessage(const QString &awayMessage) {
185 if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
186 _awayMessage = awayMessage;
187 emit awayMessageSet(awayMessage);
191 void IrcUser::setIdleTime(const QDateTime &idleTime) {
192 if(idleTime.isValid() && _idleTime != idleTime) {
193 _idleTime = idleTime;
194 _idleTimeSet = QDateTime::currentDateTime();
195 emit idleTimeSet(idleTime);
199 void IrcUser::setLoginTime(const QDateTime &loginTime) {
200 if(loginTime.isValid() && _loginTime != loginTime) {
201 _loginTime = loginTime;
202 emit loginTimeSet(loginTime);
206 void IrcUser::setServer(const QString &server) {
207 if(!server.isEmpty() && _server != server) {
209 emit serverSet(server);
213 void IrcUser::setIrcOperator(const QString &ircOperator) {
214 if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
215 _ircOperator = ircOperator;
216 emit ircOperatorSet(ircOperator);
220 void IrcUser::setLastAwayMessage(const int &lastAwayMessage) {
221 if(lastAwayMessage > _lastAwayMessage) {
222 _lastAwayMessage = lastAwayMessage;
223 emit lastAwayMessageSet(lastAwayMessage);
227 void IrcUser::setHost(const QString &host) {
228 if(!host.isEmpty() && _host != host) {
234 void IrcUser::setNick(const QString &nick) {
235 if(!nick.isEmpty() && nick != _nick) {
242 void IrcUser::updateObjectName() {
243 renameObject(QString::number(network()->networkId().toInt()) + "/" + _nick);
246 void IrcUser::updateHostmask(const QString &mask) {
247 if(mask == hostmask())
250 QString user = userFromMask(mask);
251 QString host = hostFromMask(mask);
256 void IrcUser::joinChannel(IrcChannel *channel) {
258 if(!_channels.contains(channel)) {
259 _channels.insert(channel);
260 channel->joinIrcUsers(this);
261 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
265 void IrcUser::joinChannel(const QString &channelname) {
266 joinChannel(network()->newIrcChannel(channelname));
269 void IrcUser::partChannel(IrcChannel *channel) {
270 if(_channels.contains(channel)) {
271 _channels.remove(channel);
272 disconnect(channel, 0, this, 0);
274 emit channelParted(channel->name());
275 if(_channels.isEmpty() && network()->isMe(this))
280 void IrcUser::partChannel(const QString &channelname) {
281 IrcChannel *channel = network()->ircChannel(channelname);
283 qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
285 partChannel(channel);
289 void IrcUser::channelDestroyed() {
291 IrcChannel *channel = static_cast<IrcChannel*>(sender());
292 if(_channels.contains(channel)) {
293 _channels.remove(channel);
294 if(_channels.isEmpty())
299 void IrcUser::setUserModes(const QString &modes) {
301 emit userModesSet(modes);
304 void IrcUser::addUserModes(const QString &modes) {
308 for(int i = 0; i < modes.count(); i++) {
309 if(!_userModes.contains(modes[i]))
310 _userModes += modes[i];
313 emit userModesAdded(modes);
316 void IrcUser::removeUserModes(const QString &modes) {
320 for(int i = 0; i < modes.count(); i++) {
321 _userModes.remove(modes[i]);
323 emit userModesRemoved(modes);