fixing issues after a failed connect to irc
[quassel.git] / src / core / corenetwork.cpp
index b3cbfc0..72aff92 100644 (file)
 
 #include "corenetwork.h"
 
-
 #include "core.h"
 #include "coresession.h"
-#include "identity.h"
+#include "coreidentity.h"
 
 #include "ircserverhandler.h"
 #include "userinputhandler.h"
@@ -133,7 +132,7 @@ void CoreNetwork::connectToIrc(bool reconnecting) {
     qWarning() << "Server list empty, ignoring connect request!";
     return;
   }
-  Identity *identity = identityPtr();
+  CoreIdentity *identity = identityPtr();
   if(!identity) {
     qWarning() << "Invalid identity configures, ignoring connect request!";
     return;
@@ -144,7 +143,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;
     }
   }
@@ -163,10 +162,16 @@ void CoreNetwork::connectToIrc(bool reconnecting) {
 
 #ifdef HAVE_SSL
   socket.setProtocol((QSsl::SslProtocol)server.sslVersion);
-  if(server.useSsl)
+  if(server.useSsl) {
+    CoreIdentity *identity = identityPtr();
+    if(identity) {
+      socket.setLocalCertificate(identity->sslCert());
+      socket.setPrivateKey(identity->sslKey());
+    }
     socket.connectToHostEncrypted(server.host, server.port);
-  else
+  } else {
     socket.connectToHost(server.host, server.port);
+  }
 #else
   socket.connectToHost(server.host, server.port);
 #endif
@@ -267,7 +272,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());
@@ -285,7 +293,7 @@ void CoreNetwork::socketInitialized() {
     return;
 #endif
 
-  Identity *identity = identityPtr();
+  CoreIdentity *identity = identityPtr();
   if(!identity) {
     qCritical() << "Identity invalid!";
     disconnectFromIrc();
@@ -361,6 +369,7 @@ void CoreNetwork::socketStateChanged(QAbstractSocket::SocketState socketState) {
 void CoreNetwork::networkInitialized() {
   setConnectionState(Network::Initialized);
   setConnected(true);
+  _quitRequested = false;
 
   if(useAutoReconnect()) {
     // reset counter
@@ -497,8 +506,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 {
@@ -522,7 +534,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) {