- Fixed a minor bug in IrcUser
[quassel.git] / src / common / ircuser.cpp
index 01bc963..affd64b 100644 (file)
@@ -25,6 +25,7 @@
 #include "signalproxy.h"
 #include "ircchannel.h"
 
+#include <QTextCodec>
 #include <QDebug>
 
 IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
@@ -33,13 +34,15 @@ IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
     _nick(nickFromMask(hostmask)),
     _user(userFromMask(hostmask)),
     _host(hostFromMask(hostmask)),
-    networkInfo(networkinfo)
+    networkInfo(networkinfo),
+    _codecForEncoding(0),
+    _codecForDecoding(0)
 {
   updateObjectName();
 }
 
 IrcUser::~IrcUser() {
-  qDebug() << nick() << "destroyed.";
+  //qDebug() << nick() << "destroyed.";
 }
 
 // ====================
@@ -78,6 +81,42 @@ QStringList IrcUser::channels() const {
   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:
 // ====================
@@ -97,7 +136,6 @@ void IrcUser::setHost(const QString &host) {
 
 void IrcUser::setNick(const QString &nick) {
   if(!nick.isEmpty() && nick != _nick) {
-    QString oldnick(_nick);
     _nick = nick;
     updateObjectName();
     emit nickSet(nick);
@@ -105,14 +143,14 @@ void IrcUser::setNick(const QString &nick) {
 }
 
 void IrcUser::updateObjectName() {
-  QString oldName(objectName());
-  setObjectName(QString::number(networkInfo->networkId()) + "/" + _nick);
-  if(!oldName.isEmpty()) {
-    emit renameObject(oldName, objectName());
+  QString newName = QString::number(networkInfo->networkId()) + "/" + _nick;
+  QString oldName = objectName();
+  if(oldName != newName) {
+    setObjectName(newName);
+    emit renameObject(oldName, newName);
   }
 }
 
-
 void IrcUser::updateHostmask(const QString &mask) {
   if(mask == hostmask())
     return;
@@ -126,9 +164,9 @@ void IrcUser::updateHostmask(const QString &mask) {
 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()));
-    _channels.insert(channel);
     emit channelJoined(channel->name());
   }
 }