Haha! The Inputline is now a seperate dock! Sput: I told you that I'll make it :P
[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-old.h"
24 #include "chatwidget.h"
25 #include "settings.h"
26 #include "client.h"
27 #include "identity.h"
28 #include "network.h"
29 #include "networkmodel.h"
30
31 BufferWidget::BufferWidget(QWidget *parent)
32   : QWidget(parent),
33     _bufferModel(0),
34     _selectionModel(0)
35 {
36   ui.setupUi(this);
37 }
38
39 BufferWidget::~BufferWidget() {
40 }
41
42 void BufferWidget::init() {
43 }
44
45 void BufferWidget::setModel(BufferModel *bufferModel) {
46   if(_bufferModel) {
47     disconnect(_bufferModel, 0, this, 0);
48   }
49   _bufferModel = bufferModel;
50   connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
51           this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
52 }
53
54 void BufferWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
55   if(_selectionModel) {
56     disconnect(_selectionModel, 0, this, 0);
57   }
58   _selectionModel = selectionModel;
59   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
60           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
61 }
62
63 void BufferWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
64   Q_ASSERT(model());
65   if(!parent.isValid()) {
66     // ok this means that whole networks are about to be removed
67     // we can't determine which buffers are affect, so we hope that all nets are removed
68     // this is the most common case (for example disconnecting from the core or terminating the clint)
69     if(model()->rowCount(parent) != end - start + 1)
70       return;
71
72     ChatWidget *chatWidget;
73     QHash<BufferId, ChatWidget *>::iterator iter = _chatWidgets.begin();
74     while(iter != _chatWidgets.end()) {
75       chatWidget = *iter;
76       iter = _chatWidgets.erase(iter);
77       ui.stackedWidget->removeWidget(chatWidget);
78       chatWidget->deleteLater();
79     }
80     
81   } else {
82     // check if there are explicitly buffers removed
83     for(int i = start; i <= end; i++) {
84       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
85       if(!variant.isValid())
86         continue;
87       
88       BufferId bufferId = qVariantValue<BufferId>(variant);
89       removeBuffer(bufferId);
90     }
91   }
92 }
93
94 void BufferWidget::removeBuffer(BufferId bufferId) {
95   if(!_chatWidgets.contains(bufferId))
96     return;
97
98   ChatWidget *chatWidget = _chatWidgets.take(bufferId);
99   ui.stackedWidget->removeWidget(chatWidget);
100   chatWidget->deleteLater();
101 }
102
103 void BufferWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
104   Q_UNUSED(previous);
105   QVariant variant;
106
107   variant = current.data(NetworkModel::BufferIdRole);
108   if(!variant.isValid())
109     return;
110   
111   setCurrentBuffer(qVariantValue<BufferId>(variant));
112 }
113
114 void BufferWidget::setCurrentBuffer(BufferId bufferId) {
115   ChatWidget *chatWidget;
116   if(_chatWidgets.contains(bufferId)) {
117      chatWidget = _chatWidgets[bufferId];
118   } else {
119     Buffer *buf = Client::buffer(bufferId);
120     if(!buf) {
121       qWarning() << "BufferWidget::setBuffer(BufferId): Can't show unknown Buffer:" << bufferId;
122       return;
123     }
124     chatWidget = new ChatWidget(this);
125     chatWidget->init(bufferId);
126     QList<ChatLine *> lines;
127     QList<AbstractUiMsg *> msgs = buf->contents();
128     foreach(AbstractUiMsg *msg, msgs) {
129       lines.append(dynamic_cast<ChatLine*>(msg));
130     }
131     chatWidget->setContents(lines);
132     connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
133     connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
134     _chatWidgets[bufferId] = chatWidget;
135     ui.stackedWidget->addWidget(chatWidget);
136     chatWidget->setFocusProxy(this);
137   }
138   ui.stackedWidget->setCurrentWidget(chatWidget);
139   setFocus();
140 }
141