1 /***************************************************************************
2 * Copyright (C) 2005-2018 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)
31 BufferSyncer::BufferSyncer(
32 QHash<BufferId, MsgId> lastSeenMsg,
33 QHash<BufferId, MsgId> markerLines,
34 QHash<BufferId, Message::Types> activities,
35 QHash<BufferId, int> highlightCounts,
37 ) : SyncableObject(parent),
38 _lastSeenMsg(std::move(lastSeenMsg)),
39 _markerLines(std::move(markerLines)),
40 _bufferActivities(std::move(activities)),
41 _highlightCounts(std::move(highlightCounts))
46 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
48 return _lastSeenMsg.value(buffer, MsgId());
52 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
57 const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
58 if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
59 _lastSeenMsg[buffer] = msgId;
60 SYNC(ARG(buffer), ARG(msgId))
61 emit lastSeenMsgSet(buffer, msgId);
68 MsgId BufferSyncer::markerLine(BufferId buffer) const
70 return _markerLines.value(buffer, MsgId());
74 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId)
79 if (_markerLines.value(buffer) == msgId)
82 _markerLines[buffer] = msgId;
83 SYNC(ARG(buffer), ARG(msgId))
84 emit markerLineSet(buffer, msgId);
89 QVariantList BufferSyncer::initLastSeenMsg() const
92 QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
93 while (iter != _lastSeenMsg.constEnd()) {
94 list << QVariant::fromValue<BufferId>(iter.key())
95 << QVariant::fromValue<MsgId>(iter.value());
102 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
104 _lastSeenMsg.clear();
105 Q_ASSERT(list.count() % 2 == 0);
106 for (int i = 0; i < list.count(); i += 2) {
107 setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
112 QVariantList BufferSyncer::initMarkerLines() const
115 QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
116 while (iter != _markerLines.constEnd()) {
117 list << QVariant::fromValue<BufferId>(iter.key())
118 << QVariant::fromValue<MsgId>(iter.value());
125 void BufferSyncer::initSetMarkerLines(const QVariantList &list)
127 _markerLines.clear();
128 Q_ASSERT(list.count() % 2 == 0);
129 for (int i = 0; i < list.count(); i += 2) {
130 setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
135 QVariantList BufferSyncer::initActivities() const
138 auto iter = _bufferActivities.constBegin();
139 while (iter != _bufferActivities.constEnd()) {
140 list << QVariant::fromValue<BufferId>(iter.key())
141 << QVariant::fromValue<int>((int) iter.value());
148 void BufferSyncer::initSetActivities(const QVariantList &list)
150 _bufferActivities.clear();
151 Q_ASSERT(list.count() % 2 == 0);
152 for (int i = 0; i < list.count(); i += 2) {
153 setBufferActivity(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
158 Message::Types BufferSyncer::activity(BufferId buffer) const
160 return _bufferActivities.value(buffer, Message::Types());
164 void BufferSyncer::removeBuffer(BufferId buffer)
166 if (_lastSeenMsg.contains(buffer))
167 _lastSeenMsg.remove(buffer);
168 if (_markerLines.contains(buffer))
169 _markerLines.remove(buffer);
170 if (_bufferActivities.contains(buffer))
171 _bufferActivities.remove(buffer);
172 if (_highlightCounts.contains(buffer))
173 _highlightCounts.remove(buffer);
175 emit bufferRemoved(buffer);
179 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
181 if (_lastSeenMsg.contains(buffer2))
182 _lastSeenMsg.remove(buffer2);
183 if (_markerLines.contains(buffer2))
184 _markerLines.remove(buffer2);
185 if (_bufferActivities.contains(buffer2))
186 _bufferActivities.remove(buffer2);
187 if (_highlightCounts.contains(buffer2))
188 _highlightCounts.remove(buffer2);
189 SYNC(ARG(buffer1), ARG(buffer2))
190 emit buffersPermanentlyMerged(buffer1, buffer2);
193 int BufferSyncer::highlightCount(BufferId buffer) const {
194 return _highlightCounts.value(buffer, 0);
197 QVariantList BufferSyncer::initHighlightCounts() const {
199 auto iter = _highlightCounts.constBegin();
200 while (iter != _highlightCounts.constEnd()) {
201 list << QVariant::fromValue<BufferId>(iter.key())
202 << QVariant::fromValue<int>((int) iter.value());
208 void BufferSyncer::initSetHighlightCounts(const QVariantList &list) {
209 _highlightCounts.clear();
210 Q_ASSERT(list.count() % 2 == 0);
211 for (int i = 0; i < list.count(); i += 2) {
212 setHighlightCount(list.at(i).value<BufferId>(), list.at(i+1).value<int>());