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