Goodbye SyncRelay, you will live on in our hearts...
[quassel.git] / src / client / bufferviewoverlay.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "bufferviewoverlay.h"
22
23 #include <QEvent>
24
25 #include "bufferviewconfig.h"
26 #include "client.h"
27 #include "clientbufferviewmanager.h"
28 #include "networkmodel.h"
29
30 const int BufferViewOverlay::_updateEventId = QEvent::registerEventType();
31
32 BufferViewOverlay::BufferViewOverlay(QObject *parent)
33   : QObject(parent),
34     _aboutToUpdate(false),
35     _uninitializedViewCount(0),
36     _addBuffersAutomatically(false),
37     _hideInactiveBuffers(false),
38     _allowedBufferTypes(0),
39     _minimumActivity(0)
40 {
41 }
42
43 void BufferViewOverlay::addView(int viewId) {
44   if(_bufferViewIds.contains(viewId))
45     return;
46
47   BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
48   if(!config) {
49     qDebug() << "BufferViewOverlay::addView(): no such buffer view:" << viewId;
50     return;
51   }
52
53   _bufferViewIds << viewId;
54   _uninitializedViewCount++;
55   if(config->isInitialized()) {
56     viewInitialized(config);
57   } else {
58     disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
59     // we use a queued connection here since manipulating the connection list of a sending object
60     // doesn't seem to be such a good idea while executing a connected slots.
61     connect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()), Qt::QueuedConnection);
62   }
63 }
64
65 void BufferViewOverlay::removeView(int viewId) {
66   if(!_bufferViewIds.contains(viewId))
67     return;
68
69   _bufferViewIds.remove(viewId);
70   BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
71   if(config)
72     disconnect(config, 0, this, 0);
73
74   // update initialized State:
75   bool wasInitialized = isInitialized();
76   _uninitializedViewCount = 0;
77   QSet<int>::iterator viewIter = _bufferViewIds.begin();
78   while(viewIter != _bufferViewIds.end()) {
79     config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
80     if(!config) {
81       viewIter = _bufferViewIds.erase(viewIter);
82     } else {
83       if(!config->isInitialized())
84         _uninitializedViewCount++;
85       viewIter++;
86     }
87   }
88
89   update();
90   if(!wasInitialized && isInitialized())
91     emit initDone();
92 }
93
94 void BufferViewOverlay::viewInitialized(BufferViewConfig *config) {
95   if(!config) {
96     qWarning() << "BufferViewOverlay::viewInitialized() received invalid view!";
97     return;
98   }
99   disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
100
101   connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(update()));
102   connect(config, SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(update()));
103   connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(update()));
104   connect(config, SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(update()));
105   connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(update()));
106   connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(update()));
107   connect(config, SIGNAL(bufferListSet()), this, SLOT(update()));
108   connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(update()));
109   connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(update()));
110   connect(config, SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(update()));
111
112   // check if the view was removed in the meantime...
113   if(_bufferViewIds.contains(config->bufferViewId()))
114     update();
115
116   _uninitializedViewCount--;
117   if(isInitialized())
118     emit initDone();
119 }
120
121 void BufferViewOverlay::viewInitialized() {
122   BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
123   Q_ASSERT(config);
124
125   viewInitialized(config);
126 }
127
128 void BufferViewOverlay::update() {
129   if(_aboutToUpdate) {
130     return;
131   }
132   _aboutToUpdate = true;
133   QCoreApplication::postEvent(this, new QEvent((QEvent::Type)_updateEventId));
134 }
135
136 void BufferViewOverlay::updateHelper() {
137   if(!_aboutToUpdate)
138     return;
139
140   bool changed = false;
141
142   bool addBuffersAutomatically = false;
143   bool hideInactiveBuffers = true;
144   int allowedBufferTypes = 0;
145   int minimumActivity = -1;
146   QSet<NetworkId> networkIds;
147   QSet<BufferId> buffers;
148   QSet<BufferId> removedBuffers;
149   QSet<BufferId> tempRemovedBuffers;
150
151   if(Client::bufferViewManager()) {
152     BufferViewConfig *config = 0;
153     QSet<int>::const_iterator viewIter;
154     for(viewIter = _bufferViewIds.constBegin(); viewIter != _bufferViewIds.constEnd(); viewIter++) {
155       config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
156       if(!config)
157         continue;
158
159       networkIds << config->networkId();
160       if(config->networkId().isValid()) {
161         NetworkId networkId = config->networkId();
162         // we have to filter out all the buffers that don't belong to this net... :/
163         QSet<BufferId> bufferIds;
164         foreach(BufferId bufferId, config->bufferList()) {
165           if(Client::networkModel()->networkId(bufferId) == networkId)
166             bufferIds << bufferId;
167         }
168         buffers += bufferIds;
169
170         bufferIds.clear();
171         foreach(BufferId bufferId, config->temporarilyRemovedBuffers()) {
172           if(Client::networkModel()->networkId(bufferId) == networkId)
173             bufferIds << bufferId;
174         }
175         tempRemovedBuffers += bufferIds;
176       } else {
177         buffers += config->bufferList().toSet();
178         tempRemovedBuffers += config->temporarilyRemovedBuffers();
179       }
180
181       // in the overlay a buffer is removed it is removed from all views
182       if(removedBuffers.isEmpty())
183         removedBuffers = config->removedBuffers();
184       else
185         removedBuffers.intersect(config->removedBuffers());
186
187
188       addBuffersAutomatically |= config->addNewBuffersAutomatically();
189       hideInactiveBuffers &= config->hideInactiveBuffers();
190       allowedBufferTypes |= config->allowedBufferTypes();
191       if(minimumActivity == -1 || config->minimumActivity() < minimumActivity)
192         minimumActivity = config->minimumActivity();
193     }
194     QSet<BufferId> availableBuffers = Client::networkModel()->allBufferIds().toSet();
195     buffers.intersect(availableBuffers);
196     tempRemovedBuffers.intersect(availableBuffers);
197   }
198
199   changed |= (addBuffersAutomatically != _addBuffersAutomatically);
200   changed |= (hideInactiveBuffers != _hideInactiveBuffers);
201   changed |= (allowedBufferTypes != _allowedBufferTypes);
202   changed |= (minimumActivity != _minimumActivity);
203   changed |= (networkIds != _networkIds);
204   changed |= (buffers != _buffers);
205   changed |= (removedBuffers != _removedBuffers);
206   changed |= (tempRemovedBuffers != _tempRemovedBuffers);
207
208   _addBuffersAutomatically = addBuffersAutomatically;
209   _hideInactiveBuffers = hideInactiveBuffers;
210   _allowedBufferTypes = allowedBufferTypes;
211   _minimumActivity = minimumActivity;
212   _networkIds = networkIds;
213   _buffers = buffers;
214   _removedBuffers = removedBuffers;
215   _tempRemovedBuffers = tempRemovedBuffers;
216
217   _aboutToUpdate = false;
218
219   if(changed)
220     emit hasChanged();
221 }
222
223 void BufferViewOverlay::customEvent(QEvent *event) {
224   if(event->type() == _updateEventId) {
225     updateHelper();
226   }
227 }
228
229 bool BufferViewOverlay::allNetworks() {
230   updateHelper();
231   return _networkIds.contains(NetworkId());
232 }
233
234 const QSet<NetworkId> &BufferViewOverlay::networkIds() {
235   updateHelper();
236   return _networkIds;
237 }
238
239 const QSet<BufferId> &BufferViewOverlay::bufferIds() {
240   updateHelper();
241   return _buffers;
242 }
243
244 const QSet<BufferId> &BufferViewOverlay::removedBufferIds() {
245   updateHelper();
246   return _removedBuffers;
247 }
248
249 const QSet<BufferId> &BufferViewOverlay::tempRemovedBufferIds() {
250   updateHelper();
251   return _tempRemovedBuffers;
252 }
253
254 bool BufferViewOverlay::addBuffersAutomatically() {
255   updateHelper();
256   return _addBuffersAutomatically;
257 }
258
259 bool BufferViewOverlay::hideInactiveBuffers() {
260   updateHelper();
261   return _hideInactiveBuffers;
262 }
263
264 int BufferViewOverlay::allowedBufferTypes() {
265   updateHelper();
266   return _allowedBufferTypes;
267 }
268
269 int BufferViewOverlay::minimumActivity() {
270   updateHelper();
271   return _minimumActivity;
272 }