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>
36 #include "awaylogfilter.h"
37 #include "awaylogview.h"
39 #include "actioncollection.h"
40 #include "buffermodel.h"
41 #include "bufferview.h"
42 #include "bufferviewoverlay.h"
43 #include "bufferviewoverlayfilter.h"
44 #include "bufferwidget.h"
45 #include "channellistdlg.h"
46 #include "chatlinemodel.h"
47 #include "chatmonitorfilter.h"
48 #include "chatmonitorview.h"
51 #include "clientsyncer.h"
52 #include "clientbacklogmanager.h"
53 #include "clientbufferviewconfig.h"
54 #include "clientbufferviewmanager.h"
55 #include "coreinfodlg.h"
56 #include "coreconnectdlg.h"
57 #include "contextmenuactionprovider.h"
58 #include "debuglogwidget.h"
59 #include "debugmessagemodelfilter.h"
60 #include "iconloader.h"
61 #include "inputwidget.h"
62 #include "inputline.h"
63 #include "irclistmodel.h"
64 #include "ircconnectionwizard.h"
65 #include "jumpkeyhandler.h"
66 #include "msgprocessorstatuswidget.h"
67 #include "nicklistwidget.h"
68 #include "qtuiapplication.h"
69 #include "qtuimessageprocessor.h"
70 #include "qtuisettings.h"
71 #include "settingsdlg.h"
72 #include "settingspagedlg.h"
73 #include "systemtray.h"
74 #include "toolbaractionprovider.h"
75 #include "topicwidget.h"
76 #include "verticaldock.h"
80 # include "desktopnotificationbackend.h"
83 # include "phononnotificationbackend.h"
85 # include "systraynotificationbackend.h"
86 # include "taskbarnotificationbackend.h"
88 # include "knotificationbackend.h"
91 #include "settingspages/aliasessettingspage.h"
92 #include "settingspages/appearancesettingspage.h"
93 #include "settingspages/backlogsettingspage.h"
94 #include "settingspages/bufferviewsettingspage.h"
95 #include "settingspages/chatmonitorsettingspage.h"
96 #include "settingspages/colorsettingspage.h"
97 #include "settingspages/generalsettingspage.h"
98 #include "settingspages/highlightsettingspage.h"
99 #include "settingspages/identitiessettingspage.h"
100 #include "settingspages/networkssettingspage.h"
101 #include "settingspages/notificationssettingspage.h"
103 MainWin::MainWin(QWidget *parent)
105 : KMainWindow(parent),
106 _kHelpMenu(new KHelpMenu(this, KGlobal::mainComponent().aboutData())),
108 : QMainWindow(parent),
110 coreLagLabel(new QLabel()),
111 sslLabel(new QLabel()),
112 msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
120 QtUiSettings uiSettings;
121 QString style = uiSettings.value("Style", QString()).toString();
122 if(!style.isEmpty()) {
123 QApplication::setStyle(style);
126 QApplication::setQuitOnLastWindowClosed(false);
128 setWindowTitle("Quassel IRC");
129 setWindowIconText("Quassel IRC");
132 installEventFilter(new JumpKeyHandler(this));
134 QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
135 connect(app, SIGNAL(saveStateToSession(const QString&)), SLOT(saveStateToSession(const QString&)));
136 connect(app, SIGNAL(saveStateToSessionSettings(SessionSettings&)), SLOT(saveStateToSessionSettings(SessionSettings&)));
139 void MainWin::init() {
141 if(s.value("MainWinSize").isValid())
142 resize(s.value("MainWinSize").toSize());
144 resize(QSize(800, 500));
146 connect(QApplication::instance(), SIGNAL(aboutToQuit()), SLOT(saveLayout()));
147 connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
148 connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
149 connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
150 SLOT(messagesInserted(const QModelIndex &, int, int)));
151 connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
154 setDockNestingEnabled(true);
155 setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
156 setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
157 setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
158 setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
160 // 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 // restore mainwin state
188 restoreState(s.value("MainWinState").toByteArray());
190 // restore locked state of docks
191 QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
193 setDisconnectedState(); // Disable menus and stuff
197 if(Quassel::runMode() != Quassel::Monolithic) {
198 showCoreConnectionDlg(true); // autoconnect if appropriate
204 MainWin::~MainWin() {
206 s.setValue("MainWinSize", size());
207 s.setValue("MainWinPos", pos());
208 s.setValue("MainWinState", saveState());
211 void MainWin::updateIcon() {
213 const int size = 128;
219 if(Client::isConnected())
220 icon = DesktopIcon("quassel", size);
222 icon = DesktopIcon("quassel_inactive", size);
224 qApp->setWindowIcon(icon);
227 void MainWin::setupActions() {
228 ActionCollection *coll = QtUi::actionCollection("General");
230 coll->addAction("ConnectCore", new Action(SmallIcon("network-connect"), tr("&Connect to Core..."), coll,
231 this, SLOT(showCoreConnectionDlg())));
232 coll->addAction("DisconnectCore", new Action(SmallIcon("network-disconnect"), tr("&Disconnect from Core"), coll,
233 Client::instance(), SLOT(disconnectFromCore())));
234 coll->addAction("CoreInfo", new Action(SmallIcon("help-about"), tr("Core &Info..."), coll,
235 this, SLOT(showCoreInfoDlg())));
236 coll->addAction("ConfigureNetworks", new Action(SmallIcon("configure"), tr("Configure &Networks..."), coll,
237 this, SLOT(on_actionConfigureNetworks_triggered())));
238 coll->addAction("Quit", new Action(SmallIcon("application-exit"), tr("&Quit"), coll,
239 qApp, SLOT(quit()), tr("Ctrl+Q")));
242 coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Buffer Views..."), coll,
243 this, SLOT(on_actionConfigureViews_triggered())));
245 QAction *lockAct = coll->addAction("LockLayout", new Action(tr("&Lock Layout"), coll));
246 lockAct->setCheckable(true);
247 connect(lockAct, SIGNAL(toggled(bool)), SLOT(on_actionLockLayout_toggled(bool)));
249 coll->addAction("ToggleSearchBar", new Action(SmallIcon("edit-find"), tr("Show &Search Bar"), coll,
250 0, 0, QKeySequence::Find))->setCheckable(true);
251 coll->addAction("ShowAwayLog", new Action(tr("Show Away Log"), coll,
252 this, SLOT(showAwayLog())));
253 coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
254 0, 0))->setCheckable(true);
257 coll->addAction("ConfigureQuassel", new Action(SmallIcon("configure"), tr("&Configure Quassel..."), coll,
258 this, SLOT(showSettingsDlg()), tr("F7")));
261 coll->addAction("AboutQuassel", new Action(SmallIcon("quassel"), tr("&About Quassel"), coll,
262 this, SLOT(showAboutDlg())));
263 coll->addAction("AboutQt", new Action(QIcon(":/pics/qt-logo.png"), tr("About &Qt"), coll,
264 qApp, SLOT(aboutQt())));
265 coll->addAction("DebugNetworkModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &NetworkModel"), coll,
266 this, SLOT(on_actionDebugNetworkModel_triggered())));
267 coll->addAction("DebugBufferViewOverlay", new Action(SmallIcon("tools-report-bug"), tr("Debug &BufferViewOverlay"), coll,
268 this, SLOT(on_actionDebugBufferViewOverlay_triggered())));
269 coll->addAction("DebugMessageModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &MessageModel"), coll,
270 this, SLOT(on_actionDebugMessageModel_triggered())));
271 coll->addAction("DebugLog", new Action(SmallIcon("tools-report-bug"), tr("Debug &Log"), coll,
272 this, SLOT(on_actionDebugLog_triggered())));
275 void MainWin::setupMenus() {
276 ActionCollection *coll = QtUi::actionCollection("General");
278 _fileMenu = menuBar()->addMenu(tr("&File"));
280 static const QStringList coreActions = QStringList()
281 << "ConnectCore" << "DisconnectCore" << "CoreInfo";
284 foreach(QString actionName, coreActions) {
285 coreAction = coll->action(actionName);
286 _fileMenu->addAction(coreAction);
287 flagRemoteCoreOnly(coreAction);
289 flagRemoteCoreOnly(_fileMenu->addSeparator());
291 _networksMenu = _fileMenu->addMenu(tr("&Networks"));
292 _networksMenu->addAction(coll->action("ConfigureNetworks"));
293 _networksMenu->addSeparator();
294 _fileMenu->addSeparator();
295 _fileMenu->addAction(coll->action("Quit"));
297 _viewMenu = menuBar()->addMenu(tr("&View"));
298 _bufferViewsMenu = _viewMenu->addMenu(tr("&Buffer Views"));
299 _bufferViewsMenu->addAction(coll->action("ConfigureBufferViews"));
300 _toolbarMenu = _viewMenu->addMenu(tr("&Toolbars"));
301 _viewMenu->addSeparator();
302 _viewMenu->addAction(coll->action("ToggleSearchBar"));
304 coreAction = coll->action("ShowAwayLog");
305 flagRemoteCoreOnly(coreAction);
306 _viewMenu->addAction(coreAction);
308 _viewMenu->addAction(coll->action("ToggleStatusBar"));
309 _viewMenu->addSeparator();
310 _viewMenu->addAction(coll->action("LockLayout"));
312 _settingsMenu = menuBar()->addMenu(tr("&Settings"));
314 _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
315 _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
317 _settingsMenu->addAction(coll->action("ConfigureQuassel"));
319 _helpMenu = menuBar()->addMenu(tr("&Help"));
320 _helpMenu->addAction(coll->action("AboutQuassel"));
322 _helpMenu->addAction(coll->action("AboutQt"));
324 _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
326 _helpMenu->addSeparator();
327 _helpDebugMenu = _helpMenu->addMenu(SmallIcon("tools-report-bug"), tr("Debug"));
328 _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
329 _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
330 _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
331 _helpDebugMenu->addAction(coll->action("DebugLog"));
334 void MainWin::setupBufferWidget() {
335 _bufferWidget = new BufferWidget(this);
336 _bufferWidget->setModel(Client::bufferModel());
337 _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
338 setCentralWidget(_bufferWidget);
341 void MainWin::addBufferView(int bufferViewConfigId) {
342 addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
345 void MainWin::addBufferView(ClientBufferViewConfig *config) {
349 config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
350 BufferViewDock *dock = new BufferViewDock(config, this);
352 //create the view and initialize it's filter
353 BufferView *view = new BufferView(dock);
354 view->setFilteredModel(Client::bufferModel(), config);
355 view->installEventFilter(_inputWidget->inputLine()); // for key presses
358 Client::bufferModel()->synchronizeView(view);
360 dock->setWidget(view);
363 addDockWidget(Qt::LeftDockWidgetArea, dock);
364 _bufferViewsMenu->addAction(dock->toggleViewAction());
366 connect(dock->toggleViewAction(), SIGNAL(toggled(bool)), this, SLOT(bufferViewToggled(bool)));
367 _bufferViews.append(dock);
370 void MainWin::removeBufferView(int bufferViewConfigId) {
372 BufferViewDock *dock;
373 foreach(QAction *action, _bufferViewsMenu->actions()) {
374 actionData = action->data();
375 if(!actionData.isValid())
378 dock = qobject_cast<BufferViewDock *>(action->parent());
379 if(dock && actionData.toInt() == bufferViewConfigId) {
380 removeAction(action);
386 void MainWin::bufferViewToggled(bool enabled) {
387 QAction *action = qobject_cast<QAction *>(sender());
389 BufferViewDock *dock = qobject_cast<BufferViewDock *>(action->parent());
392 Client::bufferViewOverlay()->addView(dock->bufferViewId());
393 BufferViewConfig *config = dock->config();
394 if(config && config->isInitialized()) {
395 BufferIdList buffers;
396 if(config->networkId().isValid()) {
397 foreach(BufferId bufferId, config->bufferList()) {
398 if(Client::networkModel()->networkId(bufferId) == config->networkId())
401 foreach(BufferId bufferId, config->temporarilyRemovedBuffers().toList()) {
402 if(Client::networkModel()->networkId(bufferId) == config->networkId())
406 buffers = BufferIdList::fromSet(config->bufferList().toSet() + config->temporarilyRemovedBuffers());
408 Client::backlogManager()->checkForBacklog(buffers);
411 Client::bufferViewOverlay()->removeView(dock->bufferViewId());
415 BufferView *MainWin::allBuffersView() const {
416 // "All Buffers" is always the first dock created
417 if(_bufferViews.count() > 0)
418 return _bufferViews[0]->bufferView();
422 void MainWin::showNotificationsDlg() {
423 SettingsPageDlg dlg(new NotificationsSettingsPage(this), this);
427 void MainWin::on_actionConfigureNetworks_triggered() {
428 SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
432 void MainWin::on_actionConfigureViews_triggered() {
433 SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
437 void MainWin::on_actionLockLayout_toggled(bool lock) {
438 QList<VerticalDock *> docks = findChildren<VerticalDock *>();
439 foreach(VerticalDock *dock, docks) {
440 dock->showTitle(!lock);
442 if(Client::bufferViewManager()) {
443 foreach(ClientBufferViewConfig *config, Client::bufferViewManager()->clientBufferViewConfigs()) {
444 config->setLocked(lock);
447 QtUiSettings().setValue("LockLayout", lock);
450 void MainWin::setupNickWidget() {
452 NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
453 nickDock->setObjectName("NickDock");
454 nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
456 _nickListWidget = new NickListWidget(nickDock);
457 nickDock->setWidget(_nickListWidget);
459 addDockWidget(Qt::RightDockWidgetArea, nickDock);
460 _viewMenu->addAction(nickDock->toggleViewAction());
461 nickDock->toggleViewAction()->setText(tr("Show Nick List"));
463 // See NickListDock::NickListDock();
464 // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
466 // attach the NickListWidget to the BufferModel and the default selection
467 _nickListWidget->setModel(Client::bufferModel());
468 _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
471 void MainWin::setupChatMonitor() {
472 VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
473 dock->setObjectName("ChatMonitorDock");
475 ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
476 ChatMonitorView *chatView = new ChatMonitorView(filter, this);
478 dock->setWidget(chatView);
481 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
482 _viewMenu->addAction(dock->toggleViewAction());
483 dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
486 void MainWin::setupInputWidget() {
487 VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
488 dock->setObjectName("InputDock");
490 _inputWidget = new InputWidget(dock);
491 dock->setWidget(_inputWidget);
493 addDockWidget(Qt::BottomDockWidgetArea, dock);
495 _viewMenu->addAction(dock->toggleViewAction());
496 dock->toggleViewAction()->setText(tr("Show Input Line"));
498 _inputWidget->setModel(Client::bufferModel());
499 _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
501 _bufferWidget->setFocusProxy(_inputWidget);
503 _inputWidget->inputLine()->installEventFilter(_bufferWidget);
506 void MainWin::setupTopicWidget() {
507 VerticalDock *dock = new VerticalDock(tr("Topic"), this);
508 dock->setObjectName("TopicDock");
509 TopicWidget *topicwidget = new TopicWidget(dock);
511 dock->setWidget(topicwidget);
513 topicwidget->setModel(Client::bufferModel());
514 topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
516 addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
518 _viewMenu->addAction(dock->toggleViewAction());
519 dock->toggleViewAction()->setText(tr("Show Topic Line"));
522 void MainWin::setupTitleSetter() {
523 _titleSetter.setModel(Client::bufferModel());
524 _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
527 void MainWin::setupStatusBar() {
528 // MessageProcessor progress
529 statusBar()->addPermanentWidget(msgProcessorStatusWidget);
532 updateLagIndicator();
533 statusBar()->addPermanentWidget(coreLagLabel);
534 coreLagLabel->hide();
535 connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
538 sslLabel->setPixmap(QPixmap());
539 statusBar()->addPermanentWidget(sslLabel);
542 QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
544 QtUiSettings uiSettings;
546 bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
547 showStatusbar->setChecked(enabled);
548 enabled ? statusBar()->show() : statusBar()->hide();
550 connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
551 connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
554 void MainWin::saveStatusBarStatus(bool enabled) {
555 QtUiSettings uiSettings;
556 uiSettings.setValue("ShowStatusBar", enabled);
559 void MainWin::setupSystray() {
560 _systemTray = new SystemTray(this);
563 void MainWin::setupToolBars() {
564 connect(_bufferWidget, SIGNAL(currentChanged(QModelIndex)),
565 QtUi::toolBarActionProvider(), SLOT(currentBufferChanged(QModelIndex)));
566 connect(_nickListWidget, SIGNAL(nickSelectionChanged(QModelIndexList)),
567 QtUi::toolBarActionProvider(), SLOT(nickSelectionChanged(QModelIndexList)));
570 setUnifiedTitleAndToolBarOnMac(true);
572 _mainToolBar = addToolBar(tr("Main Toolbar"));
573 _mainToolBar->setObjectName("MainToolBar");
575 QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
576 _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
578 //_nickToolBar = addToolBar("User");
579 //_nickToolBar->setObjectName("NickToolBar");
580 //QtUi::toolBarActionProvider()->addActions(_nickToolBar, ToolBarActionProvider::NickToolBar);
583 _mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
584 //_nickToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
588 void MainWin::connectedToCore() {
589 Q_CHECK_PTR(Client::bufferViewManager());
590 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
591 connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
592 connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
597 void MainWin::setConnectedState() {
598 ActionCollection *coll = QtUi::actionCollection("General");
600 coll->action("ConnectCore")->setEnabled(false);
601 coll->action("DisconnectCore")->setEnabled(true);
602 coll->action("CoreInfo")->setEnabled(true);
604 foreach(QAction *action, _fileMenu->actions()) {
605 if(isRemoteCoreOnly(action))
606 action->setVisible(!Client::internalCore());
609 disconnect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
610 disconnect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
611 disconnect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
612 if(!Client::internalCore()) {
613 connect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
614 connect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
615 connect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
618 // _viewMenu->setEnabled(true);
619 if(!Client::internalCore())
620 statusBar()->showMessage(tr("Connected to core."));
622 statusBar()->clearMessage();
624 if(Client::signalProxy()->isSecure()) {
625 sslLabel->setPixmap(SmallIcon("security-high"));
627 sslLabel->setPixmap(SmallIcon("security-low"));
630 sslLabel->setVisible(!Client::internalCore());
631 coreLagLabel->setVisible(!Client::internalCore());
633 systemTray()->setState(SystemTray::Active);
635 if(Client::networkIds().isEmpty()) {
636 IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
641 void MainWin::loadLayout() {
643 int accountId = Client::currentCoreAccount().toInt();
644 restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
647 void MainWin::saveLayout() {
649 int accountId = Client::currentCoreAccount().toInt();
650 if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
653 void MainWin::updateLagIndicator(int lag) {
654 QString text = tr("Core Lag: %1");
656 text = text.arg('-');
658 text = text.arg("%1 msec").arg(lag);
659 coreLagLabel->setText(text);
662 void MainWin::disconnectedFromCore() {
663 // save core specific layout and remove bufferviews;
666 BufferViewDock *dock;
667 foreach(QAction *action, _bufferViewsMenu->actions()) {
668 actionData = action->data();
669 if(!actionData.isValid())
672 dock = qobject_cast<BufferViewDock *>(action->parent());
673 if(dock && actionData.toInt() != -1) {
674 removeAction(action);
679 restoreState(s.value("MainWinState").toByteArray());
680 setDisconnectedState();
683 void MainWin::setDisconnectedState() {
684 ActionCollection *coll = QtUi::actionCollection("General");
685 //ui.menuCore->setEnabled(false);
686 coll->action("ConnectCore")->setEnabled(true);
687 coll->action("DisconnectCore")->setEnabled(false);
688 coll->action("CoreInfo")->setEnabled(false);
689 //_viewMenu->setEnabled(false);
690 statusBar()->showMessage(tr("Not connected to core."));
691 sslLabel->setPixmap(QPixmap());
693 updateLagIndicator();
694 coreLagLabel->hide();
695 if(msgProcessorStatusWidget)
696 msgProcessorStatusWidget->setProgress(0, 0);
698 systemTray()->setState(SystemTray::Inactive);
701 void MainWin::startInternalCore() {
702 ClientSyncer *syncer = new ClientSyncer();
703 Client::registerClientSyncer(syncer);
704 connect(syncer, SIGNAL(syncFinished()), syncer, SLOT(deleteLater()), Qt::QueuedConnection);
705 syncer->useInternalCore();
708 void MainWin::showCoreConnectionDlg(bool autoConnect) {
709 CoreConnectDlg(autoConnect, this).exec();
712 void MainWin::showChannelList(NetworkId netId) {
713 ChannelListDlg *channelListDlg = new ChannelListDlg();
715 if(!netId.isValid()) {
716 QAction *action = qobject_cast<QAction *>(sender());
718 netId = action->data().value<NetworkId>();
721 channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
722 channelListDlg->setNetwork(netId);
723 channelListDlg->show();
726 void MainWin::showCoreInfoDlg() {
727 CoreInfoDlg(this).exec();
730 void MainWin::showAwayLog() {
733 AwayLogFilter *filter = new AwayLogFilter(Client::messageModel());
734 _awayLog = new AwayLogView(filter, 0);
735 filter->setParent(_awayLog);
736 connect(_awayLog, SIGNAL(destroyed()), this, SLOT(awayLogDestroyed()));
737 _awayLog->setAttribute(Qt::WA_DeleteOnClose);
741 void MainWin::awayLogDestroyed() {
745 void MainWin::showSettingsDlg() {
746 SettingsDlg *dlg = new SettingsDlg();
748 //Category: Appearance
749 dlg->registerSettingsPage(new AppearanceSettingsPage(dlg)); //General
750 dlg->registerSettingsPage(new ColorSettingsPage(dlg));
751 dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
752 dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
753 dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
754 dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
755 dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
758 dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
759 dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
760 dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
761 dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
766 void MainWin::showAboutDlg() {
767 AboutDlg(this).exec();
771 void MainWin::showShortcutsDlg() {
772 KShortcutsDialog::configure(QtUi::actionCollection("General"), KShortcutsEditor::LetterShortcutsDisallowed);
776 void MainWin::closeEvent(QCloseEvent *event) {
778 QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
780 if(!app->aboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
781 toggleMinimizedToTray();
785 QApplication::quit();
789 bool MainWin::event(QEvent *event) {
790 if(event->type() == QEvent::WindowActivate)
791 QtUi::closeNotifications();
792 return QMainWindow::event(event);
795 void MainWin::changeEvent(QEvent *event) {
796 if(event->type() == QEvent::WindowStateChange) {
797 if(windowState() & Qt::WindowMinimized) {
799 if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
808 else if(event->type() == QEvent::ActivationChange)
809 dwTickCount = GetTickCount(); // needed for toggleMinimizedToTray()
815 void MainWin::hideToTray() {
816 if(!systemTray()->isSystemTrayAvailable()) {
817 qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
821 systemTray()->setIconVisible();
824 void MainWin::toggleMinimizedToTray() {
826 // the problem is that we lose focus when the systray icon is activated
827 // and we don't know the former active window
828 // therefore we watch for activation event and use our stopwatch :)
829 // courtesy: KSystemTrayIcon
830 if(GetTickCount() - dwTickCount >= 300)
831 // we weren't active in the last 300ms -> activate
838 if(!isVisible() || isMinimized())
847 void MainWin::forceActivated() {
849 // Bypass focus stealing prevention
850 QX11Info::setAppUserTime(QX11Info::appTime());
853 if(windowState() & Qt::WindowMinimized) {
855 setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
858 move(frameGeometry().topLeft()); // avoid placement policies
864 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
867 bool hasFocus = QApplication::activeWindow() != 0;
869 for(int i = start; i <= end; i++) {
870 QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
872 qDebug() << "MainWin::messagesInserted(): Invalid model index!";
875 Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
876 if(flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
878 flags |= Message::Backlog; // we only want to trigger a highlight once!
879 Client::messageModel()->setData(idx, (int)flags, ChatLineModel::FlagsRole);
881 BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
882 BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
884 if(hasFocus && bufId == _bufferWidget->currentBuffer())
887 if(flags & Message::Highlight || bufType == BufferInfo::QueryBuffer) {
888 QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
889 QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
890 QString contents = idx.data(ChatLineModel::DisplayRole).toString();
891 AbstractNotificationBackend::NotificationType type;
893 if(bufType == BufferInfo::QueryBuffer && !hasFocus)
894 type = AbstractNotificationBackend::PrivMsg;
895 else if(bufType == BufferInfo::QueryBuffer && hasFocus)
896 type = AbstractNotificationBackend::PrivMsgFocused;
897 else if(flags & Message::Highlight && !hasFocus)
898 type = AbstractNotificationBackend::Highlight;
900 type = AbstractNotificationBackend::HighlightFocused;
902 QtUi::invokeNotification(bufId, type, sender, contents);
907 void MainWin::clientNetworkCreated(NetworkId id) {
908 const Network *net = Client::network(id);
909 QAction *act = new QAction(net->networkName(), this);
910 act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
911 act->setData(QVariant::fromValue<NetworkId>(id));
912 connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
913 connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
915 QAction *beforeAction = 0;
916 foreach(QAction *action, _networksMenu->actions()) {
917 if(!action->data().isValid()) // ignore stock actions
919 if(net->networkName().localeAwareCompare(action->text()) < 0) {
920 beforeAction = action;
924 _networksMenu->insertAction(beforeAction, act);
927 void MainWin::clientNetworkUpdated() {
928 const Network *net = qobject_cast<const Network *>(sender());
932 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
936 action->setText(net->networkName());
938 switch(net->connectionState()) {
939 case Network::Initialized:
940 action->setIcon(SmallIcon("network-connect"));
942 case Network::Disconnected:
943 action->setIcon(SmallIcon("network-disconnect"));
946 action->setIcon(SmallIcon("network-wired"));
950 void MainWin::clientNetworkRemoved(NetworkId id) {
951 QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
955 action->deleteLater();
958 void MainWin::connectOrDisconnectFromNet() {
959 QAction *act = qobject_cast<QAction *>(sender());
961 const Network *net = Client::network(act->data().value<NetworkId>());
963 if(net->connectionState() == Network::Disconnected) net->requestConnect();
964 else net->requestDisconnect();
967 void MainWin::on_actionDebugNetworkModel_triggered() {
968 QTreeView *view = new QTreeView;
969 view->setAttribute(Qt::WA_DeleteOnClose);
970 view->setWindowTitle("Debug NetworkModel View");
971 view->setModel(Client::networkModel());
972 view->setColumnWidth(0, 250);
973 view->setColumnWidth(1, 250);
974 view->setColumnWidth(2, 80);
975 view->resize(610, 300);
979 void MainWin::on_actionDebugBufferViewOverlay_triggered() {
980 QTreeView *view = new QTreeView;
981 view->setAttribute(Qt::WA_DeleteOnClose);
982 view->setWindowTitle("Debug BufferViewOverlay View");
983 BufferViewOverlayFilter *filter = new BufferViewOverlayFilter(Client::bufferModel(), Client::bufferViewOverlay());
984 filter->setParent(view);
985 view->setModel(filter);
986 view->setColumnWidth(0, 250);
987 view->setColumnWidth(1, 250);
988 view->setColumnWidth(2, 80);
989 view->resize(610, 300);
993 void MainWin::on_actionDebugMessageModel_triggered() {
994 QTableView *view = new QTableView(0);
995 DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
996 filter->setSourceModel(Client::messageModel());
997 view->setModel(filter);
998 view->setAttribute(Qt::WA_DeleteOnClose, true);
999 view->verticalHeader()->hide();
1000 view->horizontalHeader()->setStretchLastSection(true);
1004 void MainWin::on_actionDebugLog_triggered() {
1005 DebugLogWidget *logWidget = new DebugLogWidget(0);
1009 void MainWin::saveStateToSession(const QString &sessionId) {
1011 SessionSettings s(sessionId);
1013 s.setValue("MainWinSize", size());
1014 s.setValue("MainWinPos", pos());
1015 s.setValue("MainWinState", saveState());
1018 void MainWin::saveStateToSessionSettings(SessionSettings & s)
1020 s.setValue("MainWinSize", size());
1021 s.setValue("MainWinPos", pos());
1022 s.setValue("MainWinState", saveState());
1025 void MainWin::showStatusBarMessage(const QString &message) {
1026 statusBar()->showMessage(message, 10000);