Save the mainwindow state properly when exiting the client.
[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 #include "client.h"
26
27 MainWidget::MainWidget(QWidget *parent) : AbstractBufferContainer(parent) {
28   ui.setupUi(this);
29   ui.inputLine->hide(); ui.topicBar->hide();
30   connect(ui.inputLine, SIGNAL(sendText(const QString &)), this, SLOT(userInput(const QString &)));
31   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
32 }
33
34 MainWidget::~MainWidget() {
35
36
37
38 }
39
40 AbstractChatView *MainWidget::createChatView(BufferId id) {
41   Q_UNUSED(id)
42   ChatWidget *widget = new ChatWidget(this);
43   AbstractChatView *chatView = static_cast<AbstractChatView *>(widget); // can't use dynamic_cast on some Qtopia devices
44   Q_ASSERT(chatView);
45   _chatViews[id] = widget;
46   ui.stack->addWidget(widget);
47   widget->setFocusProxy(this);
48   return chatView;
49 }
50
51 void MainWidget::removeChatView(BufferId id) {
52   ChatWidget *view = _chatViews.value(id, 0);
53   if(!view) return;
54   ui.stack->removeWidget(view);
55   view->deleteLater();
56 }
57
58 void MainWidget::showChatView(BufferId id) {
59   if(id.isValid()) currentBufferInfo = Client::buffer(id)->bufferInfo();
60   else currentBufferInfo = BufferInfo();
61   ChatWidget *widget = _chatViews.value(id, 0);
62   if(!widget) ui.stack->setCurrentIndex(0);
63   else {
64     ui.stack->setCurrentWidget(widget);
65     ui.inputLine->show(); ui.topicBar->show();
66     ui.inputLine->setFocus();
67   }
68 }
69
70
71 /*
72 void MainWidget::setBuffer(Buffer *buf) {
73
74   if(!buf) {
75     ui.stack->setCurrentIndex(0);
76     currentBuffer = 0;
77     return;
78   }
79   //  TODO update topic if changed; handle status buffer display
80 //  QString title = QString("%1 (%2): \"%3\"").arg(buf->bufferInfo().bufferName()).arg(buf->bufferInfo().networkName()).arg(buf->topic());
81   QString title = "foobar";
82   ui.topicBar->setContents(title);
83
84   //ui.chatWidget->setStyleSheet("div { color: #777777; }");
85   //ui.chatWidget->setHtml("<style type=\"text/css\">.foo { color: #777777; } .bar { font-style: italic }</style>"
86   //                       "<div class=\"foo\">foo</div> <div class=\"bar\">bar</div> baz");
87   //ui.chatWidget->moveCursor(QTextCursor::End);
88   //ui.chatWidget->insertHtml("<div class=\"foo\"> brumm</div>");
89
90   ChatWidget *chatWidget;
91   if(!chatWidgets.contains(buf)) {
92     chatWidget = new ChatWidget(this);
93     QList<ChatLine *> lines;
94     QList<AbstractUiMsg *> msgs = buf->contents();
95     foreach(AbstractUiMsg *msg, msgs) {
96       lines.append((ChatLine *)(msg));
97     }
98     chatWidget->setContents(lines);
99     connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
100     connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
101     connect(buf, SIGNAL(topicSet(QString)), this, SLOT(setTopic(QString)));
102     //connect(buf, SIGNAL(ownNickSet(QString)), this, SLOT(setOwnNick(QString)));
103     ui.stack->addWidget(chatWidget);
104     chatWidgets.insert(buf, chatWidget);
105     chatWidget->setFocusProxy(ui.inputLine);
106   } else chatWidget = chatWidgets[buf];
107   ui.inputLine->show(); ui.topicBar->show();
108   ui.stack->setCurrentWidget(chatWidget);
109   ui.inputLine->setFocus();
110   currentBuffer = buf;
111   
112 }
113 */
114
115 void MainWidget::userInput(const QString &input) {
116   if(!currentBufferInfo.isValid()) return;
117   QStringList lines = input.split('\n', QString::SkipEmptyParts);
118   foreach(QString msg, lines) {
119     if(msg.isEmpty()) continue;
120     emit userInput(currentBufferInfo, msg);
121   }
122   ui.inputLine->clear();
123 }