Add socket warnings, make strings translatable, adjust whitespace
[quassel.git] / src / core / core.cpp
index 830a79b..75d0bcb 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  *
@@ -63,7 +63,7 @@ Core::Core() : storage(0) {
     exit(1); // TODO make this less brutal (especially for mono client -> popup)
   }
   connect(&_storageSyncTimer, SIGNAL(timeout()), this, SLOT(syncStorage()));
-  _storageSyncTimer.start(10 * 60 * 1000); // in msecs
+  _storageSyncTimer.start(10 * 60 * 1000); // 10 minutes
 }
 
 void Core::init() {
@@ -244,20 +244,62 @@ bool Core::startListening() {
   bool success = false;
   uint port = Quassel::optionValue("port").toUInt();
 
-  if(_server.listen(QHostAddress::Any, port)) {
-    quInfo() << "Listening for GUI clients on IPv4 port" << _server.serverPort()
-             << "using protocol version" << Quassel::buildInfo().protocolVersion;
-    success = true;
-  }
-  if(_v6server.listen(QHostAddress::AnyIPv6, port)) {
-    quInfo() << "Listening for GUI clients on IPv6 port" << _v6server.serverPort()
-             << "using protocol version" << Quassel::buildInfo().protocolVersion;
-    success = true;
-  }
-
-  if(!success) {
-    qCritical() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(_server.errorString()));
+  const QString listen = Quassel::optionValue("listen");
+  const QStringList listen_list = listen.split(",", QString::SkipEmptyParts);
+  if(listen_list.size() > 0) {
+    foreach (const QString listen_term, listen_list) {  // TODO: handle multiple interfaces for same TCP version gracefully
+      QHostAddress addr;
+      if(!addr.setAddress(listen_term)) {
+        qCritical() << qPrintable(
+          tr("Invalid listen address %1")
+            .arg(listen_term)
+        );
+      } else {
+        switch(addr.protocol()) {
+          case QAbstractSocket::IPv4Protocol:
+            if(_server.listen(addr, port)) {
+              quInfo() << qPrintable(
+                tr("Listening for GUI clients on IPv4 %1 port %2 using protocol version %3")
+                  .arg(addr.toString())
+                  .arg(_server.serverPort())
+                  .arg(Quassel::buildInfo().protocolVersion)
+              );
+              success = true;
+            } else
+              quWarning() << qPrintable(
+                tr("Could not open GUI client interface %1:%2: %3")
+                  .arg(addr.toString())
+                  .arg(port)
+                  .arg(_server.errorString()));
+            break;
+          case QAbstractSocket::IPv6Protocol:
+            if (_v6server.listen(addr, port)) {
+              quInfo() << qPrintable(
+                tr("Listening for GUI clients on IPv6 %1 port %2 using protocol version %3")
+                  .arg(addr.toString())
+                  .arg(_server.serverPort())
+                  .arg(Quassel::buildInfo().protocolVersion)
+              );
+              success = true;
+            } else
+              quWarning() << qPrintable(
+              tr("Could not open GUI client interface %1:%2: %3")
+              .arg(addr.toString())
+              .arg(port)
+              .arg(_server.errorString()));
+            break;
+          default:
+            qCritical() << qPrintable(
+              tr("Invalid listen address %1, unknown network protocol")
+                  .arg(listen_term)
+            );
+            break;
+        }
+      }
+    }
   }
+  if(!success)
+    quError() << qPrintable(tr("Could not open any network interfaces to listen on!"));
 
   return success;
 }
@@ -458,17 +500,19 @@ void Core::clientDisconnected() {
     QHash<QTcpSocket *, quint32>::iterator blockSizeIter = blocksizes.begin();
     while(blockSizeIter != blocksizes.end()) {
       if(blockSizeIter.key() == socket) {
-       blocksizes.erase(blockSizeIter);
+       blockSizeIter = blocksizes.erase(blockSizeIter);
+      } else {
+       blockSizeIter++;
       }
-      blockSizeIter++;
     }
 
     QHash<QTcpSocket *, QVariantMap>::iterator clientInfoIter = clientInfo.begin();
     while(clientInfoIter != clientInfo.end()) {
       if(clientInfoIter.key() == socket) {
-       clientInfo.erase(clientInfoIter);
+       clientInfoIter = clientInfo.erase(clientInfoIter);
+      } else {
+       clientInfoIter++;
       }
-      clientInfoIter++;
     }
   }