The BufferSyncer cache is now purged when an unknown bufferId is encountered.
[quassel.git] / src / client / client.cpp
index ce734cc..759afcf 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  *
 #include "client.h"
 
 #include "abstractmessageprocessor.h"
+#include "abstractui.h"
 #include "bufferinfo.h"
 #include "buffermodel.h"
 #include "buffersettings.h"
 #include "buffersyncer.h"
-#include "bufferviewmanager.h"
+#include "bufferviewconfig.h"
 #include "clientbacklogmanager.h"
+#include "clientbufferviewmanager.h"
 #include "clientirclisthelper.h"
-#include "global.h"
-#include "identity.h"
+#include "clientidentity.h"
 #include "ircchannel.h"
 #include "ircuser.h"
 #include "message.h"
 #include "messagemodel.h"
 #include "network.h"
 #include "networkmodel.h"
-#include "quasselui.h"
+#include "quassel.h"
 #include "signalproxy.h"
 #include "util.h"
 
+#include <stdio.h>
+#include <stdlib.h>
+
 QPointer<Client> Client::instanceptr = 0;
 AccountId Client::_currentCoreAccount = 0;
 
@@ -52,20 +56,22 @@ Client *Client::instance() {
 }
 
 void Client::destroy() {
-  //delete instanceptr;
-  instanceptr->deleteLater();
+  if(instanceptr) {
+    delete instanceptr->mainUi();
+    instanceptr->deleteLater();
+    instanceptr = 0;
+  }
 }
 
 void Client::init(AbstractUi *ui) {
-  instance()->mainUi = ui;
+  instance()->_mainUi = ui;
   instance()->init();
 }
 
 Client::Client(QObject *parent)
   : QObject(parent),
-    socket(0),
     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
-    mainUi(0),
+    _mainUi(0),
     _networkModel(0),
     _bufferModel(0),
     _bufferSyncer(0),
@@ -75,7 +81,9 @@ Client::Client(QObject *parent)
     _messageModel(0),
     _messageProcessor(0),
     _connectedToCore(false),
-    _syncedToCore(false)
+    _syncedToCore(false),
+    _internalCore(false),
+    _debugLog(&_debugLogBuffer)
 {
   _signalProxy->synchronize(_ircListHelper);
 }
@@ -92,8 +100,8 @@ void Client::init() {
           _networkModel, SLOT(networkRemoved(NetworkId)));
 
   _bufferModel = new BufferModel(_networkModel);
-  _messageModel = mainUi->createMessageModel(this);
-  _messageProcessor = mainUi->createMessageProcessor(this);
+  _messageModel = mainUi()->createMessageModel(this);
+  _messageProcessor = mainUi()->createMessageProcessor(this);
 
   SignalProxy *p = signalProxy();
 
@@ -104,27 +112,34 @@ void Client::init() {
   p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
   p->attachSignal(this, SIGNAL(requestNetworkStates()));
 
-  p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
+  p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
   p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
   p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
   p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
 
-  p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
+  p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &, const QStringList &)), SIGNAL(createNetwork(const NetworkInfo &, const QStringList &)));
   p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
   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()));
+  //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 ***/
 
+AbstractUi *Client::mainUi() {
+  return instance()->_mainUi;
+}
+
 AccountId Client::currentCoreAccount() {
   return _currentCoreAccount;
 }
@@ -152,8 +167,8 @@ const Network * Client::network(NetworkId networkid) {
   else return 0;
 }
 
