Initial commit of logger.
[quassel.git] / src / core / networkconnection.cpp
index f5e6a36..617433f 100644 (file)
@@ -29,7 +29,6 @@
 
 #include "ircchannel.h"
 #include "ircuser.h"
-#include "network.h"
 #include "identity.h"
 
 #include "ircserverhandler.h"
@@ -64,7 +63,10 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session)
   _autoReconnectTimer.setSingleShot(true);
   _socketCloseTimer.setSingleShot(true);
   connect(&_socketCloseTimer, SIGNAL(timeout()), this, SLOT(socketCloseTimeout()));
-  
+
+  _pingTimer.setInterval(60000);
+  connect(&_pingTimer, SIGNAL(timeout()), this, SLOT(sendPing()));
+
   _autoWhoTimer.setInterval(_autoWhoDelay * 1000);
   _autoWhoCycleTimer.setInterval(_autoWhoInterval * 1000);
   
@@ -215,6 +217,8 @@ void NetworkConnection::networkInitialized(const QString &currentServer) {
   network()->setConnected(true);
   emit connected(networkId());
 
+  _pingTimer.start();
+
   if(_autoWhoEnabled) {
     _autoWhoCycleTimer.start();
     _autoWhoTimer.start();
@@ -248,20 +252,19 @@ void NetworkConnection::sendPerform() {
 }
 
 void NetworkConnection::disconnectFromIrc(bool requested) {
+  _quitRequested = requested; // see socketDisconnected();
   _autoReconnectTimer.stop();
-  _autoReconnectCount = 0;
+  _autoReconnectCount = 0; // prohibiting auto reconnect
   displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Disconnecting."));
-  if(socket.state() < QAbstractSocket::ConnectedState) {
-    setConnectionState(Network::Disconnected);
+  if(socket.state() == QAbstractSocket::UnconnectedState) {
+    socketDisconnected();
+  } else if(socket.state() < QAbstractSocket::ConnectedState) {
+    socket.close();
+    // we might be in a state waiting for a timeout... we don't care... set a disconnected state
     socketDisconnected();
   } else {
     _socketCloseTimer.start(10000); // the irc server has 10 seconds to close the socket
   }
-
-  // this flag triggers quitRequested() once the socket is closed
-  // it is needed to determine whether or not the connection needs to be
-  // in the automatic session restore.
-  _quitRequested = requested;
 }
 
 void NetworkConnection::socketHasData() {
@@ -278,7 +281,6 @@ void NetworkConnection::socketError(QAbstractSocket::SocketError) {
   emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Connection failure: %1").arg(socket.errorString()));
   network()->emitConnectionError(socket.errorString());
   if(socket.state() < QAbstractSocket::ConnectedState) {
-    setConnectionState(Network::Disconnected);
     socketDisconnected();
   }
   // mark last connection attempt as failed
@@ -370,6 +372,7 @@ void NetworkConnection::socketCloseTimeout() {
 }
 
 void NetworkConnection::socketDisconnected() {
+  _pingTimer.stop();
   _autoWhoCycleTimer.stop();
   _autoWhoTimer.stop();
   _autoWhoQueue.clear();
@@ -379,12 +382,15 @@ void NetworkConnection::socketDisconnected() {
   
   network()->setConnected(false);
   emit disconnected(networkId());
-  if(_autoReconnectCount != 0) {
-    setConnectionState(Network::Reconnecting);
-    if(_autoReconnectCount == network()->autoReconnectRetries()) doAutoReconnect(); // first try is immediate
-    else _autoReconnectTimer.start();
-  } else if(_quitRequested) {
+  if(_quitRequested) {
+    setConnectionState(Network::Disconnected);
     emit quitRequested(networkId());
+  } else if(_autoReconnectCount != 0) {
+    setConnectionState(Network::Reconnecting);
+    if(_autoReconnectCount == network()->autoReconnectRetries())
+      doAutoReconnect(); // first try is immediate
+    else
+      _autoReconnectTimer.start();
   }
 }
 
@@ -502,6 +508,10 @@ void NetworkConnection::putCmd(const QString &cmd, const QList<QByteArray> &para
   putRawLine(msg);
 }
 
+void NetworkConnection::sendPing() {
+  userInputHandler()->handlePing(BufferInfo(), QString());
+}
+
 void NetworkConnection::sendAutoWho() {
   while(!_autoWhoQueue.isEmpty()) {
     QString chan = _autoWhoQueue.takeFirst();