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 ***************************************************************************/
23 #include "chatwidget.h"
24 #include "bufferview.h"
25 #include "bufferviewconfig.h"
26 #include "bufferviewfilter.h"
27 #include "bufferviewmanager.h"
28 #include "channellistdlg.h"
30 #include "clientbacklogmanager.h"
31 #include "coreinfodlg.h"
32 #include "coreconnectdlg.h"
33 #include "networkmodel.h"
34 #include "buffermodel.h"
35 #include "nicklistwidget.h"
36 #include "settingsdlg.h"
37 #include "settingspagedlg.h"
38 #include "signalproxy.h"
39 #include "topicwidget.h"
40 #include "inputwidget.h"
41 #include "irclistmodel.h"
42 #include "verticaldock.h"
43 #include "uisettings.h"
45 #include "qtuisettings.h"
46 #include "jumpkeyhandler.h"
48 #include "uisettings.h"
50 #include "selectionmodelsynchronizer.h"
51 #include "mappedselectionmodel.h"
53 #include "settingspages/aliasessettingspage.h"
54 #include "settingspages/appearancesettingspage.h"
55 #include "settingspages/bufferviewsettingspage.h"
56 #include "settingspages/colorsettingspage.h"
57 #include "settingspages/fontssettingspage.h"
58 #include "settingspages/generalsettingspage.h"
59 #include "settingspages/highlightsettingspage.h"
60 #include "settingspages/identitiessettingspage.h"
61 #include "settingspages/networkssettingspage.h"
64 #include "debugconsole.h"
66 #include "qtuistyle.h"
71 MainWin::MainWin(QtUi *_gui, QWidget *parent)
72 : QMainWindow(parent),
74 coreLagLabel(new QLabel()),
75 sslLabel(new QLabel()),
77 systray(new QSystemTrayIcon(this)),
78 activeTrayIcon(":/icons/quassel-icon-active.png"),
79 onlineTrayIcon(":/icons/quassel-icon.png"),
80 offlineTrayIcon(":/icons/quassel-icon-offline.png"),
81 trayIconActive(false),
82 timer(new QTimer(this)),
83 channelListDlg(new ChannelListDlg(this)),
84 settingsDlg(new SettingsDlg(this)),
85 debugConsole(new DebugConsole(this))
87 UiSettings uiSettings;
88 loadTranslation(uiSettings.value("Locale", QLocale::system()).value<QLocale>());
90 QString style = uiSettings.value("Style", QString("")).toString();
92 QApplication::setStyle(style);
96 setWindowTitle("Quassel IRC");
97 setWindowIcon(offlineTrayIcon);
98 qApp->setWindowIcon(offlineTrayIcon);
99 systray->setIcon(offlineTrayIcon);
100 setWindowIconText("Quassel IRC");
102 statusBar()->showMessage(tr("Waiting for core..."));
104 installEventFilter(new JumpKeyHandler(this));
108 void MainWin::init() {
110 if(s.value("MainWinSize").isValid())
111 resize(s.value("MainWinSize").toSize());
113 resize(QSize(800, 500));
115 Client::signalProxy()->attachSignal(this, SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)));
117 connect(QApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(saveLayout()));
119 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), this, SLOT(clientNetworkCreated(NetworkId)));
120 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), this, SLOT(clientNetworkRemoved(NetworkId)));
124 statusBar()->showMessage(tr("Not connected to core."));
127 setDockNestingEnabled(true);
129 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
130 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
131 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
132 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
146 // restore mainwin state
147 restoreState(s.value("MainWinState").toByteArray());
149 // restore locked state of docks
150 ui.actionLockDockPositions->setChecked(s.value("LockDocks", false).toBool());
153 setDisconnectedState(); // Disable menus and stuff
154 showCoreConnectionDlg(true); // autoconnect if appropriate
156 // attach the BufferWidget to the BufferModel and the default selection
157 ui.bufferWidget->setModel(Client::bufferModel());
158 ui.bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
160 _titleSetter.setModel(Client::bufferModel());
161 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
164 MainWin::~MainWin() {
166 s.setValue("MainWinSize", size());
167 s.setValue("MainWinPos", pos());
168 s.setValue("MainWinState", saveState());
171 void MainWin::setupMenus() {
172 connect(ui.actionConnectCore, SIGNAL(triggered()), this, SLOT(showCoreConnectionDlg()));
173 connect(ui.actionDisconnectCore, SIGNAL(triggered()), Client::instance(), SLOT(disconnectFromCore()));
174 connect(ui.actionCoreInfo, SIGNAL(triggered()), this, SLOT(showCoreInfoDlg()));
175 connect(ui.actionQuit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()));
176 connect(ui.actionSettingsDlg, SIGNAL(triggered()), this, SLOT(showSettingsDlg()));
177 // connect(ui.actionDebug_Console, SIGNAL(triggered()), this, SLOT(showDebugConsole()));
178 connect(ui.actionAboutQuassel, SIGNAL(triggered()), this, SLOT(showAboutDlg()));
179 connect(ui.actionAboutQt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));
182 void MainWin::setupViews() {
186 void MainWin::addBufferView(int bufferViewConfigId) {
187 addBufferView(Client::bufferViewManager()->bufferViewConfig(bufferViewConfigId));
190 void MainWin::addBufferView(BufferViewConfig *config) {
191 BufferViewDock *dock;
193 dock = new BufferViewDock(config, this);
195 dock = new BufferViewDock(this);
197 //create the view and initialize it's filter
198 BufferView *view = new BufferView(dock);
199 view->setFilteredModel(Client::bufferModel(), config);
202 connect(&view->showChannelList, SIGNAL(triggered()), this, SLOT(showChannelList()));
204 Client::bufferModel()->synchronizeView(view);
206 dock->setWidget(view);
209 addDockWidget(Qt::LeftDockWidgetArea, dock);
210 ui.menuBufferViews->addAction(dock->toggleViewAction());
212 _netViews.append(dock);
215 void MainWin::removeBufferView(int bufferViewConfigId) {
217 BufferViewDock *dock;
218 foreach(QAction *action, ui.menuBufferViews->actions()) {
219 actionData = action->data();
220 if(!actionData.isValid())
223 dock = qobject_cast<BufferViewDock *>(action->parent());
224 if(dock && actionData.toInt() == bufferViewConfigId) {
225 removeAction(action);
231 void MainWin::setupSettingsDlg() {
232 //Category: Appearance
233 settingsDlg->registerSettingsPage(new ColorSettingsPage(settingsDlg));
234 settingsDlg->registerSettingsPage(new FontsSettingsPage(settingsDlg));
235 settingsDlg->registerSettingsPage(new AppearanceSettingsPage(settingsDlg)); //General
236 //Category: Behaviour
237 settingsDlg->registerSettingsPage(new GeneralSettingsPage(settingsDlg));
238 settingsDlg->registerSettingsPage(new HighlightSettingsPage(settingsDlg));
239 settingsDlg->registerSettingsPage(new AliasesSettingsPage(settingsDlg));
241 settingsDlg->registerSettingsPage(new IdentitiesSettingsPage(settingsDlg));
242 settingsDlg->registerSettingsPage(new NetworksSettingsPage(settingsDlg));
243 settingsDlg->registerSettingsPage(new BufferViewSettingsPage(settingsDlg));
246 void MainWin::on_actionEditNetworks_triggered() {
247 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
251 void MainWin::on_actionManageViews_triggered() {
252 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
256 void MainWin::on_actionLockDockPositions_toggled(bool lock) {
257 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
258 foreach(VerticalDock *dock, docks) {
259 dock->showTitle(!lock);
261 QtUiSettings().setValue("LockDocks", lock);
264 void MainWin::setupNickWidget() {
266 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
267 nickDock->setObjectName("NickDock");
268 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
270 nickListWidget = new NickListWidget(nickDock);
271 nickDock->setWidget(nickListWidget);
273 addDockWidget(Qt::RightDockWidgetArea, nickDock);
274 ui.menuViews->addAction(nickDock->toggleViewAction());
275 // See NickListDock::NickListDock();
276 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
278 // attach the NickListWidget to the BufferModel and the default selection
279 nickListWidget->setModel(Client::bufferModel());
280 nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
283 void MainWin::setupChatMonitor() {
285 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
286 dock->setObjectName("ChatMonitorDock");
288 ChatWidget *chatWidget = new ChatWidget(0, this);
290 dock->setWidget(chatWidget);
293 Buffer *buf = Client::monitorBuffer();
297 chatWidget->setContents(buf->contents());
298 connect(buf, SIGNAL(msgAppended(AbstractUiMsg *)), chatWidget, SLOT(appendMsg(AbstractUiMsg *)));
299 connect(buf, SIGNAL(msgPrepended(AbstractUiMsg *)), chatWidget, SLOT(prependMsg(AbstractUiMsg *)));
301 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
302 ui.menuViews->addAction(dock->toggleViewAction());
306 void MainWin::setupInputWidget() {
307 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
308 dock->setObjectName("InputDock");
310 InputWidget *inputWidget = new InputWidget(dock);
311 dock->setWidget(inputWidget);
313 addDockWidget(Qt::BottomDockWidgetArea, dock);
315 ui.menuViews->addAction(dock->toggleViewAction());
317 inputWidget->setModel(Client::bufferModel());
318 inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
320 ui.bufferWidget->setFocusProxy(inputWidget);
323 void MainWin::setupTopicWidget() {
324 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
325 dock->setObjectName("TopicDock");
326 TopicWidget *topicwidget = new TopicWidget(dock);
327 connect(topicwidget, SIGNAL(topicChanged(const QString &)), this, SLOT(changeTopic(const QString &)));
329 dock->setWidget(topicwidget);
331 topicwidget->setModel(Client::bufferModel());
332 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
334 addDockWidget(Qt::TopDockWidgetArea, dock);
336 ui.menuViews->addAction(dock->toggleViewAction());
339 void MainWin::setupStatusBar() {
341 updateLagIndicator(0);
342 statusBar()->addPermanentWidget(coreLagLabel);
343 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
346 connect(Client::instance(), SIGNAL(securedConnection()), this, SLOT(securedConnection()));
347 sslLabel->setPixmap(QPixmap());
348 statusBar()->addPermanentWidget(sslLabel);
350 ui.menuViews->addSeparator();
351 QAction *showStatusbar = ui.menuViews->addAction(tr("Statusbar"));
352 showStatusbar->setCheckable(true);
354 UiSettings uiSettings;
356 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
357 showStatusbar->setChecked(enabled);
358 enabled ? statusBar()->show() : statusBar()->hide();
360 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
361 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
364 void MainWin::saveStatusBarStatus(bool enabled) {
365 UiSettings uiSettings;
366 uiSettings.setValue("ShowStatusBar", enabled);
369 void MainWin::setupSystray() {
370 connect(timer, SIGNAL(timeout()), this, SLOT(makeTrayIconBlink()));
371 connect(Client::instance(), SIGNAL(messageReceived(const Message &)), this, SLOT(receiveMessage(const Message &)));
373 systrayMenu = new QMenu(this);
374 systrayMenu->addAction(ui.actionAboutQuassel);
375 systrayMenu->addSeparator();
376 systrayMenu->addAction(ui.actionConnectCore);
377 systrayMenu->addAction(ui.actionDisconnectCore);
378 systrayMenu->addSeparator();
379 systrayMenu->addAction(ui.actionQuit);
381 systray->setContextMenu(systrayMenu);
384 if(s.value("UseSystemTrayIcon", QVariant(true)).toBool()) {
389 connect(systray, SIGNAL(activated( QSystemTrayIcon::ActivationReason )),
390 this, SLOT(systrayActivated( QSystemTrayIcon::ActivationReason )));
395 void MainWin::changeEvent(QEvent *event) {
396 if(event->type() == QEvent::WindowStateChange) {
397 if(windowState() & Qt::WindowMinimized) {
399 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
407 // FIXME this should be made prettier...
408 void MainWin::changeTopic(const QString &topic) {
409 BufferId id = ui.bufferWidget->currentBuffer();
410 if(!id.isValid()) return;
411 Buffer *buffer = Client::buffer(id);
412 if(buffer) Client::userInput(buffer->bufferInfo(), QString("/topic %1").arg(topic));
415 void MainWin::connectedToCore() {
416 Q_CHECK_PTR(Client::bufferViewManager());
417 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
418 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
419 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
421 foreach(BufferInfo id, Client::allBufferInfos()) {
422 Client::backlogManager()->requestBacklog(id.bufferId(), 500, -1);
427 void MainWin::setConnectedState() {
428 //ui.menuCore->setEnabled(true);
429 ui.actionConnectCore->setEnabled(false);
430 ui.actionDisconnectCore->setEnabled(true);
431 ui.actionCoreInfo->setEnabled(true);
432 ui.menuViews->setEnabled(true);
433 ui.bufferWidget->show();
434 statusBar()->showMessage(tr("Connected to core."));
435 setWindowIcon(onlineTrayIcon);
436 qApp->setWindowIcon(onlineTrayIcon);
437 systray->setIcon(onlineTrayIcon);
438 if(sslLabel->width() == 0)
439 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/no-ssl")));
442 void MainWin::loadLayout() {
444 int accountId = Client::currentCoreAccount().toInt();
445 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
448 void MainWin::saveLayout() {
450 int accountId = Client::currentCoreAccount().toInt();
451 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
454 void MainWin::updateLagIndicator(int lag) {
455 coreLagLabel->setText(QString("Core Lag: %1 msec").arg(lag));
459 void MainWin::securedConnection() {
460 // todo: make status bar entry
461 sslLabel->setPixmap(QPixmap::fromImage(QImage(":/16x16/status/ssl")));
464 void MainWin::disconnectedFromCore() {
465 // save core specific layout and remove bufferviews;
468 BufferViewDock *dock;
469 foreach(QAction *action, ui.menuBufferViews->actions()) {
470 actionData = action->data();
471 if(!actionData.isValid())
474 dock = qobject_cast<BufferViewDock *>(action->parent());
475 if(dock && actionData.toInt() != -1) {
476 removeAction(action);
481 restoreState(s.value("MainWinState").toByteArray());
482 setDisconnectedState();
485 void MainWin::setDisconnectedState() {
486 //ui.menuCore->setEnabled(false);
487 ui.actionConnectCore->setEnabled(true);
488 ui.actionDisconnectCore->setEnabled(false);
489 ui.actionCoreInfo->setEnabled(false);
490 ui.menuViews->setEnabled(false);
491 ui.bufferWidget->hide();
492 statusBar()->showMessage(tr("Not connected to core."));
493 setWindowIcon(offlineTrayIcon);
494 qApp->setWindowIcon(offlineTrayIcon);
495 systray->setIcon(offlineTrayIcon);
496 sslLabel->setPixmap(QPixmap());
499 void MainWin::showCoreConnectionDlg(bool autoConnect) {
500 coreConnectDlg = new CoreConnectDlg(this, autoConnect);
501 connect(coreConnectDlg, SIGNAL(finished(int)), this, SLOT(coreConnectionDlgFinished(int)));
502 coreConnectDlg->setModal(true);
503 coreConnectDlg->show();
506 void MainWin::coreConnectionDlgFinished(int /*code*/) {
507 coreConnectDlg->close();
511 void MainWin::showChannelList(NetworkId netId) {
512 if(!netId.isValid()) {
513 QAction *action = qobject_cast<QAction *>(sender());
515 netId = action->data().value<NetworkId>();
517 channelListDlg->setNetwork(netId);
518 channelListDlg->show();
521 void MainWin::showCoreInfoDlg() {
522 CoreInfoDlg dlg(this);
526 void MainWin::showSettingsDlg() {
530 void MainWin::showDebugConsole() {
531 debugConsole->show();
534 void MainWin::showAboutDlg() {
539 void MainWin::closeEvent(QCloseEvent *event) {
541 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
549 void MainWin::systrayActivated( QSystemTrayIcon::ActivationReason activationReason) {
550 if(activationReason == QSystemTrayIcon::Trigger) {
555 void MainWin::toggleVisibility() {
556 if(isHidden() /*|| !isActiveWindow()*/) {
567 // setFocus(); //Qt::ActiveWindowFocusReason
570 if(systray->isSystemTrayAvailable ()) {
573 if(!systray->isVisible()) {
582 void MainWin::receiveMessage(const Message &msg) {
583 if(QApplication::activeWindow() != 0)
586 if(msg.flags() & Message::Highlight || msg.bufferInfo().type() == BufferInfo::QueryBuffer) {
587 QString title = msg.bufferInfo().bufferName();;
588 if(msg.bufferInfo().type() != BufferInfo::QueryBuffer) {
589 QString sender = msg.sender();
590 int i = sender.indexOf("!");
592 sender = sender.left(i);
593 title += QString(" - %1").arg(sender);
596 UiSettings uiSettings;
599 if(uiSettings.value("DisplayPopupMessages", QVariant(true)).toBool()) {
600 // FIXME don't invoke style engine for this!
601 QString text = QtUi::style()->styleString(Message::mircToInternal(msg.contents())).plainText;
602 displayTrayIconMessage(title, text);
603 sendDesktopNotification(title, text);
606 if(uiSettings.value("AnimateTrayIcon", QVariant(true)).toBool()) {
607 QApplication::alert(this);
608 setTrayIconActivity(true);
613 bool MainWin::event(QEvent *event) {
614 if(event->type() == QEvent::WindowActivate)
615 setTrayIconActivity(false);
616 return QMainWindow::event(event);
621 Using the notification-daemon from Freedesktop's Galago project
622 http://www.galago-project.org/specs/notification/0.9/x408.html#command-notify
624 void MainWin::sendDesktopNotification(const QString &title, const QString &message)
627 QMap<QString, QVariant> hints;
628 QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "", "Notify");
630 hints["x"] = 100; // Standard hint: x location for the popup to show up
631 hints["y"] = 100; // Standard hint: y location for the popup to show up
633 msg << "Quassel"; // Application name
634 msg << quint32(0); // ID of previous notification to replace
635 msg << ""; // Icon to display
636 msg << "Quassel: " + title; // Summary / Header of the message to display
637 msg << message; // Body of the message to display
638 msg << actions; // Actions from which the user may choose
639 msg << hints; // Hints to the server displaying the message
640 msg << qint32(10000); // Timeout in milliseconds
642 (void)QDBusConnection::sessionBus().call(msg); // Would return a message containing the id of this notification
646 void MainWin::displayTrayIconMessage(const QString &title, const QString &message) {
647 systray->showMessage(title, message);
650 void MainWin::setTrayIconActivity(bool active) {
652 if(!timer->isActive())
656 systray->setIcon(onlineTrayIcon);
660 void MainWin::makeTrayIconBlink() {
662 systray->setIcon(onlineTrayIcon);
663 trayIconActive = false;
665 systray->setIcon(activeTrayIcon);
666 trayIconActive = true;
670 void MainWin::clientNetworkCreated(NetworkId id) {
671 const Network *net = Client::network(id);
672 QAction *act = new QAction(net->networkName(), this);
673 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
674 act->setData(QVariant::fromValue<NetworkId>(id));
675 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
676 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
678 QAction *beforeAction = 0;
679 foreach(QAction *action, ui.menuNetworks->actions()) {
680 if(action->isSeparator()) {
681 beforeAction = action;
684 if(net->networkName().localeAwareCompare(action->text()) < 0) {
685 beforeAction = action;
689 Q_CHECK_PTR(beforeAction);
690 ui.menuNetworks->insertAction(beforeAction, act);
693 void MainWin::clientNetworkUpdated() {
694 const Network *net = qobject_cast<const Network *>(sender());
698 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
702 action->setText(net->networkName());
704 switch(net->connectionState()) {
705 case Network::Initialized:
706 action->setIcon(QIcon(":/16x16/actions/network-connect"));
708 case Network::Disconnected:
709 action->setIcon(QIcon(":/16x16/actions/network-disconnect"));
712 action->setIcon(QIcon(":/16x16/actions/gear"));
716 void MainWin::clientNetworkRemoved(NetworkId id) {
717 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
721 action->deleteLater();
724 void MainWin::connectOrDisconnectFromNet() {
725 QAction *act = qobject_cast<QAction *>(sender());
727 const Network *net = Client::network(act->data().value<NetworkId>());
729 if(net->connectionState() == Network::Disconnected) net->requestConnect();
730 else net->requestDisconnect();