Close notifications on buffer switch rather than activation change
[quassel.git] / src / qtui / mainwin.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20 #include "mainwin.h"
21
22 #ifdef HAVE_KDE
23 #  include <KAction>
24 #  include <KActionCollection>
25 #  include <KHelpMenu>
26 #  include <KMenuBar>
27 #  include <KShortcutsDialog>
28 #  include <KStatusBar>
29 #  include <KToolBar>
30 #endif
31
32 #ifdef Q_WS_X11
33 #  include <QX11Info>
34 #endif
35
36 #include "aboutdlg.h"
37 #include "awaylogfilter.h"
38 #include "awaylogview.h"
39 #include "action.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"
51 #include "chatview.h"
52 #include "client.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"
82
83 #ifndef HAVE_KDE
84 #  ifdef HAVE_DBUS
85 #    include "desktopnotificationbackend.h"
86 #  endif
87 #  ifdef HAVE_PHONON
88 #    include "phononnotificationbackend.h"
89 #  endif
90 #  include "systraynotificationbackend.h"
91 #  include "taskbarnotificationbackend.h"
92 #else /* HAVE_KDE */
93 #  include "knotificationbackend.h"
94 #endif /* HAVE_KDE */
95
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"
112
113 MainWin::MainWin(QWidget *parent)
114 #ifdef HAVE_KDE
115   : KMainWindow(parent),
116   _kHelpMenu(new KHelpMenu(this, KGlobal::mainComponent().aboutData())),
117 #else
118   : QMainWindow(parent),
119 #endif
120     coreLagLabel(new QLabel()),
121     sslLabel(new QLabel()),
122     msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
123     _titleSetter(this),
124     _awayLog(0)
125 {
126 #ifdef Q_WS_WIN
127   dwTickCount = 0;
128 #endif
129
130   QtUiSettings uiSettings;
131   QString style = uiSettings.value("Style", QString()).toString();
132   if(!style.isEmpty()) {
133     QApplication::setStyle(style);
134   }
135
136   QApplication::setQuitOnLastWindowClosed(false);
137
138   setWindowTitle("Quassel IRC");
139   setWindowIconText("Quassel IRC");
140   updateIcon();
141
142   installEventFilter(new JumpKeyHandler(this));
143 }
144
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)));
151
152   // Setup Dock Areas
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);
158
159   // Order is sometimes important
160   setupActions();
161   setupBufferWidget();
162   setupMenus();
163   setupTopicWidget();
164   setupChatMonitor();
165   setupNickWidget();
166   setupInputWidget();
167   setupStatusBar();
168   setupToolBars();
169   setupSystray();
170   setupTitleSetter();
171   setupHotList();
172
173 #ifndef HAVE_KDE
174   QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
175   QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
176 #  ifdef HAVE_PHONON
177   QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
178 #  endif
179 #  ifdef HAVE_DBUS
180   QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
181 #  endif
182
183 #else /* HAVE_KDE */
184   QtUi::registerNotificationBackend(new KNotificationBackend(this));
185 #endif /* HAVE_KDE */
186
187   connect(bufferWidget(), SIGNAL(currentChanged(BufferId)), SLOT(currentBufferChanged(BufferId)));
188
189   setDisconnectedState();  // Disable menus and stuff
190
191 #ifdef HAVE_KDE
192   setAutoSaveSettings();
193 #endif
194
195   // restore mainwin state
196   QtUiSettings s;
197   restoreStateFromSettings(s);
198
199   // restore locked state of docks
200   QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
201
202   if(Quassel::runMode() != Quassel::Monolithic) {
203     showCoreConnectionDlg(true); // autoconnect if appropriate
204   } else {
205     startInternalCore();
206   }
207 }
208
209 MainWin::~MainWin() {
210
211 }
212
213 void MainWin::quit() {
214   QtUiSettings s;
215   saveStateToSettings(s);
216   saveLayout();
217   QApplication::quit();
218 }
219
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());
228
229 #ifdef HAVE_KDE
230   saveAutoSaveSettings();
231 #endif
232 }
233
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();
238
239 #ifndef HAVE_KDE
240   restoreGeometry(s.value("MainWinGeometry").toByteArray());
241
242   if(maximized) {
243     // restoreGeometry() fails if the windows was maximized, so we resize and position explicitly
244     resize(_normalSize);
245     move(_normalPos);
246   }
247
248   restoreState(s.value("MainWinState").toByteArray());
249
250 #else
251   move(_normalPos);
252 #endif
253
254   if(s.value("MainWinHidden").toBool())
255     hideToTray();
256   else if(s.value("MainWinMinimized").toBool())
257     showMinimized();
258   else if(maximized)
259     showMaximized();
260   else
261     show();
262 }
263
264 void MainWin::updateIcon() {
265 #ifdef Q_WS_MAC
266   const int size = 128;
267 #else
268   const int size = 48;
269 #endif
270
271   QPixmap icon;
272   if(Client::isConnected())
273     icon = DesktopIcon("quassel", size);
274   else
275     icon = DesktopIcon("quassel_inactive", size);
276   setWindowIcon(icon);
277   qApp->setWindowIcon(icon);
278 }
279
280 void MainWin::setupActions() {
281   ActionCollection *coll = QtUi::actionCollection("General");
282   // File
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")));
293
294   // View
295   coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Chat Lists..."), coll,
296                                              this, SLOT(on_actionConfigureViews_triggered())));
297
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)));
301
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);
308
309   coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
310                                                 0, 0))->setCheckable(true);
311
312   // Settings
313   coll->addAction("ConfigureQuassel", new Action(SmallIcon("configure"), tr("&Configure Quassel..."), coll,
314                                                   this, SLOT(showSettingsDlg()), tr("F7")));
315
316   // Help
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));
333
334   // Navigation
335   coll->addAction("JumpHotBuffer", new Action(tr("Jump to hot chat"), coll,
336                                               this, SLOT(on_jumpHotBuffer_triggered()), QKeySequence(Qt::META + Qt::Key_A)));
337 }
338
339 void MainWin::setupMenus() {
340   ActionCollection *coll = QtUi::actionCollection("General");
341
342   _fileMenu = menuBar()->addMenu(tr("&File"));
343
344   static const QStringList coreActions = QStringList()
345     << "ConnectCore" << "DisconnectCore" << "CoreInfo";
346
347   QAction *coreAction;
348   foreach(QString actionName, coreActions) {
349     coreAction = coll->action(actionName);
350     _fileMenu->addAction(coreAction);
351     flagRemoteCoreOnly(coreAction);
352   }
353   flagRemoteCoreOnly(_fileMenu->addSeparator());
354
355   _networksMenu = _fileMenu->addMenu(tr("&Networks"));
356   _networksMenu->addAction(coll->action("ConfigureNetworks"));
357   _networksMenu->addSeparator();
358   _fileMenu->addSeparator();
359   _fileMenu->addAction(coll->action("Quit"));
360
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();
366
367   _viewMenu->addAction(coll->action("ToggleMenuBar"));
368   _viewMenu->addAction(coll->action("ToggleStatusBar"));
369   _viewMenu->addAction(coll->action("ToggleSearchBar"));
370
371   coreAction = coll->action("ShowAwayLog");
372   flagRemoteCoreOnly(coreAction);
373   _viewMenu->addAction(coreAction);
374
375   _viewMenu->addSeparator();
376   _viewMenu->addAction(coll->action("LockLayout"));
377
378   _settingsMenu = menuBar()->addMenu(tr("&Settings"));
379 #ifdef HAVE_KDE
380   _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
381   _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
382 #endif
383   _settingsMenu->addAction(coll->action("ConfigureQuassel"));
384
385   _helpMenu = menuBar()->addMenu(tr("&Help"));
386   _helpMenu->addAction(coll->action("AboutQuassel"));
387 #ifndef HAVE_KDE
388   _helpMenu->addAction(coll->action("AboutQt"));
389 #else
390   _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
391 #endif
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"));
401
402   // Toggle visibility
403   QAction *showMenuBar = QtUi::actionCollection("General")->action("ToggleMenuBar");
404
405   QtUiSettings uiSettings;
406   bool enabled = uiSettings.value("ShowMenuBar", QVariant(true)).toBool();
407   showMenuBar->setChecked(enabled);
408   enabled ? menuBar()->show() : menuBar()->hide();
409
410   connect(showMenuBar, SIGNAL(toggled(bool)), menuBar(), SLOT(setVisible(bool)));
411   connect(showMenuBar, SIGNAL(toggled(bool)), this, SLOT(saveMenuBarStatus(bool)));
412 }
413
414 void MainWin::setupBufferWidget() {
415   _bufferWidget = new BufferWidget(this);
416   _bufferWidget->setModel(Client::bufferModel());
417   _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
418   setCentralWidget(_bufferWidget);
419 }
420
421 void MainWin::addBufferView(int bufferViewConfigId) {
422   addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
423 }
424
425 void MainWin::addBufferView(ClientBufferViewConfig *config) {
426   if(!config)
427     return;
428
429   config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
430   BufferViewDock *dock = new BufferViewDock(config, this);
431
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
436   view->show();
437
438   Client::bufferModel()->synchronizeView(view);
439
440   dock->setWidget(view);
441   dock->show();
442
443   addDockWidget(Qt::LeftDockWidgetArea, dock);
444   _bufferViewsMenu->addAction(dock->toggleViewAction());
445
446   connect(dock->toggleViewAction(), SIGNAL(toggled(bool)), this, SLOT(bufferViewToggled(bool)));
447   _bufferViews.append(dock);
448 }
449
450 void MainWin::removeBufferView(int bufferViewConfigId) {
451   QVariant actionData;
452   BufferViewDock *dock;
453   foreach(QAction *action, _bufferViewsMenu->actions()) {
454     actionData = action->data();
455     if(!actionData.isValid())
456       continue;
457
458     dock = qobject_cast<BufferViewDock *>(action->parent());
459     if(dock && actionData.toInt() == bufferViewConfigId) {
460       removeAction(action);
461       dock->deleteLater();
462     }
463   }
464 }
465
466 void MainWin::bufferViewToggled(bool enabled) {
467   QAction *action = qobject_cast<QAction *>(sender());
468   Q_ASSERT(action);
469   BufferViewDock *dock = qobject_cast<BufferViewDock *>(action->parent());
470   Q_ASSERT(dock);
471   if(enabled) {
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())
479             buffers << bufferId;
480         }
481         foreach(BufferId bufferId, config->temporarilyRemovedBuffers().toList()) {
482           if(Client::networkModel()->networkId(bufferId) == config->networkId())
483             buffers << bufferId;
484         }
485       } else {
486         buffers = BufferIdList::fromSet(config->bufferList().toSet() + config->temporarilyRemovedBuffers());
487       }
488       Client::backlogManager()->checkForBacklog(buffers);
489     }
490   } else {
491     Client::bufferViewOverlay()->removeView(dock->bufferViewId());
492   }
493 }
494
495 BufferView *MainWin::allBuffersView() const {
496   // "All Buffers" is always the first dock created
497   if(_bufferViews.count() > 0)
498     return _bufferViews[0]->bufferView();
499   return 0;
500 }
501
502 void MainWin::showNotificationsDlg() {
503   SettingsPageDlg dlg(new NotificationsSettingsPage(this), this);
504   dlg.exec();
505 }
506
507 void MainWin::on_actionConfigureNetworks_triggered() {
508   SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
509   dlg.exec();
510 }
511
512 void MainWin::on_actionConfigureViews_triggered() {
513   SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
514   dlg.exec();
515 }
516
517 void MainWin::on_actionLockLayout_toggled(bool lock) {
518   QList<VerticalDock *> docks = findChildren<VerticalDock *>();
519   foreach(VerticalDock *dock, docks) {
520     dock->showTitle(!lock);
521   }
522   if(Client::bufferViewManager()) {
523     foreach(ClientBufferViewConfig *config, Client::bufferViewManager()->clientBufferViewConfigs()) {
524       config->setLocked(lock);
525     }
526   }
527   QtUiSettings().setValue("LockLayout", lock);
528 }
529
530 void MainWin::setupNickWidget() {
531   // create nick dock
532   NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
533   nickDock->setObjectName("NickDock");
534   nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
535
536   _nickListWidget = new NickListWidget(nickDock);
537   nickDock->setWidget(_nickListWidget);
538
539   addDockWidget(Qt::RightDockWidgetArea, nickDock);
540   _viewMenu->addAction(nickDock->toggleViewAction());
541   nickDock->toggleViewAction()->setText(tr("Show Nick List"));
542
543   // See NickListDock::NickListDock();
544   // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
545
546   // attach the NickListWidget to the BufferModel and the default selection
547   _nickListWidget->setModel(Client::bufferModel());
548   _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
549 }
550
551 void MainWin::setupChatMonitor() {
552   VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
553   dock->setObjectName("ChatMonitorDock");
554
555   ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
556   ChatMonitorView *chatView = new ChatMonitorView(filter, this);
557   chatView->show();
558   dock->setWidget(chatView);
559   dock->hide();
560
561   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
562   _viewMenu->addAction(dock->toggleViewAction());
563   dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
564 }
565
566 void MainWin::setupInputWidget() {
567   VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
568   dock->setObjectName("InputDock");
569
570   _inputWidget = new InputWidget(dock);
571   dock->setWidget(_inputWidget);
572
573   addDockWidget(Qt::BottomDockWidgetArea, dock);
574
575   _viewMenu->addAction(dock->toggleViewAction());
576   dock->toggleViewAction()->setText(tr("Show Input Line"));
577
578   _inputWidget->setModel(Client::bufferModel());
579   _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
580
581   _bufferWidget->setFocusProxy(_inputWidget);
582
583   _inputWidget->inputLine()->installEventFilter(_bufferWidget);
584 }
585
586 void MainWin::setupTopicWidget() {
587   VerticalDock *dock = new VerticalDock(tr("Topic"), this);
588   dock->setObjectName("TopicDock");
589   TopicWidget *topicwidget = new TopicWidget(dock);
590
591   dock->setWidget(topicwidget);
592
593   topicwidget->setModel(Client::bufferModel());
594   topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
595
596   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
597
598   _viewMenu->addAction(dock->toggleViewAction());
599   dock->toggleViewAction()->setText(tr("Show Topic Line"));
600 }
601
602 void MainWin::setupTitleSetter() {
603   _titleSetter.setModel(Client::bufferModel());
604   _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
605 }
606
607 void MainWin::setupStatusBar() {
608   // MessageProcessor progress
609   statusBar()->addPermanentWidget(msgProcessorStatusWidget);
610
611   // Core Lag:
612   updateLagIndicator();
613   statusBar()->addPermanentWidget(coreLagLabel);
614   coreLagLabel->hide();
615   connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
616
617   // SSL indicator
618   sslLabel->setPixmap(QPixmap());
619   statusBar()->addPermanentWidget(sslLabel);
620   sslLabel->hide();
621
622   QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
623
624   QtUiSettings uiSettings;
625
626   bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
627   showStatusbar->setChecked(enabled);
628   enabled ? statusBar()->show() : statusBar()->hide();
629
630   connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
631   connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
632 }
633
634 void MainWin::setupHotList() {
635   FlatProxyModel *flatProxy = new FlatProxyModel(this);
636   flatProxy->setSourceModel(Client::bufferModel());
637   _bufferHotList = new BufferHotListFilter(flatProxy);
638 }
639
640 void MainWin::saveMenuBarStatus(bool enabled) {
641   QtUiSettings uiSettings;
642   uiSettings.setValue("ShowMenuBar", enabled);
643 }
644
645 void MainWin::saveStatusBarStatus(bool enabled) {
646   QtUiSettings uiSettings;
647   uiSettings.setValue("ShowStatusBar", enabled);
648 }
649
650 void MainWin::setupSystray() {
651   _systemTray = new SystemTray(this);
652 }
653
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)));
659
660 #ifdef Q_WS_MAC
661   setUnifiedTitleAndToolBarOnMac(true);
662 #endif
663
664 #ifdef HAVE_KDE
665   _mainToolBar = new KToolBar("MainToolBar", this, Qt::TopToolBarArea, false, true, true);
666 #else
667   _mainToolBar = new QToolBar(this);
668   _mainToolBar->setObjectName("MainToolBar");
669 #endif
670   _mainToolBar->setWindowTitle(tr("Main Toolbar"));
671   addToolBar(_mainToolBar);
672
673   QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
674   _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
675 }
676
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()));
682
683   setConnectedState();
684 }
685
686 void MainWin::setConnectedState() {
687   ActionCollection *coll = QtUi::actionCollection("General");
688
689   coll->action("ConnectCore")->setEnabled(false);
690   coll->action("DisconnectCore")->setEnabled(true);
691   coll->action("CoreInfo")->setEnabled(true);
692
693   foreach(QAction *action, _fileMenu->actions()) {
694     if(isRemoteCoreOnly(action))
695       action->setVisible(!Client::internalCore());
696   }
697
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 &)));
705   }
706
707   // _viewMenu->setEnabled(true);
708   if(!Client::internalCore())
709     statusBar()->showMessage(tr("Connected to core."));
710   else
711     statusBar()->clearMessage();
712
713   if(Client::signalProxy()->isSecure()) {
714     sslLabel->setPixmap(SmallIcon("security-high"));
715   } else {
716     sslLabel->setPixmap(SmallIcon("security-low"));
717   }
718
719   sslLabel->setVisible(!Client::internalCore());
720   coreLagLabel->setVisible(!Client::internalCore());
721   updateIcon();
722   systemTray()->setState(SystemTray::Active);
723
724   if(Client::networkIds().isEmpty()) {
725     IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
726     wizard->show();
727   }
728 }
729
730 void MainWin::loadLayout() {
731   QtUiSettings s;
732   int accountId = Client::currentCoreAccount().toInt();
733   restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
734 }
735
736 void MainWin::saveLayout() {
737   QtUiSettings s;
738   int accountId = Client::currentCoreAccount().toInt();
739   if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
740 }
741
742 void MainWin::updateLagIndicator(int lag) {
743   QString text = tr("Core Lag: %1");
744   if(lag == -1)
745     text = text.arg('-');
746   else
747     text = text.arg("%1 msec").arg(lag);
748   coreLagLabel->setText(text);
749 }
750
751 void MainWin::disconnectedFromCore() {
752   // save core specific layout and remove bufferviews;
753   saveLayout();
754   QVariant actionData;
755   BufferViewDock *dock;
756   foreach(QAction *action, _bufferViewsMenu->actions()) {
757     actionData = action->data();
758     if(!actionData.isValid())
759       continue;
760
761     dock = qobject_cast<BufferViewDock *>(action->parent());
762     if(dock && actionData.toInt() != -1) {
763       removeAction(action);
764       dock->deleteLater();
765     }
766   }
767   QtUiSettings s;
768   restoreState(s.value("MainWinState").toByteArray());
769   setDisconnectedState();
770 }
771
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());
781   sslLabel->hide();
782   updateLagIndicator();
783   coreLagLabel->hide();
784   if(msgProcessorStatusWidget)
785     msgProcessorStatusWidget->setProgress(0, 0);
786   updateIcon();
787   systemTray()->setState(SystemTray::Inactive);
788 }
789
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();
795 }
796
797 void MainWin::showCoreConnectionDlg(bool autoConnect) {
798   CoreConnectDlg(autoConnect, this).exec();
799 }
800
801 void MainWin::showChannelList(NetworkId netId) {
802   ChannelListDlg *channelListDlg = new ChannelListDlg();
803
804   if(!netId.isValid()) {
805     QAction *action = qobject_cast<QAction *>(sender());
806     if(action)
807       netId = action->data().value<NetworkId>();
808   }
809
810   channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
811   channelListDlg->setNetwork(netId);
812   channelListDlg->show();
813 }
814
815 void MainWin::showCoreInfoDlg() {
816   CoreInfoDlg(this).exec();
817 }
818
819 void MainWin::showAwayLog() {
820   if(_awayLog)
821     return;
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);
827   _awayLog->show();
828 }
829
830 void MainWin::awayLogDestroyed() {
831   _awayLog = 0;
832 }
833
834 void MainWin::showSettingsDlg() {
835   SettingsDlg *dlg = new SettingsDlg();
836
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));
848
849   //Category: Misc
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));
856
857   dlg->show();
858 }
859
860 void MainWin::showAboutDlg() {
861   AboutDlg(this).exec();
862 }
863
864 #ifdef HAVE_KDE
865 void MainWin::showShortcutsDlg() {
866   KShortcutsDialog::configure(QtUi::actionCollection("General"), KShortcutsEditor::LetterShortcutsDisallowed);
867 }
868 #endif
869
870 /********************************************************************************************************/
871
872 bool MainWin::event(QEvent *event) {
873   if(event->type() == QEvent::WindowActivate) {
874     BufferId buffer = Client::bufferModel()->currentBuffer();
875     if(buffer.isValid())
876       QtUi::closeNotifications(buffer);
877   }
878   return QMainWindow::event(event);
879 }
880
881 void MainWin::moveEvent(QMoveEvent *event) {
882   if(!(windowState() & Qt::WindowMaximized))
883     _normalPos = event->pos();
884
885   QMainWindow::moveEvent(event);
886 }
887
888 void MainWin::resizeEvent(QResizeEvent *event) {
889   if(!(windowState() & Qt::WindowMaximized))
890     _normalSize = event->size();
891
892   QMainWindow::resizeEvent(event);
893 }
894
895 void MainWin::closeEvent(QCloseEvent *event) {
896   QtUiSettings s;
897   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
898   Q_ASSERT(app);
899   if(!app->isAboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
900     hideToTray();
901     event->ignore();
902   } else {
903     event->accept();
904     quit();
905   }
906 }
907
908 void MainWin::changeEvent(QEvent *event) {
909 #ifdef Q_WS_WIN
910   if(event->type() == QEvent::ActivationChange)
911     dwTickCount = GetTickCount();  // needed for toggleMinimizedToTray()
912 #endif
913
914   QMainWindow::changeEvent(event);
915 }
916
917 void MainWin::hideToTray() {
918   if(!systemTray()->isSystemTrayAvailable()) {
919     qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
920     return;
921   }
922   hide();
923   systemTray()->setIconVisible();
924 }
925
926 void MainWin::toggleMinimizedToTray() {
927 #ifdef Q_WS_WIN
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
934     forceActivated();
935   else
936     hideToTray();
937
938 #else
939
940   if(!isVisible() || isMinimized())
941     // restore
942     forceActivated();
943   else
944     hideToTray();
945
946 #endif
947 }
948
949 void MainWin::forceActivated() {
950 #ifdef Q_WS_X11
951   // Bypass focus stealing prevention
952   QX11Info::setAppUserTime(QX11Info::appTime());
953 #endif
954
955   if(windowState() & Qt::WindowMinimized) {
956     // restore
957     setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
958   }
959
960   // this does not actually work on all platforms... and causes more evil than good
961   // move(frameGeometry().topLeft()); // avoid placement policies
962   show();
963   raise();
964   activateWindow();
965 }
966
967 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
968   Q_UNUSED(parent);
969
970   bool hasFocus = QApplication::activeWindow() != 0;
971
972   for(int i = start; i <= end; i++) {
973     QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
974     if(!idx.isValid()) {
975       qDebug() << "MainWin::messagesInserted(): Invalid model index!";
976       continue;
977     }
978     Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
979     if(flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
980       continue;
981
982     BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
983     BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
984
985     if(hasFocus && bufId == Client::bufferModel()->currentBuffer())
986       continue;
987
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))))
991     {
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;
996
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;
1003       else
1004         type = AbstractNotificationBackend::HighlightFocused;
1005
1006       QtUi::invokeNotification(bufId, type, sender, contents);
1007     }
1008   }
1009 }
1010
1011 void MainWin::currentBufferChanged(BufferId buffer) {
1012   if(buffer.isValid())
1013     QtUi::closeNotifications(buffer);
1014 }
1015
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()));
1023
1024   QAction *beforeAction = 0;
1025   foreach(QAction *action, _networksMenu->actions()) {
1026     if(!action->data().isValid())  // ignore stock actions
1027       continue;
1028     if(net->networkName().localeAwareCompare(action->text()) < 0) {
1029       beforeAction = action;
1030       break;
1031     }
1032   }
1033   _networksMenu->insertAction(beforeAction, act);
1034 }
1035
1036 void MainWin::clientNetworkUpdated() {
1037   const Network *net = qobject_cast<const Network *>(sender());
1038   if(!net)
1039     return;
1040
1041   QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1042   if(!action)
1043     return;
1044
1045   action->setText(net->networkName());
1046
1047   switch(net->connectionState()) {
1048   case Network::Initialized:
1049     action->setIcon(SmallIcon("network-connect"));
1050     break;
1051   case Network::Disconnected:
1052     action->setIcon(SmallIcon("network-disconnect"));
1053     break;
1054   default:
1055     action->setIcon(SmallIcon("network-wired"));
1056   }
1057 }
1058
1059 void MainWin::clientNetworkRemoved(NetworkId id) {
1060   QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
1061   if(!action)
1062     return;
1063
1064   action->deleteLater();
1065 }
1066
1067 void MainWin::connectOrDisconnectFromNet() {
1068   QAction *act = qobject_cast<QAction *>(sender());
1069   if(!act) return;
1070   const Network *net = Client::network(act->data().value<NetworkId>());
1071   if(!net) return;
1072   if(net->connectionState() == Network::Disconnected) net->requestConnect();
1073   else net->requestDisconnect();
1074 }
1075
1076 void MainWin::on_jumpHotBuffer_triggered() {
1077   if(!_bufferHotList->rowCount())
1078     return;
1079
1080   QModelIndex topIndex = _bufferHotList->index(0, 0);
1081   BufferId bufferId = _bufferHotList->data(topIndex, NetworkModel::BufferIdRole).value<BufferId>();
1082   Client::bufferModel()->switchToBuffer(bufferId);
1083 }
1084
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);
1094   view->show();
1095 }
1096
1097 void MainWin::on_actionDebugHotList_triggered() {
1098   QTreeView *view = new QTreeView;
1099   view->setAttribute(Qt::WA_DeleteOnClose);
1100   view->setModel(_bufferHotList);
1101   view->show();
1102 }
1103
1104 void MainWin::on_actionDebugBufferViewOverlay_triggered() {
1105   DebugBufferViewOverlay *overlay = new DebugBufferViewOverlay(0);
1106   overlay->setAttribute(Qt::WA_DeleteOnClose);
1107   overlay->show();
1108 }
1109
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);
1118   view->show();
1119 }
1120
1121 void MainWin::on_actionDebugLog_triggered() {
1122   DebugLogWidget *logWidget = new DebugLogWidget(0);
1123   logWidget->show();
1124 }
1125
1126 void MainWin::showStatusBarMessage(const QString &message) {
1127   statusBar()->showMessage(message, 10000);
1128 }
1129