Who stole the ! from main.cpp?
[quassel.git] / src / qtopia / mainwidget.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 "mainwidget.h"
22
23 #include "buffer.h"
24 #include "chatwidget.h"
25
26 MainWidget::MainWidget(QWidget *parent) : QWidget(parent) {
27   ui.setupUi(this);
28   ui.inputLine->hide(); ui.topicBar->hide();
29   connect(ui.inputLine, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
30   currentBuffer = 0;
31 }
32
33 MainWidget::~MainWidget() {
34
35
36
37 }
38
39 void MainWidget::setBuffer(Buffer *buf) {
40   if(!buf) {
41     ui.stack->setCurrentIndex(0);
42     currentBuffer = 0;
43     return;
44   }
45   //  TODO update topic if changed; handle status buffer display
46   QString title = QString("%1 (%2): \"%3\"").arg(buf->name()).arg(buf->networkName()).arg(buf->topic());
47   ui.topicBar->setContents(title);
48
49   //ui.chatWidget->setStyleSheet("div { color: #777777; }");
50   //ui.chatWidget->setHtml("<style type=\"text/css\">.foo { color: #777777; } .bar { font-style: italic }</style>"
51   //                       "<div class=\"foo\">foo</div> <div class=\"bar\">bar</div> baz");
52   //ui.chatWidget->moveCursor(QTextCursor::End);
53   //ui.chatWidget->insertHtml("<div class=\"foo\"> brumm</div>");
54
55   ChatWidget *chatWidget;
56   if(!chatWidgets.contains(buf)) {
57     chatWidget = new ChatWidget(this);
58     QList<ChatLine *> lines;
59     QList<AbstractUiMsg *> msgs = buf->contents();
60     foreach(AbstractUiMsg *msg, msgs) {
61       lines.append((ChatLine*)(msg));
62     }
63     chatWidget->setContents(lines);
64     connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
65     connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
66     connect(buf, SIGNAL(topicSet(QString)), this, SLOT(setTopic(QString)));
67     //connect(buf, SIGNAL(ownNickSet(QString)), this, SLOT(setOwnNick(QString)));
68     ui.stack->addWidget(chatWidget);
69     chatWidgets.insert(buf, chatWidget);
70     chatWidget->setFocusProxy(ui.inputLine);
71   } else chatWidget = chatWidgets[buf];
72   ui.inputLine->show(); ui.topicBar->show();
73   ui.stack->setCurrentWidget(chatWidget);
74   ui.inputLine->setFocus();
75   currentBuffer = buf;
76 }
77
78 void MainWidget::enterPressed() {
79   QStringList lines = ui.inputLine->text().split('\n', QString::SkipEmptyParts);
80   foreach(QString msg, lines) {
81     if(msg.isEmpty()) continue;
82     if(currentBuffer) currentBuffer->processUserInput(msg);
83   }
84   ui.inputLine->clear();
85 }
86
87 // FIXME make this more elegant, we don't need to send a string around...
88 void MainWidget::setTopic(QString topic) {
89   Q_UNUSED(topic);
90   if(currentBuffer) {
91     QString title = QString("%1 (%2): \"%3\"").arg(currentBuffer->name()).arg(currentBuffer->networkName()).arg(currentBuffer->topic());
92     ui.topicBar->setContents(title);
93   }
94 }