modernize: Prefer default member init over ctor init
[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 #include <utility>
24
25 BufferSyncer::BufferSyncer(QObject *parent)
26     : SyncableObject(parent)
27 {
28 }
29
30
31 BufferSyncer::BufferSyncer(
32         QHash<BufferId, MsgId> lastSeenMsg,
33         QHash<BufferId, MsgId> markerLines,
34         QHash<BufferId, Message::Types> activities,
35         QHash<BufferId, int> highlightCounts,
36         QObject *parent
37 ) : SyncableObject(parent),
38     _lastSeenMsg(std::move(lastSeenMsg)),
39     _markerLines(std::move(markerLines)),
40     _bufferActivities(std::move(activities)),
41     _highlightCounts(std::move(highlightCounts))
42 {
43 }
44
45
46 MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
47 {
48     return _lastSeenMsg.value(buffer, MsgId());
49 }
50
51
52 bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
53 {
54     if (!msgId.isValid())
55         return false;
56
57     const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
58     if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
59         _lastSeenMsg[buffer] = msgId;
60         SYNC(ARG(buffer), ARG(msgId))
61         emit lastSeenMsgSet(buffer, msgId);
62         return true;
63     }
64     return false;
65 }
66
67
68 MsgId BufferSyncer::markerLine(BufferId buffer) const
69 {
70     return _markerLines.value(buffer, MsgId());
71 }
72
73
74 bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId)
75 {
76     if (!msgId.isValid())
77         return false;
78
79     if (_markerLines.value(buffer) == msgId)
80         return false;
81
82     _markerLines[buffer] = msgId;
83     SYNC(ARG(buffer), ARG(msgId))
84     emit markerLineSet(buffer, msgId);
85     return true;
86 }
87
88
89 QVariantList BufferSyncer::initLastSeenMsg() const
90 {
91     QVariantList list;
92     QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
93     while (iter != _lastSeenMsg.constEnd()) {
94         list << QVariant::fromValue<BufferId>(iter.key())
95              << QVariant::fromValue<MsgId>(iter.value());
96         ++iter;
97     }
98     return list;
99 }
100
101
102 void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
103 {
104     _lastSeenMsg.clear();
105     Q_ASSERT(list.count() % 2 == 0);
106     for (int i = 0; i < list.count(); i += 2) {
107         setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
108     }
109 }
110
111
112 QVariantList BufferSyncer::initMarkerLines() const
113 {
114     QVariantList list;
115     QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
116     while (iter != _markerLines.constEnd()) {
117         list << QVariant::fromValue<BufferId>(iter.key())
118              << QVariant::fromValue<MsgId>(iter.value());
119         ++iter;
120     }
121     return list;
122 }
123
124
125 void BufferSyncer::initSetMarkerLines(const QVariantList &list)
126 {
127     _markerLines.clear();
128     Q_ASSERT(list.count() % 2 == 0);
129     for (int i = 0; i < list.count(); i += 2) {
130         setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
131     }
132 }
133
134
135 QVariantList BufferSyncer::initActivities() const
136 {
137     QVariantList list;
138     auto iter = _bufferActivities.constBegin();
139     while (iter != _bufferActivities.constEnd()) {
140         list << QVariant::fromValue<BufferId>(iter.key())
141              << QVariant::fromValue<int>((int) iter.value());
142         ++iter;
143     }
144     return list;
145 }
146
147
148 void BufferSyncer::initSetActivities(const QVariantList &list)
149 {
150     _bufferActivities.clear();
151     Q_ASSERT(list.count() % 2 == 0);
152     for (int i = 0; i < list.count(); i += 2) {
153         setBufferActivity(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
154     }
155 }
156
157
158 Message::Types BufferSyncer::activity(BufferId buffer) const
159 {
160     return _bufferActivities.value(buffer, Message::Types());
161 }
162
163
164 void BufferSyncer::removeBuffer(BufferId buffer)
165 {
166     if (_lastSeenMsg.contains(buffer))
167         _lastSeenMsg.remove(buffer);
168     if (_markerLines.contains(buffer))
169         _markerLines.remove(buffer);
170     if (_bufferActivities.contains(buffer))
171         _bufferActivities.remove(buffer);
172     if (_highlightCounts.contains(buffer))
173         _highlightCounts.remove(buffer);
174     SYNC(ARG(buffer))
175     emit bufferRemoved(buffer);
176 }
177
178
179 void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
180 {
181     if (_lastSeenMsg.contains(buffer2))
182         _lastSeenMsg.remove(buffer2);
183     if (_markerLines.contains(buffer2))
184         _markerLines.remove(buffer2);
185     if (_bufferActivities.contains(buffer2))
186         _bufferActivities.remove(buffer2);
187     if (_highlightCounts.contains(buffer2))
188         _highlightCounts.remove(buffer2);
189     SYNC(ARG(buffer1), ARG(buffer2))
190     emit buffersPermanentlyMerged(buffer1, buffer2);
191 }
192
193 int BufferSyncer::highlightCount(BufferId buffer) const {
194     return _highlightCounts.value(buffer, 0);
195 }
196
197 QVariantList BufferSyncer::initHighlightCounts() const {
198     QVariantList list;
199     auto iter = _highlightCounts.constBegin();
200     while (iter != _highlightCounts.constEnd()) {
201         list << QVariant::fromValue<BufferId>(iter.key())
202              << QVariant::fromValue<int>((int) iter.value());
203         ++iter;
204     }
205     return list;
206 }
207
208 void BufferSyncer::initSetHighlightCounts(const QVariantList &list) {
209     _highlightCounts.clear();
210     Q_ASSERT(list.count() % 2 == 0);
211     for (int i = 0; i < list.count(); i += 2) {
212         setHighlightCount(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
213     }
214 }