cmake: Autogenerate most of the .qrc resource files
[quassel.git] / src / common / buffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "buffersyncer.h"
22
23 INIT_SYNCABLE_OBJECT(BufferSyncer)
24 BufferSyncer::BufferSyncer(QObject *parent)
25     : SyncableObject(parent)
26 {
27 }
28
29
30 BufferSyncer::BufferSyncer(
31         const QHash<BufferId, MsgId> &lastSeenMsg,
32         const QHash<BufferId, MsgId> &markerLines,
33         const QHash<BufferId, Message::Types> &activities,
34         const QHash<BufferId, int> &highlightCounts,
35         QObject *parent
36 ) : SyncableObject(parent),
37     _lastSeenMsg(lastSeenMsg),
38     _markerLines(markerLines),
39     _bufferActivities(activities),
40     _highlightCounts(highlightCounts)
41 {
42 }
43
44
45 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
46 {
47     return _lastSeenMsg.value(buffer, MsgId());
48 }
49
50
51 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
52 {
53     if (!msgId.isValid())
54         return false;
55
56     const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
57     if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
58         _lastSeenMsg[buffer] = msgId;
59         SYNC(ARG(buffer), ARG(msgId))
60         emit lastSeenMsgSet(buffer, msgId);
61         return true;
62     }
63     return false;
64 }
65
66
67 MsgId BufferSyncer::markerLine(BufferId buffer) const
68 {
69     return _markerLines.value(buffer, MsgId());
70 }
71
72
73 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId)
74 {
75     if (!msgId.isValid())
76         return false;
77
78     if (_markerLines.value(buffer) == msgId)
79         return false;
80
81     _markerLines[buffer] = msgId;
82     SYNC(ARG(buffer), ARG(msgId))
83     emit markerLineSet(buffer, msgId);
84     return true;
85 }
86
87
88 QVariantList BufferSyncer::initLastSeenMsg() const
89 {
90     QVariantList list;
91     QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
92     while (iter != _lastSeenMsg.constEnd()) {
93         list << QVariant::fromValue<BufferId>(iter.key())
94              << QVariant::fromValue<MsgId>(iter.value());
95         ++iter;
96     }
97     return list;
98 }
99
100
101 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
102 {
103     _lastSeenMsg.clear();
104     Q_ASSERT(list.count() % 2 == 0);
105     for (int i = 0; i < list.count(); i += 2) {
106         setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
107     }
108 }
109
110
111 QVariantList BufferSyncer::initMarkerLines() const
112 {
113     QVariantList list;
114     QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
115     while (iter != _markerLines.constEnd()) {
116         list << QVariant::fromValue<BufferId>(iter.key())
117              << QVariant::fromValue<MsgId>(iter.value());
118         ++iter;
119     }
120     return list;
121 }
122
123
124 void BufferSyncer::initSetMarkerLines(const QVariantList &list)
125 {
126     _markerLines.clear();
127     Q_ASSERT(list.count() % 2 == 0);
128     for (int i = 0; i < list.count(); i += 2) {
129         setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
130     }
131 }
132
133
134 QVariantList BufferSyncer::initActivities() const
135 {
136     QVariantList list;
137     auto iter = _bufferActivities.constBegin();
138     while (iter != _bufferActivities.constEnd()) {
139         list << QVariant::fromValue<BufferId>(iter.key())
140              << QVariant::fromValue<int>((int) iter.value());
141         ++iter;
142     }
143     return list;
144 }
145
146
147 void BufferSyncer::initSetActivities(const QVariantList &list)
148 {
149     _bufferActivities.clear();
150     Q_ASSERT(list.count() % 2 == 0);
151     for (int i = 0; i < list.count(); i += 2) {
152         setBufferActivity(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
153     }
154 }
155
156
157 Message::Types BufferSyncer::activity(BufferId buffer) const
158 {
159     return _bufferActivities.value(buffer, Message::Types());
160 }
161
162
163 void BufferSyncer::removeBuffer(BufferId buffer)
164 {
165     if (_lastSeenMsg.contains(buffer))
166         _lastSeenMsg.remove(buffer);
167     if (_markerLines.contains(buffer))
168         _markerLines.remove(buffer);
169     if (_bufferActivities.contains(buffer))
170         _bufferActivities.remove(buffer);
171     if (_highlightCounts.contains(buffer))
172         _highlightCounts.remove(buffer);
173     SYNC(ARG(buffer))
174     emit bufferRemoved(buffer);
175 }
176
177
178 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
179 {
180     if (_lastSeenMsg.contains(buffer2))
181         _lastSeenMsg.remove(buffer2);
182     if (_markerLines.contains(buffer2))
183         _markerLines.remove(buffer2);
184     if (_bufferActivities.contains(buffer2))
185         _bufferActivities.remove(buffer2);
186     if (_highlightCounts.contains(buffer2))
187         _highlightCounts.remove(buffer2);
188     SYNC(ARG(buffer1), ARG(buffer2))
189     emit buffersPermanentlyMerged(buffer1, buffer2);
190 }
191
192 int BufferSyncer::highlightCount(BufferId buffer) const {
193     return _highlightCounts.value(buffer, 0);
194 }
195
196 QVariantList BufferSyncer::initHighlightCounts() const {
197     QVariantList list;
198     auto iter = _highlightCounts.constBegin();
199     while (iter != _highlightCounts.constEnd()) {
200         list << QVariant::fromValue<BufferId>(iter.key())
201              << QVariant::fromValue<int>((int) iter.value());
202         ++iter;
203     }
204     return list;
205 }
206
207 void BufferSyncer::initSetHighlightCounts(const QVariantList &list) {
208     _highlightCounts.clear();
209     Q_ASSERT(list.count() % 2 == 0);
210     for (int i = 0; i < list.count(); i += 2) {
211         setHighlightCount(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
212     }
213 }