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 INIT_SYNCABLE_OBJECT(BufferSyncer)
24 BufferSyncer::BufferSyncer(QObject *parent)
25 : SyncableObject(parent)
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,
36 ) : SyncableObject(parent),
37 _lastSeenMsg(lastSeenMsg),
38 _markerLines(markerLines),
39 _bufferActivities(activities),
40 _highlightCounts(highlightCounts)
45 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
47 return _lastSeenMsg.value(buffer, MsgId());
51 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
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);
67 MsgId BufferSyncer::markerLine(BufferId buffer) const
69 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);
88 QVariantList BufferSyncer::initLastSeenMsg() const
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());
101 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
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>());
111 QVariantList BufferSyncer::initMarkerLines() const
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());
124 void BufferSyncer::initSetMarkerLines(const QVariantList &list)
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>());
134 QVariantList BufferSyncer::initActivities() const
137 auto iter = _bufferActivities.constBegin();
138 while (iter != _bufferActivities.constEnd()) {
139 list << QVariant::fromValue<BufferId>(iter.key())
140 << QVariant::fromValue<int>((int) iter.value());
147 void BufferSyncer::initSetActivities(const QVariantList &list)
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>());
157 Message::Types BufferSyncer::activity(BufferId buffer) const
159 return _bufferActivities.value(buffer, Message::Types());
163 void BufferSyncer::removeBuffer(BufferId buffer)
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);
174 emit bufferRemoved(buffer);
178 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
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);
192 int BufferSyncer::highlightCount(BufferId buffer) const {
193 return _highlightCounts.value(buffer, 0);
196 QVariantList BufferSyncer::initHighlightCounts() const {
198 auto iter = _highlightCounts.constBegin();
199 while (iter != _highlightCounts.constEnd()) {
200 list << QVariant::fromValue<BufferId>(iter.key())
201 << QVariant::fromValue<int>((int) iter.value());
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>());