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"
23 #include "chatline-old.h"
24 #include "chatwidget.h"
29 #include "networkmodel.h"
31 BufferWidget::BufferWidget(QWidget *parent)
40 BufferWidget::~BufferWidget() {
43 void BufferWidget::init() {
46 void BufferWidget::setModel(BufferModel *bufferModel) {
48 disconnect(_bufferModel, 0, this, 0);
50 _bufferModel = bufferModel;
53 connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
54 this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
58 void BufferWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
60 disconnect(_selectionModel, 0, this, 0);
62 _selectionModel = selectionModel;
65 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
66 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
70 void BufferWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
72 if(!parent.isValid()) {
73 // ok this means that whole networks are about to be removed
74 // we can't determine which buffers are affect, so we hope that all nets are removed
75 // this is the most common case (for example disconnecting from the core or terminating the clint)
76 if(model()->rowCount(parent) != end - start + 1)
79 ChatWidget *chatWidget;
80 QHash<BufferId, ChatWidget *>::iterator iter = _chatWidgets.begin();
81 while(iter != _chatWidgets.end()) {
83 iter = _chatWidgets.erase(iter);
84 ui.stackedWidget->removeWidget(chatWidget);
85 chatWidget->deleteLater();
89 // check if there are explicitly buffers removed
90 for(int i = start; i <= end; i++) {
91 QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
92 if(!variant.isValid())
95 BufferId bufferId = qVariantValue<BufferId>(variant);
96 removeBuffer(bufferId);
101 void BufferWidget::removeBuffer(BufferId bufferId) {
102 if(!_chatWidgets.contains(bufferId))
105 if(Client::buffer(bufferId)) Client::buffer(bufferId)->setVisible(false);
106 ChatWidget *chatWidget = _chatWidgets.take(bufferId);
107 ui.stackedWidget->removeWidget(chatWidget);
108 chatWidget->deleteLater();
111 void BufferWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
112 BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
113 BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
114 if(newBufferId != oldBufferId)
115 setCurrentBuffer(newBufferId);
118 void BufferWidget::setCurrentBuffer(BufferId bufferId) {
119 if(!bufferId.isValid()) {
120 ui.stackedWidget->setCurrentWidget(ui.page);
124 ChatWidget *chatWidget = 0;
125 Buffer *buf = Client::buffer(bufferId);
127 qWarning() << "BufferWidget::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
130 Buffer *prevBuffer = Client::buffer(currentBuffer());
131 if(prevBuffer) prevBuffer->setVisible(false);
132 if(_chatWidgets.contains(bufferId)) {
133 chatWidget = _chatWidgets[bufferId];
135 chatWidget = new ChatWidget(this);
136 chatWidget->init(bufferId);
137 QList<ChatLine *> lines;
138 QList<AbstractUiMsg *> msgs = buf->contents();
139 foreach(AbstractUiMsg *msg, msgs) {
140 lines.append(dynamic_cast<ChatLine*>(msg));
142 chatWidget->setContents(lines);
143 connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
144 connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
145 _chatWidgets[bufferId] = chatWidget;
146 ui.stackedWidget->addWidget(chatWidget);
147 chatWidget->setFocusProxy(this);
149 _currentBuffer = bufferId;
150 ui.stackedWidget->setCurrentWidget(chatWidget);
151 buf->setVisible(true);