f07c4a3afe54142155769a6f85fd639f8d9ecfaa
[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     update();
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   update();
66 }
67
68 void BufferViewOverlay::viewInitialized() {
69   BufferViewConfig *config = qobject_cast<BufferViewConfig *>(sender());
70   Q_ASSERT(config);
71
72   disconnect(config, SIGNAL(initDone()), this, SLOT(viewInitialized()));
73
74   // check if the view was removed in the meantime...
75   if(_bufferViewIds.contains(config->bufferViewId()))
76     update();
77 }
78
79 void BufferViewOverlay::update() {
80   if(_aboutToUpdate) {
81     return;
82   }
83   _aboutToUpdate = true;
84 }
85
86 void BufferViewOverlay::updateHelper() {
87   bool changed = false;
88
89   bool addBuffersAutomatically = false;
90   bool hideInactiveBuffers = true;
91   int allowedBufferTypes = 0;
92   int minimumActivity = -1;
93   QSet<NetworkId> networkIds;
94   QSet<BufferId> buffers;
95   QSet<BufferId> removedBuffers;
96   QSet<BufferId> tempRemovedBuffers;
97   
98   BufferViewConfig *config = 0;
99   QSet<int>::const_iterator viewIter;
100   for(viewIter = _bufferViewIds.constBegin(); viewIter != _bufferViewIds.constEnd(); viewIter++) {
101     config = Client::bufferViewManager()->bufferViewConfig(*viewIter);
102     if(!config)
103       continue;
104
105     networkIds << config->networkId();
106     buffers += config->bufferList().toSet();
107     removedBuffers += config->removedBuffers();
108     tempRemovedBuffers += config->temporarilyRemovedBuffers();
109
110     addBuffersAutomatically |= config->addNewBuffersAutomatically();
111     hideInactiveBuffers &= config->hideInactiveBuffers();
112     allowedBufferTypes |= config->allowedBufferTypes();
113     if(minimumActivity == -1 || config->minimumActivity() < minimumActivity)
114       minimumActivity = config->minimumActivity();
115   }
116
117   changed |= (addBuffersAutomatically != _addBuffersAutomatically);
118   changed |= (hideInactiveBuffers != _hideInactiveBuffers);
119   changed |= (allowedBufferTypes != _allowedBufferTypes);
120   changed |= (minimumActivity != _minimumActivity);
121   changed |= (networkIds != _networkIds);
122   changed |= (buffers != _buffers);
123   changed |= (removedBuffers != _removedBuffers);
124   changed |= (tempRemovedBuffers != _tempRemovedBuffers);
125
126   _addBuffersAutomatically = addBuffersAutomatically;
127   _hideInactiveBuffers = hideInactiveBuffers;
128   _allowedBufferTypes = allowedBufferTypes;
129   _minimumActivity = minimumActivity;
130   _networkIds = networkIds;
131   _buffers = buffers;
132   _removedBuffers = removedBuffers;
133   _tempRemovedBuffers = tempRemovedBuffers;
134   
135   if(changed)
136     emit hasChanged();
137 }
138
139 void BufferViewOverlay::customEvent(QEvent *event) {
140   if(event->type() == _updateEventId) {
141     updateHelper();
142     _aboutToUpdate = false;
143   }
144 }