1 /***************************************************************************
2 * Copyright (C) 2005-2013 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) {}
35 INIT_SYNCABLE_OBJECT(CoreBufferSyncer)
36 CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
37 : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), Core::bufferMarkerLineMsgIds(parent->user()), parent),
44 void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId)
46 if (setLastSeenMsg(buffer, msgId))
47 dirtyLastSeenBuffers << buffer;
51 void CoreBufferSyncer::requestSetMarkerLine(BufferId buffer, const MsgId &msgId)
53 if (setMarkerLine(buffer, msgId))
54 dirtyMarkerLineBuffers << buffer;
58 void CoreBufferSyncer::storeDirtyIds()
60 UserId userId = _coreSession->user();
62 foreach(BufferId bufferId, dirtyLastSeenBuffers) {
63 msgId = lastSeenMsg(bufferId);
65 Core::setBufferLastSeenMsg(userId, bufferId, msgId);
68 foreach(BufferId bufferId, dirtyMarkerLineBuffers) {
69 msgId = markerLine(bufferId);
71 Core::setBufferMarkerLineMsg(userId, bufferId, msgId);
74 dirtyLastSeenBuffers.clear();
75 dirtyMarkerLineBuffers.clear();
79 void CoreBufferSyncer::removeBuffer(BufferId bufferId)
81 BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
82 if (!bufferInfo.isValid()) {
83 qWarning() << "CoreBufferSyncer::removeBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
87 if (bufferInfo.type() == BufferInfo::StatusBuffer) {
88 qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
92 if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
93 CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
95 qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
98 IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
100 qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
104 if (Core::removeBuffer(_coreSession->user(), bufferId))
105 BufferSyncer::removeBuffer(bufferId);
109 void CoreBufferSyncer::renameBuffer(BufferId bufferId, QString newName)
111 BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
112 if (!bufferInfo.isValid()) {
113 qWarning() << "CoreBufferSyncer::renameBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
117 if (bufferInfo.type() != BufferInfo::QueryBuffer) {
118 qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
122 if (Core::renameBuffer(_coreSession->user(), bufferId, newName))
123 BufferSyncer::renameBuffer(bufferId, newName);
127 void CoreBufferSyncer::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2)
129 BufferInfo bufferInfo1 = Core::getBufferInfo(_coreSession->user(), bufferId1);
130 BufferInfo bufferInfo2 = Core::getBufferInfo(_coreSession->user(), bufferId2);
131 if (!bufferInfo1.isValid() || !bufferInfo2.isValid()) {
132 qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): invalid BufferIds:" << bufferId1 << bufferId2 << "for User:" << _coreSession->user();
136 if (bufferInfo1.type() != BufferInfo::QueryBuffer || bufferInfo2.type() != BufferInfo::QueryBuffer) {
137 qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): only QueryBuffers can be merged!" << bufferId1 << bufferId2;
141 if (Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
142 BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
147 void CoreBufferSyncer::customEvent(QEvent *event)
149 if (event->type() != QEvent::User)
157 void CoreBufferSyncer::requestPurgeBufferIds()
162 _purgeBuffers = true;
163 QCoreApplication::postEvent(this, new PurgeEvent());
167 void CoreBufferSyncer::purgeBufferIds()
169 _purgeBuffers = false;
170 QList<BufferInfo> bufferInfos = Core::requestBuffers(_coreSession->user());
171 QSet<BufferId> actualBuffers;
172 foreach(BufferInfo bufferInfo, bufferInfos) {
173 actualBuffers << bufferInfo.bufferId();
176 QSet<BufferId> storedIds = lastSeenBufferIds().toSet() + markerLineBufferIds().toSet();
177 foreach(BufferId bufferId, storedIds) {
178 if (!actualBuffers.contains(bufferId)) {
179 BufferSyncer::removeBuffer(bufferId);