+#include "client.h"
+#include "networkmodel.h"
+#include "quassel.h"
+
+BufferModel::BufferModel(NetworkModel* parent)
+ : QSortFilterProxyModel(parent)
+ , _selectionModelSynchronizer(this)
+{
+ setSourceModel(parent);
+ if (Quassel::isOptionSet("debugbufferswitches")) {
+ connect(_selectionModelSynchronizer.selectionModel(), &QItemSelectionModel::currentChanged, this, &BufferModel::debug_currentChanged);
+ }
+ connect(Client::instance(), &Client::networkCreated, this, &BufferModel::newNetwork);
+ connect(this, &QAbstractItemModel::rowsInserted, this, &BufferModel::newBuffers);
+}
+
+bool BufferModel::filterAcceptsRow(int sourceRow, const QModelIndex& parent) const
+{
+ Q_UNUSED(sourceRow);
+ // only networks and buffers are allowed
+ if (!parent.isValid())
+ return true;
+ if (parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
+ return true;
+
+ return false;
+}
+
+void BufferModel::newNetwork(NetworkId id)
+{
+ const Network* net = Client::network(id);
+ Q_ASSERT(net);
+ connect(net, &Network::connectionStateSet, this, &BufferModel::networkConnectionChanged);
+}
+
+void BufferModel::networkConnectionChanged(Network::ConnectionState state)
+{
+ switch (state) {
+ case Network::Connecting:
+ case Network::Initializing:
+ if (currentIndex().isValid())
+ return;
+ {
+ auto* net = qobject_cast<Network*>(sender());
+ Q_ASSERT(net);
+ setCurrentIndex(mapFromSource(Client::networkModel()->networkIndex(net->networkId())));
+ }
+ break;
+ default:
+ return;
+ }
+}
+
+void BufferModel::synchronizeView(QAbstractItemView* view)