1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "bufferwidget.h"
24 #include "chatline-old.h"
25 #include "chatwidget.h"
30 #include "networkmodel.h"
34 BufferWidget::BufferWidget(QWidget *parent)
35 : AbstractItemView(parent),
41 BufferWidget::~BufferWidget() {
44 void BufferWidget::init() {
47 void BufferWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
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)
56 ChatWidget *chatWidget;
57 QHash<BufferId, ChatWidget *>::iterator iter = _chatWidgets.begin();
58 while(iter != _chatWidgets.end()) {
60 iter = _chatWidgets.erase(iter);
61 ui.stackedWidget->removeWidget(chatWidget);
62 chatWidget->deleteLater();
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())
72 BufferId bufferId = qVariantValue<BufferId>(variant);
73 removeBuffer(bufferId);
78 void BufferWidget::removeBuffer(BufferId bufferId) {
79 if(!_chatWidgets.contains(bufferId))
82 if(Client::buffer(bufferId)) Client::buffer(bufferId)->setVisible(false);
83 ChatWidget *chatWidget = _chatWidgets.take(bufferId);
84 ui.stackedWidget->removeWidget(chatWidget);
85 chatWidget->deleteLater();
88 void BufferWidget::currentChanged(const QModelIndex ¤t, 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);
95 void BufferWidget::setCurrentBuffer(BufferId bufferId) {
96 if(!bufferId.isValid()) {
97 ui.stackedWidget->setCurrentWidget(ui.page);
101 ChatWidget *chatWidget = 0;
102 ChatView *chatView = 0;
103 Buffer *buf = Client::buffer(bufferId);
105 qWarning() << "BufferWidget::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
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];
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));
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);
128 _currentBuffer = bufferId;
129 ui.stackedWidget->setCurrentWidget(chatView);
131 if(_chatWidgets.contains(bufferId)) {
132 chatWidget = _chatWidgets[bufferId];
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));
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);
148 _currentBuffer = bufferId;
149 ui.stackedWidget->setCurrentWidget(chatWidget);
151 buf->setVisible(true);