Handle invalid handshake data properly in the core
[quassel.git] / src / common / bufferviewconfig.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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     _hideInactiveNetworks(false),
33     _disableDecoration(false),
34     _allowedBufferTypes(BufferInfo::StatusBuffer | BufferInfo::ChannelBuffer | BufferInfo::QueryBuffer | BufferInfo::GroupBuffer),
35     _minimumActivity(0)
36 {
37     setObjectName(QString::number(bufferViewId));
38 }
39
40
41 BufferViewConfig::BufferViewConfig(int bufferViewId, const QVariantMap &properties, QObject *parent)
42     : SyncableObject(parent),
43     _bufferViewId(bufferViewId)
44 {
45     fromVariantMap(properties);
46     setObjectName(QString::number(bufferViewId));
47 }
48
49
50 void BufferViewConfig::setBufferViewName(const QString &bufferViewName)
51 {
52     if (_bufferViewName == bufferViewName)
53         return;
54
55     _bufferViewName = bufferViewName;
56     SYNC(ARG(bufferViewName))
57     emit bufferViewNameSet(bufferViewName);
58 }
59
60
61 void BufferViewConfig::setNetworkId(const NetworkId &networkId)
62 {
63     if (_networkId == networkId)
64         return;
65
66     _networkId = networkId;
67     SYNC(ARG(networkId))
68     emit networkIdSet(networkId);
69     emit configChanged();
70 }
71
72
73 void BufferViewConfig::setAddNewBuffersAutomatically(bool addNewBuffersAutomatically)
74 {
75     if (_addNewBuffersAutomatically == addNewBuffersAutomatically)
76         return;
77
78     _addNewBuffersAutomatically = addNewBuffersAutomatically;
79     SYNC(ARG(addNewBuffersAutomatically))
80     emit configChanged();
81 }
82
83
84 void BufferViewConfig::setSortAlphabetically(bool sortAlphabetically)
85 {
86     if (_sortAlphabetically == sortAlphabetically)
87         return;
88
89     _sortAlphabetically = sortAlphabetically;
90     SYNC(ARG(sortAlphabetically))
91     emit configChanged();
92 }
93
94
95 void BufferViewConfig::setDisableDecoration(bool disableDecoration)
96 {
97     if (_disableDecoration == disableDecoration)
98         return;
99
100     _disableDecoration = disableDecoration;
101     SYNC(ARG(disableDecoration))
102 }
103
104
105 void BufferViewConfig::setAllowedBufferTypes(int bufferTypes)
106 {
107     if (_allowedBufferTypes == bufferTypes)
108         return;
109
110     _allowedBufferTypes = bufferTypes;
111     SYNC(ARG(bufferTypes))
112     emit configChanged();
113 }
114
115
116 void BufferViewConfig::setMinimumActivity(int activity)
117 {
118     if (_minimumActivity == activity)
119         return;
120
121     _minimumActivity = activity;
122     SYNC(ARG(activity))
123     emit configChanged();
124 }
125
126
127 void BufferViewConfig::setHideInactiveBuffers(bool hideInactiveBuffers)
128 {
129     if (_hideInactiveBuffers == hideInactiveBuffers)
130         return;
131
132     _hideInactiveBuffers = hideInactiveBuffers;
133     SYNC(ARG(hideInactiveBuffers))
134     emit configChanged();
135 }
136
137 void BufferViewConfig::setHideInactiveNetworks(bool hideInactiveNetworks)
138 {
139     if (_hideInactiveNetworks == hideInactiveNetworks)
140         return;
141
142     _hideInactiveNetworks = hideInactiveNetworks;
143     SYNC(ARG(hideInactiveNetworks))
144     emit configChanged();
145 }
146
147
148 QVariantList BufferViewConfig::initBufferList() const
149 {
150     QVariantList buffers;
151
152     foreach(BufferId bufferId, _buffers) {
153         buffers << qVariantFromValue(bufferId);
154     }
155
156     return buffers;
157 }
158
159
160 void BufferViewConfig::initSetBufferList(const QVariantList &buffers)
161 {
162     _buffers.clear();
163
164     foreach(QVariant buffer, buffers) {
165         _buffers << buffer.value<BufferId>();
166     }
167
168     emit configChanged(); // used to track changes in the settingspage
169 }
170
171
172 void BufferViewConfig::initSetBufferList(const QList<BufferId> &buffers)
173 {
174     _buffers.clear();
175
176     foreach(BufferId bufferId, buffers) {
177         _buffers << bufferId;
178     }
179
180     emit configChanged(); // used to track changes in the settingspage
181 }
182
183
184 QVariantList BufferViewConfig::initRemovedBuffers() const
185 {
186     QVariantList removedBuffers;
187
188     foreach(BufferId bufferId, _removedBuffers) {
189         removedBuffers << qVariantFromValue(bufferId);
190     }
191
192     return removedBuffers;
193 }
194
195
196 void BufferViewConfig::initSetRemovedBuffers(const QVariantList &buffers)
197 {
198     _removedBuffers.clear();
199
200     foreach(QVariant buffer, buffers) {
201         _removedBuffers << buffer.value<BufferId>();
202     }
203 }
204
205
206 QVariantList BufferViewConfig::initTemporarilyRemovedBuffers() const
207 {
208     QVariantList temporarilyRemovedBuffers;
209
210     foreach(BufferId bufferId, _temporarilyRemovedBuffers) {
211         temporarilyRemovedBuffers << qVariantFromValue(bufferId);
212     }
213
214     return temporarilyRemovedBuffers;
215 }
216
217
218 void BufferViewConfig::initSetTemporarilyRemovedBuffers(const QVariantList &buffers)
219 {
220     _temporarilyRemovedBuffers.clear();
221
222     foreach(QVariant buffer, buffers) {
223         _temporarilyRemovedBuffers << buffer.value<BufferId>();
224     }
225 }
226
227
228 void BufferViewConfig::addBuffer(const BufferId &bufferId, int pos)
229 {
230     if (_buffers.contains(bufferId))
231         return;
232
233     if (pos < 0)
234         pos = 0;
235     if (pos > _buffers.count())
236         pos = _buffers.count();
237
238     if (_removedBuffers.contains(bufferId))
239         _removedBuffers.remove(bufferId);
240
241     if (_temporarilyRemovedBuffers.contains(bufferId))
242         _temporarilyRemovedBuffers.remove(bufferId);
243
244     _buffers.insert(pos, bufferId);
245     SYNC(ARG(bufferId), ARG(pos))
246     emit bufferAdded(bufferId, pos);
247     emit configChanged();
248 }
249
250
251 void BufferViewConfig::moveBuffer(const BufferId &bufferId, int pos)
252 {
253     if (!_buffers.contains(bufferId))
254         return;
255
256     if (pos < 0)
257         pos = 0;
258     if (pos >= _buffers.count())
259         pos = _buffers.count() - 1;
260
261     _buffers.move(_buffers.indexOf(bufferId), pos);
262     SYNC(ARG(bufferId), ARG(pos))
263     emit bufferMoved(bufferId, pos);
264     emit configChanged();
265 }
266
267
268 void BufferViewConfig::removeBuffer(const BufferId &bufferId)
269 {
270     if (_buffers.contains(bufferId))
271         _buffers.removeAt(_buffers.indexOf(bufferId));
272
273     if (_removedBuffers.contains(bufferId))
274         _removedBuffers.remove(bufferId);
275
276     _temporarilyRemovedBuffers << bufferId;
277     SYNC(ARG(bufferId))
278     emit bufferRemoved(bufferId);
279     emit configChanged();
280 }
281
282
283 void BufferViewConfig::removeBufferPermanently(const BufferId &bufferId)
284 {
285     if (_buffers.contains(bufferId))
286         _buffers.removeAt(_buffers.indexOf(bufferId));
287
288     if (_temporarilyRemovedBuffers.contains(bufferId))
289         _temporarilyRemovedBuffers.remove(bufferId);
290
291     _removedBuffers << bufferId;
292
293     SYNC(ARG(bufferId))
294     emit bufferPermanentlyRemoved(bufferId);
295     emit configChanged();
296 }