common: Simplify SyncableObject macros and usage
[quassel.git] / src / common / buffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 BufferSyncer::BufferSyncer(QObject *parent)
24     : SyncableObject(parent)
25 {
26 }
27
28
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,
34         QObject *parent
35 ) : SyncableObject(parent),
36     _lastSeenMsg(lastSeenMsg),
37     _markerLines(markerLines),
38     _bufferActivities(activities),
39     _highlightCounts(highlightCounts)
40 {
41 }
42
43
44 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
45 {
46     return _lastSeenMsg.value(buffer, MsgId());
47 }
48
49
50 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
51 {
52     if (!msgId.isValid())
53         return false;
54
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);
60         return true;
61     }
62     return false;
63 }
64
65
66 MsgId BufferSyncer::markerLine(BufferId buffer) const
67 {
68     return _markerLines.value(buffer, MsgId());
69 }
70
71
72 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId)
73 {
74     if (!msgId.isValid())
75         return false;
76
77     if (_markerLines.value(buffer) == msgId)
78         return false;
79
80     _markerLines[buffer] = msgId;
81     SYNC(ARG(buffer), ARG(msgId))
82     emit markerLineSet(buffer, msgId);
83     return true;
84 }
85
86
87 QVariantList BufferSyncer::initLastSeenMsg() const
88 {
89     QVariantList list;
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());
94         ++iter;
95     }
96     return list;
97 }
98
99
100 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
101 {
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>());
106     }
107 }
108
109
110 QVariantList BufferSyncer::initMarkerLines() const
111 {
112     QVariantList list;
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());
117         ++iter;
118     }
119     return list;
120 }
121
122
123 void BufferSyncer::initSetMarkerLines(const QVariantList &list)
124 {
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>());
129     }
130 }
131
132
133 QVariantList BufferSyncer::initActivities() const
134 {
135     QVariantList list;
136     auto iter = _bufferActivities.constBegin();
137     while (iter != _bufferActivities.constEnd()) {
138         list << QVariant::fromValue<BufferId>(iter.key())
139              << QVariant::fromValue<int>((int) iter.value());
140         ++iter;
141     }
142     return list;
143 }
144
145
146 void BufferSyncer::initSetActivities(const QVariantList &list)
147 {
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>());
152     }
153 }
154
155
156 Message::Types BufferSyncer::activity(BufferId buffer) const
157 {
158     return _bufferActivities.value(buffer, Message::Types());
159 }
160
161
162 void BufferSyncer::removeBuffer(BufferId buffer)
163 {
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);
172     SYNC(ARG(buffer))
173     emit bufferRemoved(buffer);
174 }
175
176
177 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
178 {
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);
189 }
190
191 int BufferSyncer::highlightCount(BufferId buffer) const {
192     return _highlightCounts.value(buffer, 0);
193 }
194
195 QVariantList BufferSyncer::initHighlightCounts() const {
196     QVariantList list;
197     auto iter = _highlightCounts.constBegin();
198     while (iter != _highlightCounts.constEnd()) {
199         list << QVariant::fromValue<BufferId>(iter.key())
200              << QVariant::fromValue<int>((int) iter.value());
201         ++iter;
202     }
203     return list;
204 }
205
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>());
211     }
212 }