1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "corebuffersyncer.h"
24 #include "coresession.h"
25 #include "corenetwork.h"
26 #include "ircchannel.h"
28 class PurgeEvent : public QEvent
31 PurgeEvent() : QEvent(QEvent::User) {}
34 CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
35 : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), Core::bufferMarkerLineMsgIds(parent->user()), Core::bufferActivities(parent->user()), Core::highlightCounts(parent->user()), parent),
39 connect(parent, &CoreSession::displayMsg, this, &CoreBufferSyncer::addBufferActivity);
40 connect(parent, &CoreSession::displayMsg, this, &CoreBufferSyncer::addCoreHighlight);
44 void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId)
46 if (setLastSeenMsg(buffer, msgId)) {
47 int activity = Core::bufferActivity(buffer, msgId);
48 int highlightCount = Core::highlightCount(buffer, msgId);
50 setBufferActivity(buffer, activity);
51 setHighlightCount(buffer, highlightCount);
53 dirtyLastSeenBuffers << buffer;
58 void CoreBufferSyncer::requestSetMarkerLine(BufferId buffer, const MsgId &msgId)
60 if (setMarkerLine(buffer, msgId))
61 dirtyMarkerLineBuffers << buffer;
65 void CoreBufferSyncer::storeDirtyIds()
67 UserId userId = _coreSession->user();
69 foreach(BufferId bufferId, dirtyLastSeenBuffers) {
70 msgId = lastSeenMsg(bufferId);
72 Core::setBufferLastSeenMsg(userId, bufferId, msgId);
75 foreach(BufferId bufferId, dirtyMarkerLineBuffers) {
76 msgId = markerLine(bufferId);
78 Core::setBufferMarkerLineMsg(userId, bufferId, msgId);
81 foreach(BufferId bufferId, dirtyActivities) {
82 Core::setBufferActivity(userId, bufferId, activity(bufferId));
85 foreach(BufferId bufferId, dirtyHighlights) {
86 Core::setHighlightCount(userId, bufferId, highlightCount(bufferId));
89 dirtyLastSeenBuffers.clear();
90 dirtyMarkerLineBuffers.clear();
91 dirtyActivities.clear();
92 dirtyHighlights.clear();
96 void CoreBufferSyncer::removeBuffer(BufferId bufferId)
98 BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
99 if (!bufferInfo.isValid()) {
100 qWarning() << "CoreBufferSyncer::removeBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
104 if (bufferInfo.type() == BufferInfo::StatusBuffer) {
105 qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
109 if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
110 CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
112 qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
115 IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
117 qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
121 if (Core::removeBuffer(_coreSession->user(), bufferId))
122 BufferSyncer::removeBuffer(bufferId);
126 void CoreBufferSyncer::renameBuffer(BufferId bufferId, QString newName)
128 BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
129 if (!bufferInfo.isValid()) {
130 qWarning() << "CoreBufferSyncer::renameBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
134 if (bufferInfo.type() != BufferInfo::QueryBuffer) {
135 qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
139 if (Core::renameBuffer(_coreSession->user(), bufferId, newName))
140 BufferSyncer::renameBuffer(bufferId, newName);
144 void CoreBufferSyncer::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2)
146 BufferInfo bufferInfo1 = Core::getBufferInfo(_coreSession->user(), bufferId1);
147 BufferInfo bufferInfo2 = Core::getBufferInfo(_coreSession->user(), bufferId2);
148 if (!bufferInfo1.isValid() || !bufferInfo2.isValid()) {
149 qWarning() << "CoreBufferSyncer::mergeBuffersPermanently(): invalid BufferIds:" << bufferId1 << bufferId2 << "for User:" << _coreSession->user();
153 if ((bufferInfo1.type() != BufferInfo::QueryBuffer && bufferInfo1.type() != BufferInfo::ChannelBuffer)
154 || (bufferInfo2.type() != BufferInfo::QueryBuffer && bufferInfo2.type() != BufferInfo::ChannelBuffer)) {
155 qWarning() << "CoreBufferSyncer::mergeBuffersPermanently(): only QueryBuffers and/or ChannelBuffers can be merged!" << bufferId1 << bufferId2;
159 if (Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
160 BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
165 void CoreBufferSyncer::customEvent(QEvent *event)
167 if (event->type() != QEvent::User)
175 void CoreBufferSyncer::requestPurgeBufferIds()
180 _purgeBuffers = true;
181 QCoreApplication::postEvent(this, new PurgeEvent());
185 void CoreBufferSyncer::purgeBufferIds()
187 _purgeBuffers = false;
188 QList<BufferInfo> bufferInfos = Core::requestBuffers(_coreSession->user());
189 QSet<BufferId> actualBuffers;
190 foreach(BufferInfo bufferInfo, bufferInfos) {
191 actualBuffers << bufferInfo.bufferId();
194 QSet<BufferId> storedIds = lastSeenBufferIds().toSet() + markerLineBufferIds().toSet();
195 foreach(BufferId bufferId, storedIds) {
196 if (!actualBuffers.contains(bufferId)) {
197 BufferSyncer::removeBuffer(bufferId);
202 void CoreBufferSyncer::setBufferActivity(BufferId buffer, int activity) {
203 BufferSyncer::setBufferActivity(buffer, activity);
204 dirtyActivities << buffer;
207 void CoreBufferSyncer::setHighlightCount(BufferId buffer, int highlightCount) {
208 BufferSyncer::setHighlightCount(buffer, highlightCount);
209 dirtyHighlights << buffer;