better handling of log messages (internal stuff only)
[quassel.git] / src / client / client.cpp
index 9611f71..ca008e9 100644 (file)
@@ -35,6 +35,7 @@
 #include "messagemodel.h"
 #include "network.h"
 #include "networkmodel.h"
+#include "quassel.h"
 #include "quasselui.h"
 #include "signalproxy.h"
 #include "util.h"
@@ -65,7 +66,6 @@ void Client::init(AbstractUi *ui) {
 
 Client::Client(QObject *parent)
   : QObject(parent),
-    socket(0),
     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
     mainUi(0),
     _networkModel(0),
@@ -77,7 +77,8 @@ Client::Client(QObject *parent)
     _messageModel(0),
     _messageProcessor(0),
     _connectedToCore(false),
-    _syncedToCore(false)
+    _syncedToCore(false),
+    _debugLog(&_debugLogBuffer)
 {
   _signalProxy->synchronize(_ircListHelper);
 }
@@ -116,13 +117,16 @@ void Client::init() {
   p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
   p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
 
-  connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore()));
+  connect(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
 
   //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
   connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
   connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
   connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
 
+  // attach backlog manager
+  p->synchronize(backlogManager());
+  connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
 }
 
 /*** public static methods ***/
@@ -252,13 +256,20 @@ void Client::userInput(BufferInfo bufferInfo, QString message) {
 
 /*** core connection stuff ***/
 
-void Client::setConnectedToCore(QIODevice *sock, AccountId id) {
-  socket = sock;
+void Client::setConnectedToCore(QIODevice *socket, AccountId id) {
+  // if the socket is an orphan, the signalProxy adopts it.
+  // -> we don't need to care about it anymore
+  socket->setParent(0);
   signalProxy()->addPeer(socket);
   _connectedToCore = true;
   setCurrentCoreAccount(id);
 }
 
+void Client::setConnectedToInternalCore() {
+  _connectedToCore = true;
+  setCurrentCoreAccount(AccountId());
+}
+
 void Client::setSyncedToCore() {
   // create buffersyncer
   Q_ASSERT(!_bufferSyncer);
@@ -268,9 +279,6 @@ void Client::setSyncedToCore() {
   connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
   signalProxy()->synchronize(bufferSyncer());
 
-  // attach backlog manager
-  signalProxy()->synchronize(backlogManager());
-
   // create a new BufferViewManager
   _bufferViewManager = new BufferViewManager(signalProxy(), this);
 
@@ -286,12 +294,12 @@ void Client::setSecuredConnection() {
 void Client::disconnectFromCore() {
   if(!isConnected())
     return;
-  _connectedToCore = false;
 
-  if(socket) {
-    socket->close();
-    socket->deleteLater();
-  }
+  signalProxy()->removeAllPeers();
+}
+
+void Client::disconnectedFromCore() {
+  _connectedToCore = false;
   _syncedToCore = false;
   emit disconnected();
   emit coreConnectionStateChanged(false);
@@ -336,10 +344,6 @@ void Client::disconnectFromCore() {
 
 }
 
-void Client::setCoreConfiguration(const QVariantMap &settings) {
-  SignalProxy::writeDataToDevice(socket, settings);
-}
-
 /*** ***/
 
 void Client::networkDestroyed() {
@@ -360,9 +364,9 @@ void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
   //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
 }
 
-void Client::recvMessage(const Message &msg_) {
-  Message msg = msg_;
-  messageProcessor()->process(msg);
+void Client::recvMessage(const Message &msg) {
+  Message msg_ = msg;
+  messageProcessor()->process(msg_);
 }
 
 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
@@ -400,3 +404,25 @@ void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
     networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
   }
 }
+
+void Client::logMessage(QtMsgType type, const char *msg) {
+  QString prefix;
+  switch (type) {
+  case QtDebugMsg:
+    prefix = "Debug";
+    break;
+  case QtWarningMsg:
+    prefix = "Warning";
+    break;
+  case QtCriticalMsg:
+    prefix = "Critical";
+    break;
+  case QtFatalMsg:
+    Quassel::logFatalMessage(msg);
+    return;
+  }
+  QString msgString = QString("%1: %3\n").arg(prefix, msg);
+  instance()->_debugLog << msgString;
+  emit instance()->logUpdated(msgString);
+}
+