figured this would probably be a good Idea for the ircusers too
[quassel.git] / src / common / ircuser.cpp
index 665b10f..01bc963 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        *
@@ -35,7 +35,11 @@ IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
     _host(hostFromMask(hostmask)),
     networkInfo(networkinfo)
 {
-  setObjectName(QString::number(networkInfo->networkId()) + "/" + IrcUser::hostmask());
+  updateObjectName();
+}
+
+IrcUser::~IrcUser() {
+  qDebug() << nick() << "destroyed.";
 }
 
 // ====================
@@ -66,12 +70,12 @@ QString IrcUser::userModes() const {
 }
 
 QStringList IrcUser::channels() const {
-  return _channels.toList();
-}
-
-void IrcUser::updateObjectName() {
-  setObjectName(QString::number(networkInfo->networkId()) + "/" +  hostmask());
-  emit objectNameSet();
+  QStringList chanList;
+  IrcChannel *channel;
+  foreach(channel, _channels) {
+    chanList << channel->name();
+  }
+  return chanList;
 }
 
 // ====================
@@ -81,9 +85,6 @@ void IrcUser::setUser(const QString &user) {
   if(!user.isEmpty() && _user != user) {
     _user = user;
     emit userSet(user);
-
-    setObjectName(hostmask());
-    emit objectNameSet();
   }
 }
 
@@ -91,7 +92,6 @@ void IrcUser::setHost(const QString &host) {
   if(!host.isEmpty() && _host != host) {
     _host = host;
     emit hostSet(host);
-    updateObjectName();
   }
 }
 
@@ -99,53 +99,69 @@ void IrcUser::setNick(const QString &nick) {
   if(!nick.isEmpty() && nick != _nick) {
     QString oldnick(_nick);
     _nick = nick;
-    emit nickSet(nick);
     updateObjectName();
+    emit nickSet(nick);
+  }
+}
+
+void IrcUser::updateObjectName() {
+  QString oldName(objectName());
+  setObjectName(QString::number(networkInfo->networkId()) + "/" + _nick);
+  if(!oldName.isEmpty()) {
+    emit renameObject(oldName, objectName());
   }
 }
 
+
 void IrcUser::updateHostmask(const QString &mask) {
   if(mask == hostmask())
     return;
 
   QString user = userFromMask(mask);
   QString host = hostFromMask(mask);
+  setUser(user);
+  setHost(host);
+}
 
-  // we only need to check user and hostmask.
-  // nick can't have changed since we're identifying IrcUsers by nick
-
-  // we don't use setUser and setHost here.
-  // though this is unpretty code duplication this saves us one emit objectNameSet()
-  // the second one would be erroneous
-  
-  if(!user.isEmpty() && _user != user) {
-    _user = user;
+void IrcUser::joinChannel(IrcChannel *channel) {
+  Q_ASSERT(channel);
+  if(!_channels.contains(channel)) {
+    channel->join(this);
+    connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
+    _channels.insert(channel);
+    emit channelJoined(channel->name());
   }
+}
 
-  if(!host.isEmpty() && _host != host) {
-    _host = host;
-  }
+void IrcUser::joinChannel(const QString &channelname) {
+  joinChannel(networkInfo->newIrcChannel(channelname));
+}
 
-  emit hostmaskUpdated(mask);
-  updateObjectName();
+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::joinChannel(const QString &channel) {
-  if(!_channels.contains(channel)) {
-    _channels.insert(channel);
-    networkInfo->newIrcChannel(channel)->join(this);
-    emit channelJoined(channel);
+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::partChannel(const QString &channel) {
+void IrcUser::channelDestroyed() {
+  // private slot!
+  IrcChannel *channel = static_cast<IrcChannel*>(sender());
+  Q_ASSERT(channel);
   if(_channels.contains(channel)) {
     _channels.remove(channel);
-
-    Q_ASSERT(networkInfo->ircChannel(channel));
-    networkInfo->ircChannel(channel)->part(this);
-    
-    emit channelParted(channel);
+    disconnect(channel, 0, this, 0);
   }
 }