propagate topic changes properly
[quassel.git] / src / common / ircchannel.cpp
index 9a66d80..3fd0ad5 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -30,7 +30,7 @@
 
 #include <QDebug>
 
-
+INIT_SYNCABLE_OBJECT(IrcChannel)
 IrcChannel::IrcChannel(const QString &channelname, Network *network)
   : SyncableObject(network),
     _initialized(false),
@@ -43,6 +43,9 @@ IrcChannel::IrcChannel(const QString &channelname, Network *network)
   setObjectName(QString::number(network->networkId().toInt()) + "/" +  channelname);
 }
 
+IrcChannel::~IrcChannel() {
+}
+
 // ====================
 //  PUBLIC:
 // ====================
@@ -113,12 +116,13 @@ QByteArray IrcChannel::encodeString(const QString &string) const {
 // ====================
 void IrcChannel::setTopic(const QString &topic) {
   _topic = topic;
+  SYNC(ARG(topic))
   emit topicSet(topic);
 }
 
 void IrcChannel::setPassword(const QString &password) {
   _password = password;
-  emit passwordSet(password);
+  SYNC(ARG(password))
 }
 
 void IrcChannel::joinIrcUsers(const QList<IrcUser *> &users, const QStringList &modes) {
@@ -137,14 +141,16 @@ void IrcChannel::joinIrcUsers(const QList<IrcUser *> &users, const QStringList &
   IrcUser *ircuser;
   for(int i = 0; i < users.count(); i++) {
     ircuser = users[i];
-    if(!ircuser || _userModes.contains(ircuser))
+    if(!ircuser || _userModes.contains(ircuser)) {
+      addUserMode(ircuser, modes[i]);
       continue;
+    }
 
     _userModes[ircuser] = modes[i];
     ircuser->joinChannel(this);
-    //qDebug() << "JOIN" << name() << ircuser->nick() << ircUsers().count();
     connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
-    connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
+
+    // connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
     // if you wonder why there is no counterpart to ircUserJoined:
     // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
 
@@ -155,9 +161,9 @@ void IrcChannel::joinIrcUsers(const QList<IrcUser *> &users, const QStringList &
 
   if(newNicks.isEmpty())
     return;
-  
+
+  SYNC_OTHER(joinIrcUsers, ARG(newNicks), ARG(newModes));
   emit ircUsersJoined(newUsers);
-  emit ircUsersJoined(newNicks, newModes);
 }
 
 void IrcChannel::joinIrcUsers(const QStringList &nicks, const QStringList &modes) {
@@ -167,7 +173,7 @@ void IrcChannel::joinIrcUsers(const QStringList &nicks, const QStringList &modes
   joinIrcUsers(users, modes);
 }
                      
-void IrcChannel::joinIrcUsers(IrcUser *ircuser) {
+void IrcChannel::joinIrcUser(IrcUser *ircuser) {
   QList <IrcUser *> users;
   users << ircuser;
   QStringList modes;
@@ -175,21 +181,27 @@ void IrcChannel::joinIrcUsers(IrcUser *ircuser) {
   joinIrcUsers(users, modes);
 }
 
-void IrcChannel::joinIrcUsers(const QString &nick) {
-  joinIrcUsers(network->newIrcUser(nick));
-}
-
 void IrcChannel::part(IrcUser *ircuser) {
   if(isKnownUser(ircuser)) {
     _userModes.remove(ircuser);
     ircuser->partChannel(this);
-    //qDebug() << "PART" << name() << ircuser->nick() << ircUsers().count();
     // if you wonder why there is no counterpart to ircUserParted:
     // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
     disconnect(ircuser, 0, this, 0);
     emit ircUserParted(ircuser);
-    if(network->isMe(ircuser))
-       deleteLater();
+    
+    if(network->isMe(ircuser) || _userModes.isEmpty()) {
+      // in either case we're no longer in the channel
+      //  -> clean up the channel and destroy it
+      QList<IrcUser *> users = _userModes.keys();
+      _userModes.clear();
+      foreach(IrcUser *user, users) {
+       disconnect(user, 0, this, 0);
+       user->partChannel(this);
+      }
+      emit parted();
+      network->removeIrcChannel(this);
+    }
   }
 }
 
@@ -201,7 +213,8 @@ void IrcChannel::part(const QString &nick) {
 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
   if(isKnownUser(ircuser)) {
     _userModes[ircuser] = modes;
-    emit userModesSet(ircuser->nick(), modes);
+    QString nick = ircuser->nick();
+    SYNC_OTHER(setUserModes, ARG(nick), ARG(modes))
     emit ircUserModesSet(ircuser, modes);
   }
 }
@@ -217,7 +230,8 @@ void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
 
   if(!_userModes[ircuser].contains(mode)) {
     _userModes[ircuser] += mode;
-    emit userModeAdded(ircuser->nick(), mode);
+    QString nick = ircuser->nick();
+    SYNC_OTHER(addUserMode, ARG(nick), ARG(mode))
     emit ircUserModeAdded(ircuser, mode);
   }
 
@@ -234,7 +248,8 @@ void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
 
   if(_userModes[ircuser].contains(mode)) {
     _userModes[ircuser].remove(mode);
-    emit userModeRemoved(ircuser->nick(), mode);
+    QString nick = ircuser->nick();
+    SYNC_OTHER(removeUserMode, ARG(nick), ARG(mode));
     emit ircUserModeRemoved(ircuser, mode);
   }
 }
@@ -418,7 +433,7 @@ void IrcChannel::addChannelMode(const QChar &mode, const QString &value) {
     _D_channelModes << mode;
     break;
   }
-  emit channelModeAdded(mode, value);
+  SYNC(ARG(mode), ARG(value))
 }
 
 void IrcChannel::removeChannelMode(const QChar &mode, const QString &value) {
@@ -444,7 +459,7 @@ void IrcChannel::removeChannelMode(const QChar &mode, const QString &value) {
     _D_channelModes.remove(mode);
     break;
   }
-  emit channelModeRemoved(mode, value);
+  SYNC(ARG(mode), ARG(value))
 }
 
 bool IrcChannel::hasMode(const QChar &mode) const {