Introducing Network::requestSetNetworkInfo() to simplify client-side updates
[quassel.git] / src / client / client.cpp
index 6a7faa9..a2ffd8e 100644 (file)
 
 #include "client.h"
 
+#include "abstractmessageprocessor.h"
 #include "bufferinfo.h"
 #include "buffermodel.h"
 #include "buffersettings.h"
 #include "buffersyncer.h"
 #include "bufferviewmanager.h"
 #include "clientbacklogmanager.h"
+#include "clientirclisthelper.h"
 #include "global.h"
 #include "identity.h"
 #include "ircchannel.h"
 #include "ircuser.h"
 #include "message.h"
-#ifdef SPUTDEV
-# include "messagemodel.h"
-#endif
+#include "messagemodel.h"
 #include "network.h"
 #include "networkmodel.h"
 #include "quasselui.h"
@@ -71,11 +71,14 @@ Client::Client(QObject *parent)
     _bufferSyncer(0),
     _backlogManager(new ClientBacklogManager(this)),
     _bufferViewManager(0),
+    _ircListHelper(new ClientIrcListHelper(this)),
     _messageModel(0),
+    _messageProcessor(0),
     _connectedToCore(false),
     _syncedToCore(false)
 {
-  _monitorBuffer = new Buffer(BufferInfo(), this);
+  _signalProxy->synchronize(_ircListHelper);
+
   connect(_backlogManager, SIGNAL(backlog(BufferId, const QVariantList &)),
          this, SLOT(receiveBacklog(BufferId, const QVariantList &)));
 }
@@ -91,12 +94,12 @@ void Client::init() {
   connect(this, SIGNAL(bufferUpdated(BufferInfo)),
           _networkModel, SLOT(bufferUpdated(BufferInfo)));
   connect(this, SIGNAL(networkRemoved(NetworkId)),
-         _networkModel, SLOT(networkRemoved(NetworkId)));
+          _networkModel, SLOT(networkRemoved(NetworkId)));
 
   _bufferModel = new BufferModel(_networkModel);
-#ifdef SPUTDEV
   _messageModel = mainUi->createMessageModel(this);
-#endif
+  _messageProcessor = mainUi->createMessageProcessor(this);
+
   SignalProxy *p = signalProxy();
 
   p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
@@ -107,13 +110,11 @@ void Client::init() {
   p->attachSignal(this, SIGNAL(requestNetworkStates()));
 
   p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
-  p->attachSignal(this, SIGNAL(requestUpdateIdentity(const Identity &)), SIGNAL(updateIdentity(const Identity &)));
   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(requestUpdateNetwork(const NetworkInfo &)), SIGNAL(updateNetwork(const NetworkInfo &)));
   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)));
@@ -125,11 +126,6 @@ void Client::init() {
   connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
   connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
 
-  layoutTimer = new QTimer(this);
-  layoutTimer->setInterval(0);
-  layoutTimer->setSingleShot(false);
-  connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg()));
-
 }
 
 /*** public static methods ***/
@@ -162,15 +158,10 @@ Buffer *Client::statusBuffer(const NetworkId &networkId) const {
     return 0;
 }
 
