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"
23 BufferSyncer::BufferSyncer(QObject *parent)
24 : SyncableObject(parent)
29 BufferSyncer::BufferSyncer(
30 const QHash<BufferId, MsgId> &lastSeenMsg,
31 const QHash<BufferId, MsgId> &markerLines,
32 const QHash<BufferId, Message::Types> &activities,
33 const QHash<BufferId, int> &highlightCounts,
35 ) : SyncableObject(parent),
36 _lastSeenMsg(lastSeenMsg),
37 _markerLines(markerLines),
38 _bufferActivities(activities),
39 _highlightCounts(highlightCounts)
44 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
46 return _lastSeenMsg.value(buffer, MsgId());
50 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
55 const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
56 if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
57 _lastSeenMsg[buffer] = msgId;
58 SYNC(ARG(buffer), ARG(msgId))
59 emit lastSeenMsgSet(buffer, msgId);
66 MsgId BufferSyncer::markerLine(BufferId buffer) const
68 return _markerLines.value(buffer, MsgId());
72 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId)
77 if (_markerLines.value(buffer) == msgId)
80 _markerLines[buffer] = msgId;
81 SYNC(ARG(buffer), ARG(msgId))
82 emit markerLineSet(buffer, msgId);
87 QVariantList BufferSyncer::initLastSeenMsg() const
90 QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
91 while (iter != _lastSeenMsg.constEnd()) {
92 list << QVariant::fromValue<BufferId>(iter.key())
93 << QVariant::fromValue<MsgId>(iter.value());
100 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
102 _lastSeenMsg.clear();
103 Q_ASSERT(list.count() % 2 == 0);
104 for (int i = 0; i < list.count(); i += 2) {
105 setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
110 QVariantList BufferSyncer::initMarkerLines() const
113 QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
114 while (iter != _markerLines.constEnd()) {
115 list << QVariant::fromValue<BufferId>(iter.key())
116 << QVariant::fromValue<MsgId>(iter.value());
123 void BufferSyncer::initSetMarkerLines(const QVariantList &list)
125 _markerLines.clear();
126 Q_ASSERT(list.count() % 2 == 0);
127 for (int i = 0; i < list.count(); i += 2) {
128 setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
133 QVariantList BufferSyncer::initActivities() const
136 auto iter = _bufferActivities.constBegin();
137 while (iter != _bufferActivities.constEnd()) {
138 list << QVariant::fromValue<BufferId>(iter.key())
139 << QVariant::fromValue<int>((int) iter.value());
146 void BufferSyncer::initSetActivities(const QVariantList &list)
148 _bufferActivities.clear();
149 Q_ASSERT(list.count() % 2 == 0);
150 for (int i = 0; i < list.count(); i += 2) {
151 setBufferActivity(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
156 Message::Types BufferSyncer::activity(BufferId buffer) const
158 return _bufferActivities.value(buffer, Message::Types());
162 void BufferSyncer::removeBuffer(BufferId buffer)
164 if (_lastSeenMsg.contains(buffer))
165 _lastSeenMsg.remove(buffer);
166 if (_markerLines.contains(buffer))
167 _markerLines.remove(buffer);
168 if (_bufferActivities.contains(buffer))
169 _bufferActivities.remove(buffer);
170 if (_highlightCounts.contains(buffer))
171 _highlightCounts.remove(buffer);
173 emit bufferRemoved(buffer);
177 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
179 if (_lastSeenMsg.contains(buffer2))
180 _lastSeenMsg.remove(buffer2);
181 if (_markerLines.contains(buffer2))
182 _markerLines.remove(buffer2);
183 if (_bufferActivities.contains(buffer2))
184 _bufferActivities.remove(buffer2);
185 if (_highlightCounts.contains(buffer2))
186 _highlightCounts.remove(buffer2);
187 SYNC(ARG(buffer1), ARG(buffer2))
188 emit buffersPermanentlyMerged(buffer1, buffer2);
191 int BufferSyncer::highlightCount(BufferId buffer) const {
192 return _highlightCounts.value(buffer, 0);
195 QVariantList BufferSyncer::initHighlightCounts() const {
197 auto iter = _highlightCounts.constBegin();
198 while (iter != _highlightCounts.constEnd()) {
199 list << QVariant::fromValue<BufferId>(iter.key())
200 << QVariant::fromValue<int>((int) iter.value());
206 void BufferSyncer::initSetHighlightCounts(const QVariantList &list) {
207 _highlightCounts.clear();
208 Q_ASSERT(list.count() % 2 == 0);
209 for (int i = 0; i < list.count(); i += 2) {
210 setHighlightCount(list.at(i).value<BufferId>(), list.at(i+1).value<int>());