1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
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 "abstractbuffercontainer.h"
24 #include "networkmodel.h"
26 AbstractBufferContainer::AbstractBufferContainer(QWidget *parent)
27 : AbstractItemView(parent),
32 AbstractBufferContainer::~AbstractBufferContainer() {
36 void AbstractBufferContainer::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
38 if(!parent.isValid()) {
39 // ok this means that whole networks are about to be removed
40 // we can't determine which buffers are affect, so we hope that all nets are removed
41 // this is the most common case (for example disconnecting from the core or terminating the client)
42 if(model()->rowCount(parent) != end - start + 1)
45 foreach(BufferId id, _chatViews.keys()) {
50 // check if there are explicitly buffers removed
51 for(int i = start; i <= end; i++) {
52 QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
53 if(!variant.isValid())
56 BufferId bufferId = variant.value<BufferId>();
57 removeBuffer(bufferId);
62 void AbstractBufferContainer::removeBuffer(BufferId bufferId) {
63 if(Client::buffer(bufferId)) Client::buffer(bufferId)->setVisible(false);
64 if(!_chatViews.contains(bufferId))
67 removeChatView(bufferId);
68 _chatViews.take(bufferId);
71 void AbstractBufferContainer::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
72 BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
73 BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
74 if(newBufferId != oldBufferId) {
75 setCurrentBuffer(newBufferId);
76 emit currentChanged(newBufferId);
80 void AbstractBufferContainer::setCurrentBuffer(BufferId bufferId) {
81 AbstractChatView *chatView = 0;
82 Buffer *prevBuffer = Client::buffer(currentBuffer());
83 if(prevBuffer) prevBuffer->setVisible(false);
86 if(!bufferId.isValid() || !(buf = Client::buffer(bufferId))) {
87 if(bufferId.isValid())
88 qWarning() << "AbstractBufferContainer::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
93 if(_chatViews.contains(bufferId)) {
94 chatView = _chatViews[bufferId];
96 chatView = createChatView(bufferId);
97 _chatViews[bufferId] = chatView;
99 _currentBuffer = bufferId;
100 showChatView(bufferId);
101 buf->setVisible(true);