1 /***************************************************************************
2 * Copyright (C) 2005-09 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 <KActionCollection>
27 # include <KShortcutsDialog>
28 # include <KStatusBar>
37 #include "awaylogfilter.h"
38 #include "awaylogview.h"
40 #include "actioncollection.h"
41 #include "bufferhotlistfilter.h"
42 #include "buffermodel.h"
43 #include "bufferview.h"
44 #include "bufferviewoverlay.h"
45 #include "bufferviewoverlayfilter.h"
46 #include "bufferwidget.h"
47 #include "channellistdlg.h"
48 #include "chatlinemodel.h"
49 #include "chatmonitorfilter.h"
50 #include "chatmonitorview.h"
53 #include "clientsyncer.h"
54 #include "clientbacklogmanager.h"
55 #include "clientbufferviewconfig.h"
56 #include "clientbufferviewmanager.h"
57 #include "clientignorelistmanager.h"
58 #include "coreinfodlg.h"
59 #include "coreconnectdlg.h"
60 #include "contextmenuactionprovider.h"
61 #include "debugbufferviewoverlay.h"
62 #include "debuglogwidget.h"
63 #include "debugmessagemodelfilter.h"
64 #include "flatproxymodel.h"
65 #include "iconloader.h"
66 #include "inputwidget.h"
67 #include "irclistmodel.h"
68 #include "ircconnectionwizard.h"
69 #include "jumpkeyhandler.h"
70 #include "msgprocessorstatuswidget.h"
71 #include "nicklistwidget.h"
72 #include "qtuiapplication.h"
73 #include "qtuimessageprocessor.h"
74 #include "qtuisettings.h"
75 #include "qtuistyle.h"
76 #include "settingsdlg.h"
77 #include "settingspagedlg.h"
78 #include "systemtray.h"
79 #include "toolbaractionprovider.h"
80 #include "topicwidget.h"
81 #include "verticaldock.h"
85 # include "desktopnotificationbackend.h"
88 # include "phononnotificationbackend.h"
90 # include "systraynotificationbackend.h"
91 # include "taskbarnotificationbackend.h"
93 # include "knotificationbackend.h"
96 #include "settingspages/aliasessettingspage.h"
97 #include "settingspages/appearancesettingspage.h"
98 #include "settingspages/backlogsettingspage.h"
99 #include "settingspages/bufferviewsettingspage.h"
100 #include "settingspages/chatmonitorsettingspage.h"
101 #include "settingspages/chatviewsettingspage.h"
102 #include "settingspages/connectionsettingspage.h"
103 #include "settingspages/generalsettingspage.h"
104 #include "settingspages/highlightsettingspage.h"
105 #include "settingspages/identitiessettingspage.h"
106 #include "settingspages/ignorelistsettingspage.h"
107 #include "settingspages/inputwidgetsettingspage.h"
108 #include "settingspages/itemviewsettingspage.h"
109 #include "settingspages/networkssettingspage.h"
110 #include "settingspages/notificationssettingspage.h"
111 #include "settingspages/topicwidgetsettingspage.h"
113 MainWin::MainWin(QWidget *parent)
115 : KMainWindow(parent),
116 _kHelpMenu(new KHelpMenu(this, KGlobal::mainComponent().aboutData())),
118 : QMainWindow(parent),
120 coreLagLabel(new QLabel()),
121 sslLabel(new QLabel()),
122 msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
130 QtUiSettings uiSettings;
131 QString style = uiSettings.value("Style", QString()).toString();
132 if(!style.isEmpty()) {
133 QApplication::setStyle(style);
136 QApplication::setQuitOnLastWindowClosed(false);
138 setWindowTitle("Quassel IRC");
139 setWindowIconText("Quassel IRC");
142 installEventFilter(new JumpKeyHandler(this));
145 void MainWin::init() {
146 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
147 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
148 connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
149 SLOT(messagesInserted(const QModelIndex &, int, int)));
150 connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
153 setDockNestingEnabled(true);
154 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
155 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
156 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
157 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
159 // Order is sometimes important
174 QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
175 QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
177 QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
180 QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
184 QtUi::registerNotificationBackend(new KNotificationBackend(this));
185 #endif /* HAVE_KDE */
187 connect(bufferWidget(), SIGNAL(currentChanged(BufferId)), SLOT(currentBufferChanged(BufferId)));
189 setDisconnectedState(); // Disable menus and stuff
192 setAutoSaveSettings();
195 // restore mainwin state
197 restoreStateFromSettings(s);
199 // restore locked state of docks
200 QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
202 if(Quassel::runMode() != Quassel::Monolithic) {
203 showCoreConnectionDlg(true); // autoconnect if appropriate
209 MainWin::~MainWin() {
213 void MainWin::quit() {
215 saveStateToSettings(s);
217 QApplication::quit();
220 void MainWin::saveStateToSettings(UiSettings &s) {
221 s.setValue("MainWinSize", _normalSize);
222 s.setValue("MainWinPos", _normalPos);
223 s.setValue("MainWinState", saveState());
224 s.setValue("MainWinGeometry", saveGeometry());
225 s.setValue("MainWinMinimized", isMinimized());
226 s.setValue("MainWinMaximized", isMaximized());
227 s.setValue("MainWinHidden", !isVisible());
230 saveAutoSaveSettings();
234 void MainWin::restoreStateFromSettings(UiSettings &s) {
235 _normalSize = s.value("MainWinSize", size()).toSize();
236 _normalPos = s.value("MainWinPos", pos()).toPoint();
237 bool maximized = s.value("MainWinMaximized", false).toBool();
240 restoreGeometry(s.value("MainWinGeometry").toByteArray());
243 // restoreGeometry() fails if the windows was maximized, so we resize and position explicitly
248 restoreState(s.value("MainWinState").toByteArray());
254 if(s.value("MainWinHidden").toBool())
256 else if(s.value("MainWinMinimized").toBool())
264 void MainWin::updateIcon() {
266 const int size = 128;
272 if(Client::isConnected())
273 icon = DesktopIcon("quassel", size);
275 icon = DesktopIcon("quassel_inactive", size);
277 qApp->setWindowIcon(icon);
280 void MainWin::setupActions() {
281 ActionCollection *coll = QtUi::actionCollection("General");
283 coll->addAction("ConnectCore", new Action(SmallIcon("network-connect"), tr("&Connect to Core..."), coll,
284 this, SLOT(showCoreConnectionDlg())));
285 coll->addAction("DisconnectCore", new Action(SmallIcon("network-disconnect"), tr("&Disconnect from Core"), coll,
286 Client::instance(), SLOT(disconnectFromCore())));
287 coll->addAction("CoreInfo", new Action(SmallIcon("help-about"), tr("Core &Info..."), coll,
288 this, SLOT(showCoreInfoDlg())));
289 coll->addAction("ConfigureNetworks", new Action(SmallIcon("configure"), tr("Configure &Networks..."), coll,
290 this, SLOT(on_actionConfigureNetworks_triggered())));
291 coll->addAction("Quit", new Action(SmallIcon("application-exit"), tr("&Quit"), coll,
292 this, SLOT(quit()), tr("Ctrl+Q")));
295 coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Chat Lists..."), coll,
296 this, SLOT(on_actionConfigureViews_triggered())));
298 QAction *lockAct = coll->addAction("LockLayout", new Action(tr("&Lock Layout"), coll));
299 lockAct->setCheckable(true);
300 connect(lockAct, SIGNAL(toggled(bool)), SLOT(on_actionLockLayout_toggled(bool)));
302 coll->addAction("ToggleSearchBar", new Action(SmallIcon("edit-find"), tr("Show &Search Bar"), coll,
303 0, 0, QKeySequence::Find))->setCheckable(true);
304 coll->addAction("ShowAwayLog", new Action(tr("Show Away Log"), coll,
305 this, SLOT(showAwayLog())));
306 coll->addAction("ToggleMenuBar", new Action(SmallIcon("show-menu"), tr("Show &Menubar"), coll,
307 0, 0, tr("Ctrl+M")))->setCheckable(true);
309 coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
310 0, 0))->setCheckable(true);
313 coll->addAction("ConfigureQuassel", new Action(SmallIcon("configure"), tr("&Configure Quassel..."), coll,
314 this, SLOT(showSettingsDlg()), tr("F7")));
317 coll->addAction("AboutQuassel", new Action(SmallIcon("quassel"), tr("&About Quassel"), coll,
318 this, SLOT(showAboutDlg())));
319 coll->addAction("AboutQt", new Action(QIcon(":/pics/qt-logo.png"), tr("About &Qt"), coll,
320 qApp, SLOT(aboutQt())));
321 coll->addAction("DebugNetworkModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &NetworkModel"), coll,
322 this, SLOT(on_actionDebugNetworkModel_triggered())));
323 coll->addAction("DebugBufferViewOverlay", new Action(SmallIcon("tools-report-bug"), tr("Debug &BufferViewOverlay"), coll,
324 this, SLOT(on_actionDebugBufferViewOverlay_triggered())));
325 coll->addAction("DebugMessageModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &MessageModel"), coll,
326 this, SLOT(on_actionDebugMessageModel_triggered())));
327 coll->addAction("DebugHotList", new Action(SmallIcon("tools-report-bug"), tr("Debug &HotList"), coll,
328 this, SLOT(on_actionDebugHotList_triggered())));
329 coll->addAction("DebugLog", new Action(SmallIcon("tools-report-bug"), tr("Debug &Log"), coll,
330 this, SLOT(on_actionDebugLog_triggered())));
331 coll->addAction("ReloadStyle", new Action(SmallIcon("view-refresh"), tr("Reload Stylesheet"), coll,
332 QtUi::style(), SLOT(reload()), QKeySequence::Refresh));
335 coll->addAction("JumpHotBuffer", new Action(tr("Jump to hot chat"), coll,
336 this, SLOT(on_jumpHotBuffer_triggered()), QKeySequence(Qt::META + Qt::Key_A)));
339 void MainWin::setupMenus() {
340 ActionCollection *coll = QtUi::actionCollection("General");
342 _fileMenu = menuBar()->addMenu(tr("&File"));
344 static const QStringList coreActions = QStringList()
345 << "ConnectCore" << "DisconnectCore" << "CoreInfo";
348 foreach(QString actionName, coreActions) {
349 coreAction = coll->action(actionName);
350 _fileMenu->addAction(coreAction);
351 flagRemoteCoreOnly(coreAction);
353 flagRemoteCoreOnly(_fileMenu->addSeparator());
355 _networksMenu = _fileMenu->addMenu(tr("&Networks"));
356 _networksMenu->addAction(coll->action("ConfigureNetworks"));
357 _networksMenu->addSeparator();
358 _fileMenu->addSeparator();
359 _fileMenu->addAction(coll->action("Quit"));
361 _viewMenu = menuBar()->addMenu(tr("&View"));
362 _bufferViewsMenu = _viewMenu->addMenu(tr("&Chat Lists"));
363 _bufferViewsMenu->addAction(coll->action("ConfigureBufferViews"));
364 _toolbarMenu = _viewMenu->addMenu(tr("&Toolbars"));
365 _viewMenu->addSeparator();
367 _viewMenu->addAction(coll->action("ToggleMenuBar"));
368 _viewMenu->addAction(coll->action("ToggleStatusBar"));
369 _viewMenu->addAction(coll->action("ToggleSearchBar"));
371 coreAction = coll->action("ShowAwayLog");
372 flagRemoteCoreOnly(coreAction);
373 _viewMenu->addAction(coreAction);
375 _viewMenu->addSeparator();
376 _viewMenu->addAction(coll->action("LockLayout"));
378 _settingsMenu = menuBar()->addMenu(tr("&Settings"));
380 _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
381 _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
383 _settingsMenu->addAction(coll->action("ConfigureQuassel"));
385 _helpMenu = menuBar()->addMenu(tr("&Help"));
386 _helpMenu->addAction(coll->action("AboutQuassel"));
388 _helpMenu->addAction(coll->action("AboutQt"));
390 _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
392 _helpMenu->addSeparator();
393 _helpDebugMenu = _helpMenu->addMenu(SmallIcon("tools-report-bug"), tr("Debug"));
394 _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
395 _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
396 _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
397 _helpDebugMenu->addAction(coll->action("DebugHotList"));
398 _helpDebugMenu->addAction(coll->action("DebugLog"));
399 _helpDebugMenu->addSeparator();
400 _helpDebugMenu->addAction(coll->action("ReloadStyle"));
403 QAction *showMenuBar = QtUi::actionCollection("General")->action("ToggleMenuBar");
405 QtUiSettings uiSettings;
406 bool enabled = uiSettings.value("ShowMenuBar", QVariant(true)).toBool();
407 showMenuBar->setChecked(enabled);
408 enabled ? menuBar()->show() : menuBar()->hide();
410 connect(showMenuBar, SIGNAL(toggled(bool)), menuBar(), SLOT(setVisible(bool)));
411 connect(showMenuBar, SIGNAL(toggled(bool)), this, SLOT(saveMenuBarStatus(bool)));
414 void MainWin::setupBufferWidget() {
415 _bufferWidget = new BufferWidget(this);
416 _bufferWidget->setModel(Client::bufferModel());
417 _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
418 setCentralWidget(_bufferWidget);
421 void MainWin::addBufferView(int bufferViewConfigId) {
422 addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
425 void MainWin::addBufferView(ClientBufferViewConfig *config) {
429 config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
430 BufferViewDock *dock = new BufferViewDock(config, this);
432 //create the view and initialize it's filter
433 BufferView *view = new BufferView(dock);
434 view->setFilteredModel(Client::bufferModel(), config);
435 view->installEventFilter(_inputWidget); // for key presses
438 Client::bufferModel()->synchronizeView(view);
440 dock->setWidget(view);
443 addDockWidget(Qt::LeftDockWidgetArea, dock);
444 _bufferViewsMenu->addAction(dock->toggleViewAction());
446 connect(dock->toggleViewAction(), SIGNAL(toggled(bool)), this, SLOT(bufferViewToggled(bool)));
447 _bufferViews.append(dock);
450 void MainWin::removeBufferView(int bufferViewConfigId) {
452 BufferViewDock *dock;
453 foreach(QAction *action, _bufferViewsMenu->actions()) {
454 actionData = action->data();
455 if(!actionData.isValid())
458 dock = qobject_cast<BufferViewDock *>(action->parent());
459 if(dock && actionData.toInt() == bufferViewConfigId) {
460 removeAction(action);
466 void MainWin::bufferViewToggled(bool enabled) {
467 QAction *action = qobject_cast<QAction *>(sender());
469 BufferViewDock *dock = qobject_cast<BufferViewDock *>(action->parent());
472 Client::bufferViewOverlay()->addView(dock->bufferViewId());
473 BufferViewConfig *config = dock->config();
474 if(config && config->isInitialized()) {
475 BufferIdList buffers;
476 if(config->networkId().isValid()) {
477 foreach(BufferId bufferId, config->bufferList()) {
478 if(Client::networkModel()->networkId(bufferId) == config->networkId())
481 foreach(BufferId bufferId, config->temporarilyRemovedBuffers().toList()) {
482 if(Client::networkModel()->networkId(bufferId) == config->networkId())
486 buffers = BufferIdList::fromSet(config->bufferList().toSet() + config->temporarilyRemovedBuffers());
488 Client::backlogManager()->checkForBacklog(buffers);
491 Client::bufferViewOverlay()->removeView(dock->bufferViewId());
495 BufferView *MainWin::allBuffersView() const {
496 // "All Buffers" is always the first dock created
497 if(_bufferViews.count() > 0)
498 return _bufferViews[0]->bufferView();
502 void MainWin::showNotificationsDlg() {
503 SettingsPageDlg dlg(new NotificationsSettingsPage(this), this);
507 void MainWin::on_actionConfigureNetworks_triggered() {
508 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
512 void MainWin::on_actionConfigureViews_triggered() {
513 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
517 void MainWin::on_actionLockLayout_toggled(bool lock) {
518 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
519 foreach(VerticalDock *dock, docks) {
520 dock->showTitle(!lock);
522 if(Client::bufferViewManager()) {
523 foreach(ClientBufferViewConfig *config, Client::bufferViewManager()->clientBufferViewConfigs()) {
524 config->setLocked(lock);
527 QtUiSettings().setValue("LockLayout", lock);
530 void MainWin::setupNickWidget() {
532 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
533 nickDock->setObjectName("NickDock");
534 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
536 _nickListWidget = new NickListWidget(nickDock);
537 nickDock->setWidget(_nickListWidget);
539 addDockWidget(Qt::RightDockWidgetArea, nickDock);
540 _viewMenu->addAction(nickDock->toggleViewAction());
541 nickDock->toggleViewAction()->setText(tr("Show Nick List"));
543 // See NickListDock::NickListDock();
544 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
546 // attach the NickListWidget to the BufferModel and the default selection
547 _nickListWidget->setModel(Client::bufferModel());
548 _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
551 void MainWin::setupChatMonitor() {
552 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
553 dock->setObjectName("ChatMonitorDock");
555 ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
556 ChatMonitorView *chatView = new ChatMonitorView(filter, this);
558 dock->setWidget(chatView);
561 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
562 _viewMenu->addAction(dock->toggleViewAction());
563 dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
566 void MainWin::setupInputWidget() {
567 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
568 dock->setObjectName("InputDock");
570 _inputWidget = new InputWidget(dock);
571 dock->setWidget(_inputWidget);
573 addDockWidget(Qt::BottomDockWidgetArea, dock);
575 _viewMenu->addAction(dock->toggleViewAction());
576 dock->toggleViewAction()->setText(tr("Show Input Line"));
578 _inputWidget->setModel(Client::bufferModel());
579 _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
581 _bufferWidget->setFocusProxy(_inputWidget);
583 _inputWidget->inputLine()->installEventFilter(_bufferWidget);
586 void MainWin::setupTopicWidget() {
587 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
588 dock->setObjectName("TopicDock");
589 TopicWidget *topicwidget = new TopicWidget(dock);
591 dock->setWidget(topicwidget);
593 topicwidget->setModel(Client::bufferModel());
594 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
596 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
598 _viewMenu->addAction(dock->toggleViewAction());
599 dock->toggleViewAction()->setText(tr("Show Topic Line"));
602 void MainWin::setupTitleSetter() {
603 _titleSetter.setModel(Client::bufferModel());
604 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
607 void MainWin::setupStatusBar() {
608 // MessageProcessor progress
609 statusBar()->addPermanentWidget(msgProcessorStatusWidget);
612 updateLagIndicator();
613 statusBar()->addPermanentWidget(coreLagLabel);
614 coreLagLabel->hide();
615 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
618 sslLabel->setPixmap(QPixmap());
619 statusBar()->addPermanentWidget(sslLabel);
622 QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
624 QtUiSettings uiSettings;
626 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
627 showStatusbar->setChecked(enabled);
628 enabled ? statusBar()->show() : statusBar()->hide();
630 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
631 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
634 void MainWin::setupHotList() {
635 FlatProxyModel *flatProxy = new FlatProxyModel(this);
636 flatProxy->setSourceModel(Client::bufferModel());
637 _bufferHotList = new BufferHotListFilter(flatProxy);
640 void MainWin::saveMenuBarStatus(bool enabled) {
641 QtUiSettings uiSettings;
642 uiSettings.setValue("ShowMenuBar", enabled);
645 void MainWin::saveStatusBarStatus(bool enabled) {
646 QtUiSettings uiSettings;
647 uiSettings.setValue("ShowStatusBar", enabled);
650 void MainWin::setupSystray() {
651 _systemTray = new SystemTray(this);
654 void MainWin::setupToolBars() {
655 connect(_bufferWidget, SIGNAL(currentChanged(QModelIndex)),
656 QtUi::toolBarActionProvider(), SLOT(currentBufferChanged(QModelIndex)));
657 connect(_nickListWidget, SIGNAL(nickSelectionChanged(QModelIndexList)),
658 QtUi::toolBarActionProvider(), SLOT(nickSelectionChanged(QModelIndexList)));
661 setUnifiedTitleAndToolBarOnMac(true);
665 _mainToolBar = new KToolBar("MainToolBar", this, Qt::TopToolBarArea, false, true, true);
667 _mainToolBar = new QToolBar(this);
668 _mainToolBar->setObjectName("MainToolBar");
670 _mainToolBar->setWindowTitle(tr("Main Toolbar"));
671 addToolBar(_mainToolBar);
673 QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
674 _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
677 void MainWin::connectedToCore() {
678 Q_CHECK_PTR(Client::bufferViewManager());
679 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
680 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
681 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
686 void MainWin::setConnectedState() {
687 ActionCollection *coll = QtUi::actionCollection("General");
689 coll->action("ConnectCore")->setEnabled(false);
690 coll->action("DisconnectCore")->setEnabled(true);
691 coll->action("CoreInfo")->setEnabled(true);
693 foreach(QAction *action, _fileMenu->actions()) {
694 if(isRemoteCoreOnly(action))
695 action->setVisible(!Client::internalCore());
698 disconnect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
699 disconnect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
700 disconnect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
701 if(!Client::internalCore()) {
702 connect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
703 connect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
704 connect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
707 // _viewMenu->setEnabled(true);
708 if(!Client::internalCore())
709 statusBar()->showMessage(tr("Connected to core."));
711 statusBar()->clearMessage();
713 if(Client::signalProxy()->isSecure()) {
714 sslLabel->setPixmap(SmallIcon("security-high"));
716 sslLabel->setPixmap(SmallIcon("security-low"));
719 sslLabel->setVisible(!Client::internalCore());
720 coreLagLabel->setVisible(!Client::internalCore());
722 systemTray()->setState(SystemTray::Active);
724 if(Client::networkIds().isEmpty()) {
725 IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
730 void MainWin::loadLayout() {
732 int accountId = Client::currentCoreAccount().toInt();
733 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
736 void MainWin::saveLayout() {
738 int accountId = Client::currentCoreAccount().toInt();
739 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
742 void MainWin::updateLagIndicator(int lag) {
743 QString text = tr("Core Lag: %1");
745 text = text.arg('-');
747 text = text.arg("%1 msec").arg(lag);
748 coreLagLabel->setText(text);
751 void MainWin::disconnectedFromCore() {
752 // save core specific layout and remove bufferviews;
755 BufferViewDock *dock;
756 foreach(QAction *action, _bufferViewsMenu->actions()) {
757 actionData = action->data();
758 if(!actionData.isValid())
761 dock = qobject_cast<BufferViewDock *>(action->parent());
762 if(dock && actionData.toInt() != -1) {
763 removeAction(action);
768 restoreState(s.value("MainWinState").toByteArray());
769 setDisconnectedState();
772 void MainWin::setDisconnectedState() {
773 ActionCollection *coll = QtUi::actionCollection("General");
774 //ui.menuCore->setEnabled(false);
775 coll->action("ConnectCore")->setEnabled(true);
776 coll->action("DisconnectCore")->setEnabled(false);
777 coll->action("CoreInfo")->setEnabled(false);
778 //_viewMenu->setEnabled(false);
779 statusBar()->showMessage(tr("Not connected to core."));
780 sslLabel->setPixmap(QPixmap());
782 updateLagIndicator();
783 coreLagLabel->hide();
784 if(msgProcessorStatusWidget)
785 msgProcessorStatusWidget->setProgress(0, 0);
787 systemTray()->setState(SystemTray::Inactive);
790 void MainWin::startInternalCore() {
791 ClientSyncer *syncer = new ClientSyncer();
792 Client::registerClientSyncer(syncer);
793 connect(syncer, SIGNAL(syncFinished()), syncer, SLOT(deleteLater()), Qt::QueuedConnection);
794 syncer->useInternalCore();
797 void MainWin::showCoreConnectionDlg(bool autoConnect) {
798 CoreConnectDlg(autoConnect, this).exec();
801 void MainWin::showChannelList(NetworkId netId) {
802 ChannelListDlg *channelListDlg = new ChannelListDlg();
804 if(!netId.isValid()) {
805 QAction *action = qobject_cast<QAction *>(sender());
807 netId = action->data().value<NetworkId>();
810 channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
811 channelListDlg->setNetwork(netId);
812 channelListDlg->show();
815 void MainWin::showCoreInfoDlg() {
816 CoreInfoDlg(this).exec();
819 void MainWin::showAwayLog() {
822 AwayLogFilter *filter = new AwayLogFilter(Client::messageModel());
823 _awayLog = new AwayLogView(filter, 0);
824 filter->setParent(_awayLog);
825 connect(_awayLog, SIGNAL(destroyed()), this, SLOT(awayLogDestroyed()));
826 _awayLog->setAttribute(Qt::WA_DeleteOnClose);
830 void MainWin::awayLogDestroyed() {
834 void MainWin::showSettingsDlg() {
835 SettingsDlg *dlg = new SettingsDlg();
837 //Category: Interface
838 dlg->registerSettingsPage(new AppearanceSettingsPage(dlg));
839 dlg->registerSettingsPage(new ChatViewSettingsPage(dlg));
840 dlg->registerSettingsPage(new ItemViewSettingsPage(dlg));
841 dlg->registerSettingsPage(new InputWidgetSettingsPage(dlg));
842 dlg->registerSettingsPage(new TopicWidgetSettingsPage(dlg));
843 dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
844 dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
845 dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
846 dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
847 dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
850 dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
851 dlg->registerSettingsPage(new ConnectionSettingsPage(dlg));
852 dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
853 dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
854 dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
855 dlg->registerSettingsPage(new IgnoreListSettingsPage(dlg));
860 void MainWin::showAboutDlg() {
861 AboutDlg(this).exec();
865 void MainWin::showShortcutsDlg() {
866 KShortcutsDialog::configure(QtUi::actionCollection("General"), KShortcutsEditor::LetterShortcutsDisallowed);
870 /********************************************************************************************************/
872 bool MainWin::event(QEvent *event) {
873 if(event->type() == QEvent::WindowActivate) {
874 BufferId buffer = Client::bufferModel()->currentBuffer();
876 QtUi::closeNotifications(buffer);
878 return QMainWindow::event(event);
881 void MainWin::moveEvent(QMoveEvent *event) {
882 if(!(windowState() & Qt::WindowMaximized))
883 _normalPos = event->pos();
885 QMainWindow::moveEvent(event);
888 void MainWin::resizeEvent(QResizeEvent *event) {
889 if(!(windowState() & Qt::WindowMaximized))
890 _normalSize = event->size();
892 QMainWindow::resizeEvent(event);
895 void MainWin::closeEvent(QCloseEvent *event) {
897 QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
899 if(!app->isAboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
908 void MainWin::changeEvent(QEvent *event) {
910 if(event->type() == QEvent::ActivationChange)
911 dwTickCount = GetTickCount(); // needed for toggleMinimizedToTray()
914 QMainWindow::changeEvent(event);
917 void MainWin::hideToTray() {
918 if(!systemTray()->isSystemTrayAvailable()) {
919 qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
923 systemTray()->setIconVisible();
926 void MainWin::toggleMinimizedToTray() {
928 // the problem is that we lose focus when the systray icon is activated
929 // and we don't know the former active window
930 // therefore we watch for activation event and use our stopwatch :)
931 // courtesy: KSystemTrayIcon
932 if(GetTickCount() - dwTickCount >= 300)
933 // we weren't active in the last 300ms -> activate
940 if(!isVisible() || isMinimized())
949 void MainWin::forceActivated() {
951 // Bypass focus stealing prevention
952 QX11Info::setAppUserTime(QX11Info::appTime());
955 if(windowState() & Qt::WindowMinimized) {
957 setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
960 // this does not actually work on all platforms... and causes more evil than good
961 // move(frameGeometry().topLeft()); // avoid placement policies
967 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
970 bool hasFocus = QApplication::activeWindow() != 0;
972 for(int i = start; i <= end; i++) {
973 QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
975 qDebug() << "MainWin::messagesInserted(): Invalid model index!";
978 Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
979 if(flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
982 BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
983 BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
985 if(hasFocus && bufId == Client::bufferModel()->currentBuffer())
988 if((flags & Message::Highlight || bufType == BufferInfo::QueryBuffer)
989 && !(Client::ignoreListManager() && Client::ignoreListManager()->match(idx.data(MessageModel::MessageRole).value<Message>(),
990 Client::networkModel()->networkName(bufId))))
992 QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
993 QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
994 QString contents = idx.data(ChatLineModel::DisplayRole).toString();
995 AbstractNotificationBackend::NotificationType type;
997 if(bufType == BufferInfo::QueryBuffer && !hasFocus)
998 type = AbstractNotificationBackend::PrivMsg;
999 else if(bufType == BufferInfo::QueryBuffer && hasFocus)
1000 type = AbstractNotificationBackend::PrivMsgFocused;
1001 else if(flags & Message::Highlight && !hasFocus)
1002 type = AbstractNotificationBackend::Highlight;
1004 type = AbstractNotificationBackend::HighlightFocused;
1006 QtUi::invokeNotification(bufId, type, sender, contents);
1011 void MainWin::currentBufferChanged(BufferId buffer) {
1012 if(buffer.isValid())
1013 QtUi::closeNotifications(buffer);
1016 void MainWin::clientNetworkCreated(NetworkId id) {
1017 const Network *net = Client::network(id);
1018 QAction *act = new QAction(net->networkName(), this);
1019 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
1020 act->setData(QVariant::fromValue<NetworkId>(id));
1021 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
1022 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
1024 QAction *beforeAction = 0;
1025 foreach(QAction *action, _networksMenu->actions()) {
1026 if(!action->data().isValid()) // ignore stock actions
1028 if(net->networkName().localeAwareCompare(action->text()) < 0) {
1029 beforeAction = action;
1033 _networksMenu->insertAction(beforeAction, act);
1036 void MainWin::clientNetworkUpdated() {
1037 const Network *net = qobject_cast<const Network *>(sender());
1041 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1045 action->setText(net->networkName());
1047 switch(net->connectionState()) {
1048 case Network::Initialized:
1049 action->setIcon(SmallIcon("network-connect"));
1051 case Network::Disconnected:
1052 action->setIcon(SmallIcon("network-disconnect"));
1055 action->setIcon(SmallIcon("network-wired"));
1059 void MainWin::clientNetworkRemoved(NetworkId id) {
1060 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
1064 action->deleteLater();
1067 void MainWin::connectOrDisconnectFromNet() {
1068 QAction *act = qobject_cast<QAction *>(sender());
1070 const Network *net = Client::network(act->data().value<NetworkId>());
1072 if(net->connectionState() == Network::Disconnected) net->requestConnect();
1073 else net->requestDisconnect();
1076 void MainWin::on_jumpHotBuffer_triggered() {
1077 if(!_bufferHotList->rowCount())
1080 QModelIndex topIndex = _bufferHotList->index(0, 0);
1081 BufferId bufferId = _bufferHotList->data(topIndex, NetworkModel::BufferIdRole).value<BufferId>();
1082 Client::bufferModel()->switchToBuffer(bufferId);
1085 void MainWin::on_actionDebugNetworkModel_triggered() {
1086 QTreeView *view = new QTreeView;
1087 view->setAttribute(Qt::WA_DeleteOnClose);
1088 view->setWindowTitle("Debug NetworkModel View");
1089 view->setModel(Client::networkModel());
1090 view->setColumnWidth(0, 250);
1091 view->setColumnWidth(1, 250);
1092 view->setColumnWidth(2, 80);
1093 view->resize(610, 300);
1097 void MainWin::on_actionDebugHotList_triggered() {
1098 QTreeView *view = new QTreeView;
1099 view->setAttribute(Qt::WA_DeleteOnClose);
1100 view->setModel(_bufferHotList);
1104 void MainWin::on_actionDebugBufferViewOverlay_triggered() {
1105 DebugBufferViewOverlay *overlay = new DebugBufferViewOverlay(0);
1106 overlay->setAttribute(Qt::WA_DeleteOnClose);
1110 void MainWin::on_actionDebugMessageModel_triggered() {
1111 QTableView *view = new QTableView(0);
1112 DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
1113 filter->setSourceModel(Client::messageModel());
1114 view->setModel(filter);
1115 view->setAttribute(Qt::WA_DeleteOnClose, true);
1116 view->verticalHeader()->hide();
1117 view->horizontalHeader()->setStretchLastSection(true);
1121 void MainWin::on_actionDebugLog_triggered() {
1122 DebugLogWidget *logWidget = new DebugLogWidget(0);
1126 void MainWin::showStatusBarMessage(const QString &message) {
1127 statusBar()->showMessage(message, 10000);