1/***************************************************************************
2 * Copyright (C) 2005-2014 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
23INIT_SYNCABLE_OBJECT(BufferSyncer)
24BufferSyncer::BufferSyncer(QObject *parent)
25 : SyncableObject(parent)
26{
27}
28
29
30BufferSyncer::BufferSyncer(const QHash<BufferId, MsgId> &lastSeenMsg, const QHash<BufferId, MsgId> &markerLines, QObject *parent)
31 : SyncableObject(parent),
32 _lastSeenMsg(lastSeenMsg),
33 _markerLines(markerLines)
34{
35}
36
37
38MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
39{
40 return _lastSeenMsg.value(buffer, MsgId());
41}
42
43
44bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
45{
46 if (!msgId.isValid())
47 return false;
48
49 const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
50 if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
51 _lastSeenMsg[buffer] = msgId;
52 SYNC(ARG(buffer), ARG(msgId))
53 emit lastSeenMsgSet(buffer, msgId);
54 return true;
55 }
56 return false;
57}
58
59
60MsgId BufferSyncer::markerLine(BufferId buffer) const
61{
62 return _markerLines.value(buffer, MsgId());
63}
64
65
66bool 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
81QVariantList BufferSyncer::initLastSeenMsg() const
82{
83 QVariantList list;
84 QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
85 while (iter != _lastSeenMsg.constEnd()) {
86 list << QVariant::fromValue<BufferId>(iter.key())
87 << QVariant::fromValue<MsgId>(iter.value());
88 ++iter;
89 }
90 return list;
91}
92
93
94void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
95{
96 _lastSeenMsg.clear();
97 Q_ASSERT(list.count() % 2 == 0);
98 for (int i = 0; i < list.count(); i += 2) {
99 setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
100 }
101}
102
103
104QVariantList BufferSyncer::initMarkerLines() const
105{
106 QVariantList list;
107 QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
108 while (iter != _markerLines.constEnd()) {
109 list << QVariant::fromValue<BufferId>(iter.key())
110 << QVariant::fromValue<MsgId>(iter.value());
111 ++iter;
112 }
113 return list;
114}
115
116
117void BufferSyncer::initSetMarkerLines(const QVariantList &list)
118{
119 _markerLines.clear();
120 Q_ASSERT(list.count() % 2 == 0);
121 for (int i = 0; i < list.count(); i += 2) {
122 setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
123 }
124}
125
126
127void BufferSyncer::removeBuffer(BufferId buffer)
128{
129 if (_lastSeenMsg.contains(buffer))
130 _lastSeenMsg.remove(buffer);
131 if (_markerLines.contains(buffer))
132 _markerLines.remove(buffer);
133 SYNC(ARG(buffer))
134 emit bufferRemoved(buffer);
135}
136
137
138void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
139{
140 if (_lastSeenMsg.contains(buffer2))
141 _lastSeenMsg.remove(buffer2);
142 if (_markerLines.contains(buffer2))
143 _markerLines.remove(buffer2);
144 SYNC(ARG(buffer1), ARG(buffer2))
145 emit buffersPermanentlyMerged(buffer1, buffer2);
146}
147