1c432a487d06aa43df081e76caef5084f62a3196
[quassel.git] / src / common / buffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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> lastSeenMsg,
30                            QHash<BufferId, MsgId> markerLines,
31                            QHash<BufferId, Message::Types> activities,
32                            QHash<BufferId, int> highlightCounts,
33                            QObject* parent)
34     : SyncableObject(parent)
35     , _lastSeenMsg(std::move(lastSeenMsg))
36     , _markerLines(std::move(markerLines))
37     , _bufferActivities(std::move(activities))
38     , _highlightCounts(std::move(highlightCounts))
39 {}
40
41 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
42 {
43     return _lastSeenMsg.value(buffer, MsgId());
44 }
45
46 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId& msgId)
47 {
48     if (!msgId.isValid())
49         return false;
50
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);
56         return true;
57     }
58     return false;
59 }
60
61 MsgId BufferSyncer::markerLine(BufferId buffer) const
62 {
63     return _markerLines.value(buffer, MsgId());
64 }
65
66 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId& msgId)
67 {
68     if (!msgId.isValid())
69         return false;
70
71     if (_markerLines.value(buffer) == msgId)
72         return false;
73
74     _markerLines[buffer] = msgId;
75     SYNC(ARG(buffer), ARG(msgId))
76     emit markerLineSet(buffer, msgId);
77     return true;
78 }
79
80 QVariantList BufferSyncer::initLastSeenMsg() const
81 {
82     QVariantList list;
83     QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
84     while (iter != _lastSeenMsg.constEnd()) {
85         list << QVariant::fromValue<BufferId>(iter.key()) << QVariant::fromValue<MsgId>(iter.value());
86         ++iter;
87     }
88     return list;
89 }
90
91 void BufferSyncer::initSetLastSeenMsg(const QVariantList& list)
92 {
93     _lastSeenMsg.clear();
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>());
97     }
98 }
99
100 QVariantList BufferSyncer::initMarkerLines() const
101 {
102     QVariantList list;
103     QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
104     while (iter != _markerLines.constEnd()) {
105         list << QVariant::fromValue<BufferId>(iter.key()) << QVariant::fromValue<MsgId>(iter.value());
106         ++iter;
107     }
108     return list;
109 }
110
111 void BufferSyncer::initSetMarkerLines(const QVariantList& list)
112 {
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>());
117     }
118 }
119
120 QVariantList BufferSyncer::initActivities() const
121 {
122     QVariantList list;
123     auto iter = _bufferActivities.constBegin();
124     while (iter != _bufferActivities.constEnd()) {
125         list << QVariant::fromValue<BufferId>(iter.key()) << QVariant::fromValue<int>((int)iter.value());
126         ++iter;
127     }
128     return list;
129 }
130
131 void BufferSyncer::initSetActivities(const QVariantList& list)
132 {
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>());
137     }
138 }
139
140 Message::Types BufferSyncer::activity(BufferId buffer) const
141 {
142     return _bufferActivities.value(buffer, Message::Types());
143 }
144
145 void BufferSyncer::removeBuffer(BufferId buffer)
146 {
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);
155     SYNC(ARG(buffer))
156     emit bufferRemoved(buffer);
157 }
158
159 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
160 {
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);
171 }
172
173 int BufferSyncer::highlightCount(BufferId buffer) const
174 {
175     return _highlightCounts.value(buffer, 0);
176 }
177
178 QVariantList BufferSyncer::initHighlightCounts() const
179 {
180     QVariantList list;
181     auto iter = _highlightCounts.constBegin();
182     while (iter != _highlightCounts.constEnd()) {
183         list << QVariant::fromValue<BufferId>(iter.key()) << QVariant::fromValue<int>((int)iter.value());
184         ++iter;
185     }
186     return list;
187 }
188
189 void BufferSyncer::initSetHighlightCounts(const QVariantList& list)
190 {
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>());
195     }
196 }