Fix an issue with the event loop
[quassel.git] / src / common / bufferviewconfig.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "bufferviewconfig.h"
22
23 #include "bufferinfo.h"
24
25 INIT_SYNCABLE_OBJECT(BufferViewConfig)
26 BufferViewConfig::BufferViewConfig(int bufferViewId, QObject *parent)
27   : SyncableObject(parent),
28     _bufferViewId(bufferViewId),
29     _addNewBuffersAutomatically(true),
30     _sortAlphabetically(true),
31     _hideInactiveBuffers(false),
32     _disableDecoration(false),
33     _allowedBufferTypes(BufferInfo::StatusBuffer | BufferInfo::ChannelBuffer | BufferInfo::QueryBuffer | BufferInfo::GroupBuffer),
34     _minimumActivity(0)
35 {
36   setObjectName(QString::number(bufferViewId));
37 }
38
39 BufferViewConfig::BufferViewConfig(int bufferViewId, const QVariantMap &properties, QObject *parent)
40   : SyncableObject(parent),
41     _bufferViewId(bufferViewId)
42 {
43   fromVariantMap(properties);
44   setObjectName(QString::number(bufferViewId));
45 }
46
47 void BufferViewConfig::setBufferViewName(const QString &bufferViewName) {
48   if(_bufferViewName == bufferViewName)
49     return;
50
51   _bufferViewName = bufferViewName;
52   SYNC(ARG(bufferViewName))
53   emit bufferViewNameSet(bufferViewName);
54 }
55
56 void BufferViewConfig::setNetworkId(const NetworkId &networkId) {
57   if(_networkId == networkId)
58     return;
59
60   _networkId = networkId;
61   SYNC(ARG(networkId))
62   emit networkIdSet(networkId);
63   emit configChanged();
64 }
65
66 void BufferViewConfig::setAddNewBuffersAutomatically(bool addNewBuffersAutomatically) {
67   if(_addNewBuffersAutomatically == addNewBuffersAutomatically)
68     return;
69
70   _addNewBuffersAutomatically = addNewBuffersAutomatically;
71   SYNC(ARG(addNewBuffersAutomatically))
72   emit configChanged();
73 }
74
75 void BufferViewConfig::setSortAlphabetically(bool sortAlphabetically) {
76   if(_sortAlphabetically == sortAlphabetically)
77     return;
78
79   _sortAlphabetically = sortAlphabetically;
80   SYNC(ARG(sortAlphabetically))
81   emit configChanged();
82 }
83
84 void BufferViewConfig::setDisableDecoration(bool disableDecoration) {
85   if(_disableDecoration == disableDecoration)
86     return;
87
88   _disableDecoration = disableDecoration;
89   SYNC(ARG(disableDecoration))
90 }
91
92 void BufferViewConfig::setAllowedBufferTypes(int bufferTypes) {
93   if(_allowedBufferTypes == bufferTypes)
94     return;
95
96   _allowedBufferTypes = bufferTypes;
97   SYNC(ARG(bufferTypes))
98   emit configChanged();
99 }
100
101 void BufferViewConfig::setMinimumActivity(int activity) {
102   if(_minimumActivity == activity)
103     return;
104
105   _minimumActivity = activity;
106   SYNC(ARG(activity))
107   emit configChanged();
108 }
109
110 void BufferViewConfig::setHideInactiveBuffers(bool hideInactiveBuffers) {
111   if(_hideInactiveBuffers == hideInactiveBuffers)
112     return;
113
114   _hideInactiveBuffers = hideInactiveBuffers;
115   SYNC(ARG(hideInactiveBuffers))
116   emit configChanged();
117 }
118
119 QVariantList BufferViewConfig::initBufferList() const {
120   QVariantList buffers;
121
122   foreach(BufferId bufferId, _buffers) {
123     buffers << qVariantFromValue(bufferId);
124   }
125
126   return buffers;
127 }
128
129 void BufferViewConfig::initSetBufferList(const QVariantList &buffers) {
130   _buffers.clear();
131
132   foreach(QVariant buffer, buffers) {
133     _buffers << buffer.value<BufferId>();
134   }
135
136   emit configChanged(); // used to track changes in the settingspage
137 }
138
139 void BufferViewConfig::initSetBufferList(const QList<BufferId> &buffers) {
140   _buffers.clear();
141
142   foreach(BufferId bufferId, buffers) {
143     _buffers << bufferId;
144   }
145
146   emit configChanged(); // used to track changes in the settingspage
147 }
148
149 QVariantList BufferViewConfig::initRemovedBuffers() const {
150   QVariantList removedBuffers;
151
152   foreach(BufferId bufferId, _removedBuffers) {
153     removedBuffers << qVariantFromValue(bufferId);
154   }
155
156   return removedBuffers;
157 }
158
159 void BufferViewConfig::initSetRemovedBuffers(const QVariantList &buffers) {
160   _removedBuffers.clear();
161
162   foreach(QVariant buffer, buffers) {
163     _removedBuffers << buffer.value<BufferId>();
164   }
165 }
166
167 QVariantList BufferViewConfig::initTemporarilyRemovedBuffers() const {
168   QVariantList temporarilyRemovedBuffers;
169
170   foreach(BufferId bufferId, _temporarilyRemovedBuffers) {
171     temporarilyRemovedBuffers << qVariantFromValue(bufferId);
172   }
173
174   return temporarilyRemovedBuffers;
175 }
176
177 void BufferViewConfig::initSetTemporarilyRemovedBuffers(const QVariantList &buffers) {
178   _temporarilyRemovedBuffers.clear();
179
180   foreach(QVariant buffer, buffers) {
181     _temporarilyRemovedBuffers << buffer.value<BufferId>();
182   }
183 }
184
185 void BufferViewConfig::addBuffer(const BufferId &bufferId, int pos) {
186   if(_buffers.contains(bufferId))
187     return;
188
189   if(pos < 0)
190     pos = 0;
191   if(pos > _buffers.count())
192     pos = _buffers.count();
193
194   if(_removedBuffers.contains(bufferId))
195     _removedBuffers.remove(bufferId);
196
197   if(_temporarilyRemovedBuffers.contains(bufferId))
198     _temporarilyRemovedBuffers.remove(bufferId);
199
200   _buffers.insert(pos, bufferId);
201   SYNC(ARG(bufferId), ARG(pos))
202   emit bufferAdded(bufferId, pos);
203   emit configChanged();
204 }
205
206 void BufferViewConfig::moveBuffer(const BufferId &bufferId, int pos) {
207   if(!_buffers.contains(bufferId))
208     return;
209
210   if(pos < 0)
211     pos = 0;
212   if(pos >= _buffers.count())
213     pos = _buffers.count() - 1;
214
215   _buffers.move(_buffers.indexOf(bufferId), pos);
216   SYNC(ARG(bufferId), ARG(pos))
217   emit bufferMoved(bufferId, pos);
218   emit configChanged();
219 }
220
221 void BufferViewConfig::removeBuffer(const BufferId &bufferId) {
222   if(_buffers.contains(bufferId))
223     _buffers.removeAt(_buffers.indexOf(bufferId));
224
225   if(_removedBuffers.contains(bufferId))
226     _removedBuffers.remove(bufferId);
227
228   _temporarilyRemovedBuffers << bufferId;
229   SYNC(ARG(bufferId))
230   emit bufferRemoved(bufferId);
231   emit configChanged();
232 }
233
234 void BufferViewConfig::removeBufferPermanently(const BufferId &bufferId) {
235   if(_buffers.contains(bufferId))
236     _buffers.removeAt(_buffers.indexOf(bufferId));
237
238   if(_temporarilyRemovedBuffers.contains(bufferId))
239     _temporarilyRemovedBuffers.remove(bufferId);
240
241   _removedBuffers << bufferId;
242
243   SYNC(ARG(bufferId))
244   emit bufferPermanentlyRemoved(bufferId);
245   emit configChanged();
246 }