adding new settings for proper message redirection
[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 "client.h"
23 #include "networkmodel.h"
24
25 AbstractBufferContainer::AbstractBufferContainer(QWidget *parent)
26   : AbstractItemView(parent),
27     _currentBuffer(0)
28 {
29 }
30
31 AbstractBufferContainer::~AbstractBufferContainer() {
32 }
33
34 void AbstractBufferContainer::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
35   Q_ASSERT(model());
36   if(!parent.isValid()) {
37     // ok this means that whole networks are about to be removed
38     // we can't determine which buffers are affect, so we hope that all nets are removed
39     // this is the most common case (for example disconnecting from the core or terminating the client)
40     if(model()->rowCount(parent) != end - start + 1)
41       return;
42
43     foreach(BufferId id, _chatViews.keys()) {
44       removeChatView(id);
45     }
46     _chatViews.clear();
47   } else {
48     // check if there are explicitly buffers removed
49     for(int i = start; i <= end; i++) {
50       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
51       if(!variant.isValid())
52         continue;
53
54       BufferId bufferId = variant.value<BufferId>();
55       removeBuffer(bufferId);
56     }
57   }
58 }
59
60 void AbstractBufferContainer::removeBuffer(BufferId bufferId) {
61   if(!_chatViews.contains(bufferId))
62     return;
63
64   removeChatView(bufferId);
65   _chatViews.take(bufferId);
66 }
67
68 void AbstractBufferContainer::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
69   BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
70   BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
71   if(newBufferId != oldBufferId) {
72     setCurrentBuffer(newBufferId);
73     emit currentChanged(newBufferId);
74   }
75 }
76
77 void AbstractBufferContainer::setCurrentBuffer(BufferId bufferId) {
78   BufferId prevBufferId = currentBuffer();
79   if(prevBufferId.isValid() && _chatViews.contains(prevBufferId)) {
80     Client::setBufferLastSeenMsg(prevBufferId, _chatViews[prevBufferId]->lastMsgId());
81   }
82
83   if(!bufferId.isValid()) {
84     _currentBuffer = 0;
85     showChatView(0);
86     return;
87   }
88
89   if(!_chatViews.contains(bufferId))
90     _chatViews[bufferId] = createChatView(bufferId);
91
92   _currentBuffer = bufferId;
93   showChatView(bufferId);
94   Client::networkModel()->setBufferActivity(bufferId, BufferInfo::NoActivity);
95   Client::setBufferLastSeenMsg(bufferId, _chatViews[bufferId]->lastMsgId());
96   setFocus();
97 }