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