Networks can now be removed even when they're connected.
[quassel.git] / src / common / network.cpp
index da1be33..0d367af 100644 (file)
@@ -51,6 +51,7 @@ Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(p
 // I think this is unnecessary since IrcUsers have us as their daddy :)
 
 Network::~Network() {
+  emit aboutToBeDestroyed();
 //  QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
 //  while (ircuser.hasNext()) {
 //    ircuser.next();
@@ -95,7 +96,8 @@ bool Network::isConnected() const {
   return _connected;
 }
 
-Network::ConnectionState Network::connectionState() const {
+//Network::ConnectionState Network::connectionState() const {
+int Network::connectionState() const {
   return _connectionState;
 }
 
@@ -217,7 +219,6 @@ IrcUser *Network::newIrcUser(const QString &hostmask) {
     
     connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
     connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
-    connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
     _ircUsers[nick] = ircuser;
     emit ircUserAdded(hostmask);
     emit ircUserAdded(ircuser);
@@ -235,12 +236,24 @@ void Network::removeIrcUser(IrcUser *ircuser) {
     return;
 
   _ircUsers.remove(nick);
+  disconnect(ircuser, 0, this, 0);
   emit ircUserRemoved(nick);
   emit ircUserRemoved(ircuser);
   ircuser->deleteLater();
 }
 
-void Network::removeIrcUser(QString nick) {
+void Network::removeChansAndUsers() {
+  QList<IrcUser *> users = ircUsers();
+  foreach(IrcUser *user, users) {
+    removeIrcUser(user);
+  }
+  QList<IrcChannel *> channels = ircChannels();
+  foreach(IrcChannel *channel, channels) {
+    removeIrcChannel(channel);
+  }
+}
+
+void Network::removeIrcUser(const QString &nick) {
   IrcUser *ircuser;
   if((ircuser = ircUser(nick)) != 0)
     removeIrcUser(ircuser);
@@ -366,11 +379,16 @@ void Network::setCurrentServer(const QString &currentServer) {
 
 void Network::setConnected(bool connected) {
   _connected = connected;
+  if(!connected)
+    removeChansAndUsers();
   emit connectedSet(connected);
 }
 
-void Network::setConnectionState(ConnectionState state) {
+//void Network::setConnectionState(ConnectionState state) {
+void Network::setConnectionState(int state) {
   _connectionState = (ConnectionState)state;
+  //qDebug() << "netstate" << networkId() << networkName() << state;
+  emit connectionStateSet(state);
   emit connectionStateSet(_connectionState);
 }
 
@@ -493,17 +511,29 @@ void Network::ircChannelInitDone() {
   emit ircChannelInitDone(ircchannel);
 }
 
-void Network::ircUserDestroyed() {
-  IrcUser *ircuser = static_cast<IrcUser *>(sender());
-  Q_ASSERT(ircuser);
-  removeIrcUser(ircuser);
+void Network::removeIrcChannel(IrcChannel *channel) {
+  QString chanName = _ircChannels.key(channel);
+  if(chanName.isNull())
+    return;
+  
+  _ircChannels.remove(chanName);
+  disconnect(channel, 0, this, 0);
+  emit ircChannelRemoved(chanName);
+  emit ircChannelRemoved(channel);
+  channel->deleteLater();
+}
+
+void Network::removeIrcChannel(const QString &channel) {
+  IrcChannel *chan;
+  if((chan = ircChannel(channel)) != 0)
+    removeIrcChannel(chan);
 }
 
 void Network::channelDestroyed() {
   IrcChannel *channel = static_cast<IrcChannel *>(sender());
   Q_ASSERT(channel);
-  emit ircChannelRemoved(sender());
   _ircChannels.remove(_ircChannels.key(channel));
+  emit ircChannelRemoved(channel);
 }
 
 void Network::requestConnect() const {