fixing client crash if a buffer is deleted during backlog rendering
[quassel.git] / src / client / client.cpp
index 69ffbfc..6ce23d5 100644 (file)
 #include "client.h"
 
 #include "bufferinfo.h"
+#include "buffermodel.h"
+#include "buffersettings.h"
 #include "buffersyncer.h"
-#include "clientbacklogmanager.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 "network.h"
 #include "networkmodel.h"
-#include "buffermodel.h"
 #include "quasselui.h"
 #include "signalproxy.h"
 #include "util.h"
-#include "buffersettings.h"
 
 QPointer<Client> Client::instanceptr = 0;
 AccountId Client::_currentCoreAccount = 0;
@@ -68,10 +72,14 @@ Client::Client(QObject *parent)
     _bufferSyncer(0),
     _backlogManager(new ClientBacklogManager(this)),
     _bufferViewManager(0),
+    _ircListHelper(new ClientIrcListHelper(this)),
+    _messageModel(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 &)));
 }
@@ -83,13 +91,16 @@ Client::~Client() {
 void Client::init() {
   _currentCoreAccount = 0;
   _networkModel = new NetworkModel(this);
+
   connect(this, SIGNAL(bufferUpdated(BufferInfo)),
           _networkModel, SLOT(bufferUpdated(BufferInfo)));
   connect(this, SIGNAL(networkRemoved(NetworkId)),
          _networkModel, SLOT(networkRemoved(NetworkId)));
 
   _bufferModel = new BufferModel(_networkModel);
-
+#ifdef SPUTDEV
+  _messageModel = mainUi->createMessageModel(this);
+#endif
   SignalProxy *p = signalProxy();
 
   p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
@@ -235,7 +246,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();
@@ -318,21 +330,26 @@ void Client::setSyncedToCore() {
   emit coreConnectionStateChanged(true);
 }
 
+void Client::setSecuredConnection() {
+  emit securedConnection();
+}
+
 void Client::disconnectFromCore() {
   if(!isConnected())
     return;
+  _connectedToCore = false;
   
   if(socket) {
     socket->close();
     socket->deleteLater();
   }
-  _connectedToCore = false;
   _syncedToCore = false;
-  setCurrentCoreAccount(0);
   emit disconnected();
   emit coreConnectionStateChanged(false);
 
   // Clear internal data. Hopefully nothing relies on it at this point.
+  setCurrentCoreAccount(0);
+
   if(_bufferSyncer) {
     _bufferSyncer->deleteLater();
     _bufferSyncer = 0;
@@ -342,7 +359,7 @@ void Client::disconnectFromCore() {
     _bufferViewManager->deleteLater();
     _bufferViewManager = 0;
   }
-  
+
   _networkModel->clear();
 
   QHash<BufferId, Buffer *>::iterator bufferIter =  _buffers.begin();
@@ -423,6 +440,7 @@ void Client::networkDestroyed() {
   }
 }
 
+#ifndef SPUTDEV
 void Client::recvMessage(const Message &message) {
   Message msg = message;
   Buffer *b;
@@ -431,6 +449,8 @@ void Client::recvMessage(const Message &message) {
 
   // 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();
@@ -474,7 +494,6 @@ void Client::recvMessage(const Message &message) {
     b = buffer(msg.bufferInfo());
     b->appendMsg(msg);
   }
-  
   //bufferModel()->updateBufferActivity(msg);
 
   if(msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) {
@@ -483,21 +502,38 @@ void Client::recvMessage(const Message &message) {
       ? net->networkName() + ":"
       : QString();
     QString sender = networkName + msg.bufferInfo().bufferName() + ":" + msg.sender();
-    Message mmsg = Message(msg.timestamp(), msg.bufferInfo(), msg.type(), msg.text(), sender, msg.flags());
+    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 */
 
 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>());
+  }
+}
+
+#else
+
 void Client::receiveBacklog(BufferId bufferId, const QVariantList &msgs) {
   Buffer *buffer_ = buffer(bufferId);
   if(!buffer_) {
-    qWarning() << "Client::recvBacklogData(): received Backlog for unknown Buffer:" << bufferId;
+    qWarning() << "Client::receiveBacklog(): received Backlog for unknown Buffer:" << bufferId;
     return;
   }
 
@@ -521,6 +557,7 @@ void Client::receiveBacklog(BufferId bufferId, const QVariantList &msgs) {
     layoutTimer->start();
   }
 }
+#endif /* SPUTDEV */
 
 void Client::layoutMsg() {
   if(layoutQueue.isEmpty()) {
@@ -532,7 +569,7 @@ void Client::layoutMsg() {
   if(buffer->layoutMsg()) {
     layoutQueue.append(buffer);  // Buffer has more messages in its queue --> Round Robin
     return;
-  } 
+  }
 
   if(layoutQueue.isEmpty())
     layoutTimer->stop();
@@ -546,29 +583,37 @@ void Client::checkForHighlight(Message &msg) {
   NotificationSettings notificationSettings;
   const Network *net = network(msg.bufferInfo().networkId());
   if(net && !net->myNick().isEmpty()) {
-    if(notificationSettings.highlightCurrentNick()) {
-      QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(net->myNick()) + "(\\W.*)?$");
+    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.text())) {
-            msg.setFlags(msg.flags() | Message::Highlight);
-            return;
+          && 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);
+        userRegExp = QRegExp(name, caseSensitivity);
       } else {
-        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$");
+        userRegExp = QRegExp("^(.*\\W)?" + QRegExp::escape(name) + "(\\W.*)?$", caseSensitivity);
       }
       if((msg.type() & (Message::Plain | Message::Notice | Message::Action))
           && !(msg.flags() & Message::Self)
-          && userRegExp.exactMatch(msg.text())) {
+          && userRegExp.exactMatch(msg.contents())) {
         msg.setFlags(msg.flags() | Message::Highlight);
         return;
       }
@@ -597,10 +642,11 @@ 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);
+    layoutQueue.removeAll(buff);
     disconnect(buff, 0, this, 0);
   }