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