Sanified multiple inheritance for AbstractChatView.
[quassel.git] / src / uisupport / abstractbuffercontainer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 "abstractbuffercontainer.h"
22 #include "buffer.h"
23 #include "client.h"
24 #include "networkmodel.h"
25
26 AbstractBufferContainer::AbstractBufferContainer(QWidget *parent) : AbstractItemView(parent), _currentBuffer(0)
27 {
28
29 }
30
31 AbstractBufferContainer::~AbstractBufferContainer() {
32
33 }
34
35
36 void AbstractBufferContainer::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
37   Q_ASSERT(model());
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)
43       return;
44
45     foreach(BufferId id, _chatViews.keys()) {
46       removeChatView(id);
47     }
48     _chatViews.clear();
49   } else {
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())
54         continue;
55
56       BufferId bufferId = variant.value<BufferId>();
57       removeBuffer(bufferId);
58     }
59   }
60 }
61
62 void AbstractBufferContainer::removeBuffer(BufferId bufferId) {
63   if(Client::buffer(bufferId)) Client::buffer(bufferId)->setVisible(false);
64   if(!_chatViews.contains(bufferId))
65     return;
66
67   removeChatView(bufferId);
68   _chatViews.take(bufferId);
69 }
70
71 void AbstractBufferContainer::currentChanged(const QModelIndex &current, 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);
77   }
78 }
79
80 void AbstractBufferContainer::setCurrentBuffer(BufferId bufferId) {
81   if(!bufferId.isValid()) {
82     showChatView(0);
83     return;
84   }
85
86   AbstractChatView *chatView = 0;
87   Buffer *buf = Client::buffer(bufferId);
88   if(!buf) {
89     qWarning() << "AbstractBufferContainer::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
90     return;
91   }
92   Buffer *prevBuffer = Client::buffer(currentBuffer());
93   if(prevBuffer) prevBuffer->setVisible(false);
94   if(_chatViews.contains(bufferId)) {
95     chatView = _chatViews[bufferId];
96   } else {
97     chatView = createChatView(bufferId);
98     chatView->setContents(buf->contents());
99     connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), this, SLOT(appendMsg(AbstractUiMsg *)));
100     connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), this, SLOT(prependMsg(AbstractUiMsg *)));
101     _chatViews[bufferId] = chatView;
102   }
103   _currentBuffer = bufferId;
104   showChatView(bufferId);
105   buf->setVisible(true);
106   setFocus();
107 }
108
109 void AbstractBufferContainer::appendMsg(AbstractUiMsg *msg) {
110   Buffer *buf = qobject_cast<Buffer *>(sender());
111   if(!buf) {
112     qWarning() << "AbstractBufferContainer::appendMsg(): Invalid slot caller!";
113     return;
114   }
115   BufferId id = buf->bufferInfo().bufferId();
116   if(!_chatViews.contains(id)) {
117     qWarning() << "AbstractBufferContainer::appendMsg(): Received message for unknown buffer!";
118     return;
119   }
120   _chatViews[id]->appendMsg(msg);
121 }
122
123 void AbstractBufferContainer::prependMsg(AbstractUiMsg *msg) {
124   Buffer *buf = qobject_cast<Buffer *>(sender());
125   if(!buf) {
126     qWarning() << "AbstractBufferContainer:prependMsg(): Invalid slot caller!";
127     return;
128   }
129   BufferId id = buf->bufferInfo().bufferId();
130   if(!_chatViews.contains(id)) {
131     qWarning() << "AbstractBufferContainer::prependMsg(): Received message for unknown buffer!";
132     return;
133   }
134   _chatViews[id]->prependMsg(msg);
135 }