File: buffersyncer.cpp

package info (click to toggle)
quassel 1:0.13.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,640 kB
  • sloc: cpp: 66,953; perl: 15,837; ansic: 4,418; sql: 1,225; sh: 328; xml: 263; python: 224; makefile: 24
file content (209 lines) | stat: -rw-r--r-- 6,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "buffersyncer.h"

INIT_SYNCABLE_OBJECT(BufferSyncer)
BufferSyncer::BufferSyncer(QObject *parent)
    : SyncableObject(parent)
{
}


BufferSyncer::BufferSyncer(
        const QHash<BufferId, MsgId> &lastSeenMsg,
        const QHash<BufferId, MsgId> &markerLines,
        const QHash<BufferId, Message::Types> &activities,
        const QHash<BufferId, int> &highlightCounts,
        QObject *parent
) : SyncableObject(parent),
    _lastSeenMsg(lastSeenMsg),
    _markerLines(markerLines),
    _bufferActivities(activities),
    _highlightCounts(highlightCounts)
{
}


MsgId BufferSyncer::lastSeenMsg(BufferId buffer) const
{
    return _lastSeenMsg.value(buffer, MsgId());
}


bool BufferSyncer::setLastSeenMsg(BufferId buffer, const MsgId &msgId)
{
    if (!msgId.isValid())
        return false;

    const MsgId oldLastSeenMsg = lastSeenMsg(buffer);
    if (!oldLastSeenMsg.isValid() || oldLastSeenMsg < msgId) {
        _lastSeenMsg[buffer] = msgId;
        SYNC(ARG(buffer), ARG(msgId))
        emit lastSeenMsgSet(buffer, msgId);
        return true;
    }
    return false;
}


MsgId BufferSyncer::markerLine(BufferId buffer) const
{
    return _markerLines.value(buffer, MsgId());
}


bool BufferSyncer::setMarkerLine(BufferId buffer, const MsgId &msgId)
{
    if (!msgId.isValid())
        return false;

    if (_markerLines.value(buffer) == msgId)
        return false;

    _markerLines[buffer] = msgId;
    SYNC(ARG(buffer), ARG(msgId))
    emit markerLineSet(buffer, msgId);
    return true;
}


QVariantList BufferSyncer::initLastSeenMsg() const
{
    QVariantList list;
    QHash<BufferId, MsgId>::const_iterator iter = _lastSeenMsg.constBegin();
    while (iter != _lastSeenMsg.constEnd()) {
        list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
        ++iter;
    }
    return list;
}


void BufferSyncer::initSetLastSeenMsg(const QVariantList &list)
{
    _lastSeenMsg.clear();
    Q_ASSERT(list.count() % 2 == 0);
    for (int i = 0; i < list.count(); i += 2) {
        setLastSeenMsg(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
    }
}


QVariantList BufferSyncer::initMarkerLines() const
{
    QVariantList list;
    QHash<BufferId, MsgId>::const_iterator iter = _markerLines.constBegin();
    while (iter != _markerLines.constEnd()) {
        list << QVariant::fromValue(iter.key()) << QVariant::fromValue(iter.value());
        ++iter;
    }
    return list;
}


void BufferSyncer::initSetMarkerLines(const QVariantList &list)
{
    _markerLines.clear();
    Q_ASSERT(list.count() % 2 == 0);
    for (int i = 0; i < list.count(); i += 2) {
        setMarkerLine(list.at(i).value<BufferId>(), list.at(i+1).value<MsgId>());
    }
}


QVariantList BufferSyncer::initActivities() const
{
    QVariantList list;
    auto iter = _bufferActivities.constBegin();
    while (iter != _bufferActivities.constEnd()) {
        list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
        ++iter;
    }
    return list;
}


void BufferSyncer::initSetActivities(const QVariantList &list)
{
    _bufferActivities.clear();
    Q_ASSERT(list.count() % 2 == 0);
    for (int i = 0; i < list.count(); i += 2) {
        setBufferActivity(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
    }
}


Message::Types BufferSyncer::activity(BufferId buffer) const
{
    return _bufferActivities.value(buffer, Message::Types());
}


void BufferSyncer::removeBuffer(BufferId buffer)
{
    if (_lastSeenMsg.contains(buffer))
        _lastSeenMsg.remove(buffer);
    if (_markerLines.contains(buffer))
        _markerLines.remove(buffer);
    if (_bufferActivities.contains(buffer))
        _bufferActivities.remove(buffer);
    if (_highlightCounts.contains(buffer))
        _highlightCounts.remove(buffer);
    SYNC(ARG(buffer))
    emit bufferRemoved(buffer);
}


void BufferSyncer::mergeBuffersPermanently(BufferId buffer1, BufferId buffer2)
{
    if (_lastSeenMsg.contains(buffer2))
        _lastSeenMsg.remove(buffer2);
    if (_markerLines.contains(buffer2))
        _markerLines.remove(buffer2);
    if (_bufferActivities.contains(buffer2))
        _bufferActivities.remove(buffer2);
    if (_highlightCounts.contains(buffer2))
        _highlightCounts.remove(buffer2);
    SYNC(ARG(buffer1), ARG(buffer2))
    emit buffersPermanentlyMerged(buffer1, buffer2);
}

int BufferSyncer::highlightCount(BufferId buffer) const {
    return _highlightCounts.value(buffer, 0);
}

QVariantList BufferSyncer::initHighlightCounts() const {
    QVariantList list;
    auto iter = _highlightCounts.constBegin();
    while (iter != _highlightCounts.constEnd()) {
        list << QVariant::fromValue(iter.key()) << QVariant::fromValue((int)iter.value());
        ++iter;
    }
    return list;
}

void BufferSyncer::initSetHighlightCounts(const QVariantList &list) {
    _highlightCounts.clear();
    Q_ASSERT(list.count() % 2 == 0);
    for (int i = 0; i < list.count(); i += 2) {
        setHighlightCount(list.at(i).value<BufferId>(), list.at(i+1).value<int>());
    }
}