fixes 898
[quassel.git] / src / uisupport / abstractbuffercontainer.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 "abstractbuffercontainer.h"
22 #include "client.h"
23 #include "clientbacklogmanager.h"
24 #include "networkmodel.h"
25
26 AbstractBufferContainer::AbstractBufferContainer(QWidget *parent)
27   : AbstractItemView(parent),
28     _currentBuffer(0)
29 {
30 }
31
32 AbstractBufferContainer::~AbstractBufferContainer() {
33 }
34
35 void AbstractBufferContainer::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
36   Q_ASSERT(model());
37   if(!parent.isValid()) {
38     // ok this means that whole networks are about to be removed
39     // we can't determine which buffers are affect, so we hope that all nets are removed
40     // this is the most common case (for example disconnecting from the core or terminating the client)
41     if(model()->rowCount(parent) != end - start + 1)
42       return;
43
44     foreach(BufferId id, _chatViews.keys()) {
45       removeChatView(id);
46     }
47     _chatViews.clear();
48   } else {
49     // check if there are explicitly buffers removed
50     for(int i = start; i <= end; i++) {
51       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
52       if(!variant.isValid())
53         continue;
54
55       BufferId bufferId = variant.value<BufferId>();
56       removeBuffer(bufferId);
57     }
58   }
59 }
60
61 void AbstractBufferContainer::removeBuffer(BufferId bufferId) {
62   if(!_chatViews.contains(bufferId))
63     return;
64
65   removeChatView(bufferId);
66   _chatViews.take(bufferId);
67 }
68
69 void AbstractBufferContainer::rowsInserted(const QModelIndex &parent, int start, int end) {
70   Q_UNUSED(end)
71
72   if(currentBuffer().isValid())
73     return;
74
75   // we want to make sure the very first valid buffer is selected
76   QModelIndex index = model()->index(start, 0, parent);
77   if(index.isValid()) {
78     BufferId id = index.data(NetworkModel::BufferIdRole).value<BufferId>();
79     if(id.isValid())
80       setCurrentBuffer(id);
81   }
82 }
83
84 void AbstractBufferContainer::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
85   Q_UNUSED(previous)
86
87   BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
88   if(newBufferId.isValid() && currentBuffer() != newBufferId) {
89     setCurrentBuffer(newBufferId);
90     emit currentChanged(newBufferId);
91     emit currentChanged(current);
92   }
93 }
94
95 void AbstractBufferContainer::setCurrentBuffer(BufferId bufferId) {
96   BufferId prevBufferId = currentBuffer();
97   if(prevBufferId.isValid() && _chatViews.contains(prevBufferId)) {
98     MsgId msgId = _chatViews.value(prevBufferId)->lastMsgId();
99     Client::setBufferLastSeenMsg(prevBufferId, msgId);
100     if(autoSetMarkerLine())
101       Client::setBufferMarkerLine(prevBufferId, msgId);
102   }
103
104   if(!bufferId.isValid()) {
105     _currentBuffer = 0;
106     showChatView(0);
107     return;
108   }
109
110   if(!_chatViews.contains(bufferId))
111     _chatViews[bufferId] = createChatView(bufferId);
112
113   _currentBuffer = bufferId;
114   showChatView(bufferId);
115   Client::networkModel()->clearBufferActivity(bufferId);
116   Client::setBufferLastSeenMsg(bufferId, _chatViews[bufferId]->lastMsgId());
117   Client::backlogManager()->checkForBacklog(bufferId);
118   setFocus();
119 }