Add GPL header to mpris script
[quassel.git] / src / core / corebuffersyncer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 public:
30   PurgeEvent() : QEvent(QEvent::User) {}
31 };
32
33 CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
34   : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), parent),
35     _coreSession(parent),
36     _purgeBuffers(false)
37 {
38 }
39
40 void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId) {
41   if(setLastSeenMsg(buffer, msgId))
42     dirtyBuffers << buffer;
43 }
44
45 void CoreBufferSyncer::storeDirtyIds() {
46   UserId userId = _coreSession->user();
47   MsgId msgId;
48   foreach(BufferId bufferId, dirtyBuffers) {
49     msgId = lastSeenMsg(bufferId);
50     if(msgId.isValid())
51       Core::setBufferLastSeenMsg(userId, bufferId, msgId);
52   }
53   dirtyBuffers.clear();
54 }
55
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();
60     return;
61   }
62
63   if(bufferInfo.type() == BufferInfo::StatusBuffer) {
64     qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
65     return;
66   }
67
68   if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
69     CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
70     if(!net) {
71       qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
72       return;
73     }
74     IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
75     if(chan) {
76       qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
77       return;
78     }
79   }
80   if(Core::removeBuffer(_coreSession->user(), bufferId))
81     BufferSyncer::removeBuffer(bufferId);
82 }
83
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();
88     return;
89   }
90
91   if(bufferInfo.type() != BufferInfo::QueryBuffer) {
92     qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
93     return;
94   }
95
96   if(Core::renameBuffer(_coreSession->user(), bufferId, newName))
97     BufferSyncer::renameBuffer(bufferId, newName);
98 }
99
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();
105     return;
106   }
107
108   if(bufferInfo1.type() != BufferInfo::QueryBuffer || bufferInfo2.type() != BufferInfo::QueryBuffer) {
109     qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): only QueryBuffers can be merged!" << bufferId1 << bufferId2;
110     return;
111   }
112
113   if(Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
114     BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
115   }
116 }
117
118 void CoreBufferSyncer::customEvent(QEvent *event) {
119   if(event->type() != QEvent::User)
120     return;
121
122   purgeBufferIds();
123   event->accept();
124 }
125
126 void CoreBufferSyncer::requestPurgeBufferIds() {
127   if(_purgeBuffers)
128     return;
129
130   _purgeBuffers = true;
131   QCoreApplication::postEvent(this, new PurgeEvent());
132 }
133
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();
140   }
141
142   QList<BufferId> storedIds = bufferIds();
143   foreach(BufferId bufferId, storedIds) {
144     if(!actualBuffers.contains(bufferId)) {
145       BufferSyncer::removeBuffer(bufferId);
146     }
147   }
148 }