1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "corebuffersyncer.h"
24 #include "coresession.h"
25 #include "corenetwork.h"
26 #include "ircchannel.h"
28 class PurgeEvent : public QEvent {
30 PurgeEvent() : QEvent(QEvent::User) {}
33 CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
34 : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), parent),
40 void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId) {
41 if(setLastSeenMsg(buffer, msgId))
42 dirtyBuffers << buffer;
45 void CoreBufferSyncer::storeDirtyIds() {
46 UserId userId = _coreSession->user();
48 foreach(BufferId bufferId, dirtyBuffers) {
49 msgId = lastSeenMsg(bufferId);
51 Core::setBufferLastSeenMsg(userId, bufferId, msgId);
56 void CoreBufferSyncer::removeBuffer(BufferId bufferId) {
57 BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
58 if(!bufferInfo.isValid()) {
59 qWarning() << "CoreBufferSyncer::removeBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
63 if(bufferInfo.type() == BufferInfo::StatusBuffer) {
64 qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
68 if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
69 CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
71 qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
74 IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
76 qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
80 if(Core::removeBuffer(_coreSession->user(), bufferId))
81 BufferSyncer::removeBuffer(bufferId);
84 void CoreBufferSyncer::renameBuffer(BufferId bufferId, QString newName) {
85 BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
86 if(!bufferInfo.isValid()) {
87 qWarning() << "CoreBufferSyncer::renameBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
91 if(bufferInfo.type() != BufferInfo::QueryBuffer) {
92 qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
96 if(Core::renameBuffer(_coreSession->user(), bufferId, newName))
97 BufferSyncer::renameBuffer(bufferId, newName);
100 void CoreBufferSyncer::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2) {
101 BufferInfo bufferInfo1 = Core::getBufferInfo(_coreSession->user(), bufferId1);
102 BufferInfo bufferInfo2 = Core::getBufferInfo(_coreSession->user(), bufferId2);
103 if(!bufferInfo1.isValid() || !bufferInfo2.isValid()) {
104 qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): invalid BufferIds:" << bufferId1 << bufferId2 << "for User:" << _coreSession->user();
108 if(bufferInfo1.type() != BufferInfo::QueryBuffer || bufferInfo2.type() != BufferInfo::QueryBuffer) {
109 qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): only QueryBuffers can be merged!" << bufferId1 << bufferId2;
113 if(Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
114 BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
118 void CoreBufferSyncer::customEvent(QEvent *event) {
119 if(event->type() != QEvent::User)
126 void CoreBufferSyncer::requestPurgeBufferIds() {
130 _purgeBuffers = true;
131 QCoreApplication::postEvent(this, new PurgeEvent());
134 void CoreBufferSyncer::purgeBufferIds() {
135 _purgeBuffers = false;
136 QList<BufferInfo> bufferInfos = Core::requestBuffers(_coreSession->user());
137 QSet<BufferId> actualBuffers;
138 foreach(BufferInfo bufferInfo, bufferInfos) {
139 actualBuffers << bufferInfo.bufferId();
142 QList<BufferId> storedIds = bufferIds();
143 foreach(BufferId bufferId, storedIds) {
144 if(!actualBuffers.contains(bufferId)) {
145 BufferSyncer::removeBuffer(bufferId);