topic changes are now handled properly
[quassel.git] / src / qtui / bufferwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "bufferwidget.h"
22 #include "buffer.h"
23 #include "chatline.h"
24 #include "chatline-old.h"
25 #include "chatwidget.h"
26 #include "settings.h"
27 #include "client.h"
28 #include "identity.h"
29 #include "network.h"
30 #include "networkmodel.h"
31
32 #include "global.h"
33
34 BufferWidget::BufferWidget(QWidget *parent)
35   : AbstractItemView(parent),
36     _currentBuffer(0)
37 {
38   ui.setupUi(this);
39 }
40
41 BufferWidget::~BufferWidget() {
42 }
43
44 void BufferWidget::init() {
45 }
46
47 void BufferWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
48   Q_ASSERT(model());
49   if(!parent.isValid()) {
50     // ok this means that whole networks are about to be removed
51     // we can't determine which buffers are affect, so we hope that all nets are removed
52     // this is the most common case (for example disconnecting from the core or terminating the clint)
53     if(model()->rowCount(parent) != end - start + 1)
54       return;
55
56     ChatWidget *chatWidget;
57     QHash<BufferId, ChatWidget *>::iterator iter = _chatWidgets.begin();
58     while(iter != _chatWidgets.end()) {
59       chatWidget = *iter;
60       iter = _chatWidgets.erase(iter);
61       ui.stackedWidget->removeWidget(chatWidget);
62       chatWidget->deleteLater();
63     }
64     
65   } else {
66     // check if there are explicitly buffers removed
67     for(int i = start; i <= end; i++) {
68       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
69       if(!variant.isValid())
70         continue;
71       
72       BufferId bufferId = qVariantValue<BufferId>(variant);
73       removeBuffer(bufferId);
74     }
75   }
76 }
77
78 void BufferWidget::removeBuffer(BufferId bufferId) {
79   if(!_chatWidgets.contains(bufferId))
80     return;
81
82   if(Client::buffer(bufferId)) Client::buffer(bufferId)->setVisible(false);
83   ChatWidget *chatWidget = _chatWidgets.take(bufferId);
84   ui.stackedWidget->removeWidget(chatWidget);
85   chatWidget->deleteLater();
86 }
87
88 void BufferWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
89   BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
90   BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
91   if(newBufferId != oldBufferId)
92     setCurrentBuffer(newBufferId);
93 }
94
95 void BufferWidget::setCurrentBuffer(BufferId bufferId) {
96   if(!bufferId.isValid()) {
97     ui.stackedWidget->setCurrentWidget(ui.page);
98     return;
99   }
100   
101   ChatWidget *chatWidget = 0;
102   ChatView *chatView = 0;
103   Buffer *buf = Client::buffer(bufferId);
104   if(!buf) {
105     qWarning() << "BufferWidget::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
106     return;
107   }
108   Buffer *prevBuffer = Client::buffer(currentBuffer());
109   if(prevBuffer) prevBuffer->setVisible(false);
110   if(Global::SPUTDEV) {
111     if(_chatViews.contains(bufferId)) {
112       chatView = _chatViews[bufferId];
113     } else {
114       chatView = new ChatView(buf, this);
115       //chatView->init(bufferId);
116       QList<ChatLine *> lines;
117       QList<AbstractUiMsg *> msgs = buf->contents();
118       foreach(AbstractUiMsg *msg, msgs) {
119         lines.append(dynamic_cast<ChatLine *>(msg));
120       }
121       chatView->setContents(lines);
122       connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatView, SLOT(appendMsg(AbstractUiMsg *)));
123       connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatView, SLOT(prependMsg(AbstractUiMsg *)));
124       _chatViews[bufferId] = chatView;
125       ui.stackedWidget->addWidget(chatView);
126       chatView->setFocusProxy(this);
127     }
128     _currentBuffer = bufferId;
129     ui.stackedWidget->setCurrentWidget(chatView);
130   } else {
131     if(_chatWidgets.contains(bufferId)) {
132       chatWidget = _chatWidgets[bufferId];
133     } else {
134       chatWidget = new ChatWidget(this);
135       chatWidget->init(bufferId);
136       QList<ChatLineOld *> lines;
137       QList<AbstractUiMsg *> msgs = buf->contents();
138       foreach(AbstractUiMsg *msg, msgs) {
139         lines.append(dynamic_cast<ChatLineOld*>(msg));
140       }
141       chatWidget->setContents(lines);
142       connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
143       connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
144       _chatWidgets[bufferId] = chatWidget;
145       ui.stackedWidget->addWidget(chatWidget);
146       chatWidget->setFocusProxy(this);
147     }
148     _currentBuffer = bufferId;
149     ui.stackedWidget->setCurrentWidget(chatWidget);
150   }
151   buf->setVisible(true);
152   setFocus();
153 }
154