-void Client::createNetwork(const NetworkInfo &info) {
-  emit instance()->requestCreateNetwork(info);
+void Client::createNetwork(const NetworkInfo &info, const QStringList &persistentChannels) {
+  emit instance()->requestCreateNetwork(info, persistentChannels);
 }
 
 void Client::removeNetwork(NetworkId id) {
@@ -201,13 +216,18 @@ QList<IdentityId> Client::identityIds() {
   return instance()->_identities.keys();
 }
 
-const Identity * Client::identity(IdentityId id) {
+const Identity *Client::identity(IdentityId id) {
   if(instance()->_identities.contains(id)) return instance()->_identities[id];
   else return 0;
 }
 
-void Client::createIdentity(const Identity &id) {
-  emit instance()->requestCreateIdentity(id);
+void Client::createIdentity(const CertIdentity &id) {
+  QVariantMap additional;
+#ifdef HAVE_SSL
+  additional["KeyPem"] = id.sslKey().toPem();
+  additional["CertPem"] = id.sslCert().toPem();
+#endif
+  emit instance()->requestCreateIdentity(id, additional);
 }
 
 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
@@ -250,9 +270,14 @@ void Client::userInput(BufferInfo bufferInfo, QString message) {
 
 /*** core connection stuff ***/
 
-void Client::setConnectedToCore(QIODevice *sock, AccountId id) {
-  socket = sock;
-  signalProxy()->addPeer(socket);
+void Client::setConnectedToCore(AccountId id, QIODevice *socket) {
+  if(socket) { // external core
+    // 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);
+  }
+  _internalCore = !socket;
   _connectedToCore = true;
   setCurrentCoreAccount(id);
 }
@@ -264,36 +289,51 @@ void Client::setSyncedToCore() {
   connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
   connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
   connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
+  connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), this, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
+  connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), _messageModel, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
+  connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
+  connect(networkModel(), SIGNAL(setLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
   signalProxy()->synchronize(bufferSyncer());
 
-  // attach backlog manager
-  signalProxy()->synchronize(backlogManager());
-
   // create a new BufferViewManager
-  _bufferViewManager = new BufferViewManager(signalProxy(), this);
+  Q_ASSERT(!_bufferViewManager);
+  _bufferViewManager = new ClientBufferViewManager(signalProxy(), this);
+  connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
+  connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(createDefaultBufferView()));
 
   _syncedToCore = true;
   emit connected();
   emit coreConnectionStateChanged(true);
 }
 
-void Client::setSecuredConnection() {
-  emit securedConnection();
+void Client::requestInitialBacklog() {
+  if(bufferViewManager()->isInitialized() && bufferSyncer()->isInitialized())
+    Client::backlogManager()->requestInitialBacklog();
+}
+
+void Client::createDefaultBufferView() {
+  if(bufferViewManager()->bufferViewConfigs().isEmpty()) {
+    BufferViewConfig config(-1);
+    config.setBufferViewName(tr("All Buffers"));
+    config.initSetBufferList(networkModel()->allBufferIdsSorted());
+    bufferViewManager()->requestCreateBufferView(config.toVariantMap());
+  }
 }
 
 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);
 
+  backlogManager()->reset();
   messageProcessor()->reset();
 
   // Clear internal data. Hopefully nothing relies on it at this point.
@@ -322,10 +362,10 @@ void Client::disconnectFromCore() {
   }
   Q_ASSERT(_networks.isEmpty());
 
-  QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
+  QHash<IdentityId, Identity *>::iterator idIter = _identities.begin();
   while(idIter != _identities.end()) {
+    emit identityRemoved(idIter.key());
     Identity *id = idIter.value();
-    emit identityRemoved(id->id());
     idIter = _identities.erase(idIter);
     id->deleteLater();
   }
@@ -333,10 +373,6 @@ void Client::disconnectFromCore() {
 
 }
 
-void Client::setCoreConfiguration(const QVariantMap &settings) {
-  SignalProxy::writeDataToDevice(socket, settings);
-}
-
 /*** ***/
 
 void Client::networkDestroyed() {
@@ -357,9 +393,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) {
@@ -373,6 +409,24 @@ void Client::removeBuffer(BufferId id) {
   bufferSyncer()->requestRemoveBuffer(id);
 }
 
+void Client::renameBuffer(BufferId bufferId, const QString &newName) {
+  if(!bufferSyncer())
+    return;
+  bufferSyncer()->requestRenameBuffer(bufferId, newName);
+}
+
+void Client::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2) {
+  if(!bufferSyncer())
+    return;
+  bufferSyncer()->requestMergeBuffersPermanently(bufferId1, bufferId2);
+}
+
+void Client::purgeKnownBufferIds() {
+  if(!bufferSyncer())
+    return;
+  bufferSyncer()->requestPurgeBufferIds();
+}
+
 void Client::bufferRemoved(BufferId bufferId) {
   // select a sane buffer (status buffer)
   /* we have to manually select a buffer because otherwise inconsitent changes
@@ -397,3 +451,22 @@ void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
     networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
   }
 }
+
+void Client::buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2) {
+  QModelIndex idx = networkModel()->bufferIndex(bufferId1);
+  bufferModel()->setCurrentIndex(bufferModel()->mapFromSource(idx));
+  networkModel()->removeBuffer(bufferId2);
+}
+
+void Client::logMessage(QtMsgType type, const char *msg) {
+  fprintf(stderr, "%s\n", msg);
+  fflush(stderr);
+  if(type == QtFatalMsg) {
+    Quassel::logFatalMessage(msg);
+  } else {
+    QString msgString = QString("%1\n").arg(msg);
+    instance()->_debugLog << msgString;
+    emit instance()->logUpdated(msgString);
+  }
+}
+