1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "buffersyncer.h"
25 BufferSyncer::BufferSyncer(QObject* parent)
26 : SyncableObject(parent)
29 BufferSyncer::BufferSyncer(QHash<BufferId, MsgId> lastMsg,
30 QHash<BufferId, MsgId> lastSeenMsg,
31 QHash<BufferId, MsgId> markerLines,
32 QHash<BufferId, Message::Types> activities,
33 QHash<BufferId, int> highlightCounts,
35 : SyncableObject(parent)
36 , _lastMsg(std::move(lastMsg))
37 , _lastSeenMsg(std::move(lastSeenMsg))
38 , _markerLines(std::move(markerLines))
39 , _bufferActivities(std::move(activities))
40 , _highlightCounts(std::move(highlightCounts))
43 MsgId BufferSyncer::lastMsg(BufferId buffer) const
45 return _lastMsg.value(buffer, MsgId());
48 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
50 return _lastSeenMsg.value(buffer, MsgId());
53 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId& msgId)
58 const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
59 if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
60 _lastSeenMsg[buffer] = msgId;
61 SYNC(ARG(buffer), ARG(msgId))
62 emit lastSeenMsgSet(buffer, msgId);
68 MsgId BufferSyncer::markerLine(BufferId buffer) const
70 return _markerLines.value(buffer, MsgId());
73 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId& msgId)
78 if (_markerLines.value(buffer) == msgId)
81 _markerLines[buffer] = msgId;
82 SYNC(ARG(buffer), ARG(msgId))
83 emit markerLineSet(buffer, msgId);
87 QVariantList BufferSyncer::initLastMsg() const
90 QHash<BufferId, MsgId>::const_iterator iter = _lastMsg.constBegin();
91 while (iter != _lastMsg.constEnd()) {
92 list << QVariant::fromValue<BufferId>(iter.key()) << QVariant::fromValue<MsgId>(iter.value());
98 void BufferSyncer::initSetLastMsg(const QVariantList& list)
101 Q_ASSERT(list.count() % 2 == 0);
102 for (int i = 0; i < list.count(); i += 2) {
103 setLastMsg(list.at(i).value<BufferId>(), list.at(i + 1).value<MsgId>());
107 QVariantList BufferSyncer::initLastSeenMsg() const
110 QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
111 while (iter != _lastSeenMsg.constEnd()) {
112 list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
118 void BufferSyncer::initSetLastSeenMsg(const QVariantList& list)
120 _lastSeenMsg.clear();
121 Q_ASSERT(list.count() % 2 == 0);
122 for (int i = 0; i < list.count(); i += 2) {
123 setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i + 1).value<MsgId>());
127 QVariantList BufferSyncer::initMarkerLines() const
130 QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
131 while (iter != _markerLines.constEnd()) {
132 list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
138 void BufferSyncer::initSetMarkerLines(const QVariantList& list)
140 _markerLines.clear();
141 Q_ASSERT(list.count() % 2 == 0);
142 for (int i = 0; i < list.count(); i += 2) {
143 setMarkerLine(list.at(i).value<BufferId>(), list.at(i + 1).value<MsgId>());
147 QVariantList BufferSyncer::initActivities() const
150 auto iter = _bufferActivities.constBegin();
151 while (iter != _bufferActivities.constEnd()) {
152 list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
158 void BufferSyncer::initSetActivities(const QVariantList& list)
160 _bufferActivities.clear();
161 Q_ASSERT(list.count() % 2 == 0);
162 for (int i = 0; i < list.count(); i += 2) {
163 setBufferActivity(list.at(i).value<BufferId>(), list.at(i + 1).value<int>());
167 Message::Types BufferSyncer::activity(BufferId buffer) const
169 return _bufferActivities.value(buffer, Message::Types());
172 void BufferSyncer::removeBuffer(BufferId buffer)
174 if (_lastMsg.contains(buffer))
175 _lastMsg.remove(buffer);
176 if (_lastSeenMsg.contains(buffer))
177 _lastSeenMsg.remove(buffer);
178 if (_markerLines.contains(buffer))
179 _markerLines.remove(buffer);
180 if (_bufferActivities.contains(buffer))
181 _bufferActivities.remove(buffer);
182 if (_highlightCounts.contains(buffer))
183 _highlightCounts.remove(buffer);
185 emit bufferRemoved(buffer);
188 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
190 setLastMsg(buffer1, std::max(_lastMsg[buffer1], _lastMsg[buffer2]));
191 setBufferActivity(buffer1, _bufferActivities[buffer1] | _bufferActivities[buffer2]);
192 setHighlightCount(buffer1, _highlightCounts[buffer1] + _highlightCounts[buffer2]);
194 if (_lastMsg.contains(buffer2))
195 _lastMsg.remove(buffer2);
196 if (_lastSeenMsg.contains(buffer2))
197 _lastSeenMsg.remove(buffer2);
198 if (_markerLines.contains(buffer2))
199 _markerLines.remove(buffer2);
200 if (_bufferActivities.contains(buffer2))
201 _bufferActivities.remove(buffer2);
202 if (_highlightCounts.contains(buffer2))
203 _highlightCounts.remove(buffer2);
204 SYNC(ARG(buffer1), ARG(buffer2))
205 emit buffersPermanentlyMerged(buffer1, buffer2);
208 int BufferSyncer::highlightCount(BufferId buffer) const
210 return _highlightCounts.value(buffer, 0);
213 QVariantList BufferSyncer::initHighlightCounts() const
216 auto iter = _highlightCounts.constBegin();
217 while (iter != _highlightCounts.constEnd()) {
218 list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
224 void BufferSyncer::initSetHighlightCounts(const QVariantList& list)
226 _highlightCounts.clear();
227 Q_ASSERT(list.count() % 2 == 0);
228 for (int i = 0; i < list.count(); i += 2) {
229 setHighlightCount(list.at(i).value<BufferId>(), list.at(i + 1).value<int>());