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 _addBuffersAutomatically(false),
36 _hideInactiveBuffers(false),
37 _allowedBufferTypes(0),
42 void BufferViewOverlay::addView(int viewId) {
43 if(_bufferViewIds.contains(viewId))
46 BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
48 qDebug() << "BufferViewOverlay::addView(): no such buffer view:" << viewId;
52 _bufferViewIds << viewId;
53 if(config->isInitialized()) {
54 viewInitialized(config);
56 disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
57 connect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
61 void BufferViewOverlay::removeView(int viewId) {
62 if(!_bufferViewIds.contains(viewId))
65 _bufferViewIds.remove(viewId);
66 BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
68 disconnect(config, 0, this, 0);
72 void BufferViewOverlay::viewInitialized(BufferViewConfig *config) {
74 qWarning() << "BufferViewOverlay::viewInitialized() received invalid view!";
77 disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
79 connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(update()));
80 connect(config, SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(update()));
81 connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(update()));
82 connect(config, SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(update()));
83 connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(update()));
84 connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(update()));
85 connect(config, SIGNAL(bufferListSet()), this, SLOT(update()));
86 connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(update()));
87 connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(update()));
88 connect(config, SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(update()));
90 // check if the view was removed in the meantime...
91 if(_bufferViewIds.contains(config->bufferViewId()))
95 void BufferViewOverlay::viewInitialized() {
96 BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
99 viewInitialized(config);
102 void BufferViewOverlay::update() {
106 _aboutToUpdate = true;
107 QCoreApplication::postEvent(this, new QEvent((QEvent::Type)_updateEventId));
110 void BufferViewOverlay::updateHelper() {
114 bool changed = false;
116 bool addBuffersAutomatically = false;
117 bool hideInactiveBuffers = true;
118 int allowedBufferTypes = 0;
119 int minimumActivity = -1;
120 QSet<NetworkId> networkIds;
121 QSet<BufferId> buffers;
122 QSet<BufferId> removedBuffers;
123 QSet<BufferId> tempRemovedBuffers;
125 if(Client::bufferViewManager()) {
126 BufferViewConfig *config = 0;
127 QSet<int>::const_iterator viewIter;
128 for(viewIter = _bufferViewIds.constBegin(); viewIter != _bufferViewIds.constEnd(); viewIter++) {
129 config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
133 networkIds << config->networkId();
134 if(config->networkId().isValid()) {
135 NetworkId networkId = config->networkId();
136 // we have to filter out all the buffers that don't belong to this net... :/
137 QSet<BufferId> bufferIds;
138 foreach(BufferId bufferId, config->bufferList()) {
139 if(Client::networkModel()->networkId(bufferId) == networkId)
140 bufferIds << bufferId;
142 buffers += bufferIds;
145 foreach(BufferId bufferId, config->temporarilyRemovedBuffers()) {
146 if(Client::networkModel()->networkId(bufferId) == networkId)
147 bufferIds << bufferId;
149 tempRemovedBuffers += bufferIds;
151 buffers += config->bufferList().toSet();
152 tempRemovedBuffers += config->temporarilyRemovedBuffers();
155 // in the overlay a buffer is removed it is removed from all views
156 if(removedBuffers.isEmpty())
157 removedBuffers = config->removedBuffers();
159 removedBuffers.intersect(config->removedBuffers());
162 addBuffersAutomatically |= config->addNewBuffersAutomatically();
163 hideInactiveBuffers &= config->hideInactiveBuffers();
164 allowedBufferTypes |= config->allowedBufferTypes();
165 if(minimumActivity == -1 || config->minimumActivity() < minimumActivity)
166 minimumActivity = config->minimumActivity();
168 QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
169 buffers.intersect(availableBuffers);
170 tempRemovedBuffers.intersect(availableBuffers);
173 changed |= (addBuffersAutomatically != _addBuffersAutomatically);
174 changed |= (hideInactiveBuffers != _hideInactiveBuffers);
175 changed |= (allowedBufferTypes != _allowedBufferTypes);
176 changed |= (minimumActivity != _minimumActivity);
177 changed |= (networkIds != _networkIds);
178 changed |= (buffers != _buffers);
179 changed |= (removedBuffers != _removedBuffers);
180 changed |= (tempRemovedBuffers != _tempRemovedBuffers);
182 _addBuffersAutomatically = addBuffersAutomatically;
183 _hideInactiveBuffers = hideInactiveBuffers;
184 _allowedBufferTypes = allowedBufferTypes;
185 _minimumActivity = minimumActivity;
186 _networkIds = networkIds;
188 _removedBuffers = removedBuffers;
189 _tempRemovedBuffers = tempRemovedBuffers;
191 _aboutToUpdate = false;
197 void BufferViewOverlay::customEvent(QEvent *event) {
198 if(event->type() == _updateEventId) {
203 bool BufferViewOverlay::allNetworks() {
205 return _networkIds.contains(NetworkId());
208 const QSet<NetworkId> &BufferViewOverlay::networkIds() {
213 const QSet<BufferId> &BufferViewOverlay::bufferIds() {
218 const QSet<BufferId> &BufferViewOverlay::removedBufferIds() {
220 return _removedBuffers;
223 const QSet<BufferId> &BufferViewOverlay::tempRemovedBufferIds() {
225 return _tempRemovedBuffers;
228 bool BufferViewOverlay::addBuffersAutomatically() {
230 return _addBuffersAutomatically;
233 bool BufferViewOverlay::hideInactiveBuffers() {
235 return _hideInactiveBuffers;
238 int BufferViewOverlay::allowedBufferTypes() {
240 return _allowedBufferTypes;
243 int BufferViewOverlay::minimumActivity() {
245 return _minimumActivity;