Fix ALL the license headers!
[quassel.git] / src / core / corebuffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "corebuffersyncer.h"
22
23 #include "core.h"
24 #include "coresession.h"
25 #include "corenetwork.h"
26 #include "ircchannel.h"
27
28 class PurgeEvent : public QEvent
29 {
30 public:
31     PurgeEvent() : QEvent(QEvent::User) {}
32 };
33
34
35 INIT_SYNCABLE_OBJECT(CoreBufferSyncer)
36 CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
37     : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), Core::bufferMarkerLineMsgIds(parent->user()), parent),
38     _coreSession(parent),
39     _purgeBuffers(false)
40 {
41 }
42
43
44 void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId)
45 {
46     if (setLastSeenMsg(buffer, msgId))
47         dirtyLastSeenBuffers << buffer;
48 }
49
50
51 void CoreBufferSyncer::requestSetMarkerLine(BufferId buffer, const MsgId &msgId)
52 {
53     if (setMarkerLine(buffer, msgId))
54         dirtyMarkerLineBuffers << buffer;
55 }
56
57
58 void CoreBufferSyncer::storeDirtyIds()
59 {
60     UserId userId = _coreSession->user();
61     MsgId msgId;
62     foreach(BufferId bufferId, dirtyLastSeenBuffers) {
63         msgId = lastSeenMsg(bufferId);
64         if (msgId.isValid())
65             Core::setBufferLastSeenMsg(userId, bufferId, msgId);
66     }
67
68     foreach(BufferId bufferId, dirtyMarkerLineBuffers) {
69         msgId = markerLine(bufferId);
70         if (msgId.isValid())
71             Core::setBufferMarkerLineMsg(userId, bufferId, msgId);
72     }
73
74     dirtyLastSeenBuffers.clear();
75     dirtyMarkerLineBuffers.clear();
76 }
77
78
79 void CoreBufferSyncer::removeBuffer(BufferId bufferId)
80 {
81     BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
82     if (!bufferInfo.isValid()) {
83         qWarning() << "CoreBufferSyncer::removeBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
84         return;
85     }
86
87     if (bufferInfo.type() == BufferInfo::StatusBuffer) {
88         qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
89         return;
90     }
91
92     if (bufferInfo.type() == BufferInfo::ChannelBuffer) {
93         CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
94         if (!net) {
95             qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
96             return;
97         }
98         IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
99         if (chan) {
100             qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
101             return;
102         }
103     }
104     if (Core::removeBuffer(_coreSession->user(), bufferId))
105         BufferSyncer::removeBuffer(bufferId);
106 }
107
108
109 void CoreBufferSyncer::renameBuffer(BufferId bufferId, QString newName)
110 {
111     BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
112     if (!bufferInfo.isValid()) {
113         qWarning() << "CoreBufferSyncer::renameBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
114         return;
115     }
116
117     if (bufferInfo.type() != BufferInfo::QueryBuffer) {
118         qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
119         return;
120     }
121
122     if (Core::renameBuffer(_coreSession->user(), bufferId, newName))
123         BufferSyncer::renameBuffer(bufferId, newName);
124 }
125
126
127 void CoreBufferSyncer::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2)
128 {
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();
133         return;
134     }
135
136     if (bufferInfo1.type() != BufferInfo::QueryBuffer || bufferInfo2.type() != BufferInfo::QueryBuffer) {
137         qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): only QueryBuffers can be merged!" << bufferId1 << bufferId2;
138         return;
139     }
140
141     if (Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
142         BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
143     }
144 }
145
146
147 void CoreBufferSyncer::customEvent(QEvent *event)
148 {
149     if (event->type() != QEvent::User)
150         return;
151
152     purgeBufferIds();
153     event->accept();
154 }
155
156
157 void CoreBufferSyncer::requestPurgeBufferIds()
158 {
159     if (_purgeBuffers)
160         return;
161
162     _purgeBuffers = true;
163     QCoreApplication::postEvent(this, new PurgeEvent());
164 }
165
166
167 void CoreBufferSyncer::purgeBufferIds()
168 {
169     _purgeBuffers = false;
170     QList<BufferInfo> bufferInfos = Core::requestBuffers(_coreSession->user());
171     QSet<BufferId> actualBuffers;
172     foreach(BufferInfo bufferInfo, bufferInfos) {
173         actualBuffers << bufferInfo.bufferId();
174     }
175
176     QSet<BufferId> storedIds = lastSeenBufferIds().toSet() + markerLineBufferIds().toSet();
177     foreach(BufferId bufferId, storedIds) {
178         if (!actualBuffers.contains(bufferId)) {
179             BufferSyncer::removeBuffer(bufferId);
180         }
181     }
182 }