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