fix uptime calculation when client and core are in different timezones
[quassel.git] / src / core / core.cpp
index 6efa622..4d555cf 100644 (file)
@@ -26,6 +26,7 @@
 #include "core.h"
 #include "coresession.h"
 #include "coresettings.h"
+#include "quassel.h"
 #include "signalproxy.h"
 #include "sqlitestorage.h"
 #include "network.h"
@@ -49,7 +50,7 @@ void Core::destroy() {
 }
 
 Core::Core() : storage(0) {
-  _startTime = QDateTime::currentDateTime();  // for uptime :)
+  _startTime = QDateTime::currentDateTime().toUTC();  // for uptime :)
 
   // Register storage backends here!
   registerStorageBackend(new SqliteStorage(this));
@@ -85,8 +86,9 @@ void Core::init() {
     }
   }
 
-  connect(&server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
-  if(!startListening(cs.port())) exit(1); // TODO make this less brutal
+  connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
+  connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
+  if(!startListening()) exit(1); // TODO make this less brutal
 }
 
 Core::~Core() {
@@ -340,44 +342,48 @@ QHash<BufferId, MsgId> Core::bufferLastSeenMsgIds(UserId user) {
 
 /*** Network Management ***/
 
-bool Core::startListening(uint port) {
+bool Core::startListening() {
   bool success = false;
+  uint port = Quassel::optionValue("port").toUInt();
 
-  // let's see if ipv6 is available
-  success = server.listen(QHostAddress::AnyIPv6, port);
-
-  if(!success && server.serverError() == QAbstractSocket::UnsupportedSocketOperationError) {
-    // fall back to v4
-    success = server.listen(QHostAddress::Any, port);
+  if(_server.listen(QHostAddress::Any, port)) {
+    quInfo() << "Listening for GUI clients on IPv6 port" << _server.serverPort() << "using protocol version" << Global::protocolVersion;
+    success = true;
+  }
+  if(_v6server.listen(QHostAddress::AnyIPv6, port)) {
+    quInfo() << "Listening for GUI clients on IPv4 port" << _v6server.serverPort() << "using protocol version" << Global::protocolVersion;
+    success = true;
   }
 
   if(!success) {
-    quError() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(server.errorString()));
-  } else {
-    quInfo() << "Listening for GUI clients on port " << server.serverPort() << " using protocol version " << Global::protocolVersion;
+    quError() << qPrintable(QString("Could not open GUI client port %1: %2").arg(port).arg(_server.errorString()));
   }
-  
+
   return success;
 }
 
 void Core::stopListening() {
-  server.close();
+  _server.close();
+  _v6server.close();
   quInfo() << "No longer listening for GUI clients.";
 }
 
 void Core::incomingConnection() {
-  while(server.hasPendingConnections()) {
-    QTcpSocket *socket = server.nextPendingConnection();
+  QTcpServer *server = qobject_cast<QTcpServer *>(sender());
+  Q_ASSERT(server);
+  while(server->hasPendingConnections()) {
+    QTcpSocket *socket = server->nextPendingConnection();
     connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
     connect(socket, SIGNAL(readyRead()), this, SLOT(clientHasData()));
     connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
-    
+
     QVariantMap clientInfo;
     blocksizes.insert(socket, (quint32)0);
-    quInfo() << qPrintable(tr("Client connected from "))  << qPrintable(socket->peerAddress().toString());
+    quInfo() << qPrintable(tr("Client connected from"))  << qPrintable(socket->peerAddress().toString());
 
-    if (!configured) {
-      server.close();
+    if(!configured) {
+      _server.close();
+      _v6server.close();
       quDebug() << "Closing server for basic setup.";
     }
   }
@@ -424,7 +430,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     reply["CoreBuild"] = 860; // FIXME legacy
     reply["ProtocolVersion"] = Global::protocolVersion;
     // TODO: Make the core info configurable
-    int uptime = startTime().secsTo(QDateTime::currentDateTime());
+    int uptime = startTime().secsTo(QDateTime::currentDateTime().toUTC());
     int updays = uptime / 86400; uptime %= 86400;
     int uphours = uptime / 3600; uptime %= 3600;
     int upmins = uptime / 60;
@@ -433,8 +439,8 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
                           "Up %3d%4h%5m (since %6)").arg(Global::quasselVersion).arg(Global::quasselBuildDate)
       .arg(updays).arg(uphours,2,10,QChar('0')).arg(upmins,2,10,QChar('0')).arg(startTime().toString(Qt::TextDate));
 
-#ifndef QT_NO_OPENSSL
-    SslServer *sslServer = qobject_cast<SslServer *>(&server);
+#ifdef HAVE_SSL
+    SslServer *sslServer = qobject_cast<SslServer *>(&_server);
     QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
     bool supportSsl = (bool)sslServer && (bool)sslSocket && sslServer->certIsValid();
 #else
@@ -472,7 +478,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
     reply["MsgType"] = "ClientInitAck";
     SignalProxy::writeDataToDevice(socket, reply);
 
-#ifndef QT_NO_OPENSSL
+#ifdef HAVE_SSL
     // after we told the client that we are ssl capable we switch to ssl mode
     if(supportSsl && msg["UseSsl"].toBool()) {
       quDebug() << qPrintable(tr("Starting TLS for Client:"))  << qPrintable(socket->peerAddress().toString());
@@ -484,10 +490,10 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
 #ifndef QT_NO_COMPRESS
     if(supportsCompression && msg["UseCompression"].toBool()) {
       socket->setProperty("UseCompression", true);
-      quDebug() << "Using compression for Client: " << qPrintable(socket->peerAddress().toString());
+      quDebug() << "Using compression for Client:" << qPrintable(socket->peerAddress().toString());
     }
 #endif
-    
+
   } else {
     // for the rest, we need an initialized connection
     if(!clientInfo.contains(socket)) {
@@ -521,7 +527,7 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
       }
       reply["MsgType"] = "ClientLoginAck";
       SignalProxy::writeDataToDevice(socket, reply);
-      quInfo() << qPrintable(tr("Client ")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr(" initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
+      quInfo() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("initialized and authenticated successfully as \"%1\" (UserId: %2).").arg(msg["User"].toString()).arg(uid.toInt()));
       setupClientSession(socket, uid);
     }
   }
@@ -542,7 +548,7 @@ void Core::clientDisconnected() {
 
     // DO NOT CALL ANY METHODS ON socket!!
     socket = static_cast<QTcpSocket *>(sender());
-    
+
     QHash<QTcpSocket *, quint32>::iterator blockSizeIter = blocksizes.begin();
     while(blockSizeIter != blocksizes.end()) {
       if(blockSizeIter.key() == socket) {
@@ -597,7 +603,7 @@ SessionThread *Core::createSession(UserId uid, bool restore) {
   return sess;
 }
 
-#ifndef QT_NO_OPENSSL
+#ifdef HAVE_SSL
 void Core::sslErrors(const QList<QSslError> &errors) {
   Q_UNUSED(errors);
   QSslSocket *socket = qobject_cast<QSslSocket *>(sender());