1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "bufferviewoverlay.h"
25 #include "bufferviewconfig.h"
27 #include "clientbufferviewmanager.h"
28 #include "networkmodel.h"
30 const int BufferViewOverlay::_updateEventId = QEvent::registerEventType();
32 BufferViewOverlay::BufferViewOverlay(QObject *parent)
34 _aboutToUpdate(false),
35 _uninitializedViewCount(0),
36 _allowedBufferTypes(0),
41 void BufferViewOverlay::addView(int viewId) {
42 if(_bufferViewIds.contains(viewId))
45 BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
47 qDebug() << "BufferViewOverlay::addView(): no such buffer view:" << viewId;
51 _bufferViewIds << viewId;
52 _uninitializedViewCount++;
53 if(config->isInitialized()) {
54 viewInitialized(config);
56 disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
57 // we use a queued connection here since manipulating the connection list of a sending object
58 // doesn't seem to be such a good idea while executing a connected slots.
59 connect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()), Qt::QueuedConnection);
63 void BufferViewOverlay::removeView(int viewId) {
64 if(!_bufferViewIds.contains(viewId))
67 _bufferViewIds.remove(viewId);
68 BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
70 disconnect(config, 0, this, 0);
72 // update initialized State:
73 bool wasInitialized = isInitialized();
74 _uninitializedViewCount = 0;
75 QSet<int>::iterator viewIter = _bufferViewIds.begin();
76 while(viewIter != _bufferViewIds.end()) {
77 config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
79 viewIter = _bufferViewIds.erase(viewIter);
81 if(!config->isInitialized())
82 _uninitializedViewCount++;
88 if(!wasInitialized && isInitialized())
92 void BufferViewOverlay::viewInitialized(BufferViewConfig *config) {
94 qWarning() << "BufferViewOverlay::viewInitialized() received invalid view!";
97 disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
99 connect(config, SIGNAL(configChanged()), this, SLOT(update()));
100 // connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(update()));
101 // connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(update()));
102 // connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(update()));
103 // connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(update()));
104 // connect(config, SIGNAL(bufferListSet()), this, SLOT(update()));
105 // connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(update()));
106 // connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(update()));
107 // connect(config, SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(update()));
109 // check if the view was removed in the meantime...
110 if(_bufferViewIds.contains(config->bufferViewId()))
113 _uninitializedViewCount--;
118 void BufferViewOverlay::viewInitialized() {
119 BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
122 viewInitialized(config);
125 void BufferViewOverlay::update() {
129 _aboutToUpdate = true;
130 QCoreApplication::postEvent(this, new QEvent((QEvent::Type)_updateEventId));
133 void BufferViewOverlay::updateHelper() {
137 bool changed = false;
139 int allowedBufferTypes = 0;
140 int minimumActivity = -1;
141 QSet<NetworkId> networkIds;
142 QSet<BufferId> buffers;
143 QSet<BufferId> removedBuffers;
144 QSet<BufferId> tempRemovedBuffers;
146 if(Client::bufferViewManager()) {
147 BufferViewConfig *config = 0;
148 QSet<int>::const_iterator viewIter;
149 for(viewIter = _bufferViewIds.constBegin(); viewIter != _bufferViewIds.constEnd(); viewIter++) {
150 config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
154 allowedBufferTypes |= config->allowedBufferTypes();
155 if(minimumActivity == -1 || config->minimumActivity() < minimumActivity)
156 minimumActivity = config->minimumActivity();
158 networkIds << config->networkId();
161 // we have to apply several filters before we can add a buffer to a category (visible, removed, ...)
162 buffers += filterBuffersByConfig(config->bufferList(), config);
163 tempRemovedBuffers += filterBuffersByConfig(config->temporarilyRemovedBuffers().toList(), config);
164 removedBuffers += config->removedBuffers();
167 // prune the sets from overlap
168 QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
170 buffers.intersect(availableBuffers);
172 tempRemovedBuffers.intersect(availableBuffers);
173 tempRemovedBuffers.subtract(buffers);
175 removedBuffers.intersect(availableBuffers);
176 removedBuffers.subtract(tempRemovedBuffers);
177 removedBuffers.subtract(buffers);
180 changed |= (allowedBufferTypes != _allowedBufferTypes);
181 changed |= (minimumActivity != _minimumActivity);
182 changed |= (networkIds != _networkIds);
183 changed |= (buffers != _buffers);
184 changed |= (removedBuffers != _removedBuffers);
185 changed |= (tempRemovedBuffers != _tempRemovedBuffers);
187 _allowedBufferTypes = allowedBufferTypes;
188 _minimumActivity = minimumActivity;
189 _networkIds = networkIds;
191 _removedBuffers = removedBuffers;
192 _tempRemovedBuffers = tempRemovedBuffers;
194 _aboutToUpdate = false;
200 QSet<BufferId> BufferViewOverlay::filterBuffersByConfig(const QList<BufferId> &buffers, const BufferViewConfig *config) {
203 QSet<BufferId> bufferIds;
204 BufferInfo bufferInfo;
205 foreach(BufferId bufferId, buffers) {
206 bufferInfo = Client::networkModel()->bufferInfo(bufferId);
207 if(!(bufferInfo.type() & config->allowedBufferTypes()))
209 if(config->networkId().isValid() && bufferInfo.networkId() != config->networkId())
211 bufferIds << bufferId;
218 void BufferViewOverlay::customEvent(QEvent *event) {
219 if(event->type() == _updateEventId) {
224 bool BufferViewOverlay::allNetworks() {
226 return _networkIds.contains(NetworkId());
229 const QSet<NetworkId> &BufferViewOverlay::networkIds() {
234 const QSet<BufferId> &BufferViewOverlay::bufferIds() {
239 const QSet<BufferId> &BufferViewOverlay::removedBufferIds() {
241 return _removedBuffers;
244 const QSet<BufferId> &BufferViewOverlay::tempRemovedBufferIds() {
246 return _tempRemovedBuffers;
249 int BufferViewOverlay::allowedBufferTypes() {
251 return _allowedBufferTypes;
254 int BufferViewOverlay::minimumActivity() {
256 return _minimumActivity;