- Fixed a minor bug in IrcUser
[quassel.git] / src / common / ircuser.cpp
index dd60aa9..affd64b 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by The Quassel Team                             *
+ *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 #include "ircuser.h"
 #include "util.h"
 
-IrcUser::IrcUser(QObject *parent)
-  : QObject(parent) {
-}
+#include "networkinfo.h"
+#include "signalproxy.h"
+#include "ircchannel.h"
+
+#include <QTextCodec>
+#include <QDebug>
 
-IrcUser::IrcUser(const QString &hostmask, QObject *parent) 
-  : QObject(parent),
-    nick_(nickFromMask(hostmask)),
-    user_(userFromMask(hostmask)),
-    host_(hostFromMask(hostmask)) {
+IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
+  : QObject(networkinfo),
+    _initialized(false),
+    _nick(nickFromMask(hostmask)),
+    _user(userFromMask(hostmask)),
+    _host(hostFromMask(hostmask)),
+    networkInfo(networkinfo),
+    _codecForEncoding(0),
+    _codecForDecoding(0)
+{
+  updateObjectName();
 }
 
 IrcUser::~IrcUser() {
+  //qDebug() << nick() << "destroyed.";
 }
 
-void IrcUser::setUser(const QString &user) {
-  user_ = user;
+// ====================
+//  PUBLIC:
+// ====================
+bool IrcUser::initialized() const {
+  return _initialized;
 }
 
 QString IrcUser::user() const {
-  return user_;
+  return _user;
 }
 
-void IrcUser::setHost(const QString &host) {
-  host_ = host;
+QString IrcUser::host() const {
+  return _host;
 }
 
-QString IrcUser::host() const {
-  return host_;
+QString IrcUser::nick() const {
+  return _nick;
+}
+
+QString IrcUser::hostmask() const {
+  return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
+}
+
+QString IrcUser::userModes() const {
+  return _userModes;
+}
+
+QStringList IrcUser::channels() const {
+  QStringList chanList;
+  IrcChannel *channel;
+  foreach(channel, _channels) {
+    chanList << channel->name();
+  }
+  return chanList;
+}
+
+QTextCodec *IrcUser::codecForEncoding() const {
+  return _codecForEncoding;
+}
+
+void IrcUser::setCodecForEncoding(const QString &name) {
+  setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
+}
+
+void IrcUser::setCodecForEncoding(QTextCodec *codec) {
+  _codecForEncoding = codec;
+}
+
+QTextCodec *IrcUser::codecForDecoding() const {
+  return _codecForDecoding;
+}
+
+void IrcUser::setCodecForDecoding(const QString &name) {
+  setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
+}
+
+void IrcUser::setCodecForDecoding(QTextCodec *codec) {
+  _codecForDecoding = codec;
+}
+
+QString IrcUser::decodeString(const QByteArray &text) const {
+  if(!codecForDecoding()) return networkInfo->decodeString(text);
+  return ::decodeString(text, codecForDecoding());
+}
+
+QByteArray IrcUser::encodeString(const QString string) const {
+  if(codecForEncoding()) {
+    return codecForEncoding()->fromUnicode(string);
+  }
+  return networkInfo->encodeString(string);
+}
+
+// ====================
+//  PUBLIC SLOTS:
+// ====================
+void IrcUser::setUser(const QString &user) {
+  if(!user.isEmpty() && _user != user) {
+    _user = user;
+    emit userSet(user);
+  }
+}
+
+void IrcUser::setHost(const QString &host) {
+  if(!host.isEmpty() && _host != host) {
+    _host = host;
+    emit hostSet(host);
+  }
 }
 
 void IrcUser::setNick(const QString &nick) {
-  nick_ = nick;
+  if(!nick.isEmpty() && nick != _nick) {
+    _nick = nick;
+    updateObjectName();
+    emit nickSet(nick);
+  }
 }
 
-QString IrcUser::nick() const {
-  return nick_;
+void IrcUser::updateObjectName() {
+  QString newName = QString::number(networkInfo->networkId()) + "/" + _nick;
+  QString oldName = objectName();
+  if(oldName != newName) {
+    setObjectName(newName);
+    emit renameObject(oldName, newName);
+  }
 }
 
-void IrcUser::setUsermodes(const QSet<QString> &usermodes) {
-  usermodes_ = usermodes;
+void IrcUser::updateHostmask(const QString &mask) {
+  if(mask == hostmask())
+    return;
+
+  QString user = userFromMask(mask);
+  QString host = hostFromMask(mask);
+  setUser(user);
+  setHost(host);
 }
 
-QSet<QString> IrcUser::usermodes() const {
-  return usermodes_;
+void IrcUser::joinChannel(IrcChannel *channel) {
+  Q_ASSERT(channel);
+  if(!_channels.contains(channel)) {
+    _channels.insert(channel);
+    channel->join(this);
+    connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
+    emit channelJoined(channel->name());
+  }
 }
 
-void IrcUser::setChannelmode(const QString &channel, const QSet<QString> &channelmode) {
-  if(channelmodes_.contains(channel))
-    channelmodes_[channel] |= channelmode;
-  else
-    channelmodes_[channel] = channelmode;
+void IrcUser::joinChannel(const QString &channelname) {
+  joinChannel(networkInfo->newIrcChannel(channelname));
 }
 
-QSet<QString> IrcUser::channelmode(const QString &channel) const {
-  if(channelmodes_.contains(channel))
-    throw NoSuchChannelException();
-  else
-    return QSet<QString>();
+void IrcUser::partChannel(IrcChannel *channel) {
+  if(_channels.contains(channel)) {
+    _channels.remove(channel);
+    disconnect(channel, 0, this, 0);
+    channel->part(this);
+    emit channelParted(channel->name());
+  }
 }
 
-void IrcUser::updateChannelmode(const QString &channel, const QString &channelmode, bool add) {
-  if(add)
-    addChannelmode(channel, channelmode);
-  else
-    removeChannelmode(channel, channelmode);
+void IrcUser::partChannel(const QString &channelname) {
+  IrcChannel *channel = networkInfo->ircChannel(channelname);
+  if(channel == 0) {
+    qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
+  } else {
+    partChannel(channel);
+  }
 }
 
-void IrcUser::addChannelmode(const QString &channel, const QString &channelmode) {
-  if(!channelmodes_.contains(channel))
-    channelmodes_[channel] = QSet<QString>();
-  channelmodes_[channel] << channelmode;
+void IrcUser::channelDestroyed() {
+  // private slot!
+  IrcChannel *channel = static_cast<IrcChannel*>(sender());
+  Q_ASSERT(channel);
+  if(_channels.contains(channel)) {
+    _channels.remove(channel);
+    disconnect(channel, 0, this, 0);
+  }
 }
 
-void IrcUser::removeChannelmode(const QString &channel, const QString &channelmode) {
-  if(channelmodes_.contains(channel))
-    channelmodes_[channel].remove(channelmode);
+void IrcUser::setUserModes(const QString &modes) {
+  _userModes = modes;
+  emit userModesSet(modes);
 }
 
-QStringList IrcUser::channels() const {
-  return channelmodes_.keys();
+void IrcUser::addUserMode(const QString &mode) {
+  if(!_userModes.contains(mode)) {
+    _userModes += mode;
+    emit userModeAdded(mode);
+  }
 }
 
-void IrcUser::joinChannel(const QString &channel) {
-  if(!channelmodes_.contains(channel))
-    channelmodes_[channel] = QSet<QString>();
+void IrcUser::removeUserMode(const QString &mode) {
+  if(_userModes.contains(mode)) {
+    _userModes.remove(mode);
+    emit userModeRemoved(mode);
+  }
 }
 
-void IrcUser::partChannel(const QString &channel) {
-  channelmodes_.remove(channel);
+void IrcUser::initSetChannels(const QStringList channels) {
+  foreach(QString channel, channels) {
+    joinChannel(channel);
+  }
 }
+
+void IrcUser::setInitialized() {
+  _initialized = true;
+  emit initDone();
+}
+