first working BufferViewOverlay
[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 "client.h"
26 #include "bufferviewconfig.h"
27 #include "clientbufferviewmanager.h"
28
29 const int BufferViewOverlay::_updateEventId = QEvent::registerEventType();
30
31 BufferViewOverlay::BufferViewOverlay(QObject *parent)
32   : QObject(parent),
33     _aboutToUpdate(false),
34     _addBuffersAutomatically(false),
35     _hideInactiveBuffers(false),
36     _allowedBufferTypes(0),
37     _minimumActivity(0)
38 {
39 }
40
41 void BufferViewOverlay::addView(int viewId) {
42   if(_bufferViewIds.contains(viewId))
43     return;
44
45   BufferViewConfig *config = Client::bufferViewManager()->bufferViewConfig(viewId);
46   if(!config) {
47     qDebug() << "BufferViewOverlay::addView(): no such buffer view:" << viewId;
48     return;
49   }
50
51   _bufferViewIds << viewId;
52   if(config->isInitialized()) {
53     viewInitialized(config);
54   } else {
55     disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
56     connect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
57   }
58 }
59
60 void BufferViewOverlay::removeView(int viewId) {
61   if(!_bufferViewIds.contains(viewId))
62     return;
63
64   _bufferViewIds.remove(viewId);
65   BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
66   if(config)
67     disconnect(config, 0, this, 0);
68   update();
69 }
70
71 void BufferViewOverlay::viewInitialized(BufferViewConfig *config) {
72   if(!config) {
73     qWarning() << "BufferViewOverlay::viewInitialized() received invalid view!";
74     return;
75   }
76   disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
77
78   connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(update()));
79   connect(config, SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(update()));
80   connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(update()));
81   connect(config, SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(update()));
82   connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(update()));
83   connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(update()));
84   connect(config, SIGNAL(bufferListSet()), this, SLOT(update()));
85   connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(update()));
86   connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(update()));
87   connect(config, SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(update()));
88
89   // check if the view was removed in the meantime...
90   if(_bufferViewIds.contains(config->bufferViewId()))
91     update();
92 }
93
94 void BufferViewOverlay::viewInitialized() {
95   BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
96   Q_ASSERT(config);
97
98   viewInitialized(config);
99 }
100
101 void BufferViewOverlay::update() {
102   if(_aboutToUpdate) {
103     return;
104   }
105   _aboutToUpdate = true;
106   QCoreApplication::postEvent(this, new QEvent((QEvent::Type)_updateEventId));
107 }
108
109 void BufferViewOverlay::updateHelper() {
110   bool changed = false;
111
112   bool addBuffersAutomatically = false;
113   bool hideInactiveBuffers = true;
114   int allowedBufferTypes = 0;
115   int minimumActivity = -1;
116   QSet<NetworkId> networkIds;
117   QSet<BufferId> buffers;
118   QSet<BufferId> removedBuffers;
119   QSet<BufferId> tempRemovedBuffers;
120
121   BufferViewConfig *config = 0;
122   QSet<int>::const_iterator viewIter;
123   for(viewIter = _bufferViewIds.constBegin(); viewIter != _bufferViewIds.constEnd(); viewIter++) {
124     config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
125     if(!config)
126       continue;
127
128     networkIds << config->networkId();
129     buffers += config->bufferList().toSet();
130     tempRemovedBuffers += config->temporarilyRemovedBuffers();
131
132     // in the overlay a buffer is removed it is removed from all views
133     if(removedBuffers.isEmpty())
134       removedBuffers = config->removedBuffers();
135     else
136       removedBuffers.intersect(config->removedBuffers());
137
138
139     addBuffersAutomatically |= config->addNewBuffersAutomatically();
140     hideInactiveBuffers &= config->hideInactiveBuffers();
141     allowedBufferTypes |= config->allowedBufferTypes();
142     if(minimumActivity == -1 || config->minimumActivity() < minimumActivity)
143       minimumActivity = config->minimumActivity();
144   }
145
146   changed |= (addBuffersAutomatically != _addBuffersAutomatically);
147   changed |= (hideInactiveBuffers != _hideInactiveBuffers);
148   changed |= (allowedBufferTypes != _allowedBufferTypes);
149   changed |= (minimumActivity != _minimumActivity);
150   changed |= (networkIds != _networkIds);
151   changed |= (buffers != _buffers);
152   changed |= (removedBuffers != _removedBuffers);
153   changed |= (tempRemovedBuffers != _tempRemovedBuffers);
154
155   _addBuffersAutomatically = addBuffersAutomatically;
156   _hideInactiveBuffers = hideInactiveBuffers;
157   _allowedBufferTypes = allowedBufferTypes;
158   _minimumActivity = minimumActivity;
159   _networkIds = networkIds;
160   _buffers = buffers;
161   _removedBuffers = removedBuffers;
162   _tempRemovedBuffers = tempRemovedBuffers;
163
164   if(changed)
165     emit hasChanged();
166 }
167
168 void BufferViewOverlay::customEvent(QEvent *event) {
169   if(event->type() == _updateEventId) {
170     updateHelper();
171     _aboutToUpdate = false;
172   }
173 }