BufferViewOverlay now correctly respects filtering of buffer types
[quassel.git] / src / client / bufferviewoverlay.cpp
index 4ec4123..f4b2985 100644 (file)
 
 #include <QEvent>
 
-#include "client.h"
 #include "bufferviewconfig.h"
+#include "client.h"
 #include "clientbufferviewmanager.h"
+#include "networkmodel.h"
 
 const int BufferViewOverlay::_updateEventId = QEvent::registerEventType();
 
 BufferViewOverlay::BufferViewOverlay(QObject *parent)
   : QObject(parent),
     _aboutToUpdate(false),
-    _addBuffersAutomatically(false),
-    _hideInactiveBuffers(false),
+    _uninitializedViewCount(0),
     _allowedBufferTypes(0),
     _minimumActivity(0)
 {
@@ -49,11 +49,14 @@ void BufferViewOverlay::addView(int viewId) {
   }
 
   _bufferViewIds << viewId;
+  _uninitializedViewCount++;
   if(config->isInitialized()) {
     viewInitialized(config);
   } else {
     disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
-    connect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
+    // we use a queued connection here since manipulating the connection list of a sending object
+    // doesn't seem to be such a good idea while executing a connected slots.
+    connect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()), Qt::QueuedConnection);
   }
 }
 
@@ -62,10 +65,28 @@ void BufferViewOverlay::removeView(int viewId) {
     return;
 
   _bufferViewIds.remove(viewId);
-  BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
+  BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
   if(config)
     disconnect(config, 0, this, 0);
+
+  // update initialized State:
+  bool wasInitialized = isInitialized();
+  _uninitializedViewCount = 0;
+  QSet<int>::iterator viewIter = _bufferViewIds.begin();
+  while(viewIter != _bufferViewIds.end()) {
+    config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
+    if(!config) {
+      viewIter = _bufferViewIds.erase(viewIter);
+    } else {
+      if(!config->isInitialized())
+        _uninitializedViewCount++;
+      viewIter++;
+    }
+  }
+
   update();
+  if(!wasInitialized && isInitialized())
+    emit initDone();
 }
 
 void BufferViewOverlay::viewInitialized(BufferViewConfig *config) {
@@ -75,20 +96,23 @@ void BufferViewOverlay::viewInitialized(BufferViewConfig *config) {
   }
   disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
 
-  connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(update()));
-  connect(config, SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(update()));
-  connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(update()));
-  connect(config, SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(update()));
-  connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(update()));
-  connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(update()));
-  connect(config, SIGNAL(bufferListSet()), this, SLOT(update()));
-  connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(update()));
-  connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(update()));
-  connect(config, SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(update()));
+  connect(config, SIGNAL(configChanged()), this, SLOT(update()));
+//   connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(update()));
+//   connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(update()));
+//   connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(update()));
+//   connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(update()));
+//   connect(config, SIGNAL(bufferListSet()), this, SLOT(update()));
+//   connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(update()));
+//   connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(update()));
+//   connect(config, SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(update()));
 
   // check if the view was removed in the meantime...
   if(_bufferViewIds.contains(config->bufferViewId()))
     update();
