74126a14bd760d31b6a6e7a1ca1e31bb7a654598
[quassel.git] / src / common / buffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "buffersyncer.h"
22
23 #include <utility>
24
25 BufferSyncer::BufferSyncer(QObject* parent)
26     : SyncableObject(parent)
27 {}
28
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,
34                            QObject* parent)
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))
41 {}
42
43 MsgId BufferSyncer::lastMsg(BufferId buffer) const
44 {
45     return _lastMsg.value(buffer, MsgId());
46 }
47
48 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
49 {
50     return _lastSeenMsg.value(buffer, MsgId());
51 }
52
53 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId& msgId)
54 {
55     if (!msgId.isValid())
56         return false;
57
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);
63         return true;
64     }
65     return false;
66 }
67
68 MsgId BufferSyncer::markerLine(BufferId buffer) const
69 {
70     return _markerLines.value(buffer, MsgId());
71 }
72
73 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId& msgId)
74 {
75     if (!msgId.isValid())
76         return false;
77
78     if (_markerLines.value(buffer) == msgId)
79         return false;
80
81     _markerLines[buffer] = msgId;
82     SYNC(ARG(buffer), ARG(msgId))
83     emit markerLineSet(buffer, msgId);
84     return true;
85 }
86
87 QVariantList BufferSyncer::initLastMsg() const
88 {
89     QVariantList list;
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());
93         ++iter;
94     }
95     return list;
96 }
97
98 void BufferSyncer::initSetLastMsg(const QVariantList& list)
99 {
100     _lastMsg.clear();
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>());
104     }
105 }
106
107 QVariantList BufferSyncer::initLastSeenMsg() const
108 {
109     QVariantList list;
110     QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
111     while (iter != _lastSeenMsg.constEnd()) {
112         list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
113         ++iter;
114     }
115     return list;
116 }
117
118 void BufferSyncer::initSetLastSeenMsg(const QVariantList& list)
119 {
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>());
124     }
125 }
126
127 QVariantList BufferSyncer::initMarkerLines() const
128 {
129     QVariantList list;
130     QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
131     while (iter != _markerLines.constEnd()) {
132         list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
133         ++iter;
134     }
135     return list;
136 }
137
138 void BufferSyncer::initSetMarkerLines(const QVariantList& list)
139 {
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>());
144     }
145 }
146
147 QVariantList BufferSyncer::initActivities() const
148 {
149     QVariantList list;
150     auto iter = _bufferActivities.constBegin();
151     while (iter != _bufferActivities.constEnd()) {
152         list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
153         ++iter;
154     }
155     return list;
156 }
157
158 void BufferSyncer::initSetActivities(const QVariantList& list)
159 {
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>());
164     }
165 }
166
167 Message::Types BufferSyncer::activity(BufferId buffer) const
168 {
169     return _bufferActivities.value(buffer, Message::Types());
170 }
171
172 void BufferSyncer::removeBuffer(BufferId buffer)
173 {
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);
184     SYNC(ARG(buffer))
185     emit bufferRemoved(buffer);
186 }
187
188 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
189 {
190     setLastMsg(buffer1, std::max(_lastMsg[buffer1], _lastMsg[buffer2]));
191     setBufferActivity(buffer1, _bufferActivities[buffer1] | _bufferActivities[buffer2]);
192     setHighlightCount(buffer1, _highlightCounts[buffer1] + _highlightCounts[buffer2]);
193
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);
206 }
207
208 int BufferSyncer::highlightCount(BufferId buffer) const
209 {
210     return _highlightCounts.value(buffer, 0);
211 }
212
213 QVariantList BufferSyncer::initHighlightCounts() const
214 {
215     QVariantList list;
216     auto iter = _highlightCounts.constBegin();
217     while (iter != _highlightCounts.constEnd()) {
218         list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
219         ++iter;
220     }
221     return list;
222 }
223
224 void BufferSyncer::initSetHighlightCounts(const QVariantList& list)
225 {
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>());
230     }
231 }