1 /***************************************************************************
2 * Copyright (C) 2005-2015 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "bufferviewfilter.h"
23 #include <QApplication>
27 #include "bufferinfo.h"
28 #include "buffermodel.h"
29 #include "buffersettings.h"
31 #include "clientbufferviewconfig.h"
32 #include "graphicalui.h"
33 #include "networkmodel.h"
37 /*****************************************
38 * The Filter for the Tree View
39 *****************************************/
40 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
41 : QSortFilterProxyModel(model),
43 _sortOrder(Qt::AscendingOrder),
44 _showServerQueries(false),
46 _enableEditMode(tr("Show / Hide Chats"), this)
49 setSourceModel(model);
51 setDynamicSortFilter(true);
53 _enableEditMode.setCheckable(true);
54 _enableEditMode.setChecked(_editMode);
55 connect(&_enableEditMode, SIGNAL(toggled(bool)), this, SLOT(enableEditMode(bool)));
57 BufferSettings defaultSettings;
58 defaultSettings.notify("ServerNoticesTarget", this, SLOT(showServerQueriesChanged()));
59 showServerQueriesChanged();
63 void BufferViewFilter::setConfig(BufferViewConfig *config)
65 if (_config == config)
69 disconnect(_config, 0, this, 0);
80 if (config->isInitialized()) {
84 // we use a queued connection here since manipulating the connection list of a sending object
85 // doesn't seem to be such a good idea while executing a connected slots.
86 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()), Qt::QueuedConnection);
92 void BufferViewFilter::configInitialized()
97 // connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
98 connect(config(), SIGNAL(configChanged()), this, SLOT(invalidate()));
99 // connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
100 // connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
101 // connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
102 // connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
103 // connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
104 // connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
105 // connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
106 // connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
107 // connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
108 // connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
109 // connect(config(), SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(invalidate()));
111 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
113 setObjectName(config()->bufferViewName());
116 emit configChanged();
120 void BufferViewFilter::showServerQueriesChanged()
122 BufferSettings bufferSettings;
124 bool showQueries = (bufferSettings.serverNoticesTarget() & BufferSettings::DefaultBuffer);
125 if (_showServerQueries != showQueries) {
126 _showServerQueries = showQueries;
132 QList<QAction *> BufferViewFilter::actions(const QModelIndex &index)
135 QList<QAction *> actionList;
136 actionList << &_enableEditMode;
141 void BufferViewFilter::enableEditMode(bool enable)
143 if (_editMode == enable) {
151 if (enable == false) {
152 addBuffers(QList<BufferId>::fromSet(_toAdd));
153 QSet<BufferId>::const_iterator iter;
154 for (iter = _toTempRemove.constBegin(); iter != _toTempRemove.constEnd(); ++iter) {
155 if (config()->temporarilyRemovedBuffers().contains(*iter))
157 config()->requestRemoveBuffer(*iter);
159 for (iter = _toRemove.constBegin(); iter != _toRemove.constEnd(); ++iter) {
160 if (config()->removedBuffers().contains(*iter))
162 config()->requestRemoveBufferPermanently(*iter);
166 _toTempRemove.clear();
173 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const
175 QModelIndex source_index = mapToSource(index);
176 Qt::ItemFlags flags = sourceModel()->flags(source_index);
178 NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
179 BufferInfo::Type bufferType = (BufferInfo::Type)sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt();
180 if (source_index == QModelIndex() || itemType == NetworkModel::NetworkItemType) {
181 flags |= Qt::ItemIsDropEnabled;
183 else if (_editMode) {
184 flags |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
187 // prohibit dragging of most items. and most drop places
188 // only query to query is allowed for merging
189 if (bufferType != BufferInfo::QueryBuffer) {
190 ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
191 if (clientConf && clientConf->isLocked()) {
192 flags &= ~(Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled);
200 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
202 if (!config() || !NetworkModel::mimeContainsBufferList(data))
203 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
205 NetworkId droppedNetworkId;
206 QModelIndex source_parent = mapToSource(parent);
207 if (sourceModel()->data(source_parent, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
208 droppedNetworkId = sourceModel()->data(source_parent, NetworkModel::NetworkIdRole).value<NetworkId>();
210 QList<QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
214 for (int i = 0; i < bufferList.count(); i++) {
215 networkId = bufferList[i].first;
216 bufferId = bufferList[i].second;
217 if (droppedNetworkId == networkId) {
221 if (row < rowCount(parent)) {
222 QModelIndex source_child = mapToSource(index(row, 0, parent));
223 BufferId beforeBufferId = sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>();
224 pos = config()->bufferList().indexOf(beforeBufferId);
225 if (_sortOrder == Qt::DescendingOrder)
229 if (_sortOrder == Qt::AscendingOrder)
230 pos = config()->bufferList().count();
235 if (config()->bufferList().contains(bufferId) && !config()->sortAlphabetically()) {
236 if (config()->bufferList().indexOf(bufferId) < pos)
238 ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
239 if (!clientConf || !clientConf->isLocked())
240 config()->requestMoveBuffer(bufferId, pos);
243 config()->requestAddBuffer(bufferId, pos);
254 void BufferViewFilter::sort(int column, Qt::SortOrder order)
257 QSortFilterProxyModel::sort(column, order);
261 void BufferViewFilter::addBuffer(const BufferId &bufferId) const
263 if (!config() || config()->bufferList().contains(bufferId))
266 int pos = config()->bufferList().count();
268 for (int i = 0; i < config()->bufferList().count(); i++) {
269 if (config() && config()->sortAlphabetically())
270 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
272 lt = bufferId < config()->bufferList()[i];
279 config()->requestAddBuffer(bufferId, pos);
283 void BufferViewFilter::addBuffers(const QList<BufferId> &bufferIds) const
288 QList<BufferId> bufferList = config()->bufferList();
289 foreach(BufferId bufferId, bufferIds) {
290 if (bufferList.contains(bufferId))
293 int pos = bufferList.count();
295 for (int i = 0; i < bufferList.count(); i++) {
296 if (config() && config()->sortAlphabetically())
297 lt = bufferIdLessThan(bufferId, bufferList[i]);
299 lt = bufferId < config()->bufferList()[i];
303 bufferList.insert(pos, bufferId);
307 config()->requestAddBuffer(bufferId, pos);
312 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const
314 // no config -> "all buffers" -> accept everything
318 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
319 Q_ASSERT(bufferId.isValid());
321 int activityLevel = sourceModel()->data(source_bufferIndex, NetworkModel::BufferActivityRole).toInt();
323 if (!config()->bufferList().contains(bufferId) && !_editMode) {
324 // add the buffer if...
325 if (config()->isInitialized()
326 && !config()->removedBuffers().contains(bufferId) // it hasn't been manually removed and either
327 && ((config()->addNewBuffersAutomatically() && !config()->temporarilyRemovedBuffers().contains(bufferId)) // is totally unknown to us (a new buffer)...
328 || (config()->temporarilyRemovedBuffers().contains(bufferId) && activityLevel > BufferInfo::OtherActivity))) { // or was just temporarily hidden and has a new message waiting for us.
331 // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
335 if (config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
338 int allowedBufferTypes = config()->allowedBufferTypes();
339 if (!config()->networkId().isValid())
340 allowedBufferTypes &= ~BufferInfo::StatusBuffer;
341 int bufferType = sourceModel()->data(source_bufferIndex, NetworkModel::BufferTypeRole).toInt();
342 if (!(allowedBufferTypes & bufferType))
345 if (bufferType & BufferInfo::QueryBuffer && !_showServerQueries && sourceModel()->data(source_bufferIndex, Qt::DisplayRole).toString().contains('.')) {
349 // the following dynamic filters may not trigger if the buffer is currently selected.
350 QModelIndex currentIndex = Client::bufferModel()->standardSelectionModel()->currentIndex();
351 if (bufferId == Client::bufferModel()->data(currentIndex, NetworkModel::BufferIdRole).value<BufferId>())
354 if (config()->hideInactiveBuffers() && !sourceModel()->data(source_bufferIndex, NetworkModel::ItemActiveRole).toBool() && activityLevel <= BufferInfo::OtherActivity)
357 if (config()->minimumActivity() > activityLevel)
364 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const
369 if (config()->hideInactiveNetworks() && !(sourceModel()->data(source_index, NetworkModel::ItemActiveRole).toBool())) {
373 if (!config()->networkId().isValid()) {
377 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
382 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
384 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
386 if (!child.isValid()) {
387 qWarning() << "filterAcceptsRow has been called with an invalid Child";
391 NetworkModel::ItemType childType = (NetworkModel::ItemType)sourceModel()->data(child, NetworkModel::ItemTypeRole).toInt();
393 case NetworkModel::NetworkItemType:
394 return filterAcceptNetwork(child);
395 case NetworkModel::BufferItemType:
396 return filterAcceptBuffer(child);
403 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
405 int leftItemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
406 int rightItemType = sourceModel()->data(source_right, NetworkModel::ItemTypeRole).toInt();
407 int itemType = leftItemType & rightItemType;
409 case NetworkModel::NetworkItemType:
410 return networkLessThan(source_left, source_right);
411 case NetworkModel::BufferItemType:
412 return bufferLessThan(source_left, source_right);
414 return QSortFilterProxyModel::lessThan(source_left, source_right);
419 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
421 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
422 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
424 int leftPos = config()->bufferList().indexOf(leftBufferId);
425 int rightPos = config()->bufferList().indexOf(rightBufferId);
426 if (leftPos == -1 && rightPos == -1)
427 return QSortFilterProxyModel::lessThan(source_left, source_right);
428 if (leftPos == -1 || rightPos == -1)
429 return !(leftPos < rightPos);
430 return leftPos < rightPos;
433 return bufferIdLessThan(leftBufferId, rightBufferId);
437 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
439 // NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
440 // NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
442 return QSortFilterProxyModel::lessThan(source_left, source_right);
446 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const
450 case Qt::ForegroundRole:
451 case Qt::BackgroundRole:
452 case Qt::DecorationRole:
453 if ((config() && config()->disableDecoration()))
455 return GraphicalUi::uiStyle()->bufferViewItemData(mapToSource(index), role);
456 case Qt::CheckStateRole:
457 return checkedState(index);
459 return QSortFilterProxyModel::data(index, role);
464 QVariant BufferViewFilter::checkedState(const QModelIndex &index) const
466 if (!_editMode || !config())
469 QModelIndex source_index = mapToSource(index);
470 if (source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
473 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
474 if (_toAdd.contains(bufferId))
477 if (_toTempRemove.contains(bufferId))
478 return Qt::PartiallyChecked;
480 if (_toRemove.contains(bufferId))
481 return Qt::Unchecked;
483 if (config()->bufferList().contains(bufferId))
486 if (config()->temporarilyRemovedBuffers().contains(bufferId))
487 return Qt::PartiallyChecked;
489 return Qt::Unchecked;
493 bool BufferViewFilter::setData(const QModelIndex &index, const QVariant &value, int role)
496 case Qt::CheckStateRole:
497 return setCheckedState(index, Qt::CheckState(value.toInt()));
499 return QSortFilterProxyModel::setData(index, value, role);
504 bool BufferViewFilter::setCheckedState(const QModelIndex &index, Qt::CheckState state)
506 QModelIndex source_index = mapToSource(index);
507 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
508 if (!bufferId.isValid())
513 _toAdd.remove(bufferId);
514 _toTempRemove.remove(bufferId);
515 _toRemove << bufferId;
517 case Qt::PartiallyChecked:
518 _toAdd.remove(bufferId);
519 _toTempRemove << bufferId;
520 _toRemove.remove(bufferId);
524 _toTempRemove.remove(bufferId);
525 _toRemove.remove(bufferId);
530 emit dataChanged(index, index);
535 bool BufferViewFilter::bufferIdLessThan(const BufferId &left, const BufferId &right)
537 Q_CHECK_PTR(Client::networkModel());
538 if (!Client::networkModel())
541 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
542 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
544 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
545 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
547 if (leftType != rightType)
548 return leftType < rightType;
550 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;