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"
51 #include "desktopnotificationbackend.h"
52 #include "systraynotificationbackend.h"
53 #include "taskbarnotificationbackend.h"
55 #include "settingspages/aliasessettingspage.h"
56 #include "settingspages/appearancesettingspage.h"
57 #include "settingspages/bufferviewsettingspage.h"
58 #include "settingspages/colorsettingspage.h"
59 #include "settingspages/fontssettingspage.h"
60 #include "settingspages/generalsettingspage.h"
61 #include "settingspages/highlightsettingspage.h"
62 #include "settingspages/identitiessettingspage.h"
63 #include "settingspages/networkssettingspage.h"
64 #include "settingspages/notificationssettingspage.h"
66 MainWin::MainWin(QWidget *parent)
67 : QMainWindow(parent),
68 coreLagLabel(new QLabel()),
69 sslLabel(new QLabel()),
70 msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
72 _trayIcon(new QSystemTrayIcon(this)),
73 _actionCollection(new ActionCollection(this))
75 QtUiSettings uiSettings;
76 QString style = uiSettings.value("Style", QString("")).toString();
78 QApplication::setStyle(style);
81 setWindowTitle("Quassel IRC");
82 setWindowIconText("Quassel IRC");
85 QtUi::actionCollection()->addAssociatedWidget(this);
87 statusBar()->showMessage(tr("Waiting for core..."));
89 installEventFilter(new JumpKeyHandler(this));
91 QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
92 QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
94 QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
97 QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
98 connect(app, SIGNAL(saveStateToSession(const QString&)), this, SLOT(saveStateToSession(const QString&)));
99 connect(app, SIGNAL(saveStateToSessionSettings(SessionSettings&)), this, SLOT(saveStateToSessionSettings(SessionSettings&)));
102 void MainWin::init() {
104 if(s.value("MainWinSize").isValid())
105 resize(s.value("MainWinSize").toSize());
107 resize(QSize(800, 500));
109 connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(saveLayout()));
111 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkCreated(NetworkId)));
112 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
116 statusBar()->showMessage(tr("Not connected to core."));
119 setDockNestingEnabled(true);
121 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
122 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
123 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
124 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
137 // restore mainwin state
138 restoreState(s.value("MainWinState").toByteArray());
140 // restore locked state of docks
141 ui.actionLockDockPositions->setChecked(s.value("LockDocks", false).toBool());
143 setDisconnectedState(); // Disable menus and stuff
144 showCoreConnectionDlg(true); // autoconnect if appropriate
146 // attach the BufferWidget to the BufferModel and the default selection
147 ui.bufferWidget->setModel(Client::bufferModel());
148 ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
149 ui.menuViews->addAction(QtUi::actionCollection()->action("toggleSearchBar"));
151 _titleSetter.setModel(Client::bufferModel());
152 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
155 MainWin::~MainWin() {
157 s.setValue("MainWinSize", size());
158 s.setValue("MainWinPos", pos());
159 s.setValue("MainWinState", saveState());
162 void MainWin::updateIcon() {
164 if(Client::isConnected())
165 icon = DesktopIcon("quassel", IconLoader::SizeEnormous);
167 icon = DesktopIcon("quassel_disconnected", IconLoader::SizeEnormous);
169 qApp->setWindowIcon(icon);
170 systemTrayIcon()->setIcon(icon);
173 void MainWin::setupActions() {
174 // TODO don't get these from *.ui anymore... we shouldn't need one
175 ui.actionQuit->setIcon(SmallIcon("application-exit"));
176 ui.actionSettingsDlg->setIcon(SmallIcon("configure"));
177 ui.actionManageViews->setIcon(SmallIcon("view-tree"));
178 ui.actionManageViews2->setIcon(SmallIcon("view-tree"));
179 ui.actionAboutQt->setIcon(SmallIcon("qt"));
180 ui.actionAboutQuassel->setIcon(SmallIcon("quassel"));
181 ui.actionConnectCore->setIcon(SmallIcon("network-connect"));
182 ui.actionDisconnectCore->setIcon(SmallIcon("network-disconnect"));
183 ui.actionCoreInfo->setIcon(SmallIcon("help-about"));
186 void MainWin::setupMenus() {
187 connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
188 connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
189 connect(ui.actionCoreInfo, SIGNAL(triggered()), this, SLOT(showCoreInfoDlg()));
190 connect(ui.actionQuit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()));
191 connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
192 connect(ui.actionAboutQuassel, SIGNAL(triggered()), this, SLOT(showAboutDlg()));
193 connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
196 void MainWin::setupViews() {
200 void MainWin::addBufferView(int bufferViewConfigId) {
201 addBufferView(Client::bufferViewManager()->bufferViewConfig(bufferViewConfigId));
204 void MainWin::addBufferView(BufferViewConfig *config) {
205 BufferViewDock *dock;
207 dock = new BufferViewDock(config, this);
209 dock = new BufferViewDock(this);
211 //create the view and initialize it's filter
212 BufferView *view = new BufferView(dock);
213 view->setFilteredModel(Client::bufferModel(), config);
216 connect(&view->showChannelList, SIGNAL(triggered()), this, SLOT(showChannelList()));
218 Client::bufferModel()->synchronizeView(view);
220 dock->setWidget(view);
223 addDockWidget(Qt::LeftDockWidgetArea, dock);
224 ui.menuBufferViews->addAction(dock->toggleViewAction());
226 _netViews.append(dock);
229 void MainWin::removeBufferView(int bufferViewConfigId) {
231 BufferViewDock *dock;
232 foreach(QAction *action, ui.menuBufferViews->actions()) {
233 actionData = action->data();
234 if(!actionData.isValid())
237 dock = qobject_cast<BufferViewDock *>(action->parent());
238 if(dock && actionData.toInt() == bufferViewConfigId) {
239 removeAction(action);
245 void MainWin::on_actionEditNetworks_triggered() {
246 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
250 void MainWin::on_actionManageViews_triggered() {
251 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
255 void MainWin::on_actionLockDockPositions_toggled(bool lock) {
256 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
257 foreach(VerticalDock *dock, docks) {
258 dock->showTitle(!lock);
260 QtUiSettings().setValue("LockDocks", lock);
263 void MainWin::setupNickWidget() {
265 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
266 nickDock->setObjectName("NickDock");
267 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
269 nickListWidget = new NickListWidget(nickDock);
270 nickDock->setWidget(nickListWidget);
272 addDockWidget(Qt::RightDockWidgetArea, nickDock);
273 ui.menuViews->addAction(nickDock->toggleViewAction());
274 // See NickListDock::NickListDock();
275 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
277 // attach the NickListWidget to the BufferModel and the default selection
278 nickListWidget->setModel(Client::bufferModel());
279 nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
282 void MainWin::setupChatMonitor() {
283 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
284 dock->setObjectName("ChatMonitorDock");
286 ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
287 ChatMonitorView *chatView = new ChatMonitorView(filter, this);
289 dock->setWidget(chatView);
292 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
293 ui.menuViews->addAction(dock->toggleViewAction());
296 void MainWin::setupInputWidget() {
297 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
298 dock->setObjectName("InputDock");
300 InputWidget *inputWidget = new InputWidget(dock);
301 dock->setWidget(inputWidget);
303 addDockWidget(Qt::BottomDockWidgetArea, dock);
305 ui.menuViews->addAction(dock->toggleViewAction());
307 inputWidget->setModel(Client::bufferModel());
308 inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
310 ui.bufferWidget->setFocusProxy(inputWidget);
313 void MainWin::setupTopicWidget() {
314 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
315 dock->setObjectName("TopicDock");
316 TopicWidget *topicwidget = new TopicWidget(dock);
318 dock->setWidget(topicwidget);
320 topicwidget->setModel(Client::bufferModel());
321 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
323 addDockWidget(Qt::TopDockWidgetArea, dock);
325 ui.menuViews->addAction(dock->toggleViewAction());
328 void MainWin::setupStatusBar() {
329 // MessageProcessor progress
330 statusBar()->addPermanentWidget(msgProcessorStatusWidget);
331 connect(Client::messageProcessor(), SIGNAL(progressUpdated(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
334 updateLagIndicator(0);
335 statusBar()->addPermanentWidget(coreLagLabel);
336 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
339 connect(Client::instance(), SIGNAL(securedConnection()), this, SLOT(securedConnection()));
340 sslLabel->setPixmap(QPixmap());
341 statusBar()->addPermanentWidget(sslLabel);
343 ui.menuViews->addSeparator();
344 QAction *showStatusbar = ui.menuViews->addAction(tr("Statusbar"));
345 showStatusbar->setCheckable(true);
347 UiSettings uiSettings;
349 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
350 showStatusbar->setChecked(enabled);
351 enabled ? statusBar()->show() : statusBar()->hide();
353 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
354 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
357 void MainWin::saveStatusBarStatus(bool enabled) {
358 UiSettings uiSettings;
359 uiSettings.setValue("ShowStatusBar", enabled);
362 void MainWin::setupSystray() {
363 connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
364 SLOT(messagesInserted(const QModelIndex &, int, int)));
366 systrayMenu = new QMenu(this);
367 systrayMenu->addAction(ui.actionAboutQuassel);
368 systrayMenu->addSeparator();
369 systrayMenu->addAction(ui.actionConnectCore);
370 systrayMenu->addAction(ui.actionDisconnectCore);
371 systrayMenu->addSeparator();
372 systrayMenu->addAction(ui.actionQuit);
374 systemTrayIcon()->setContextMenu(systrayMenu);
377 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
378 systemTrayIcon()->show();
382 connect(systemTrayIcon(), SIGNAL(activated( QSystemTrayIcon::ActivationReason )),
383 this, SLOT(systrayActivated( QSystemTrayIcon::ActivationReason )));
388 void MainWin::changeEvent(QEvent *event) {
389 if(event->type() == QEvent::WindowStateChange) {
390 if(windowState() & Qt::WindowMinimized) {
392 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
400 void MainWin::connectedToCore() {
401 Q_CHECK_PTR(Client::bufferViewManager());
402 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
403 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
404 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
406 Client::backlogManager()->requestInitialBacklog();
410 void MainWin::setConnectedState() {
411 //ui.menuCore->setEnabled(true);
412 ui.actionConnectCore->setEnabled(false);
413 ui.actionDisconnectCore->setEnabled(true);
414 ui.actionCoreInfo->setEnabled(true);
415 ui.menuViews->setEnabled(true);
416 ui.bufferWidget->show();
417 statusBar()->showMessage(tr("Connected to core."));
418 if(sslLabel->width() == 0)
419 sslLabel->setPixmap(SmallIcon("security-low"));
423 void MainWin::loadLayout() {
425 int accountId = Client::currentCoreAccount().toInt();
426 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
429 void MainWin::saveLayout() {
431 int accountId = Client::currentCoreAccount().toInt();
432 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
435 void MainWin::updateLagIndicator(int lag) {
436 coreLagLabel->setText(QString(tr("Core Lag: %1 msec")).arg(lag));
440 void MainWin::securedConnection() {
441 // todo: make status bar entry
442 sslLabel->setPixmap(SmallIcon("security-high"));
445 void MainWin::disconnectedFromCore() {
446 // save core specific layout and remove bufferviews;
449 BufferViewDock *dock;
450 foreach(QAction *action, ui.menuBufferViews->actions()) {
451 actionData = action->data();
452 if(!actionData.isValid())
455 dock = qobject_cast<BufferViewDock *>(action->parent());
456 if(dock && actionData.toInt() != -1) {
457 removeAction(action);
462 restoreState(s.value("MainWinState").toByteArray());
463 setDisconnectedState();
466 void MainWin::setDisconnectedState() {
467 //ui.menuCore->setEnabled(false);
468 ui.actionConnectCore->setEnabled(true);
469 ui.actionDisconnectCore->setEnabled(false);
470 ui.actionCoreInfo->setEnabled(false);
471 ui.menuViews->setEnabled(false);
472 ui.bufferWidget->hide();
473 statusBar()->showMessage(tr("Not connected to core."));
474 sslLabel->setPixmap(QPixmap());
478 void MainWin::showCoreConnectionDlg(bool autoConnect) {
479 CoreConnectDlg(autoConnect, this).exec();
482 void MainWin::showChannelList(NetworkId netId) {
483 ChannelListDlg *channelListDlg = new ChannelListDlg();
485 if(!netId.isValid()) {
486 QAction *action = qobject_cast<QAction *>(sender());
488 netId = action->data().value<NetworkId>();
491 channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
492 channelListDlg->setNetwork(netId);
493 channelListDlg->show();
496 void MainWin::showCoreInfoDlg() {
497 CoreInfoDlg(this).exec();
500 void MainWin::showSettingsDlg() {
501 SettingsDlg *dlg = new SettingsDlg();
503 //Category: Appearance
504 dlg->registerSettingsPage(new ColorSettingsPage(dlg));
505 dlg->registerSettingsPage(new FontsSettingsPage(dlg));
506 dlg->registerSettingsPage(new AppearanceSettingsPage(dlg)); //General
507 //Category: Behaviour
508 dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
509 dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
510 dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
511 dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
513 dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
514 dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
515 dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
520 void MainWin::showAboutDlg() {
521 AboutDlg(this).exec();
524 void MainWin::closeEvent(QCloseEvent *event) {
526 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
534 void MainWin::systrayActivated( QSystemTrayIcon::ActivationReason activationReason) {
535 if(activationReason == QSystemTrayIcon::Trigger) {
540 void MainWin::toggleVisibility() {
541 if(isHidden() /*|| !isActiveWindow()*/) {
552 // setFocus(); //Qt::ActiveWindowFocusReason
555 if(systemTrayIcon()->isSystemTrayAvailable ()) {
558 if(!systemTrayIcon()->isVisible()) {
559 systemTrayIcon()->show();
567 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
570 if(QApplication::activeWindow() != 0)
573 for(int i = start; i <= end; i++) {
574 QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
576 qDebug() << "MainWin::messagesInserted(): Invalid model index!";
579 Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
580 if(flags.testFlag(Message::Backlog)) continue;
581 flags |= Message::Backlog; // we only want to trigger a highlight once!
582 Client::messageModel()->setData(idx, (int)flags, ChatLineModel::FlagsRole);
584 BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
585 BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
587 if(flags & Message::Highlight || bufType == BufferInfo::QueryBuffer) {
588 QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
589 QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
590 QString contents = idx.data(ChatLineModel::DisplayRole).toString();
591 QtUi::invokeNotification(bufId, sender, contents);
596 bool MainWin::event(QEvent *event) {
597 if(event->type() == QEvent::WindowActivate)
598 QtUi::closeNotifications();
599 return QMainWindow::event(event);
602 void MainWin::clientNetworkCreated(NetworkId id) {
603 const Network *net = Client::network(id);
604 QAction *act = new QAction(net->networkName(), this);
605 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
606 act->setData(QVariant::fromValue<NetworkId>(id));
607 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
608 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
610 QAction *beforeAction = 0;
611 foreach(QAction *action, ui.menuNetworks->actions()) {
612 if(action->isSeparator()) {
613 beforeAction = action;
616 if(net->networkName().localeAwareCompare(action->text()) < 0) {
617 beforeAction = action;
621 Q_CHECK_PTR(beforeAction);
622 ui.menuNetworks->insertAction(beforeAction, act);
625 void MainWin::clientNetworkUpdated() {
626 const Network *net = qobject_cast<const Network *>(sender());
630 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
634 action->setText(net->networkName());
636 switch(net->connectionState()) {
637 case Network::Initialized:
638 action->setIcon(SmallIcon("network-connect"));
640 case Network::Disconnected:
641 action->setIcon(SmallIcon("network-disconnect"));
644 action->setIcon(SmallIcon("network-wired"));
648 void MainWin::clientNetworkRemoved(NetworkId id) {
649 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
653 action->deleteLater();
656 void MainWin::connectOrDisconnectFromNet() {
657 QAction *act = qobject_cast<QAction *>(sender());
659 const Network *net = Client::network(act->data().value<NetworkId>());
661 if(net->connectionState() == Network::Disconnected) net->requestConnect();
662 else net->requestDisconnect();
665 void MainWin::on_actionDebugNetworkModel_triggered(bool) {
666 QTreeView *view = new QTreeView;
667 view->setAttribute(Qt::WA_DeleteOnClose);
668 view->setWindowTitle("Debug NetworkModel View");
669 view->setModel(Client::networkModel());
670 view->setColumnWidth(0, 250);
671 view->setColumnWidth(1, 250);
672 view->setColumnWidth(2, 80);
673 view->resize(610, 300);
677 void MainWin::saveStateToSession(const QString &sessionId) {
679 SessionSettings s(sessionId);
681 s.setValue("MainWinSize", size());
682 s.setValue("MainWinPos", pos());
683 s.setValue("MainWinState", saveState());
686 void MainWin::saveStateToSessionSettings(SessionSettings & s)
688 s.setValue("MainWinSize", size());
689 s.setValue("MainWinPos", pos());
690 s.setValue("MainWinState", saveState());