Fixed those nasty "Client::updateLastSeen(): Unknown buffer $bufferId" messages.
[quassel.git] / src / qtopia / qtopiamainwin.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 "qtopiamainwin.h"
22
23 #include "networkmodel.h"
24 #include "bufferviewwidget.h"
25 #include "nicklistwidget.h"
26 #include "chatline.h"
27 #include "coreconnectdlg.h"
28 #include "global.h"
29 #include "mainwidget.h"
30 #include "message.h"
31 #include "qtopiaui.h"
32 #include "signalproxy.h"
33
34 #include "ui_aboutdlg.h"
35
36 #include <Qtopia>
37 #include <QSoftMenuBar>
38
39 // This constructor is the first thing to be called for a Qtopia app, so we do the init stuff
40 // here (rather than in a main.cpp).
41 QtopiaMainWin::QtopiaMainWin(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) {
42   Global::registerMetaTypes();
43
44   Global::runMode = Global::ClientOnly;
45   Global::defaultPort = 4242;
46
47   QCoreApplication::setOrganizationDomain("quassel-irc.org");
48   QCoreApplication::setApplicationName("Quassel IRC");
49   QCoreApplication::setOrganizationName("Quassel IRC Team");
50
51   QtopiaUi *gui = new QtopiaUi(this);
52   Client::init(gui);
53
54   setWindowTitle("Quassel IRC");
55   setWindowIcon(QIcon(":/qirc-icon.png"));
56   setWindowIconText("Quassel IRC");
57
58   mainWidget = new MainWidget(this);
59   setCentralWidget(mainWidget);
60
61   NetworkModel *model = Client::networkModel();
62   connect(model, SIGNAL(bufferSelected(Buffer *)), this, SLOT(showBuffer(Buffer *)));
63
64   toolBar = new QToolBar(this);
65   toolBar->setIconSize(QSize(16, 16));
66   toolBar->setWindowTitle(tr("Show Toolbar"));
67   addToolBar(toolBar);
68
69   bufferViewWidget = new BufferViewWidget(this);
70   nickListWidget = new NickListWidget(this);
71
72   setupActions();
73
74   init();
75   //gui->init();
76
77 }
78
79 // at this point, client is fully initialized
80 void QtopiaMainWin::init() {
81   Client::signalProxy()->attachSignal(this, SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)));
82
83   showMaximized();
84   CoreConnectDlg *dlg = new CoreConnectDlg(this);
85   //setCentralWidget(dlg);
86   dlg->showMaximized();
87   dlg->exec();
88 }
89
90 QtopiaMainWin::~QtopiaMainWin() {
91
92
93 }
94
95 void QtopiaMainWin::closeEvent(QCloseEvent *event) {
96 #ifndef DEVELMODE
97   QMessageBox *box = new QMessageBox(QMessageBox::Question, tr("Quit Quassel IRC?"), tr("Do you really want to quit Quassel IRC?"),
98                                      QMessageBox::Cancel, this);
99   QAbstractButton *quit = box->addButton(tr("Quit"), QMessageBox::AcceptRole);
100   box->exec();
101   if(box->clickedButton() == quit) event->accept();
102   else event->ignore();
103   box->deleteLater();
104 #else
105   event->accept();
106 #endif
107 }
108
109 void QtopiaMainWin::setupActions() {
110   showBuffersAction = toolBar->addAction(QIcon(":icon/options-hide"), tr("Show Buffers"), this, SLOT(showBufferView()));  // FIXME provide real icon
111   showNicksAction = toolBar->addAction(QIcon(":icon/list"), tr("Show Nicks"), this, SLOT(showNickList()));
112   showNicksAction->setEnabled(false);
113
114   QMenu *menu = new QMenu(this);
115   menu->addAction(showBuffersAction);
116   menu->addAction(showNicksAction);
117   menu->addSeparator();
118   menu->addAction(toolBar->toggleViewAction());
119   menu->addSeparator();
120   menu->addAction(tr("About..."), this, SLOT(showAboutDlg()));
121
122   QSoftMenuBar::addMenuTo(this, menu);
123 }
124
125 void QtopiaMainWin::connectedToCore() {
126   foreach(BufferInfo id, Client::allBufferInfos()) {
127     emit requestBacklog(id, 100, -1);
128   }
129
130 #ifdef DEVELMODE
131   // FIXME just for testing: select first available buffer
132   if(Client::allBufferInfos().count() > 1) {
133     Buffer *b = Client::buffer(Client::allBufferInfos()[1]);
134     Client::networkModel()->selectBuffer(b);
135   }
136 #endif
137 }
138
139 void QtopiaMainWin::disconnectedFromCore() {
140
141
142 }
143
144 AbstractUiMsg *QtopiaMainWin::layoutMsg(const Message &msg) {
145   return new ChatLine(msg);
146   //return 0;
147 }
148
149 void QtopiaMainWin::showBuffer(Buffer *b) {
150   bufferViewWidget->hide();
151   mainWidget->setBuffer(b);
152   nickListWidget->setBuffer(b);
153   showNicksAction->setEnabled(b && b->bufferType() == Buffer::ChannelType);
154
155 }
156
157 void QtopiaMainWin::showBufferView() {
158   bufferViewWidget->showMaximized();
159 }
160
161 void QtopiaMainWin::showNickList() {
162   nickListWidget->showMaximized();
163 }
164
165 void QtopiaMainWin::showAboutDlg() {
166   QDialog *dlg = new QDialog(this);
167   dlg->setAttribute(Qt::WA_DeleteOnClose);
168   Ui::AboutDlg ui;
169   ui.setupUi(dlg);
170   dlg->showMaximized();
171 }
172