1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
24 #include "actioncollection.h"
25 #include "buffermodel.h"
26 #include "bufferviewmanager.h"
27 #include "channellistdlg.h"
28 #include "chatlinemodel.h"
29 #include "chatmonitorfilter.h"
30 #include "chatmonitorview.h"
33 #include "clientbacklogmanager.h"
34 #include "coreinfodlg.h"
35 #include "coreconnectdlg.h"
36 #include "iconloader.h"
37 #include "inputwidget.h"
38 #include "irclistmodel.h"
39 #include "jumpkeyhandler.h"
40 #include "msgprocessorstatuswidget.h"
41 #include "nicklistwidget.h"
42 #include "qtuiapplication.h"
43 #include "qtuimessageprocessor.h"
44 #include "qtuisettings.h"
45 #include "sessionsettings.h"
46 #include "settingsdlg.h"
47 #include "settingspagedlg.h"
48 #include "topicwidget.h"
49 #include "verticaldock.h"
52 # include "desktopnotificationbackend.h"
54 #include "systraynotificationbackend.h"
55 #include "taskbarnotificationbackend.h"
57 #include "settingspages/aliasessettingspage.h"
58 #include "settingspages/appearancesettingspage.h"
59 #include "settingspages/bufferviewsettingspage.h"
60 #include "settingspages/colorsettingspage.h"
61 #include "settingspages/fontssettingspage.h"
62 #include "settingspages/generalsettingspage.h"
63 #include "settingspages/highlightsettingspage.h"
64 #include "settingspages/identitiessettingspage.h"
65 #include "settingspages/networkssettingspage.h"
66 #include "settingspages/notificationssettingspage.h"
68 MainWin::MainWin(QWidget *parent)
69 : QMainWindow(parent),
70 coreLagLabel(new QLabel()),
71 sslLabel(new QLabel()),
72 msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
74 _trayIcon(new QSystemTrayIcon(this)),
75 _actionCollection(new ActionCollection(this))
77 QtUiSettings uiSettings;
78 QString style = uiSettings.value("Style", QString("")).toString();
80 QApplication::setStyle(style);
83 setWindowTitle("Quassel IRC");
84 setWindowIconText("Quassel IRC");
87 QtUi::actionCollection()->addAssociatedWidget(this);
89 statusBar()->showMessage(tr("Waiting for core..."));
91 installEventFilter(new JumpKeyHandler(this));
93 QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
94 QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
96 QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
99 QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
100 connect(app, SIGNAL(saveStateToSession(const QString&)), this, SLOT(saveStateToSession(const QString&)));
101 connect(app, SIGNAL(saveStateToSessionSettings(SessionSettings&)), this, SLOT(saveStateToSessionSettings(SessionSettings&)));
104 void MainWin::init() {
106 if(s.value("MainWinSize").isValid())
107 resize(s.value("MainWinSize").toSize());
109 resize(QSize(800, 500));
111 connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(saveLayout()));
113 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkCreated(NetworkId)));
114 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
118 statusBar()->showMessage(tr("Not connected to core."));
121 setDockNestingEnabled(true);
123 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
124 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
125 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
126 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
139 // restore mainwin state
140 restoreState(s.value("MainWinState").toByteArray());
142 // restore locked state of docks
143 ui.actionLockDockPositions->setChecked(s.value("LockDocks", false).toBool());
145 setDisconnectedState(); // Disable menus and stuff
146 showCoreConnectionDlg(true); // autoconnect if appropriate
148 // attach the BufferWidget to the BufferModel and the default selection
149 ui.bufferWidget->setModel(Client::bufferModel());
150 ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
151 ui.menuViews->addAction(QtUi::actionCollection()->action("toggleSearchBar"));
153 _titleSetter.setModel(Client::bufferModel());
154 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
157 MainWin::~MainWin() {
159 s.setValue("MainWinSize", size());
160 s.setValue("MainWinPos", pos());
161 s.setValue("MainWinState", saveState());
164 void MainWin::updateIcon() {
166 if(Client::isConnected())
167 icon = DesktopIcon("quassel", IconLoader::SizeEnormous);
169 icon = DesktopIcon("quassel_disconnected", IconLoader::SizeEnormous);
171 qApp->setWindowIcon(icon);
172 systemTrayIcon()->setIcon(icon);
175 void MainWin::setupActions() {
176 // TODO don't get these from *.ui anymore... we shouldn't need one
177 ui.actionQuit->setIcon(SmallIcon("application-exit"));
178 ui.actionSettingsDlg->setIcon(SmallIcon("configure"));
179 ui.actionManageViews->setIcon(SmallIcon("view-tree"));
180 ui.actionManageViews2->setIcon(SmallIcon("view-tree"));
181 ui.actionAboutQt->setIcon(SmallIcon("qt"));
182 ui.actionAboutQuassel->setIcon(SmallIcon("quassel"));
183 ui.actionConnectCore->setIcon(SmallIcon("network-connect"));
184 ui.actionDisconnectCore->setIcon(SmallIcon("network-disconnect"));
185 ui.actionCoreInfo->setIcon(SmallIcon("help-about"));
188 void MainWin::setupMenus() {
189 connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
190 connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
191 connect(ui.actionCoreInfo, SIGNAL(triggered()), this, SLOT(showCoreInfoDlg()));
192 connect(ui.actionQuit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()));
193 connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
194 connect(ui.actionAboutQuassel, SIGNAL(triggered()), this, SLOT(showAboutDlg()));
195 connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
198 void MainWin::setupViews() {
202 void MainWin::addBufferView(int bufferViewConfigId) {
203 addBufferView(Client::bufferViewManager()->bufferViewConfig(bufferViewConfigId));
206 void MainWin::addBufferView(BufferViewConfig *config) {
207 BufferViewDock *dock;
209 dock = new BufferViewDock(config, this);
211 dock = new BufferViewDock(this);
213 //create the view and initialize it's filter
214 BufferView *view = new BufferView(dock);
215 view->setFilteredModel(Client::bufferModel(), config);
218 connect(&view->showChannelList, SIGNAL(triggered()), this, SLOT(showChannelList()));
220 Client::bufferModel()->synchronizeView(view);
222 dock->setWidget(view);
225 addDockWidget(Qt::LeftDockWidgetArea, dock);
226 ui.menuBufferViews->addAction(dock->toggleViewAction());
228 _netViews.append(dock);
231 void MainWin::removeBufferView(int bufferViewConfigId) {
233 BufferViewDock *dock;
234 foreach(QAction *action, ui.menuBufferViews->actions()) {
235 actionData = action->data();
236 if(!actionData.isValid())
239 dock = qobject_cast<BufferViewDock *>(action->parent());
240 if(dock && actionData.toInt() == bufferViewConfigId) {
241 removeAction(action);
247 void MainWin::on_actionEditNetworks_triggered() {
248 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
252 void MainWin::on_actionManageViews_triggered() {
253 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
257 void MainWin::on_actionLockDockPositions_toggled(bool lock) {
258 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
259 foreach(VerticalDock *dock, docks) {
260 dock->showTitle(!lock);
262 QtUiSettings().setValue("LockDocks", lock);
265 void MainWin::setupNickWidget() {
267 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
268 nickDock->setObjectName("NickDock");
269 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
271 nickListWidget = new NickListWidget(nickDock);
272 nickDock->setWidget(nickListWidget);
274 addDockWidget(Qt::RightDockWidgetArea, nickDock);
275 ui.menuViews->addAction(nickDock->toggleViewAction());
276 // See NickListDock::NickListDock();
277 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
279 // attach the NickListWidget to the BufferModel and the default selection
280 nickListWidget->setModel(Client::bufferModel());
281 nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
284 void MainWin::setupChatMonitor() {
285 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
286 dock->setObjectName("ChatMonitorDock");
288 ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
289 ChatMonitorView *chatView = new ChatMonitorView(filter, this);
291 dock->setWidget(chatView);
294 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
295 ui.menuViews->addAction(dock->toggleViewAction());
298 void MainWin::setupInputWidget() {
299 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
300 dock->setObjectName("InputDock");
302 InputWidget *inputWidget = new InputWidget(dock);
303 dock->setWidget(inputWidget);
305 addDockWidget(Qt::BottomDockWidgetArea, dock);
307 ui.menuViews->addAction(dock->toggleViewAction());
309 inputWidget->setModel(Client::bufferModel());
310 inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
312 ui.bufferWidget->setFocusProxy(inputWidget);
315 void MainWin::setupTopicWidget() {
316 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
317 dock->setObjectName("TopicDock");
318 TopicWidget *topicwidget = new TopicWidget(dock);
320 dock->setWidget(topicwidget);
322 topicwidget->setModel(Client::bufferModel());
323 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
325 addDockWidget(Qt::TopDockWidgetArea, dock);
327 ui.menuViews->addAction(dock->toggleViewAction());
330 void MainWin::setupStatusBar() {
331 // MessageProcessor progress
332 statusBar()->addPermanentWidget(msgProcessorStatusWidget);
333 connect(Client::messageProcessor(), SIGNAL(progressUpdated(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
336 updateLagIndicator(0);
337 statusBar()->addPermanentWidget(coreLagLabel);
338 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
341 connect(Client::instance(), SIGNAL(securedConnection()), this, SLOT(securedConnection()));
342 sslLabel->setPixmap(QPixmap());
343 statusBar()->addPermanentWidget(sslLabel);
345 ui.menuViews->addSeparator();
346 QAction *showStatusbar = ui.menuViews->addAction(tr("Statusbar"));
347 showStatusbar->setCheckable(true);
349 UiSettings uiSettings;
351 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
352 showStatusbar->setChecked(enabled);
353 enabled ? statusBar()->show() : statusBar()->hide();
355 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
356 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
359 void MainWin::saveStatusBarStatus(bool enabled) {
360 UiSettings uiSettings;
361 uiSettings.setValue("ShowStatusBar", enabled);
364 void MainWin::setupSystray() {
365 connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
366 SLOT(messagesInserted(const QModelIndex &, int, int)));
368 systrayMenu = new QMenu(this);
369 systrayMenu->addAction(ui.actionAboutQuassel);
370 systrayMenu->addSeparator();
371 systrayMenu->addAction(ui.actionConnectCore);
372 systrayMenu->addAction(ui.actionDisconnectCore);
373 systrayMenu->addSeparator();
374 systrayMenu->addAction(ui.actionQuit);
376 systemTrayIcon()->setContextMenu(systrayMenu);
379 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
380 systemTrayIcon()->show();
384 connect(systemTrayIcon(), SIGNAL(activated( QSystemTrayIcon::ActivationReason )),
385 this, SLOT(systrayActivated( QSystemTrayIcon::ActivationReason )));
390 void MainWin::changeEvent(QEvent *event) {
391 if(event->type() == QEvent::WindowStateChange) {
392 if(windowState() & Qt::WindowMinimized) {
394 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
402 void MainWin::connectedToCore() {
403 Q_CHECK_PTR(Client::bufferViewManager());
404 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
405 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
406 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
408 Client::backlogManager()->requestInitialBacklog();
412 void MainWin::setConnectedState() {
413 //ui.menuCore->setEnabled(true);
414 ui.actionConnectCore->setEnabled(false);
415 ui.actionDisconnectCore->setEnabled(true);
416 ui.actionCoreInfo->setEnabled(true);
417 ui.menuViews->setEnabled(true);
418 ui.bufferWidget->show();
419 statusBar()->showMessage(tr("Connected to core."));
420 if(sslLabel->width() == 0)
421 sslLabel->setPixmap(SmallIcon("security-low"));
425 void MainWin::loadLayout() {
427 int accountId = Client::currentCoreAccount().toInt();
428 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
431 void MainWin::saveLayout() {
433 int accountId = Client::currentCoreAccount().toInt();
434 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
437 void MainWin::updateLagIndicator(int lag) {
438 coreLagLabel->setText(QString(tr("Core Lag: %1 msec")).arg(lag));
442 void MainWin::securedConnection() {
443 // todo: make status bar entry
444 sslLabel->setPixmap(SmallIcon("security-high"));
447 void MainWin::disconnectedFromCore() {
448 // save core specific layout and remove bufferviews;
451 BufferViewDock *dock;
452 foreach(QAction *action, ui.menuBufferViews->actions()) {
453 actionData = action->data();
454 if(!actionData.isValid())
457 dock = qobject_cast<BufferViewDock *>(action->parent());
458 if(dock && actionData.toInt() != -1) {
459 removeAction(action);
464 restoreState(s.value("MainWinState").toByteArray());
465 setDisconnectedState();
468 void MainWin::setDisconnectedState() {
469 //ui.menuCore->setEnabled(false);
470 ui.actionConnectCore->setEnabled(true);
471 ui.actionDisconnectCore->setEnabled(false);
472 ui.actionCoreInfo->setEnabled(false);
473 ui.menuViews->setEnabled(false);
474 ui.bufferWidget->hide();
475 statusBar()->showMessage(tr("Not connected to core."));
476 sslLabel->setPixmap(QPixmap());
480 void MainWin::showCoreConnectionDlg(bool autoConnect) {
481 CoreConnectDlg(autoConnect, this).exec();
484 void MainWin::showChannelList(NetworkId netId) {
485 ChannelListDlg *channelListDlg = new ChannelListDlg();
487 if(!netId.isValid()) {
488 QAction *action = qobject_cast<QAction *>(sender());
490 netId = action->data().value<NetworkId>();
493 channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
494 channelListDlg->setNetwork(netId);
495 channelListDlg->show();
498 void MainWin::showCoreInfoDlg() {
499 CoreInfoDlg(this).exec();
502 void MainWin::showSettingsDlg() {
503 SettingsDlg *dlg = new SettingsDlg();
505 //Category: Appearance
506 dlg->registerSettingsPage(new ColorSettingsPage(dlg));
507 dlg->registerSettingsPage(new FontsSettingsPage(dlg));
508 dlg->registerSettingsPage(new AppearanceSettingsPage(dlg)); //General
509 //Category: Behaviour
510 dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
511 dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
512 dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
513 dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
515 dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
516 dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
517 dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
522 void MainWin::showAboutDlg() {
523 AboutDlg(this).exec();
526 void MainWin::closeEvent(QCloseEvent *event) {
528 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
536 void MainWin::systrayActivated( QSystemTrayIcon::ActivationReason activationReason) {
537 if(activationReason == QSystemTrayIcon::Trigger) {
542 void MainWin::toggleVisibility() {
543 if(isHidden() /*|| !isActiveWindow()*/) {
554 // setFocus(); //Qt::ActiveWindowFocusReason
557 if(systemTrayIcon()->isSystemTrayAvailable ()) {
560 if(!systemTrayIcon()->isVisible()) {
561 systemTrayIcon()->show();
569 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
572 if(QApplication::activeWindow() != 0)
575 for(int i = start; i <= end; i++) {
576 QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
578 qDebug() << "MainWin::messagesInserted(): Invalid model index!";
581 Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
582 if(flags.testFlag(Message::Backlog)) continue;
583 flags |= Message::Backlog; // we only want to trigger a highlight once!
584 Client::messageModel()->setData(idx, (int)flags, ChatLineModel::FlagsRole);
586 BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
587 BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
589 if(flags & Message::Highlight || bufType == BufferInfo::QueryBuffer) {
590 QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
591 QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
592 QString contents = idx.data(ChatLineModel::DisplayRole).toString();
593 QtUi::invokeNotification(bufId, sender, contents);
598 bool MainWin::event(QEvent *event) {
599 if(event->type() == QEvent::WindowActivate)
600 QtUi::closeNotifications();
601 return QMainWindow::event(event);
604 void MainWin::clientNetworkCreated(NetworkId id) {
605 const Network *net = Client::network(id);
606 QAction *act = new QAction(net->networkName(), this);
607 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
608 act->setData(QVariant::fromValue<NetworkId>(id));
609 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
610 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
612 QAction *beforeAction = 0;
613 foreach(QAction *action, ui.menuNetworks->actions()) {
614 if(action->isSeparator()) {
615 beforeAction = action;
618 if(net->networkName().localeAwareCompare(action->text()) < 0) {
619 beforeAction = action;
623 Q_CHECK_PTR(beforeAction);
624 ui.menuNetworks->insertAction(beforeAction, act);
627 void MainWin::clientNetworkUpdated() {
628 const Network *net = qobject_cast<const Network *>(sender());
632 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
636 action->setText(net->networkName());
638 switch(net->connectionState()) {
639 case Network::Initialized:
640 action->setIcon(SmallIcon("network-connect"));
642 case Network::Disconnected:
643 action->setIcon(SmallIcon("network-disconnect"));
646 action->setIcon(SmallIcon("network-wired"));
650 void MainWin::clientNetworkRemoved(NetworkId id) {
651 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
655 action->deleteLater();
658 void MainWin::connectOrDisconnectFromNet() {
659 QAction *act = qobject_cast<QAction *>(sender());
661 const Network *net = Client::network(act->data().value<NetworkId>());
663 if(net->connectionState() == Network::Disconnected) net->requestConnect();
664 else net->requestDisconnect();
667 void MainWin::on_actionDebugNetworkModel_triggered(bool) {
668 QTreeView *view = new QTreeView;
669 view->setAttribute(Qt::WA_DeleteOnClose);
670 view->setWindowTitle("Debug NetworkModel View");
671 view->setModel(Client::networkModel());
672 view->setColumnWidth(0, 250);
673 view->setColumnWidth(1, 250);
674 view->setColumnWidth(2, 80);
675 view->resize(610, 300);
679 void MainWin::saveStateToSession(const QString &sessionId) {
681 SessionSettings s(sessionId);
683 s.setValue("MainWinSize", size());
684 s.setValue("MainWinPos", pos());
685 s.setValue("MainWinState", saveState());
688 void MainWin::saveStateToSessionSettings(SessionSettings & s)
690 s.setValue("MainWinSize", size());
691 s.setValue("MainWinPos", pos());
692 s.setValue("MainWinState", saveState());