-Buffer *Client::buffer(BufferId bufferId) {
-  if(instance()->_buffers.contains(bufferId))
-    return instance()->_buffers[bufferId];
-  else
-    return 0;
-}
-
 Buffer *Client::buffer(BufferInfo bufferInfo) {
-  Buffer *buff = buffer(bufferInfo.bufferId());
+  Buffer *buff = 0;
+  if(instance()->_buffers.contains(bufferInfo.bufferId()))
+    buff = instance()->_buffers[bufferInfo.bufferId()];
 
   if(!buff) {
     Client *client = Client::instance();
@@ -215,14 +206,19 @@ void Client::createNetwork(const NetworkInfo &info) {
   emit instance()->requestCreateNetwork(info);
 }
 
-void Client::updateNetwork(const NetworkInfo &info) {
-  emit instance()->requestUpdateNetwork(info);
-}
-
 void Client::removeNetwork(NetworkId id) {
   emit instance()->requestRemoveNetwork(id);
 }
 
+void Client::updateNetwork(const NetworkInfo &info) {
+  Network *netptr = instance()->_networks.value(info.networkId, 0);
+  if(!netptr) {
+    qWarning() << "Update for unknown network requested:" << info;
+    return;
+  }
+  netptr->requestSetNetworkInfo(info);
+}
+
 void Client::addNetwork(Network *net) {
   net->setProxy(signalProxy());
   signalProxy()->synchronize(net);
@@ -242,7 +238,8 @@ void Client::coreNetworkCreated(NetworkId id) {
 }
 
 void Client::coreNetworkRemoved(NetworkId id) {
-  if(!_networks.contains(id)) return;
+  if(!_networks.contains(id))
+    return;
   Network *net = _networks.take(id);
   emit networkRemoved(net->networkId());
   net->deleteLater();
@@ -263,8 +260,13 @@ void Client::createIdentity(const Identity &id) {
   emit instance()->requestCreateIdentity(id);
 }
 
-void Client::updateIdentity(const Identity &id) {
-  emit instance()->requestUpdateIdentity(id);
+void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
+  Identity *idptr = instance()->_identities.value(id, 0);
+  if(!idptr) {
+    qWarning() << "Update for unknown identity requested:" << id;
+    return;
+  }
+  idptr->requestUpdate(ser);
 }
 
 void Client::removeIdentity(IdentityId id) {
@@ -309,7 +311,7 @@ void Client::setSyncedToCore() {
   // create buffersyncer
   Q_ASSERT(!_bufferSyncer);
   _bufferSyncer = new BufferSyncer(this);
-  connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), this, SLOT(updateLastSeenMsg(BufferId, MsgId)));
+  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)));
   signalProxy()->synchronize(bufferSyncer());
@@ -319,7 +321,7 @@ void Client::setSyncedToCore() {
 
   // create a new BufferViewManager
   _bufferViewManager = new BufferViewManager(signalProxy(), this);
-  
+
   _syncedToCore = true;
   emit connected();
   emit coreConnectionStateChanged(true);
@@ -333,7 +335,7 @@ void Client::disconnectFromCore() {
   if(!isConnected())
     return;
   _connectedToCore = false;
-  
+
   if(socket) {
     socket->close();
     socket->deleteLater();
@@ -342,6 +344,8 @@ void Client::disconnectFromCore() {
   emit disconnected();
   emit coreConnectionStateChanged(false);
 
+  messageProcessor()->reset();
+
   // Clear internal data. Hopefully nothing relies on it at this point.
   setCurrentCoreAccount(0);
 
@@ -355,6 +359,7 @@ void Client::disconnectFromCore() {
     _bufferViewManager = 0;
   }
 
+  _messageModel->clear();
   _networkModel->clear();
 
   QHash<BufferId, Buffer *>::iterator bufferIter =  _buffers.begin();
@@ -387,8 +392,6 @@ void Client::disconnectFromCore() {
   }
   Q_ASSERT(_identities.isEmpty());
 
-  layoutQueue.clear();
-  layoutTimer->stop();
 }
 
 void Client::setCoreConfiguration(const QVariantMap &settings) {
@@ -435,194 +438,26 @@ void Client::networkDestroyed() {
   }
 }
 
-#ifndef SPUTDEV
-void Client::recvMessage(const Message &message) {
-  Message msg = message;
-  Buffer *b;
-
-  checkForHighlight(msg);
-
-  // FIXME clean up code! (dup)
-
-  // TODO: make redirected messages show up in the correct buffer!
-
-  if(msg.flags() & Message::Redirected) {
-    BufferSettings bufferSettings;
-    bool inStatus = bufferSettings.value("UserMessagesInStatusBuffer", QVariant(true)).toBool();
-    bool inQuery = bufferSettings.value("UserMessagesInQueryBuffer", QVariant(false)).toBool();
-    bool inCurrent = bufferSettings.value("UserMessagesInCurrentBuffer", QVariant(false)).toBool();
-
-    if(inStatus) {
-      b = statusBuffer(msg.bufferInfo().networkId());
-      if(b) {
-        b->appendMsg(msg);
-      } else if(!inQuery && !inCurrent) {      // make sure the message get's shown somewhere
-        b = buffer(msg.bufferInfo());
-        b->appendMsg(msg);
-      }
-    }
-
-    if(inQuery) {
-      b = buffer(msg.bufferInfo().bufferId());
-      if(b) {
-        b->appendMsg(msg);
-      } else if(!inStatus && !inCurrent) { // make sure the message get's shown somewhere
-        b = statusBuffer(msg.bufferInfo().networkId());
-      if(!b) b = buffer(msg.bufferInfo()); // seems like we have to create the buffer anyways...
-      b->appendMsg(msg);
-      }
-    }
-
-    if(inCurrent) {
-      BufferId currentId = bufferModel()->currentIndex().data(NetworkModel::BufferIdRole).value<BufferId>();
-      b = buffer(currentId);
-      if(b && currentId != msg.bufferInfo().bufferId() && !inQuery) {
-        b->appendMsg(msg);
-      } else if(!inStatus && !inQuery) {       // make sure the message get's shown somewhere
-        b = statusBuffer(msg.bufferInfo().networkId());
-        if(!b) b = buffer(msg.bufferInfo()); // seems like we have to create the buffer anyways...
-        b->appendMsg(msg);
-      }
-    }
-  } else {
-    // the regular case: we can deliver where it was supposed to go
-    b = buffer(msg.bufferInfo());
-    b->appendMsg(msg);
-  }
-  //bufferModel()->updateBufferActivity(msg);
-
-  if(msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) {
-    const Network *net = network(msg.bufferInfo().networkId());
-    QString networkName = net != 0
-      ? net->networkName() + ":"
-      : QString();
-    QString sender = networkName + msg.bufferInfo().bufferName() + ":" + msg.sender();
-    Message mmsg = Message(msg.timestamp(), msg.bufferInfo(), msg.type(), msg.contents(), sender, msg.flags());
-    monitorBuffer()->appendMsg(mmsg);
-  }
-  emit messageReceived(msg);
-}
-#else
-
-void Client::recvMessage(const Message &msg) {
-  //checkForHighlight(msg);
-  _messageModel->insertMessage(msg);
-}
-
-#endif /* SPUTDEV */
-
+// Hmm... we never used this...
 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
   //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
 }
 
-#ifdef SPUTDEV
-void Client::receiveBacklog(BufferId bufferId, const QVariantList &msgs) {
-  //checkForHighlight(msg);
-  foreach(QVariant v, msgs) {
-    _messageModel->insertMessage(v.value<Message>());
-  }
+void Client::recvMessage(const Message &msg_) {
+  Message msg = msg_;
+  messageProcessor()->process(msg);
 }
 
-#else
-
 void Client::receiveBacklog(BufferId bufferId, const QVariantList &msgs) {
-  Buffer *buffer_ = buffer(bufferId);
-  if(!buffer_) {
-    qWarning() << "Client::receiveBacklog(): received Backlog for unknown Buffer:" << bufferId;
-    return;
-  }
-
-  if(msgs.isEmpty())
-    return; // no work to be done...
-  
-  QVariantList::const_iterator msgIter = msgs.constBegin();
-  QVariantList::const_iterator msgIterEnd = msgs.constEnd();
-  Message msg;
-  while(msgIter != msgIterEnd) {
-    msg = (*msgIter).value<Message>();
-    checkForHighlight(msg);
-    buffer_->prependMsg(msg);
-    msgIter++;
-  }
-
-  if(!layoutQueue.contains(buffer_))
-    layoutQueue.append(buffer_);
-
-  if(!layoutTimer->isActive()) {
-    layoutTimer->start();
-  }
-}
-#endif /* SPUTDEV */
-
-void Client::layoutMsg() {
-  if(layoutQueue.isEmpty()) {
-    layoutTimer->stop();
-    return;
-  }
-  
-  Buffer *buffer = layoutQueue.takeFirst();
-  if(buffer->layoutMsg()) {
-    layoutQueue.append(buffer);  // Buffer has more messages in its queue --> Round Robin
-    return;
-  }
-
-  if(layoutQueue.isEmpty())
-    layoutTimer->stop();
-}
-
-AbstractUiMsg *Client::layoutMsg(const Message &msg) {
-  return instance()->mainUi->layoutMsg(msg);
-}
-
-void Client::checkForHighlight(Message &msg) {
-  NotificationSettings notificationSettings;
-  const Network *net = network(msg.bufferInfo().networkId());
-  if(net && !net->myNick().isEmpty()) {
-    QStringList nickList;
-    if(notificationSettings.highlightNick() == NotificationSettings::CurrentNick) {
-      nickList << net->myNick();
-    } else if(notificationSettings.highlightNick() == NotificationSettings::AllNicks) {
-      nickList = identity(net->identity())->nicks();
-    }
-    foreach(QString nickname, nickList) {
-      QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(nickname) + "(\\W.*)?$");
-      if((msg.type() & (Message::Plain | Message::Notice | Message::Action))
-          && !(msg.flags() & Message::Self)
-          && nickRegExp.exactMatch(msg.contents())) {
-        msg.setFlags(msg.flags() | Message::Highlight);
-        return;
-      }
-    }
-
-    foreach(QVariant highlight, notificationSettings.highlightList()) {
-      QVariantMap highlightRule = highlight.toMap();
-      if(!highlightRule["enable"].toBool())
-        continue;
-      Qt::CaseSensitivity caseSensitivity = highlightRule["cs"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive;
-      QString name = highlightRule["name"].toString();
-      QRegExp userRegExp;
-      if(highlightRule["regex"].toBool()) {
-        userRegExp = QRegExp(name, caseSensitivity);
-      } else {
-        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
-      }
-      if((msg.type() & (Message::Plain | Message::Notice | Message::Action))
-          && !(msg.flags() & Message::Self)
-          && userRegExp.exactMatch(msg.contents())) {
-        msg.setFlags(msg.flags() | Message::Highlight);
-        return;
-      }
-    }
-  }
-}
-
-void Client::updateLastSeenMsg(BufferId id, const MsgId &msgId) {
-  Buffer *b = buffer(id);
-  if(!b) {
-    qWarning() << "Client::updateLastSeen(): Unknown buffer" << id;
-    return;
+  //QTime start = QTime::currentTime();
+  QList<Message> msglist;
+  foreach(QVariant v, msgs) {
+    Message msg = v.value<Message>();
+    msg.setFlags(msg.flags() | Message::Backlog);
+    msglist << msg;
   }
-  b->setLastSeenMsg(msgId);
+  messageProcessor()->process(msglist);
+  //qDebug() << "processed" << msgs.count() << "backlog lines in" << start.msecsTo(QTime::currentTime());
 }
 
 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
@@ -637,7 +472,7 @@ void Client::removeBuffer(BufferId id) {
 }
 
 void Client::bufferRemoved(BufferId bufferId) {
-  // first remove the buffer from has. this prohibits further lastSeenUpdates
+  // first remove the buffer from hash. this prohibits further lastSeenUpdates
   Buffer *buff = 0;
   if(_buffers.contains(bufferId)) {
     buff = _buffers.take(bufferId);