Tweak aboutDlg some more
[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 CoreBufferSyncer::CoreBufferSyncer(CoreSession *parent)
29   : BufferSyncer(Core::bufferLastSeenMsgIds(parent->user()), parent),
30     _coreSession(parent)
31 {
32 }
33
34 void CoreBufferSyncer::requestSetLastSeenMsg(BufferId buffer, const MsgId &msgId) {
35   if(setLastSeenMsg(buffer, msgId))
36     dirtyBuffers << buffer;
37 }
38
39 void CoreBufferSyncer::storeDirtyIds() {
40   UserId userId = _coreSession->user();
41   MsgId msgId;
42   foreach(BufferId bufferId, dirtyBuffers) {
43     msgId = lastSeenMsg(bufferId);
44     if(msgId.isValid())
45       Core::setBufferLastSeenMsg(userId, bufferId, msgId);
46   }
47   dirtyBuffers.clear();
48 }
49
50 void CoreBufferSyncer::removeBuffer(BufferId bufferId) {
51   BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
52   if(!bufferInfo.isValid()) {
53     qWarning() << "CoreBufferSyncer::removeBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
54     return;
55   }
56
57   if(bufferInfo.type() == BufferInfo::StatusBuffer) {
58     qWarning() << "CoreBufferSyncer::removeBuffer(): Status Buffers cannot be removed!";
59     return;
60   }
61
62   if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
63     CoreNetwork *net = _coreSession->network(bufferInfo.networkId());
64     if(!net) {
65       qWarning() << "CoreBufferSyncer::removeBuffer(): Received BufferInfo with unknown networkId!";
66       return;
67     }
68     IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
69     if(chan) {
70       qWarning() << "CoreBufferSyncer::removeBuffer(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
71       return;
72     }
73   }
74   if(Core::removeBuffer(_coreSession->user(), bufferId))
75     BufferSyncer::removeBuffer(bufferId);
76 }
77
78 void CoreBufferSyncer::renameBuffer(BufferId bufferId, QString newName) {
79   BufferInfo bufferInfo = Core::getBufferInfo(_coreSession->user(), bufferId);
80   if(!bufferInfo.isValid()) {
81     qWarning() << "CoreBufferSyncer::renameBuffer(): invalid BufferId:" << bufferId << "for User:" << _coreSession->user();
82     return;
83   }
84
85   if(bufferInfo.type() != BufferInfo::QueryBuffer) {
86     qWarning() << "CoreBufferSyncer::renameBuffer(): only QueryBuffers can be renamed" << bufferId;
87     return;
88   }
89
90   if(Core::renameBuffer(_coreSession->user(), bufferId, newName))
91     BufferSyncer::renameBuffer(bufferId, newName);
92 }
93
94 void CoreBufferSyncer::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2) {
95   BufferInfo bufferInfo1 = Core::getBufferInfo(_coreSession->user(), bufferId1);
96   BufferInfo bufferInfo2 = Core::getBufferInfo(_coreSession->user(), bufferId2);
97   if(!bufferInfo1.isValid() || !bufferInfo2.isValid()) {
98     qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): invalid BufferIds:" << bufferId1 << bufferId2 << "for User:" << _coreSession->user();
99     return;
100   }
101
102   if(bufferInfo1.type() != BufferInfo::QueryBuffer || bufferInfo2.type() != BufferInfo::QueryBuffer) {
103     qWarning() << "CoreBufferSyncer::mergeBufferPermanently(): only QueryBuffers can be merged!" << bufferId1 << bufferId2;
104     return;
105   }
106
107   if(Core::mergeBuffersPermanently(_coreSession->user(), bufferId1, bufferId2)) {
108     BufferSyncer::mergeBuffersPermanently(bufferId1, bufferId2);
109   }
110 }