1 /***************************************************************************
2 * Copyright (C) 2005/06 by The Quassel Team *
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) any later version. *
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 ***************************************************************************/
23 #include "chatwidget.h"
25 Buffer::Buffer(QString netname, QString bufname) {
26 networkName = netname;
40 void Buffer::setActive(bool a) {
43 if(widget) widget->setActive(a);
47 void Buffer::displayMsg(Message msg) {
49 if(widget) widget->displayMsg(msg);
52 void Buffer::userInput(QString msg) {
53 emit userInput(networkName, bufferName, msg);
56 /* FIXME do we need this? */
57 void Buffer::scrollToEnd() {
59 //widget->scrollToEnd();
62 QWidget * Buffer::showWidget(QWidget *parent) {
65 return qobject_cast<QWidget*>(widget);
69 contentsWidget = new ChatWidgetContents(networkName, bufferName, 0);
70 contentsWidget->hide();
71 /* FIXME do we need this? */
72 for(int i = 0; i < contents.count(); i++) {
73 contentsWidget->appendMsg(contents[i]);
76 contentsWidget->hide();
77 widget = new BufferWidget(networkName, bufferName, isActive(), ownNick, contentsWidget, this, parent);
78 widget->setTopic(topic);
79 widget->updateNickList(nicks);
80 connect(widget, SIGNAL(userInput(QString)), this, SLOT(userInput(QString)));
81 return qobject_cast<QWidget*>(widget);
84 void Buffer::hideWidget() {
89 void Buffer::deleteWidget() {
93 QWidget * Buffer::getWidget() {
94 return qobject_cast<QWidget*>(widget);
97 void Buffer::setTopic(QString t) {
99 if(widget) widget->setTopic(t);
102 void Buffer::addNick(QString nick, VarMap props) {
103 if(nick == ownNick) setActive(true);
105 if(widget) widget->updateNickList(nicks);
108 void Buffer::updateNick(QString nick, VarMap props) {
110 if(widget) widget->updateNickList(nicks);
113 void Buffer::renameNick(QString oldnick, QString newnick) {
114 QVariant v = nicks.take(oldnick);
116 if(widget) widget->updateNickList(nicks);
119 void Buffer::removeNick(QString nick) {
120 if(nick == ownNick) setActive(false);
122 if(widget) widget->updateNickList(nicks);
125 void Buffer::setOwnNick(QString nick) {
127 if(widget) widget->setOwnNick(nick);
130 /****************************************************************************************/
134 /****************************************************************************************/
136 BufferWidget::BufferWidget(QString netname, QString bufname, bool act, QString own, ChatWidgetContents *contents, Buffer *pBuf, QWidget *parent) : QWidget(parent) {
138 networkName = netname;
139 bufferName = bufname;
143 ui.chatWidget->init(netname, bufname, contents);
146 ui.ownNick->addItem(own);
147 if(bufname.isEmpty()) {
150 ui.topicEdit->hide();
151 ui.chanSettingsButton->hide();
153 connect(ui.nickTree, SIGNAL(itemExpanded(QTreeWidgetItem *)), this, SLOT(itemExpansionChanged(QTreeWidgetItem*)));
154 connect(ui.nickTree, SIGNAL(itemCollapsed(QTreeWidgetItem *)), this, SLOT(itemExpansionChanged(QTreeWidgetItem*)));
155 connect(ui.inputEdit, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
157 opsExpanded = voicedExpanded = usersExpanded = true;
159 ui.chatWidget->setFocusProxy(ui.inputEdit);
163 BufferWidget::~BufferWidget() {
164 ui.chatWidget->takeWidget(); /* remove ownership so the chatwidget contents does not get destroyed */
165 parentBuffer->deleteWidget(); /* make sure the parent buffer knows we are gone */
168 void BufferWidget::updateTitle() {
169 QString title = QString("%1 in %2 [%3]: %4").arg(ui.ownNick->currentText()).arg(bufferName).arg(networkName).arg(ui.topicEdit->text());
170 setWindowTitle(title);
173 void BufferWidget::enterPressed() {
174 QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
175 foreach(QString msg, lines) {
176 if(msg.isEmpty()) continue;
179 ui.inputEdit->clear();
182 void BufferWidget::setActive(bool act) {
190 void BufferWidget::displayMsg(Message msg) {
191 ui.chatWidget->appendMsg(msg);
194 void BufferWidget::setOwnNick(QString nick) {
196 ui.ownNick->addItem(nick);
200 void BufferWidget::setTopic(QString topic) {
201 ui.topicEdit->setText(topic);
205 void BufferWidget::updateNickList(VarMap nicks) {
206 ui.nickTree->clear();
207 if(nicks.count() != 1) ui.nickTree->setHeaderLabel(tr("%1 Users").arg(nicks.count()));
208 else ui.nickTree->setHeaderLabel(tr("1 User"));
209 QTreeWidgetItem *ops = new QTreeWidgetItem();
210 QTreeWidgetItem *voiced = new QTreeWidgetItem();
211 QTreeWidgetItem *users = new QTreeWidgetItem();
212 // To sort case-insensitive, we have to put all nicks in a map which is sorted by (lowercase) key...
213 QMap<QString, QString> sorted;
214 foreach(QString n, nicks.keys()) { sorted[n.toLower()] = n; }
215 foreach(QString n, sorted.keys()) {
216 QString nick = sorted[n];
217 QString mode = nicks[nick].toMap()["Channels"].toMap()[bufferName].toMap()["Mode"].toString();
218 if(mode.contains('o')) { new QTreeWidgetItem(ops, QStringList(QString("@%1").arg(nick))); }
219 else if(mode.contains('v')) { new QTreeWidgetItem(voiced, QStringList(QString("+%1").arg(nick))); }
220 else new QTreeWidgetItem(users, QStringList(nick));
222 if(ops->childCount()) {
223 ops->setText(0, tr("%1 Operators").arg(ops->childCount()));
224 ui.nickTree->addTopLevelItem(ops);
225 ops->setExpanded(opsExpanded);
227 if(voiced->childCount()) {
228 voiced->setText(0, tr("%1 Voiced").arg(voiced->childCount()));
229 ui.nickTree->addTopLevelItem(voiced);
230 voiced->setExpanded(voicedExpanded);
231 } else delete voiced;
232 if(users->childCount()) {
233 users->setText(0, tr("%1 Users").arg(users->childCount()));
234 ui.nickTree->addTopLevelItem(users);
235 users->setExpanded(usersExpanded);
239 void BufferWidget::itemExpansionChanged(QTreeWidgetItem *item) {
240 if(item->child(0)->text(0).startsWith('@')) opsExpanded = item->isExpanded();
241 else if(item->child(0)->text(0).startsWith('+')) voicedExpanded = item->isExpanded();
242 else usersExpanded = item->isExpanded();