File: corebuffersyncer.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 (213 lines) | stat: -rw-r--r-- 7,738 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
210
211
212
213
/***************************************************************************
 *   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 "corebuffersyncer.h"

#include "core.h"
#include "coresession.h"
#include "corenetwork.h"
#include "ircchannel.h"
#include "util.h"

class PurgeEvent : public QEvent
{
public:
    PurgeEvent() : QEvent(QEvent::User) {}
};


INIT_SYNCABLE_OBJECT(CoreBufferSyncer)
CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
    : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), Core::bufferMarkerLineMsgIds(parent->user()), Core::bufferActivities(parent->user()), Core::highlightCounts(parent->user()), parent),
    _coreSession(parent),
    _purgeBuffers(false)
{
    connect(parent, SIGNAL(displayMsg(Message)), SLOT(addBufferActivity(Message)));
    connect(parent, SIGNAL(displayMsg(Message)), SLOT(addCoreHighlight(Message)));
}


void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId)
{
    if (setLastSeenMsg(buffer, msgId)) {
        int activity = Core::bufferActivity(buffer, msgId);
        int highlightCount = Core::highlightCount(buffer, msgId);

        setBufferActivity(buffer, activity);
        setHighlightCount(buffer, highlightCount);

        dirtyLastSeenBuffers << buffer;
    }
}


void CoreBufferSyncer::requestSetMarkerLine(BufferId buffer, const MsgId &msgId)
{
    if (setMarkerLine(buffer, msgId))
        dirtyMarkerLineBuffers << buffer;
}


void CoreBufferSyncer::storeDirtyIds()
{
    UserId userId = _coreSession->user();
    MsgId msgId;
    foreach(BufferId bufferId, dirtyLastSeenBuffers) {
        msgId = lastSeenMsg(bufferId);
        if (msgId.isValid())
            Core::setBufferLastSeenMsg(userId, bufferId, msgId);
    }

    foreach(BufferId bufferId, dirtyMarkerLineBuffers) {
        msgId = markerLine(bufferId);
        if (msgId.isValid())
            Core::setBufferMarkerLineMsg(userId, bufferId, msgId);
    }

    foreach(BufferId bufferId, dirtyActivities) {
        Core::setBufferActivity(userId, bufferId, activity(bufferId));
    }

    foreach(BufferId bufferId, dirtyHighlights) {
        Core::setHighlightCount(userId, bufferId, highlightCount(bufferId));
    }

    dirtyLastSeenBuffers.clear();
    dirtyMarkerLineBuffers.clear();
    dirtyActivities.clear();
    dirtyHighlights.clear();
}


void CoreBufferSyncer::removeBuffer(BufferId bufferId)
{
    BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
    if (!bufferInfo.isValid()) {
        qWarning() << "CoreBufferSyncer::removeBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
        return;
    }

    if (bufferInfo.type() == BufferInfo::StatusBuffer) {
        qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
        return;
    }

    if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
        CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
        if (!net) {
            qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
            return;
        }
        IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
        if (chan) {
            qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
            return;
        }
    }
    if (Core::removeBuffer(_coreSession->user(), bufferId))
        BufferSyncer::removeBuffer(bufferId);
}


void CoreBufferSyncer::renameBuffer(BufferId bufferId, QString newName)
{
    BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
    if (!bufferInfo.isValid()) {
        qWarning() << "CoreBufferSyncer::renameBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
        return;
    }

    if (bufferInfo.type() != BufferInfo::QueryBuffer) {
        qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
        return;
    }

    if (Core::renameBuffer(_coreSession->user(), bufferId, newName))
        BufferSyncer::renameBuffer(bufferId, newName);
}


void CoreBufferSyncer::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2)
{
    BufferInfo bufferInfo1 = Core::getBufferInfo(_coreSession->user(), bufferId1);
    BufferInfo bufferInfo2 = Core::getBufferInfo(_coreSession->user(), bufferId2);
    if (!bufferInfo1.isValid() || !bufferInfo2.isValid()) {
        qWarning() << "CoreBufferSyncer::mergeBuffersPermanently(): invalid BufferIds:" << bufferId1 << bufferId2 << "for User:" << _coreSession->user();
        return;
    }

    if ((bufferInfo1.type() != BufferInfo::QueryBuffer && bufferInfo1.type() != BufferInfo::ChannelBuffer)
        || (bufferInfo2.type() != BufferInfo::QueryBuffer && bufferInfo2.type() != BufferInfo::ChannelBuffer)) {
            qWarning() << "CoreBufferSyncer::mergeBuffersPermanently(): only QueryBuffers and/or ChannelBuffers can be merged!" << bufferId1 << bufferId2;
            return;
    }

    if (Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
        BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
    }
}


void CoreBufferSyncer::customEvent(QEvent *event)
{
    if (event->type() != QEvent::User)
        return;

    purgeBufferIds();
    event->accept();
}


void CoreBufferSyncer::requestPurgeBufferIds()
{
    if (_purgeBuffers)
        return;

    _purgeBuffers = true;
    QCoreApplication::postEvent(this, new PurgeEvent());
}


void CoreBufferSyncer::purgeBufferIds()
{
    _purgeBuffers = false;
    QList<BufferInfo> bufferInfos = Core::requestBuffers(_coreSession->user());
    QSet<BufferId> actualBuffers;
    foreach(BufferInfo bufferInfo, bufferInfos) {
        actualBuffers << bufferInfo.bufferId();
    }

    QSet<BufferId> storedIds = toQSet(lastSeenBufferIds()) + toQSet(markerLineBufferIds());
    foreach(BufferId bufferId, storedIds) {
        if (!actualBuffers.contains(bufferId)) {
            BufferSyncer::removeBuffer(bufferId);
        }
    }
}

void CoreBufferSyncer::setBufferActivity(BufferId buffer, int activity) {
    BufferSyncer::setBufferActivity(buffer, activity);
    dirtyActivities << buffer;
}

void CoreBufferSyncer::setHighlightCount(BufferId buffer, int highlightCount) {
    BufferSyncer::setHighlightCount(buffer, highlightCount);
    dirtyHighlights << buffer;
}