Fixes #410 - away log (you'll find it in the views menu)
[quassel.git] / src / core / corenetwork.cpp
index 0fd68b1..f056183 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  *
@@ -20,7 +20,6 @@
 
 #include "corenetwork.h"
 
-
 #include "core.h"
 #include "coresession.h"
 #include "coreidentity.h"
@@ -83,6 +82,8 @@ CoreNetwork::CoreNetwork(const NetworkId &networkid, CoreSession *session)
 CoreNetwork::~CoreNetwork() {
   if(connectionState() != Disconnected && connectionState() != Network::Reconnecting)
     disconnectFromIrc(false);      // clean up, but this does not count as requested disconnect!
+  else
+    socket.close();
   disconnect(&socket, 0, this, 0); // this keeps the socket from triggering events during clean up
   delete _ircServerHandler;
   delete _userInputHandler;
@@ -144,7 +145,7 @@ void CoreNetwork::connectToIrc(bool reconnecting) {
   } else if(_previousConnectionAttemptFailed) {
     // cycle to next server if previous connection attempt failed
     displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Connection failed. Cycling to next Server"));
-    if(++_lastUsedServerIndex == serverList().size()) {
+    if(++_lastUsedServerIndex >= serverList().size()) {
       _lastUsedServerIndex = 0;
     }
   }
@@ -273,7 +274,10 @@ void CoreNetwork::socketHasData() {
   }
 }
 
-void CoreNetwork::socketError(QAbstractSocket::SocketError) {
+void CoreNetwork::socketError(QAbstractSocket::SocketError error) {
+  if(_quitRequested && error == QAbstractSocket::RemoteHostClosedError)
+    return;
+
   _previousConnectionAttemptFailed = true;
   qWarning() << qPrintable(tr("Could not connect to %1 (%2)").arg(networkName(), socket.errorString()));
   emit connectionError(socket.errorString());
@@ -367,6 +371,7 @@ void CoreNetwork::socketStateChanged(QAbstractSocket::SocketState socketState) {
 void CoreNetwork::networkInitialized() {
   setConnectionState(Network::Initialized);
   setConnected(true);
+  _quitRequested = false;
 
   if(useAutoReconnect()) {
     // reset counter
@@ -401,19 +406,21 @@ void CoreNetwork::sendPerform() {
   }
 
   // rejoin channels we've been in
-  QStringList channels, keys;
-  foreach(QString chan, persistentChannels()) {
-    QString key = channelKey(chan);
-    if(!key.isEmpty()) {
-      channels.prepend(chan);
-      keys.prepend(key);
-    } else {
-      channels.append(chan);
+  if(rejoinChannels()) {
+    QStringList channels, keys;
+    foreach(QString chan, coreSession()->persistentChannels(networkId()).keys()) {
+      QString key = channelKey(chan);
+      if(!key.isEmpty()) {
+        channels.prepend(chan);
+        keys.prepend(key);
+      } else {
+        channels.append(chan);
+      }
     }
+    QString joinString = QString("%1 %2").arg(channels.join(",")).arg(keys.join(",")).trimmed();
+    if(!joinString.isEmpty())
+      userInputHandler()->handleJoin(statusBuf, joinString);
   }
-  QString joinString = QString("%1 %2").arg(channels.join(",")).arg(keys.join(",")).trimmed();
-  if(!joinString.isEmpty())
-    userInputHandler()->handleJoin(statusBuf, joinString);
 }
 
 void CoreNetwork::setUseAutoReconnect(bool use) {
@@ -503,8 +510,11 @@ void CoreNetwork::writeToSocket(const QByteArray &data) {
 Network::Server CoreNetwork::usedServer() const {
   if(_lastUsedServerIndex < serverList().count())
     return serverList()[_lastUsedServerIndex];
-  else
-    return Network::Server();
+
+  if(!serverList().isEmpty())
+    return serverList()[0];
+
+  return Network::Server();
 }
 
 void CoreNetwork::requestConnect() const {
@@ -528,7 +538,9 @@ void CoreNetwork::requestSetNetworkInfo(const NetworkInfo &info) {
   setNetworkInfo(info);
   Core::updateNetwork(coreSession()->user(), info);
 
-  // update _lastUsedServerIndex;
+  // the order of the servers might have changed,
+  // so we try to find the previously used server
+  _lastUsedServerIndex = 0;
   for(int i = 0; i < serverList().count(); i++) {
     Network::Server server = serverList()[i];
     if(server.host == currentServer.host && server.port == currentServer.port) {