+
+  _uninitializedViewCount--;
+  if(isInitialized())
+    emit initDone();
 }
 
 void BufferViewOverlay::viewInitialized() {
@@ -107,10 +131,11 @@ void BufferViewOverlay::update() {
 }
 
 void BufferViewOverlay::updateHelper() {
+  if(!_aboutToUpdate)
+    return;
+
   bool changed = false;
 
-  bool addBuffersAutomatically = false;
-  bool hideInactiveBuffers = true;
   int allowedBufferTypes = 0;
   int minimumActivity = -1;
   QSet<NetworkId> networkIds;
@@ -125,27 +150,33 @@ void BufferViewOverlay::updateHelper() {
       config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
       if(!config)
         continue;
-      networkIds << config->networkId();
-      buffers += config->bufferList().toSet();
-      tempRemovedBuffers += config->temporarilyRemovedBuffers();
-
-      // in the overlay a buffer is removed it is removed from all views
-      if(removedBuffers.isEmpty())
-        removedBuffers = config->removedBuffers();
-      else
-        removedBuffers.intersect(config->removedBuffers());
-
 
-      addBuffersAutomatically |= config->addNewBuffersAutomatically();
-      hideInactiveBuffers &= config->hideInactiveBuffers();
       allowedBufferTypes |= config->allowedBufferTypes();
       if(minimumActivity == -1 || config->minimumActivity() < minimumActivity)
         minimumActivity = config->minimumActivity();
+
+      networkIds << config->networkId();
+
+
+      // we have to apply several filters before we can add a buffer to a category (visible, removed, ...)
+      buffers += filterBuffersByConfig(config->bufferList(), config);
+      tempRemovedBuffers += filterBuffersByConfig(config->temporarilyRemovedBuffers().toList(), config);
+      removedBuffers += config->removedBuffers();
     }
+
+    // prune the sets from overlap
+    QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
+
+    buffers.intersect(availableBuffers);
+
+    tempRemovedBuffers.intersect(availableBuffers);
+    tempRemovedBuffers.subtract(buffers);
+
+    removedBuffers.intersect(availableBuffers);
+    removedBuffers.subtract(tempRemovedBuffers);
+    removedBuffers.subtract(buffers);
   }
 
-  changed |= (addBuffersAutomatically != _addBuffersAutomatically);
-  changed |= (hideInactiveBuffers != _hideInactiveBuffers);
   changed |= (allowedBufferTypes != _allowedBufferTypes);
   changed |= (minimumActivity != _minimumActivity);
   changed |= (networkIds != _networkIds);
@@ -153,8 +184,6 @@ void BufferViewOverlay::updateHelper() {
   changed |= (removedBuffers != _removedBuffers);
   changed |= (tempRemovedBuffers != _tempRemovedBuffers);
 
-  _addBuffersAutomatically = addBuffersAutomatically;
-  _hideInactiveBuffers = hideInactiveBuffers;
   _allowedBufferTypes = allowedBufferTypes;
   _minimumActivity = minimumActivity;
   _networkIds = networkIds;
@@ -162,13 +191,67 @@ void BufferViewOverlay::updateHelper() {
   _removedBuffers = removedBuffers;
   _tempRemovedBuffers = tempRemovedBuffers;
 
+  _aboutToUpdate = false;
+
   if(changed)
     emit hasChanged();
 }
 
+QSet<BufferId> BufferViewOverlay::filterBuffersByConfig(const QList<BufferId> &buffers, const BufferViewConfig *config) {
+  Q_ASSERT(config);
+
+  QSet<BufferId> bufferIds;
+  BufferInfo bufferInfo;
+  foreach(BufferId bufferId, buffers) {
+    bufferInfo = Client::networkModel()->bufferInfo(bufferId);
+    if(!(bufferInfo.type() & config->allowedBufferTypes()))
+      continue;
+    if(config->networkId().isValid() && bufferInfo.networkId() != config->networkId())
+      continue;
+    bufferIds << bufferId;
+  }
+
+  return bufferIds;
+}
+
+
 void BufferViewOverlay::customEvent(QEvent *event) {
   if(event->type() == _updateEventId) {
     updateHelper();
-    _aboutToUpdate = false;
   }
 }
+
+bool BufferViewOverlay::allNetworks() {
+  updateHelper();
+  return _networkIds.contains(NetworkId());
+}
+
+const QSet<NetworkId> &BufferViewOverlay::networkIds() {
+  updateHelper();
+  return _networkIds;
+}
+
+const QSet<BufferId> &BufferViewOverlay::bufferIds() {
+  updateHelper();
+  return _buffers;
+}
+
+const QSet<BufferId> &BufferViewOverlay::removedBufferIds() {
+  updateHelper();
+  return _removedBuffers;
+}
+
+const QSet<BufferId> &BufferViewOverlay::tempRemovedBufferIds() {
+  updateHelper();
+  return _tempRemovedBuffers;
+}
+
+int BufferViewOverlay::allowedBufferTypes() {
+  updateHelper();
+  return _allowedBufferTypes;
+}
+
+int BufferViewOverlay::minimumActivity() {
+  updateHelper();
+  return _minimumActivity;
+}