1 /***************************************************************************
2 * Copyright (C) 2005-2019 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> lastSeenMsg,
30 QHash<BufferId, MsgId> markerLines,
31 QHash<BufferId, Message::Types> activities,
32 QHash<BufferId, int> highlightCounts,
34 : SyncableObject(parent)
35 , _lastSeenMsg(std::move(lastSeenMsg))
36 , _markerLines(std::move(markerLines))
37 , _bufferActivities(std::move(activities))
38 , _highlightCounts(std::move(highlightCounts))
41 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
43 return _lastSeenMsg.value(buffer, MsgId());
46 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId& msgId)
51 const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
52 if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
53 _lastSeenMsg[buffer] = msgId;
54 SYNC(ARG(buffer), ARG(msgId))
55 emit lastSeenMsgSet(buffer, msgId);
61 MsgId BufferSyncer::markerLine(BufferId buffer) const
63 return _markerLines.value(buffer, MsgId());
66 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId& msgId)
71 if (_markerLines.value(buffer) == msgId)
74 _markerLines[buffer] = msgId;
75 SYNC(ARG(buffer), ARG(msgId))
76 emit markerLineSet(buffer, msgId);
80 QVariantList BufferSyncer::initLastSeenMsg() const
83 QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
84 while (iter != _lastSeenMsg.constEnd()) {
85 list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
91 void BufferSyncer::initSetLastSeenMsg(const QVariantList& list)
94 Q_ASSERT(list.count() % 2 == 0);
95 for (int i = 0; i < list.count(); i += 2) {
96 setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i + 1).value<MsgId>());
100 QVariantList BufferSyncer::initMarkerLines() const
103 QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
104 while (iter != _markerLines.constEnd()) {
105 list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
111 void BufferSyncer::initSetMarkerLines(const QVariantList& list)
113 _markerLines.clear();
114 Q_ASSERT(list.count() % 2 == 0);
115 for (int i = 0; i < list.count(); i += 2) {
116 setMarkerLine(list.at(i).value<BufferId>(), list.at(i + 1).value<MsgId>());
120 QVariantList BufferSyncer::initActivities() const
123 auto iter = _bufferActivities.constBegin();
124 while (iter != _bufferActivities.constEnd()) {
125 list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
131 void BufferSyncer::initSetActivities(const QVariantList& list)
133 _bufferActivities.clear();
134 Q_ASSERT(list.count() % 2 == 0);
135 for (int i = 0; i < list.count(); i += 2) {
136 setBufferActivity(list.at(i).value<BufferId>(), list.at(i + 1).value<int>());
140 Message::Types BufferSyncer::activity(BufferId buffer) const
142 return _bufferActivities.value(buffer, Message::Types());
145 void BufferSyncer::removeBuffer(BufferId buffer)
147 if (_lastSeenMsg.contains(buffer))
148 _lastSeenMsg.remove(buffer);
149 if (_markerLines.contains(buffer))
150 _markerLines.remove(buffer);
151 if (_bufferActivities.contains(buffer))
152 _bufferActivities.remove(buffer);
153 if (_highlightCounts.contains(buffer))
154 _highlightCounts.remove(buffer);
156 emit bufferRemoved(buffer);
159 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
161 if (_lastSeenMsg.contains(buffer2))
162 _lastSeenMsg.remove(buffer2);
163 if (_markerLines.contains(buffer2))
164 _markerLines.remove(buffer2);
165 if (_bufferActivities.contains(buffer2))
166 _bufferActivities.remove(buffer2);
167 if (_highlightCounts.contains(buffer2))
168 _highlightCounts.remove(buffer2);
169 SYNC(ARG(buffer1), ARG(buffer2))
170 emit buffersPermanentlyMerged(buffer1, buffer2);
173 int BufferSyncer::highlightCount(BufferId buffer) const
175 return _highlightCounts.value(buffer, 0);
178 QVariantList BufferSyncer::initHighlightCounts() const
181 auto iter = _highlightCounts.constBegin();
182 while (iter != _highlightCounts.constEnd()) {
183 list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
189 void BufferSyncer::initSetHighlightCounts(const QVariantList& list)
191 _highlightCounts.clear();
192 Q_ASSERT(list.count() % 2 == 0);
193 for (int i = 0; i < list.count(); i += 2) {
194 setHighlightCount(list.at(i).value<BufferId>(), list.at(i + 1).value<int>());