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