qtui: Remove remnant from debugging icon loading
[quassel.git] / src / qtui / mainwin.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "mainwin.h"
22
23 #include <QIcon>
24 #include <QMenuBar>
25 #include <QMessageBox>
26 #include <QStatusBar>
27 #include <QTableView>
28 #include <QToolBar>
29 #include <QInputDialog>
30
31 #ifdef HAVE_KDE4
32 #  include <KHelpMenu>
33 #  include <KMenuBar>
34 #  include <KShortcutsDialog>
35 #  include <KStatusBar>
36 #  include <KToggleFullScreenAction>
37 #  include <KToolBar>
38 #endif
39
40 #ifdef HAVE_KF5
41 #  include <KConfigWidgets/KStandardAction>
42 #  include <KXmlGui/KHelpMenu>
43 #  include <KXmlGui/KShortcutsDialog>
44 #  include <KXmlGui/KToolBar>
45 #  include <KWidgetsAddons/KToggleFullScreenAction>
46 #endif
47
48 #ifdef Q_WS_X11
49 #  include <QX11Info>
50 #endif
51
52 #include "aboutdlg.h"
53 #include "awaylogfilter.h"
54 #include "awaylogview.h"
55 #include "action.h"
56 #include "actioncollection.h"
57 #include "bufferhotlistfilter.h"
58 #include "buffermodel.h"
59 #include "bufferview.h"
60 #include "bufferviewoverlay.h"
61 #include "bufferviewoverlayfilter.h"
62 #include "bufferwidget.h"
63 #include "channellistdlg.h"
64 #include "chatlinemodel.h"
65 #include "chatmonitorfilter.h"
66 #include "chatmonitorview.h"
67 #include "chatview.h"
68 #include "client.h"
69 #include "clientbacklogmanager.h"
70 #include "clientbufferviewconfig.h"
71 #include "clientbufferviewmanager.h"
72 #include "clientignorelistmanager.h"
73 #include "clienttransfer.h"
74 #include "clienttransfermanager.h"
75 #include "coreconfigwizard.h"
76 #include "coreconnectdlg.h"
77 #include "coreconnection.h"
78 #include "coreconnectionstatuswidget.h"
79 #include "coreinfodlg.h"
80 #include "contextmenuactionprovider.h"
81 #include "debugbufferviewoverlay.h"
82 #include "debuglogwidget.h"
83 #include "debugmessagemodelfilter.h"
84 #include "flatproxymodel.h"
85 #include "icon.h"
86 #include "inputwidget.h"
87 #include "irclistmodel.h"
88 #include "ircconnectionwizard.h"
89 #include "legacysystemtray.h"
90 #include "msgprocessorstatuswidget.h"
91 #include "nicklistwidget.h"
92 #include "passwordchangedlg.h"
93 #include "qtuiapplication.h"
94 #include "qtuimessageprocessor.h"
95 #include "qtuisettings.h"
96 #include "qtuistyle.h"
97 #include "receivefiledlg.h"
98 #include "settingsdlg.h"
99 #include "settingspagedlg.h"
100 #include "statusnotifieritem.h"
101 #include "toolbaractionprovider.h"
102 #include "topicwidget.h"
103 #include "transfermodel.h"
104 #include "verticaldock.h"
105
106 #ifndef HAVE_KDE
107 #  ifdef HAVE_QTMULTIMEDIA
108 #    include "qtmultimedianotificationbackend.h"
109 #  endif
110 #  ifdef HAVE_PHONON
111 #    include "phononnotificationbackend.h"
112 #  endif
113 #  include "systraynotificationbackend.h"
114 #  include "taskbarnotificationbackend.h"
115 #else /* HAVE_KDE */
116 #  include "knotificationbackend.h"
117 #endif /* HAVE_KDE */
118 #include "systrayanimationnotificationbackend.h"
119
120
121 #ifdef HAVE_LIBSNORE
122 #  include "snorenotificationbackend.h"
123 #endif
124
125 #ifdef HAVE_SSL
126 #  include "sslinfodlg.h"
127 #endif
128
129 #ifdef HAVE_INDICATEQT
130   #include "indicatornotificationbackend.h"
131 #endif
132
133 #ifdef HAVE_NOTIFICATION_CENTER
134   #include "osxnotificationbackend.h"
135 #endif
136
137 #ifdef HAVE_DBUS
138   #include "dockmanagernotificationbackend.h"
139 #endif
140
141 #include "settingspages/aliasessettingspage.h"
142 #include "settingspages/appearancesettingspage.h"
143 #include "settingspages/backlogsettingspage.h"
144 #include "settingspages/bufferviewsettingspage.h"
145 #include "settingspages/chatmonitorsettingspage.h"
146 #include "settingspages/chatviewsettingspage.h"
147 #include "settingspages/chatviewcolorsettingspage.h"
148 #include "settingspages/connectionsettingspage.h"
149 #include "settingspages/coreaccountsettingspage.h"
150 #include "settingspages/coreconnectionsettingspage.h"
151 #include <settingspages/corehighlightsettingspage.h>
152 #include "settingspages/dccsettingspage.h"
153 #include "settingspages/highlightsettingspage.h"
154 #include "settingspages/identitiessettingspage.h"
155 #include "settingspages/ignorelistsettingspage.h"
156 #include "settingspages/inputwidgetsettingspage.h"
157 #include "settingspages/itemviewsettingspage.h"
158 #include "settingspages/networkssettingspage.h"
159 #include "settingspages/notificationssettingspage.h"
160 #include "settingspages/topicwidgetsettingspage.h"
161
162 #ifndef HAVE_KDE
163 #  include "settingspages/shortcutssettingspage.h"
164 #endif
165
166 #ifdef HAVE_SONNET
167 #  include "settingspages/sonnetsettingspage.h"
168 #endif
169
170
171 MainWin::MainWin(QWidget *parent)
172 #ifdef HAVE_KDE
173     : KMainWindow(parent), _kHelpMenu(new KHelpMenu(this)),
174 #else
175     : QMainWindow(parent),
176 #endif
177     _msgProcessorStatusWidget(new MsgProcessorStatusWidget(this)),
178     _coreConnectionStatusWidget(new CoreConnectionStatusWidget(Client::coreConnection(), this)),
179     _titleSetter(this),
180     _awayLog(0),
181     _layoutLoaded(false),
182     _activeBufferViewIndex(-1),
183     _aboutToQuit(false)
184 {
185     setAttribute(Qt::WA_DeleteOnClose, false); // we delete the mainwin manually
186
187     QtUiSettings uiSettings;
188     QString style = uiSettings.value("Style", QString()).toString();
189     if (!style.isEmpty()) {
190         QApplication::setStyle(style);
191     }
192
193     QApplication::setQuitOnLastWindowClosed(false);
194
195     setWindowTitle("Quassel IRC");
196     setWindowIconText("Quassel IRC");
197     // Set the default icon for all windows
198     QApplication::setWindowIcon(icon::get("quassel"));
199     updateIcon();
200 }
201
202
203 void MainWin::init()
204 {
205     connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
206     connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
207     connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
208         SLOT(messagesInserted(const QModelIndex &, int, int)));
209     connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
210     connect(Client::instance(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
211     connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showNetworkConfig(NetworkId)), SLOT(showNetworkConfig(NetworkId)));
212     connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showIgnoreList(QString)), SLOT(showIgnoreList(QString)));
213     connect(Client::instance(), SIGNAL(showIgnoreList(QString)), SLOT(showIgnoreList(QString)));
214     connect(Client::instance(), SIGNAL(dbUpgradeInProgress(bool)), SLOT(showMigrationWarning(bool)));
215
216     connect(Client::coreConnection(), SIGNAL(startCoreSetup(QVariantList, QVariantList)), SLOT(showCoreConfigWizard(QVariantList, QVariantList)));
217     connect(Client::coreConnection(), SIGNAL(connectionErrorPopup(QString)), SLOT(handleCoreConnectionError(QString)));
218     connect(Client::coreConnection(), SIGNAL(userAuthenticationRequired(CoreAccount *, bool *, QString)), SLOT(userAuthenticationRequired(CoreAccount *, bool *, QString)));
219     connect(Client::coreConnection(), SIGNAL(handleNoSslInClient(bool *)), SLOT(handleNoSslInClient(bool *)));
220     connect(Client::coreConnection(), SIGNAL(handleNoSslInCore(bool *)), SLOT(handleNoSslInCore(bool *)));
221 #ifdef HAVE_SSL
222     connect(Client::coreConnection(), SIGNAL(handleSslErrors(const QSslSocket *, bool *, bool *)), SLOT(handleSslErrors(const QSslSocket *, bool *, bool *)));
223 #endif
224
225     // Setup Dock Areas
226     setDockNestingEnabled(true);
227     setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
228     setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
229     setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
230     setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
231
232     // Order is sometimes important
233     setupActions();
234     setupBufferWidget();
235     setupMenus();
236     // setupTransferWidget(); not ready yet
237     setupChatMonitor();
238     setupTopicWidget();
239     setupInputWidget();
240     setupNickWidget();
241     setupViewMenuTail();
242     setupStatusBar();
243     setupToolBars();
244     setupSystray();
245     setupTitleSetter();
246     setupHotList();
247
248     _bufferWidget->setFocusProxy(_inputWidget);
249     _chatMonitorView->setFocusProxy(_inputWidget);
250
251 #ifndef HAVE_KDE
252 #  ifdef HAVE_QTMULTIMEDIA
253     QtUi::registerNotificationBackend(new QtMultimediaNotificationBackend(this));
254 #  endif
255 #  ifdef HAVE_PHONON
256     QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
257 #  endif
258     QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
259 #else /* HAVE_KDE */
260     QtUi::registerNotificationBackend(new KNotificationBackend(this));
261 #endif /* HAVE_KDE */
262
263
264 #ifndef QT_NO_SYSTEMTRAYICON
265     QtUi::registerNotificationBackend(new SystrayAnimationNotificationBackend(this));
266 #endif
267 #ifdef HAVE_LIBSNORE
268     QtUi::registerNotificationBackend(new SnoreNotificationBackend(this));
269 #elif !defined(QT_NO_SYSTEMTRAYICON) && !defined(HAVE_KDE)
270     QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
271 #endif
272
273 #ifdef HAVE_INDICATEQT
274     QtUi::registerNotificationBackend(new IndicatorNotificationBackend(this));
275 #endif
276
277 #ifdef HAVE_NOTIFICATION_CENTER
278     QtUi::registerNotificationBackend(new OSXNotificationBackend(this));
279 #endif
280
281 #ifdef HAVE_DBUS
282     QtUi::registerNotificationBackend(new DockManagerNotificationBackend(this));
283 #endif
284
285     // we assume that at this point, all configurable actions are defined!
286     QtUi::loadShortcuts();
287
288     connect(bufferWidget(), SIGNAL(currentChanged(BufferId)), SLOT(currentBufferChanged(BufferId)));
289
290     setDisconnectedState(); // Disable menus and stuff
291
292 #ifdef HAVE_KDE
293     setAutoSaveSettings();
294 #endif
295
296     // restore mainwin state
297     QtUiSettings s;
298     restoreStateFromSettings(s);
299
300     // restore locked state of docks
301     QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
302
303     QTimer::singleShot(0, this, SLOT(doAutoConnect()));
304 }
305
306
307 MainWin::~MainWin()
308 {
309 }
310
311
312 void MainWin::quit()
313 {
314     QtUiSettings s;
315     saveStateToSettings(s);
316     saveLayout();
317     QApplication::quit();
318 }
319
320
321 void MainWin::saveStateToSettings(UiSettings &s)
322 {
323     s.setValue("MainWinSize", _normalSize);
324     s.setValue("MainWinPos", _normalPos);
325     s.setValue("MainWinState", saveState());
326     s.setValue("MainWinGeometry", saveGeometry());
327     s.setValue("MainWinMinimized", isMinimized());
328     s.setValue("MainWinMaximized", isMaximized());
329     s.setValue("MainWinHidden", !isVisible());
330     BufferId lastBufId = Client::bufferModel()->currentBuffer();
331     if (lastBufId.isValid())
332         s.setValue("LastUsedBufferId", lastBufId.toInt());
333
334 #ifdef HAVE_KDE
335     saveAutoSaveSettings();
336 #endif
337 }
338
339
340 void MainWin::restoreStateFromSettings(UiSettings &s)
341 {
342     _normalSize = s.value("MainWinSize", size()).toSize();
343     _normalPos = s.value("MainWinPos", pos()).toPoint();
344     bool maximized = s.value("MainWinMaximized", false).toBool();
345
346 #ifndef HAVE_KDE
347     restoreGeometry(s.value("MainWinGeometry").toByteArray());
348
349     if (maximized) {
350         // restoreGeometry() fails if the windows was maximized, so we resize and position explicitly
351         resize(_normalSize);
352         move(_normalPos);
353     }
354
355     restoreState(s.value("MainWinState").toByteArray());
356
357 #else
358     move(_normalPos);
359 #endif
360
361     if ((Quassel::isOptionSet("hidewindow")
362             || s.value("MainWinHidden").toBool())
363             && _systemTray->isSystemTrayAvailable())
364         QtUi::hideMainWidget();
365     else if (s.value("MainWinMinimized").toBool())
366         showMinimized();
367     else if (maximized)
368         showMaximized();
369     else
370         show();
371 }
372
373 QMenu *MainWin::createPopupMenu()
374 {
375     QMenu *popupMenu = QMainWindow::createPopupMenu();
376     popupMenu->addSeparator();
377     ActionCollection *coll = QtUi::actionCollection("General");
378     popupMenu->addAction(coll->action("ToggleMenuBar"));
379     return popupMenu;
380 }
381
382
383 void MainWin::updateIcon()
384 {
385     QIcon icon;
386     if (Client::isConnected())
387         icon = icon::get("quassel");
388     else
389         icon = icon::get("inactive-quassel");
390     setWindowIcon(icon);
391 }
392
393
394 void MainWin::setupActions()
395 {
396     ActionCollection *coll = QtUi::actionCollection("General", tr("General"));
397     // File
398     coll->addAction("ConnectCore", new Action(icon::get("connect-quassel"), tr("&Connect to Core..."), coll,
399             this, SLOT(showCoreConnectionDlg())));
400     coll->addAction("DisconnectCore", new Action(icon::get("disconnect-quassel"), tr("&Disconnect from Core"), coll,
401             Client::instance(), SLOT(disconnectFromCore())));
402     coll->addAction("ChangePassword", new Action(icon::get("dialog-password"), tr("Change &Password..."), coll,
403             this, SLOT(showPasswordChangeDlg())));
404     coll->addAction("CoreInfo", new Action(icon::get("help-about"), tr("Core &Info..."), coll,
405             this, SLOT(showCoreInfoDlg())));
406     coll->addAction("ConfigureNetworks", new Action(icon::get("configure"), tr("Configure &Networks..."), coll,
407             this, SLOT(on_actionConfigureNetworks_triggered())));
408     // QKeySequence::Quit was added in Qt 4.6, and could be used instead.  However, that key
409     // sequence is empty by default on Windows, which would remove Ctrl-Q to quit.  It may be best
410     // to just keep it this way.
411     //
412     // See https://doc.qt.io/qt-5/qkeysequence.html
413     coll->addAction("Quit", new Action(icon::get("application-exit"), tr("&Quit"), coll,
414             this, SLOT(quit()), Qt::CTRL + Qt::Key_Q));
415
416     // View
417     coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Chat Lists..."), coll,
418             this, SLOT(on_actionConfigureViews_triggered())));
419
420     QAction *lockAct = coll->addAction("LockLayout", new Action(tr("&Lock Layout"), coll));
421     lockAct->setCheckable(true);
422     connect(lockAct, SIGNAL(toggled(bool)), SLOT(on_actionLockLayout_toggled(bool)));
423
424     coll->addAction("ToggleSearchBar", new Action(icon::get("edit-find"), tr("Show &Search Bar"), coll,
425             0, 0, QKeySequence::Find))->setCheckable(true);
426     coll->addAction("ShowAwayLog", new Action(tr("Show Away Log"), coll,
427             this, SLOT(showAwayLog())));
428     coll->addAction("ToggleMenuBar", new Action(icon::get("show-menu"), tr("Show &Menubar"), coll,
429             0, 0))->setCheckable(true);
430
431     coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
432             0, 0))->setCheckable(true);
433
434 #ifdef HAVE_KDE
435     _fullScreenAction = KStandardAction::fullScreen(this, SLOT(onFullScreenToggled()), this, coll);
436 #else
437     _fullScreenAction = new Action(icon::get("view-fullscreen"), tr("&Full Screen Mode"), coll,
438         this, SLOT(onFullScreenToggled()), QKeySequence(Qt::Key_F11));
439     _fullScreenAction->setCheckable(true);
440     coll->addAction("ToggleFullScreen", _fullScreenAction);
441 #endif
442
443     // Settings
444     QAction *configureShortcutsAct = new Action(icon::get("configure-shortcuts"), tr("Configure &Shortcuts..."), coll,
445         this, SLOT(showShortcutsDlg()));
446     configureShortcutsAct->setMenuRole(QAction::NoRole);
447     coll->addAction("ConfigureShortcuts", configureShortcutsAct);
448
449 #ifdef Q_OS_MAC
450     QAction *configureQuasselAct = new Action(icon::get("configure"), tr("&Configure Quassel..."), coll,
451         this, SLOT(showSettingsDlg()));
452     configureQuasselAct->setMenuRole(QAction::PreferencesRole);
453 #else
454     QAction *configureQuasselAct = new Action(icon::get("configure"), tr("&Configure Quassel..."), coll,
455         this, SLOT(showSettingsDlg()), QKeySequence(Qt::Key_F7));
456 #endif
457     coll->addAction("ConfigureQuassel", configureQuasselAct);
458
459     // Help
460     QAction *aboutQuasselAct = new Action(icon::get("quassel"), tr("&About Quassel"), coll,
461         this, SLOT(showAboutDlg()));
462     aboutQuasselAct->setMenuRole(QAction::AboutRole);
463     coll->addAction("AboutQuassel", aboutQuasselAct);
464
465     QAction *aboutQtAct = new Action(QIcon(":/pics/qt-logo.png"), tr("About &Qt"), coll,
466         qApp, SLOT(aboutQt()));
467     aboutQtAct->setMenuRole(QAction::AboutQtRole);
468     coll->addAction("AboutQt", aboutQtAct);
469     coll->addAction("DebugNetworkModel", new Action(icon::get("tools-report-bug"), tr("Debug &NetworkModel"), coll,
470             this, SLOT(on_actionDebugNetworkModel_triggered())));
471     coll->addAction("DebugBufferViewOverlay", new Action(icon::get("tools-report-bug"), tr("Debug &BufferViewOverlay"), coll,
472             this, SLOT(on_actionDebugBufferViewOverlay_triggered())));
473     coll->addAction("DebugMessageModel", new Action(icon::get("tools-report-bug"), tr("Debug &MessageModel"), coll,
474             this, SLOT(on_actionDebugMessageModel_triggered())));
475     coll->addAction("DebugHotList", new Action(icon::get("tools-report-bug"), tr("Debug &HotList"), coll,
476             this, SLOT(on_actionDebugHotList_triggered())));
477     coll->addAction("DebugLog", new Action(icon::get("tools-report-bug"), tr("Debug &Log"), coll,
478             this, SLOT(on_actionDebugLog_triggered())));
479     coll->addAction("ReloadStyle", new Action(icon::get("view-refresh"), tr("Reload Stylesheet"), coll,
480             QtUi::style(), SLOT(reload()), QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_R)));
481
482     coll->addAction("HideCurrentBuffer", new Action(tr("Hide Current Buffer"), coll,
483             this, SLOT(hideCurrentBuffer()), QKeySequence::Close));
484
485     // Text formatting
486     coll = QtUi::actionCollection("TextFormat", tr("Text formatting"));
487
488     coll->addAction("FormatApplyColor", new Action(
489                         icon::get("format-text-color"), tr("Apply foreground color"), coll,
490                         this, SLOT(on_inputFormatApplyColor_triggered()),
491                         QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_G)));
492
493     coll->addAction("FormatApplyColorFill", new Action(
494                         icon::get("format-fill-color"), tr("Apply background color"), coll,
495                         this, SLOT(on_inputFormatApplyColorFill_triggered()),
496                         QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_B)));
497
498     coll->addAction("FormatClear", new Action(
499                         icon::get("edit-clear"), tr("Clear formatting"), coll,
500                         this, SLOT(on_inputFormatClear_triggered()),
501                         QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C)));
502
503     coll->addAction("FormatBold", new Action(
504                         icon::get("format-text-bold"), tr("Toggle bold"), coll,
505                         this, SLOT(on_inputFormatBold_triggered()),
506                         QKeySequence::Bold));
507
508     coll->addAction("FormatItalic", new Action(
509                         icon::get("format-text-italic"), tr("Toggle italics"), coll,
510                         this, SLOT(on_inputFormatItalic_triggered()),
511                         QKeySequence::Italic));
512
513     coll->addAction("FormatUnderline", new Action(
514                         icon::get("format-text-underline"), tr("Toggle underline"), coll,
515                         this, SLOT(on_inputFormatUnderline_triggered()), QKeySequence::Underline));
516
517     // Navigation
518     coll = QtUi::actionCollection("Navigation", tr("Navigation"));
519
520     coll->addAction("JumpHotBuffer", new Action(tr("Jump to hot chat"), coll,
521             this, SLOT(on_jumpHotBuffer_triggered()), QKeySequence(Qt::META + Qt::Key_A)));
522
523     coll->addAction("ActivateBufferFilter", new Action(tr("Activate the buffer search"), coll,
524             this, SLOT(on_bufferSearch_triggered()), QKeySequence(Qt::CTRL + Qt::Key_S)));
525
526     // Jump keys
527 #ifdef Q_OS_MAC
528     const int bindModifier = Qt::ControlModifier | Qt::AltModifier;
529     const int jumpModifier = Qt::ControlModifier;
530 #else
531     const int bindModifier = Qt::ControlModifier;
532     const int jumpModifier = Qt::AltModifier;
533 #endif
534
535     coll->addAction("BindJumpKey0", new Action(tr("Set Quick Access #0"), coll, this, SLOT(bindJumpKey()),
536             QKeySequence(bindModifier + Qt::Key_0)))->setProperty("Index", 0);
537     coll->addAction("BindJumpKey1", new Action(tr("Set Quick Access #1"), coll, this, SLOT(bindJumpKey()),
538             QKeySequence(bindModifier + Qt::Key_1)))->setProperty("Index", 1);
539     coll->addAction("BindJumpKey2", new Action(tr("Set Quick Access #2"), coll, this, SLOT(bindJumpKey()),
540             QKeySequence(bindModifier + Qt::Key_2)))->setProperty("Index", 2);
541     coll->addAction("BindJumpKey3", new Action(tr("Set Quick Access #3"), coll, this, SLOT(bindJumpKey()),
542             QKeySequence(bindModifier + Qt::Key_3)))->setProperty("Index", 3);
543     coll->addAction("BindJumpKey4", new Action(tr("Set Quick Access #4"), coll, this, SLOT(bindJumpKey()),
544             QKeySequence(bindModifier + Qt::Key_4)))->setProperty("Index", 4);
545     coll->addAction("BindJumpKey5", new Action(tr("Set Quick Access #5"), coll, this, SLOT(bindJumpKey()),
546             QKeySequence(bindModifier + Qt::Key_5)))->setProperty("Index", 5);
547     coll->addAction("BindJumpKey6", new Action(tr("Set Quick Access #6"), coll, this, SLOT(bindJumpKey()),
548             QKeySequence(bindModifier + Qt::Key_6)))->setProperty("Index", 6);
549     coll->addAction("BindJumpKey7", new Action(tr("Set Quick Access #7"), coll, this, SLOT(bindJumpKey()),
550             QKeySequence(bindModifier + Qt::Key_7)))->setProperty("Index", 7);
551     coll->addAction("BindJumpKey8", new Action(tr("Set Quick Access #8"), coll, this, SLOT(bindJumpKey()),
552             QKeySequence(bindModifier + Qt::Key_8)))->setProperty("Index", 8);
553     coll->addAction("BindJumpKey9", new Action(tr("Set Quick Access #9"), coll, this, SLOT(bindJumpKey()),
554             QKeySequence(bindModifier + Qt::Key_9)))->setProperty("Index", 9);
555
556     coll->addAction("JumpKey0", new Action(tr("Quick Access #0"), coll, this, SLOT(onJumpKey()),
557             QKeySequence(jumpModifier + Qt::Key_0)))->setProperty("Index", 0);
558     coll->addAction("JumpKey1", new Action(tr("Quick Access #1"), coll, this, SLOT(onJumpKey()),
559             QKeySequence(jumpModifier + Qt::Key_1)))->setProperty("Index", 1);
560     coll->addAction("JumpKey2", new Action(tr("Quick Access #2"), coll, this, SLOT(onJumpKey()),
561             QKeySequence(jumpModifier + Qt::Key_2)))->setProperty("Index", 2);
562     coll->addAction("JumpKey3", new Action(tr("Quick Access #3"), coll, this, SLOT(onJumpKey()),
563             QKeySequence(jumpModifier + Qt::Key_3)))->setProperty("Index", 3);
564     coll->addAction("JumpKey4", new Action(tr("Quick Access #4"), coll, this, SLOT(onJumpKey()),
565             QKeySequence(jumpModifier + Qt::Key_4)))->setProperty("Index", 4);
566     coll->addAction("JumpKey5", new Action(tr("Quick Access #5"), coll, this, SLOT(onJumpKey()),
567             QKeySequence(jumpModifier + Qt::Key_5)))->setProperty("Index", 5);
568     coll->addAction("JumpKey6", new Action(tr("Quick Access #6"), coll, this, SLOT(onJumpKey()),
569             QKeySequence(jumpModifier + Qt::Key_6)))->setProperty("Index", 6);
570     coll->addAction("JumpKey7", new Action(tr("Quick Access #7"), coll, this, SLOT(onJumpKey()),
571             QKeySequence(jumpModifier + Qt::Key_7)))->setProperty("Index", 7);
572     coll->addAction("JumpKey8", new Action(tr("Quick Access #8"), coll, this, SLOT(onJumpKey()),
573             QKeySequence(jumpModifier + Qt::Key_8)))->setProperty("Index", 8);
574     coll->addAction("JumpKey9", new Action(tr("Quick Access #9"), coll, this, SLOT(onJumpKey()),
575             QKeySequence(jumpModifier + Qt::Key_9)))->setProperty("Index", 9);
576
577     // Buffer navigation
578     coll->addAction("NextBufferView", new Action(icon::get("go-next-view"), tr("Activate Next Chat List"), coll,
579             this, SLOT(nextBufferView()), QKeySequence(QKeySequence::Forward)));
580     coll->addAction("PreviousBufferView", new Action(icon::get("go-previous-view"), tr("Activate Previous Chat List"), coll,
581             this, SLOT(previousBufferView()), QKeySequence::Back));
582     coll->addAction("NextBuffer", new Action(icon::get("go-down"), tr("Go to Next Chat"), coll,
583             this, SLOT(nextBuffer()), QKeySequence(Qt::ALT + Qt::Key_Down)));
584     coll->addAction("PreviousBuffer", new Action(icon::get("go-up"), tr("Go to Previous Chat"), coll,
585             this, SLOT(previousBuffer()), QKeySequence(Qt::ALT + Qt::Key_Up)));
586 }
587
588
589 void MainWin::setupMenus()
590 {
591     ActionCollection *coll = QtUi::actionCollection("General");
592
593     _fileMenu = menuBar()->addMenu(tr("&File"));
594
595     static const QStringList coreActions = QStringList()
596         << "ConnectCore" << "DisconnectCore" << "ChangePassword" << "CoreInfo";
597
598     QAction *coreAction;
599     foreach(QString actionName, coreActions) {
600         coreAction = coll->action(actionName);
601         _fileMenu->addAction(coreAction);
602         flagRemoteCoreOnly(coreAction);
603     }
604     flagRemoteCoreOnly(_fileMenu->addSeparator());
605
606     _networksMenu = _fileMenu->addMenu(tr("&Networks"));
607     _networksMenu->addAction(coll->action("ConfigureNetworks"));
608     _networksMenu->addSeparator();
609     _fileMenu->addSeparator();
610     _fileMenu->addAction(coll->action("Quit"));
611
612     _viewMenu = menuBar()->addMenu(tr("&View"));
613     _bufferViewsMenu = _viewMenu->addMenu(tr("&Chat Lists"));
614     _bufferViewsMenu->addAction(coll->action("ConfigureBufferViews"));
615     _toolbarMenu = _viewMenu->addMenu(tr("&Toolbars"));
616     _viewMenu->addSeparator();
617
618     _viewMenu->addAction(coll->action("ToggleMenuBar"));
619     _viewMenu->addAction(coll->action("ToggleStatusBar"));
620     _viewMenu->addAction(coll->action("ToggleSearchBar"));
621
622     coreAction = coll->action("ShowAwayLog");
623     flagRemoteCoreOnly(coreAction);
624     _viewMenu->addAction(coreAction);
625
626     _viewMenu->addSeparator();
627     _viewMenu->addAction(coll->action("LockLayout"));
628
629     _settingsMenu = menuBar()->addMenu(tr("&Settings"));
630 #ifdef HAVE_KDE
631     _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
632     _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
633 #else
634     _settingsMenu->addAction(coll->action("ConfigureShortcuts"));
635 #endif
636     _settingsMenu->addAction(coll->action("ConfigureQuassel"));
637
638
639     _helpMenu = menuBar()->addMenu(tr("&Help"));
640
641     _helpMenu->addAction(coll->action("AboutQuassel"));
642 #ifndef HAVE_KDE
643     _helpMenu->addAction(coll->action("AboutQt"));
644 #else
645     _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
646 #endif
647     _helpMenu->addSeparator();
648     _helpDebugMenu = _helpMenu->addMenu(icon::get("tools-report-bug"), tr("Debug"));
649     _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
650     _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
651     _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
652     _helpDebugMenu->addAction(coll->action("DebugHotList"));
653     _helpDebugMenu->addAction(coll->action("DebugLog"));
654     _helpDebugMenu->addSeparator();
655     _helpDebugMenu->addAction(coll->action("ReloadStyle"));
656
657     // Toggle visibility
658     QAction *showMenuBar = QtUi::actionCollection("General")->action("ToggleMenuBar");
659
660     QtUiSettings uiSettings;
661     bool enabled = uiSettings.value("ShowMenuBar", QVariant(true)).toBool();
662     showMenuBar->setChecked(enabled);
663     enabled ? menuBar()->show() : menuBar()->hide();
664
665     connect(showMenuBar, SIGNAL(toggled(bool)), menuBar(), SLOT(setVisible(bool)));
666     connect(showMenuBar, SIGNAL(toggled(bool)), this, SLOT(saveMenuBarStatus(bool)));
667 }
668
669
670 void MainWin::setupBufferWidget()
671 {
672     _bufferWidget = new BufferWidget(this);
673     _bufferWidget->setModel(Client::bufferModel());
674     _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
675     setCentralWidget(_bufferWidget);
676 }
677
678
679 void MainWin::addBufferView(int bufferViewConfigId)
680 {
681     addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
682 }
683
684
685 void MainWin::addBufferView(ClientBufferViewConfig *config)
686 {
687     if (!config)
688         return;
689
690     config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
691     BufferViewDock *dock = new BufferViewDock(config, this);
692
693     //create the view and initialize it's filter
694     BufferView *view = new BufferView(dock);
695     view->setFilteredModel(Client::bufferModel(), config);
696     view->installEventFilter(_inputWidget); // for key presses
697
698     Client::bufferModel()->synchronizeView(view);
699
700     dock->setLocked(QtUiSettings().value("LockLayout", false).toBool());
701
702     dock->setWidget(view);
703     dock->setVisible(_layoutLoaded); // don't show before state has been restored
704
705     addDockWidget(Qt::LeftDockWidgetArea, dock);
706     _bufferViewsMenu->addAction(dock->toggleViewAction());
707
708     connect(dock->toggleViewAction(), SIGNAL(toggled(bool)), this, SLOT(bufferViewToggled(bool)));
709     connect(dock, SIGNAL(visibilityChanged(bool)), SLOT(bufferViewVisibilityChanged(bool)));
710     _bufferViews.append(dock);
711
712     if (!activeBufferView())
713         nextBufferView();
714 }
715
716
717 void MainWin::removeBufferView(int bufferViewConfigId)
718 {
719     QVariant actionData;
720     BufferViewDock *dock;
721     foreach(QAction *action, _bufferViewsMenu->actions()) {
722         actionData = action->data();
723         if (!actionData.isValid())
724             continue;
725
726         dock = qobject_cast<BufferViewDock *>(action->parent());
727         if (dock && actionData.toInt() == bufferViewConfigId) {
728             removeAction(action);
729             Client::bufferViewOverlay()->removeView(dock->bufferViewId());
730             _bufferViews.removeAll(dock);
731
732             if (dock->isActive()) {
733                 dock->setActive(false);
734                 _activeBufferViewIndex = -1;
735                 nextBufferView();
736             }
737
738             dock->deleteLater();
739         }
740     }
741 }
742
743
744 void MainWin::bufferViewToggled(bool enabled)
745 {
746     if (!enabled && !isMinimized()) {
747         // hiding the mainwindow triggers a toggle of the bufferview (which pretty much sucks big time)
748         // since this isn't our fault and we can't do anything about it, we suppress the resulting calls
749         return;
750     }
751     QAction *action = qobject_cast<QAction *>(sender());
752     Q_ASSERT(action);
753     BufferViewDock *dock = qobject_cast<BufferViewDock *>(action->parent());
754     Q_ASSERT(dock);
755
756     // Make sure we don't toggle backlog fetch for a view we've already removed
757     if (!_bufferViews.contains(dock))
758         return;
759
760     if (enabled)
761         Client::bufferViewOverlay()->addView(dock->bufferViewId());
762     else
763         Client::bufferViewOverlay()->removeView(dock->bufferViewId());
764 }
765
766
767 void MainWin::bufferViewVisibilityChanged(bool visible)
768 {
769     Q_UNUSED(visible);
770     BufferViewDock *dock = qobject_cast<BufferViewDock *>(sender());
771     Q_ASSERT(dock);
772     if ((!dock->isHidden() && !activeBufferView()) || (dock->isHidden() && dock->isActive()))
773         nextBufferView();
774 }
775
776
777 BufferView *MainWin::allBuffersView() const
778 {
779     // "All Buffers" is always the first dock created
780     if (_bufferViews.count() > 0)
781         return _bufferViews[0]->bufferView();
782     return 0;
783 }
784
785
786 BufferView *MainWin::activeBufferView() const
787 {
788     if (_activeBufferViewIndex < 0 || _activeBufferViewIndex >= _bufferViews.count())
789         return 0;
790     BufferViewDock *dock = _bufferViews.at(_activeBufferViewIndex);
791     return dock->isActive() ? dock->bufferView() : 0;
792 }
793
794
795 void MainWin::changeActiveBufferView(int bufferViewId)
796 {
797     if (bufferViewId < 0)
798         return;
799
800     if (_activeBufferViewIndex >= 0 && _activeBufferViewIndex < _bufferViews.count()) {
801         _bufferViews[_activeBufferViewIndex]->setActive(false);
802         _activeBufferViewIndex = -1;
803     }
804
805     for (int i = 0; i < _bufferViews.count(); i++) {
806         BufferViewDock *dock = _bufferViews.at(i);
807         if (dock->bufferViewId() == bufferViewId && !dock->isHidden()) {
808             _activeBufferViewIndex = i;
809             dock->setActive(true);
810             return;
811         }
812     }
813
814     nextBufferView(); // fallback
815 }
816
817
818 void MainWin::showPasswordChangeDlg()
819 {
820     if(Client::isCoreFeatureEnabled(Quassel::Feature::PasswordChange)) {
821         PasswordChangeDlg dlg(this);
822         dlg.exec();
823     }
824     else {
825         QMessageBox box(QMessageBox::Warning, tr("Feature Not Supported"),
826                         tr("<b>Your Quassel Core does not support this feature</b>"),
827                         QMessageBox::Ok, this);
828         box.setInformativeText(tr("You need a Quassel Core v0.12.0 or newer in order to be able to remotely change your password."));
829         box.exec();
830     }
831 }
832
833
834 void MainWin::showMigrationWarning(bool show)
835 {
836     if (show && !_migrationWarning) {
837         _migrationWarning = new QMessageBox(QMessageBox::Information,
838                                             tr("Upgrading..."),
839                                             "<b>" + tr("Your database is being upgraded") + "</b>",
840                                             QMessageBox::NoButton, this);
841         _migrationWarning->setInformativeText("<p>"
842                                               + tr("In order to support new features, we need to make changes to your backlog database. This may take a long while.")
843                                               + "</p><p>"
844                                               + tr("Do not exit Quassel until the upgrade is complete!")
845                                               + "</p>");
846         _migrationWarning->setStandardButtons(QMessageBox::NoButton);
847         _migrationWarning->show();
848     }
849     else if (!show && _migrationWarning) {
850         _migrationWarning->close();
851         _migrationWarning->deleteLater();
852         _migrationWarning = nullptr;
853     }
854 }
855
856
857 void MainWin::changeActiveBufferView(bool backwards)
858 {
859     if (_activeBufferViewIndex >= 0 && _activeBufferViewIndex < _bufferViews.count()) {
860         _bufferViews[_activeBufferViewIndex]->setActive(false);
861     }
862
863     if (!_bufferViews.count())
864         return;
865
866     int c = _bufferViews.count();
867     while (c--) { // yes, this will reactivate the current active one if all others fail
868         if (backwards) {
869             if (--_activeBufferViewIndex < 0)
870                 _activeBufferViewIndex = _bufferViews.count()-1;
871         }
872         else {
873             if (++_activeBufferViewIndex >= _bufferViews.count())
874                 _activeBufferViewIndex = 0;
875         }
876
877         BufferViewDock *dock = _bufferViews.at(_activeBufferViewIndex);
878         if (dock->isHidden())
879             continue;
880
881         dock->setActive(true);
882         return;
883     }
884
885     _activeBufferViewIndex = -1;
886 }
887
888
889 void MainWin::nextBufferView()
890 {
891     changeActiveBufferView(false);
892 }
893
894
895 void MainWin::previousBufferView()
896 {
897     changeActiveBufferView(true);
898 }
899
900
901 void MainWin::nextBuffer()
902 {
903     BufferView *view = activeBufferView();
904     if (view)
905         view->nextBuffer();
906 }
907
908
909 void MainWin::previousBuffer()
910 {
911     BufferView *view = activeBufferView();
912     if (view)
913         view->previousBuffer();
914 }
915
916
917 void MainWin::hideCurrentBuffer()
918 {
919     BufferView *view = activeBufferView();
920     if (view)
921         view->hideCurrentBuffer();
922 }
923
924
925 void MainWin::showNotificationsDlg()
926 {
927     SettingsPageDlg dlg(new NotificationsSettingsPage(this), this);
928     dlg.exec();
929 }
930
931
932 void MainWin::on_actionConfigureNetworks_triggered()
933 {
934     SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
935     dlg.exec();
936 }
937
938
939 void MainWin::on_actionConfigureViews_triggered()
940 {
941     SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
942     dlg.exec();
943 }
944
945
946 void MainWin::on_actionLockLayout_toggled(bool lock)
947 {
948     QList<VerticalDock *> docks = findChildren<VerticalDock *>();
949     foreach(VerticalDock *dock, docks) {
950         dock->showTitle(!lock);
951     }
952
953     QList<NickListDock *> nickdocks = findChildren<NickListDock *>();
954     foreach(NickListDock *nickdock, nickdocks) {
955         nickdock->setLocked(lock);
956     }
957
958     QList<BufferViewDock *> bufferdocks = findChildren<BufferViewDock *>();
959     foreach(BufferViewDock *bufferdock, bufferdocks) {
960         bufferdock->setLocked(lock);
961     }
962
963     if (Client::bufferViewManager()) {
964         foreach(ClientBufferViewConfig *config, Client::bufferViewManager()->clientBufferViewConfigs()) {
965             config->setLocked(lock);
966         }
967     }
968
969     _mainToolBar->setMovable(!lock);
970     _nickToolBar->setMovable(!lock);
971
972     QtUiSettings().setValue("LockLayout", lock);
973 }
974
975
976 void MainWin::setupNickWidget()
977 {
978     // create nick dock
979     NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
980     nickDock->setObjectName("NickDock");
981     nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
982     nickDock->setLocked(QtUiSettings().value("LockLayout", false).toBool());
983
984     _nickListWidget = new NickListWidget(nickDock);
985     nickDock->setWidget(_nickListWidget);
986
987     addDockWidget(Qt::RightDockWidgetArea, nickDock);
988     _viewMenu->addAction(nickDock->toggleViewAction());
989     nickDock->toggleViewAction()->setText(tr("Show Nick List"));
990
991     // See NickListDock::NickListDock();
992     // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
993
994     // attach the NickListWidget to the BufferModel and the default selection
995     _nickListWidget->setModel(Client::bufferModel());
996     _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
997
998     _nickListWidget->setVisible(false);
999 }
1000
1001
1002 void MainWin::setupChatMonitor()
1003 {
1004     VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
1005     dock->setObjectName("ChatMonitorDock");
1006
1007     ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
1008     _chatMonitorView = new ChatMonitorView(filter, this);
1009     _chatMonitorView->show();
1010     dock->setWidget(_chatMonitorView);
1011     dock->hide();
1012
1013     addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
1014     _viewMenu->addAction(dock->toggleViewAction());
1015     dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
1016 }
1017
1018
1019 void MainWin::setupInputWidget()
1020 {
1021     VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
1022     dock->setObjectName("InputDock");
1023
1024     _inputWidget = new InputWidget(dock);
1025     dock->setWidget(_inputWidget);
1026
1027     addDockWidget(Qt::BottomDockWidgetArea, dock);
1028
1029     _viewMenu->addAction(dock->toggleViewAction());
1030     dock->toggleViewAction()->setText(tr("Show Input Line"));
1031
1032     _inputWidget->setModel(Client::bufferModel());
1033     _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
1034
1035     _inputWidget->inputLine()->installEventFilter(_bufferWidget);
1036
1037     connect(_topicWidget, SIGNAL(switchedPlain()), _bufferWidget, SLOT(setFocus()));
1038 }
1039
1040
1041 void MainWin::setupTopicWidget()
1042 {
1043     VerticalDock *dock = new VerticalDock(tr("Topic"), this);
1044     dock->setObjectName("TopicDock");
1045     _topicWidget = new TopicWidget(dock);
1046
1047     dock->setWidget(_topicWidget);
1048
1049     _topicWidget->setModel(Client::bufferModel());
1050     _topicWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
1051
1052     addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
1053
1054     _viewMenu->addAction(dock->toggleViewAction());
1055     dock->toggleViewAction()->setText(tr("Show Topic Line"));
1056 }
1057
1058
1059 void MainWin::setupTransferWidget()
1060 {
1061     auto dock = new QDockWidget(tr("Transfers"), this);
1062     dock->setObjectName("TransferDock");
1063     dock->setAllowedAreas(Qt::TopDockWidgetArea|Qt::BottomDockWidgetArea);
1064
1065     auto view = new QTableView(dock); // to be replaced by the real thing
1066     view->setModel(Client::transferModel());
1067     dock->setWidget(view);
1068     dock->hide(); // hidden by default
1069     addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
1070
1071     auto action = dock->toggleViewAction();
1072     action->setText(tr("Show File Transfers"));
1073     action->setIcon(icon::get("download"));
1074     action->setShortcut(QKeySequence(Qt::Key_F6));
1075     QtUi::actionCollection("General")->addAction("ShowTransferWidget", action);
1076     _viewMenu->addAction(action);
1077 }
1078
1079
1080 void MainWin::setupViewMenuTail()
1081 {
1082     _viewMenu->addSeparator();
1083     _viewMenu->addAction(_fullScreenAction);
1084 }
1085
1086
1087 void MainWin::setupTitleSetter()
1088 {
1089     _titleSetter.setModel(Client::bufferModel());
1090     _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
1091 }
1092
1093
1094 void MainWin::setupStatusBar()
1095 {
1096     // MessageProcessor progress
1097     statusBar()->addPermanentWidget(_msgProcessorStatusWidget);
1098
1099     // Connection state
1100     _coreConnectionStatusWidget->update();
1101     statusBar()->addPermanentWidget(_coreConnectionStatusWidget);
1102
1103     QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
1104
1105     QtUiSettings uiSettings;
1106
1107     bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
1108     showStatusbar->setChecked(enabled);
1109     enabled ? statusBar()->show() : statusBar()->hide();
1110
1111     connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
1112     connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
1113
1114     connect(Client::coreConnection(), SIGNAL(connectionMsg(QString)), statusBar(), SLOT(showMessage(QString)));
1115 }
1116
1117
1118 void MainWin::setupHotList()
1119 {
1120     FlatProxyModel *flatProxy = new FlatProxyModel(this);
1121     flatProxy->setSourceModel(Client::bufferModel());
1122     _bufferHotList = new BufferHotListFilter(flatProxy);
1123 }
1124
1125
1126 void MainWin::saveMenuBarStatus(bool enabled)
1127 {
1128     QtUiSettings uiSettings;
1129     uiSettings.setValue("ShowMenuBar", enabled);
1130 }
1131
1132
1133 void MainWin::saveStatusBarStatus(bool enabled)
1134 {
1135     QtUiSettings uiSettings;
1136     uiSettings.setValue("ShowStatusBar", enabled);
1137 }
1138
1139
1140 void MainWin::setupSystray()
1141 {
1142 #ifdef HAVE_DBUS
1143     _systemTray = new StatusNotifierItem(this);
1144 #elif !defined QT_NO_SYSTEMTRAYICON
1145     _systemTray = new LegacySystemTray(this);
1146 #else
1147     _systemTray = new SystemTray(this); // dummy
1148 #endif
1149 }
1150
1151
1152 void MainWin::setupToolBars()
1153 {
1154     connect(_bufferWidget, SIGNAL(currentChanged(QModelIndex)),
1155         QtUi::toolBarActionProvider(), SLOT(currentBufferChanged(QModelIndex)));
1156     connect(_nickListWidget, SIGNAL(nickSelectionChanged(QModelIndexList)),
1157         QtUi::toolBarActionProvider(), SLOT(nickSelectionChanged(QModelIndexList)));
1158
1159 #ifdef Q_OS_MAC
1160     setUnifiedTitleAndToolBarOnMac(true);
1161 #endif
1162
1163 #ifdef HAVE_KDE
1164     _mainToolBar = new KToolBar("MainToolBar", this, Qt::TopToolBarArea, false, true, true);
1165 #else
1166     _mainToolBar = new QToolBar(this);
1167     _mainToolBar->setObjectName("MainToolBar");
1168 #endif
1169     _mainToolBar->setWindowTitle(tr("Main Toolbar"));
1170     addToolBar(_mainToolBar);
1171
1172     if (Quassel::runMode() != Quassel::Monolithic) {
1173         ActionCollection *coll = QtUi::actionCollection("General");
1174         _mainToolBar->addAction(coll->action("ConnectCore"));
1175         _mainToolBar->addAction(coll->action("DisconnectCore"));
1176     }
1177
1178     _mainToolBar->setMovable(!QtUiSettings().value("LockLayout", false).toBool());
1179
1180     QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
1181     _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
1182
1183 #ifdef HAVE_KDE
1184     _nickToolBar = new KToolBar("NickToolBar", this, Qt::TopToolBarArea, false, true, true);
1185 #else
1186     _nickToolBar = new QToolBar(this);
1187     _nickToolBar->setObjectName("NickToolBar");
1188 #endif
1189     _nickToolBar->setWindowTitle(tr("Nick Toolbar"));
1190     _nickToolBar->setVisible(false); //default: not visible
1191     addToolBar(_nickToolBar);
1192     _nickToolBar->setMovable(!QtUiSettings().value("LockLayout", false).toBool());
1193
1194     QtUi::toolBarActionProvider()->addActions(_nickToolBar, ToolBarActionProvider::NickToolBar);
1195     _toolbarMenu->addAction(_nickToolBar->toggleViewAction());
1196
1197 #ifdef Q_OS_MAC
1198     QtUiSettings uiSettings;
1199
1200     bool visible = uiSettings.value("ShowMainToolBar", QVariant(true)).toBool();
1201     _mainToolBar->setVisible(visible);
1202     connect(_mainToolBar, SIGNAL(visibilityChanged(bool)), this, SLOT(saveMainToolBarStatus(bool)));
1203 #endif
1204 }
1205
1206 void MainWin::saveMainToolBarStatus(bool enabled)
1207 {
1208 #ifdef Q_OS_MAC
1209     QtUiSettings uiSettings;
1210     uiSettings.setValue("ShowMainToolBar", enabled);
1211 #else
1212     Q_UNUSED(enabled);
1213 #endif
1214 }
1215
1216
1217 void MainWin::doAutoConnect()
1218 {
1219     if (!Client::coreConnection()->connectToCore()) {
1220         // No autoconnect selected (or no accounts)
1221         showCoreConnectionDlg();
1222     }
1223 }
1224
1225
1226 void MainWin::connectedToCore()
1227 {
1228     Q_CHECK_PTR(Client::bufferViewManager());
1229     connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
1230     connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
1231     connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
1232
1233     if (Client::transferManager()) {
1234         connect(Client::transferManager(), SIGNAL(transferAdded(QUuid)), SLOT(showNewTransferDlg(QUuid)));
1235     }
1236
1237     setConnectedState();
1238 }
1239
1240
1241 void MainWin::setConnectedState()
1242 {
1243     ActionCollection *coll = QtUi::actionCollection("General");
1244
1245     coll->action("ConnectCore")->setEnabled(false);
1246     coll->action("DisconnectCore")->setEnabled(true);
1247     coll->action("ChangePassword")->setEnabled(true);
1248     coll->action("CoreInfo")->setEnabled(true);
1249
1250     foreach(QAction *action, _fileMenu->actions()) {
1251         if (isRemoteCoreOnly(action))
1252             action->setVisible(!Client::internalCore());
1253     }
1254
1255     disconnect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), _msgProcessorStatusWidget, SLOT(setProgress(int, int)));
1256     disconnect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
1257     disconnect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
1258     if (!Client::internalCore()) {
1259         connect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), _msgProcessorStatusWidget, SLOT(setProgress(int, int)));
1260         connect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
1261         connect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
1262     }
1263
1264     // _viewMenu->setEnabled(true);
1265     if (!Client::internalCore())
1266         statusBar()->showMessage(tr("Connected to core."));
1267     else
1268         statusBar()->clearMessage();
1269
1270     _coreConnectionStatusWidget->setVisible(!Client::internalCore());
1271     updateIcon();
1272     systemTray()->setState(SystemTray::Active);
1273
1274     if (Client::networkIds().isEmpty()) {
1275         IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
1276         wizard->show();
1277     }
1278     else {
1279         // Monolithic always preselects last used buffer - Client only if the connection died
1280         if (Client::coreConnection()->wasReconnect() || Quassel::runMode() == Quassel::Monolithic) {
1281             QtUiSettings s;
1282             BufferId lastUsedBufferId(s.value("LastUsedBufferId").toInt());
1283             if (lastUsedBufferId.isValid())
1284                 Client::bufferModel()->switchToBuffer(lastUsedBufferId);
1285         }
1286     }
1287 }
1288
1289
1290 void MainWin::loadLayout()
1291 {
1292     QtUiSettings s;
1293     int accountId = Client::currentCoreAccount().accountId().toInt();
1294     QByteArray state = s.value(QString("MainWinState-%1").arg(accountId)).toByteArray();
1295     if (state.isEmpty()) {
1296         foreach(BufferViewDock *view, _bufferViews)
1297         view->show();
1298         _layoutLoaded = true;
1299         return;
1300     }
1301     _nickListWidget->setVisible(true);
1302     restoreState(state, accountId);
1303     int bufferViewId = s.value(QString("ActiveBufferView-%1").arg(accountId), -1).toInt();
1304     if (bufferViewId >= 0)
1305         changeActiveBufferView(bufferViewId);
1306
1307     _layoutLoaded = true;
1308 }
1309
1310
1311 void MainWin::saveLayout()
1312 {
1313     QtUiSettings s;
1314     int accountId = _bufferViews.count() ? Client::currentCoreAccount().accountId().toInt() : 0; // only save if we still have a layout!
1315     if (accountId > 0) {
1316         s.setValue(QString("MainWinState-%1").arg(accountId), saveState(accountId));
1317         BufferView *view = activeBufferView();
1318         s.setValue(QString("ActiveBufferView-%1").arg(accountId), view ? view->config()->bufferViewId() : -1);
1319     }
1320 }
1321
1322
1323 void MainWin::disconnectedFromCore()
1324 {
1325     // save core specific layout and remove bufferviews;
1326     saveLayout();
1327     _layoutLoaded = false;
1328
1329     QVariant actionData;
1330     BufferViewDock *dock;
1331     foreach(QAction *action, _bufferViewsMenu->actions()) {
1332         actionData = action->data();
1333         if (!actionData.isValid())
1334             continue;
1335
1336         dock = qobject_cast<BufferViewDock *>(action->parent());
1337         if (dock && actionData.toInt() != -1) {
1338             removeAction(action);
1339             _bufferViews.removeAll(dock);
1340             dock->deleteLater();
1341         }
1342     }
1343
1344     // store last active buffer
1345     QtUiSettings s;
1346     BufferId lastBufId = _bufferWidget->currentBuffer();
1347     if (lastBufId.isValid()) {
1348         s.setValue("LastUsedBufferId", lastBufId.toInt());
1349         // clear the current selection
1350         Client::bufferModel()->standardSelectionModel()->clearSelection();
1351     }
1352     restoreState(s.value("MainWinState").toByteArray());
1353     setDisconnectedState();
1354 }
1355
1356
1357 void MainWin::setDisconnectedState()
1358 {
1359     ActionCollection *coll = QtUi::actionCollection("General");
1360     //ui.menuCore->setEnabled(false);
1361     coll->action("ConnectCore")->setEnabled(true);
1362     coll->action("DisconnectCore")->setEnabled(false);
1363     coll->action("CoreInfo")->setEnabled(false);
1364     coll->action("ChangePassword")->setEnabled(false);
1365     //_viewMenu->setEnabled(false);
1366     statusBar()->showMessage(tr("Not connected to core."));
1367     if (_msgProcessorStatusWidget)
1368         _msgProcessorStatusWidget->setProgress(0, 0);
1369     updateIcon();
1370     systemTray()->setState(SystemTray::Passive);
1371     _nickListWidget->setVisible(false);
1372 }
1373
1374
1375 void MainWin::userAuthenticationRequired(CoreAccount *account, bool *valid, const QString &errorMessage)
1376 {
1377     Q_UNUSED(errorMessage)
1378     CoreConnectAuthDlg dlg(account, this);
1379     *valid = (dlg.exec() == QDialog::Accepted);
1380 }
1381
1382
1383 void MainWin::handleNoSslInClient(bool *accepted)
1384 {
1385     QMessageBox box(QMessageBox::Warning, tr("Unencrypted Connection"), tr("<b>Your client does not support SSL encryption</b>"),
1386         QMessageBox::Ignore|QMessageBox::Cancel, this);
1387     box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
1388     box.setDefaultButton(QMessageBox::Ignore);
1389     *accepted = box.exec() == QMessageBox::Ignore;
1390 }
1391
1392
1393 void MainWin::handleNoSslInCore(bool *accepted)
1394 {
1395     QMessageBox box(QMessageBox::Warning, tr("Unencrypted Connection"), tr("<b>Your core does not support SSL encryption</b>"),
1396         QMessageBox::Ignore|QMessageBox::Cancel, this);
1397     box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
1398     box.setDefaultButton(QMessageBox::Ignore);
1399     *accepted = box.exec() == QMessageBox::Ignore;
1400 }
1401
1402
1403 #ifdef HAVE_SSL
1404
1405 void MainWin::handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently)
1406 {
1407     QString errorString = "<ul>";
1408     foreach(const QSslError error, socket->sslErrors())
1409     errorString += QString("<li>%1</li>").arg(error.errorString());
1410     errorString += "</ul>";
1411
1412     QMessageBox box(QMessageBox::Warning,
1413         tr("Untrusted Security Certificate"),
1414         tr("<b>The SSL certificate provided by the core at %1 is untrusted for the following reasons:</b>").arg(socket->peerName()),
1415         QMessageBox::Cancel, this);
1416     box.setInformativeText(errorString);
1417     box.addButton(tr("Continue"), QMessageBox::AcceptRole);
1418     box.setDefaultButton(box.addButton(tr("Show Certificate"), QMessageBox::HelpRole));
1419
1420     QMessageBox::ButtonRole role;
1421     do {
1422         box.exec();
1423         role = box.buttonRole(box.clickedButton());
1424         if (role == QMessageBox::HelpRole) {
1425             SslInfoDlg dlg(socket, this);
1426             dlg.exec();
1427         }
1428     }
1429     while (role == QMessageBox::HelpRole);
1430
1431     *accepted = role == QMessageBox::AcceptRole;
1432     if (*accepted) {
1433         QMessageBox box2(QMessageBox::Warning,
1434             tr("Untrusted Security Certificate"),
1435             tr("Would you like to accept this certificate forever without being prompted?"),
1436             0, this);
1437         box2.setDefaultButton(box2.addButton(tr("Current Session Only"), QMessageBox::NoRole));
1438         box2.addButton(tr("Forever"), QMessageBox::YesRole);
1439         box2.exec();
1440         *permanently =  box2.buttonRole(box2.clickedButton()) == QMessageBox::YesRole;
1441     }
1442 }
1443
1444
1445 #endif /* HAVE_SSL */
1446
1447 void MainWin::handleCoreConnectionError(const QString &error)
1448 {
1449     QMessageBox::critical(this, tr("Core Connection Error"), error, QMessageBox::Ok);
1450 }
1451
1452
1453 void MainWin::showCoreConnectionDlg()
1454 {
1455     CoreConnectDlg dlg(this);
1456     if (dlg.exec() == QDialog::Accepted) {
1457         AccountId accId = dlg.selectedAccount();
1458         if (accId.isValid())
1459             Client::coreConnection()->connectToCore(accId);
1460     }
1461 }
1462
1463
1464 void MainWin::showCoreConfigWizard(const QVariantList &backends, const QVariantList &authenticators)
1465 {
1466     CoreConfigWizard *wizard = new CoreConfigWizard(Client::coreConnection(), backends, authenticators, this);
1467
1468     wizard->show();
1469 }
1470
1471
1472 void MainWin::showChannelList(NetworkId netId)
1473 {
1474     ChannelListDlg *channelListDlg = new ChannelListDlg();
1475
1476     if (!netId.isValid()) {
1477         QAction *action = qobject_cast<QAction *>(sender());
1478         if (action)
1479             netId = action->data().value<NetworkId>();
1480     }
1481
1482     channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
1483     channelListDlg->setNetwork(netId);
1484     channelListDlg->show();
1485 }
1486
1487
1488 void MainWin::showNetworkConfig(NetworkId netId)
1489 {
1490     SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
1491     if (netId.isValid())
1492         qobject_cast<NetworksSettingsPage *>(dlg.currentPage())->bufferList_Open(netId);
1493     dlg.exec();
1494 }
1495
1496
1497 void MainWin::showIgnoreList(QString newRule)
1498 {
1499     SettingsPageDlg dlg(new IgnoreListSettingsPage(this), this);
1500     // prepare config dialog for new rule
1501     if (!newRule.isEmpty())
1502         qobject_cast<IgnoreListSettingsPage *>(dlg.currentPage())->editIgnoreRule(newRule);
1503     dlg.exec();
1504 }
1505
1506
1507 void MainWin::showCoreInfoDlg()
1508 {
1509     CoreInfoDlg(this).exec();
1510 }
1511
1512
1513 void MainWin::showAwayLog()
1514 {
1515     if (_awayLog)
1516         return;
1517     AwayLogFilter *filter = new AwayLogFilter(Client::messageModel());
1518     _awayLog = new AwayLogView(filter, 0);
1519     filter->setParent(_awayLog);
1520     connect(_awayLog, SIGNAL(destroyed()), this, SLOT(awayLogDestroyed()));
1521     _awayLog->setAttribute(Qt::WA_DeleteOnClose);
1522     _awayLog->show();
1523 }
1524
1525
1526 void MainWin::awayLogDestroyed()
1527 {
1528     _awayLog = 0;
1529 }
1530
1531
1532 void MainWin::showSettingsDlg()
1533 {
1534     SettingsDlg *dlg = new SettingsDlg();
1535
1536     //Category: Interface
1537     dlg->registerSettingsPage(new AppearanceSettingsPage(dlg));
1538     dlg->registerSettingsPage(new ChatViewSettingsPage(dlg));
1539     dlg->registerSettingsPage(new ChatViewColorSettingsPage(dlg));
1540     dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
1541     dlg->registerSettingsPage(new ItemViewSettingsPage(dlg));
1542     dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
1543     dlg->registerSettingsPage(new InputWidgetSettingsPage(dlg));
1544     dlg->registerSettingsPage(new TopicWidgetSettingsPage(dlg));
1545 #ifdef HAVE_SONNET
1546     dlg->registerSettingsPage(new SonnetSettingsPage(dlg));
1547 #endif
1548     dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
1549     dlg->registerSettingsPage(new CoreHighlightSettingsPage(dlg));
1550     dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
1551     dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
1552
1553     //Category: IRC
1554     dlg->registerSettingsPage(new ConnectionSettingsPage(dlg));
1555     dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
1556     dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
1557     dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
1558     dlg->registerSettingsPage(new IgnoreListSettingsPage(dlg));
1559     // dlg->registerSettingsPage(new DccSettingsPage(dlg)); not ready yet
1560
1561     // Category: Remote Cores
1562     if (Quassel::runMode() != Quassel::Monolithic) {
1563         dlg->registerSettingsPage(new CoreAccountSettingsPage(dlg));
1564         dlg->registerSettingsPage(new CoreConnectionSettingsPage(dlg));
1565     }
1566
1567     dlg->show();
1568 }
1569
1570
1571 void MainWin::showAboutDlg()
1572 {
1573     AboutDlg(this).exec();
1574 }
1575
1576
1577 void MainWin::showShortcutsDlg()
1578 {
1579 #ifdef HAVE_KDE
1580     KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed, this);
1581     foreach(KActionCollection *coll, QtUi::actionCollections())
1582     dlg.addCollection(coll, coll->property("Category").toString());
1583     dlg.configure(true);
1584 #else
1585     SettingsPageDlg dlg(new ShortcutsSettingsPage(QtUi::actionCollections(), this), this);
1586     dlg.exec();
1587 #endif
1588 }
1589
1590
1591 void MainWin::showNewTransferDlg(const QUuid &transferId)
1592 {
1593     auto transfer = Client::transferManager()->transfer(transferId);
1594     if (transfer) {
1595         if (transfer->status() == Transfer::Status::New) {
1596             ReceiveFileDlg *dlg = new ReceiveFileDlg(transfer, this);
1597             dlg->show();
1598         }
1599     }
1600     else {
1601         qWarning() << "Unknown transfer ID" << transferId;
1602     }
1603 }
1604
1605
1606 void MainWin::onFullScreenToggled()
1607 {
1608     // Relying on QWidget::isFullScreen is discouraged, see the KToggleFullScreenAction docs
1609     // Also, one should not use showFullScreen() or showNormal(), as those reset all other window flags
1610
1611 #ifdef HAVE_KDE
1612     static_cast<KToggleFullScreenAction*>(_fullScreenAction)->setFullScreen(this, _fullScreenAction->isChecked());
1613 #else
1614     if (_fullScreenAction->isChecked())
1615         setWindowState(windowState() | Qt::WindowFullScreen);
1616     else
1617         setWindowState(windowState() & ~Qt::WindowFullScreen);
1618 #endif
1619 }
1620
1621
1622 /********************************************************************************************************/
1623
1624 bool MainWin::event(QEvent *event)
1625 {
1626     switch(event->type()) {
1627     case QEvent::WindowActivate: {
1628         BufferId bufferId = Client::bufferModel()->currentBuffer();
1629         if (bufferId.isValid())
1630             Client::instance()->markBufferAsRead(bufferId);
1631         break;
1632     }
1633     case QEvent::WindowDeactivate:
1634         if (bufferWidget()->autoMarkerLineOnLostFocus())
1635             bufferWidget()->setMarkerLine();
1636         break;
1637     default:
1638         break;
1639     }
1640     return QMainWindow::event(event);
1641 }
1642
1643
1644 void MainWin::moveEvent(QMoveEvent *event)
1645 {
1646     if (!(windowState() & Qt::WindowMaximized))
1647         _normalPos = event->pos();
1648
1649     QMainWindow::moveEvent(event);
1650 }
1651
1652
1653 void MainWin::resizeEvent(QResizeEvent *event)
1654 {
1655     if (!(windowState() & Qt::WindowMaximized))
1656         _normalSize = event->size();
1657
1658     QMainWindow::resizeEvent(event);
1659 }
1660
1661
1662 void MainWin::closeEvent(QCloseEvent *event)
1663 {
1664     QtUiSettings s;
1665     QtUiApplication *app = qobject_cast<QtUiApplication *> qApp;
1666     Q_ASSERT(app);
1667     // On OSX it can happen that the closeEvent occurs twice. (Especially if packaged with Frameworks)
1668     // This messes up MainWinState/MainWinHidden save/restore.
1669     // It's a bug in Qt: https://bugreports.qt.io/browse/QTBUG-43344
1670     if (!_aboutToQuit && !app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
1671         QtUi::hideMainWidget();
1672         event->ignore();
1673     }
1674     else if(!_aboutToQuit) {
1675         _aboutToQuit = true;
1676         event->accept();
1677         quit();
1678     }
1679     else {
1680         event->ignore();
1681     }
1682 }
1683
1684
1685 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end)
1686 {
1687     Q_UNUSED(parent);
1688
1689     bool hasFocus = QApplication::activeWindow() != 0;
1690
1691     for (int i = start; i <= end; i++) {
1692         QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
1693         if (!idx.isValid()) {
1694             qDebug() << "MainWin::messagesInserted(): Invalid model index!";
1695             continue;
1696         }
1697         Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
1698         if (flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
1699             continue;
1700
1701         BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
1702         BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
1703
1704         // check if bufferId belongs to the shown chatlists
1705         if (!(Client::bufferViewOverlay()->bufferIds().contains(bufId) ||
1706               Client::bufferViewOverlay()->tempRemovedBufferIds().contains(bufId)))
1707             continue;
1708
1709         // check if it's the buffer currently displayed
1710         if (hasFocus && bufId == Client::bufferModel()->currentBuffer())
1711             continue;
1712
1713         // only show notifications for higlights or queries
1714         if (bufType != BufferInfo::QueryBuffer && !(flags & Message::Highlight))
1715             continue;
1716
1717         // and of course: don't notify for ignored messages
1718         if (Client::ignoreListManager() && Client::ignoreListManager()->match(idx.data(MessageModel::MessageRole).value<Message>(), Client::networkModel()->networkName(bufId)))
1719             continue;
1720
1721         // seems like we have a legit notification candidate!
1722         QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
1723         QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
1724         QString contents = idx.data(ChatLineModel::DisplayRole).toString();
1725         AbstractNotificationBackend::NotificationType type;
1726
1727         if (bufType == BufferInfo::QueryBuffer && !hasFocus)
1728             type = AbstractNotificationBackend::PrivMsg;
1729         else if (bufType == BufferInfo::QueryBuffer && hasFocus)
1730             type = AbstractNotificationBackend::PrivMsgFocused;
1731         else if (flags & Message::Highlight && !hasFocus)
1732             type = AbstractNotificationBackend::Highlight;
1733         else
1734             type = AbstractNotificationBackend::HighlightFocused;
1735
1736         QtUi::instance()->invokeNotification(bufId, type, sender, contents);
1737     }
1738 }
1739
1740
1741 void MainWin::currentBufferChanged(BufferId buffer)
1742 {
1743     if (buffer.isValid())
1744         Client::instance()->markBufferAsRead(buffer);
1745 }
1746
1747
1748 void MainWin::clientNetworkCreated(NetworkId id)
1749 {
1750     const Network *net = Client::network(id);
1751     QAction *act = new QAction(net->networkName(), this);
1752     act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
1753     act->setData(QVariant::fromValue<NetworkId>(id));
1754     connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
1755     connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
1756
1757     QAction *beforeAction = 0;
1758     foreach(QAction *action, _networksMenu->actions()) {
1759         if (!action->data().isValid()) // ignore stock actions
1760             continue;
1761         if (net->networkName().localeAwareCompare(action->text()) < 0) {
1762             beforeAction = action;
1763             break;
1764         }
1765     }
1766     _networksMenu->insertAction(beforeAction, act);
1767 }
1768
1769
1770 void MainWin::clientNetworkUpdated()
1771 {
1772     const Network *net = qobject_cast<const Network *>(sender());
1773     if (!net)
1774         return;
1775
1776     QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1777     if (!action)
1778         return;
1779
1780     action->setText(net->networkName());
1781
1782     switch (net->connectionState()) {
1783     case Network::Initialized:
1784         action->setIcon(icon::get("network-connect"));
1785         // if we have no currently selected buffer, jump to the first connecting statusbuffer
1786         if (!bufferWidget()->currentBuffer().isValid()) {
1787             QModelIndex idx = Client::networkModel()->networkIndex(net->networkId());
1788             if (idx.isValid()) {
1789                 BufferId statusBufferId = idx.data(NetworkModel::BufferIdRole).value<BufferId>();
1790                 Client::bufferModel()->switchToBuffer(statusBufferId);
1791             }
1792         }
1793         break;
1794     case Network::Disconnected:
1795         action->setIcon(icon::get("network-disconnect"));
1796         break;
1797     default:
1798         action->setIcon(icon::get("network-wired"));
1799     }
1800 }
1801
1802
1803 void MainWin::clientNetworkRemoved(NetworkId id)
1804 {
1805     QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
1806     if (!action)
1807         return;
1808
1809     action->deleteLater();
1810 }
1811
1812
1813 void MainWin::connectOrDisconnectFromNet()
1814 {
1815     QAction *act = qobject_cast<QAction *>(sender());
1816     if (!act) return;
1817     const Network *net = Client::network(act->data().value<NetworkId>());
1818     if (!net) return;
1819     if (net->connectionState() == Network::Disconnected) net->requestConnect();
1820     else net->requestDisconnect();
1821 }
1822
1823
1824 void MainWin::on_inputFormatApplyColor_triggered()
1825 {
1826     if (!_inputWidget)
1827         return;
1828
1829     _inputWidget->applyFormatActiveColor();
1830 }
1831
1832
1833 void MainWin::on_inputFormatApplyColorFill_triggered()
1834 {
1835     if (!_inputWidget)
1836         return;
1837
1838     _inputWidget->applyFormatActiveColorFill();
1839 }
1840
1841
1842 void MainWin::on_inputFormatClear_triggered()
1843 {
1844     if (!_inputWidget)
1845         return;
1846
1847     _inputWidget->clearFormat();
1848 }
1849
1850
1851 void MainWin::on_inputFormatBold_triggered()
1852 {
1853     if (!_inputWidget)
1854         return;
1855
1856     _inputWidget->toggleFormatBold();
1857 }
1858
1859
1860 void MainWin::on_inputFormatItalic_triggered()
1861 {
1862     if (!_inputWidget)
1863         return;
1864
1865     _inputWidget->toggleFormatItalic();
1866 }
1867
1868
1869 void MainWin::on_inputFormatUnderline_triggered()
1870 {
1871     if (!_inputWidget)
1872         return;
1873
1874     _inputWidget->toggleFormatUnderline();
1875 }
1876
1877
1878 void MainWin::on_jumpHotBuffer_triggered()
1879 {
1880     if (!_bufferHotList->rowCount())
1881         return;
1882
1883     Client::bufferModel()->switchToBuffer(_bufferHotList->hottestBuffer());
1884 }
1885
1886 void MainWin::on_bufferSearch_triggered()
1887 {
1888     if (_activeBufferViewIndex < 0 || _activeBufferViewIndex >= _bufferViews.count()) {
1889         qWarning() << "Tried to activate filter on invalid bufferview:" << _activeBufferViewIndex;
1890         return;
1891     }
1892
1893     _bufferViews[_activeBufferViewIndex]->activateFilter();
1894 }
1895
1896
1897 void MainWin::onJumpKey()
1898 {
1899     QAction *action = qobject_cast<QAction *>(sender());
1900     if (!action || !Client::bufferModel())
1901         return;
1902     int idx = action->property("Index").toInt();
1903
1904     if (_jumpKeyMap.isEmpty())
1905         _jumpKeyMap = CoreAccountSettings().jumpKeyMap();
1906
1907     if (!_jumpKeyMap.contains(idx))
1908         return;
1909
1910     BufferId buffer = _jumpKeyMap.value(idx);
1911     if (buffer.isValid())
1912         Client::bufferModel()->switchToBuffer(buffer);
1913 }
1914
1915
1916 void MainWin::bindJumpKey()
1917 {
1918     QAction *action = qobject_cast<QAction *>(sender());
1919     if (!action || !Client::bufferModel())
1920         return;
1921     int idx = action->property("Index").toInt();
1922
1923     _jumpKeyMap[idx] = Client::bufferModel()->currentBuffer();
1924     CoreAccountSettings().setJumpKeyMap(_jumpKeyMap);
1925 }
1926
1927
1928 void MainWin::on_actionDebugNetworkModel_triggered()
1929 {
1930     QTreeView *view = new QTreeView;
1931     view->setAttribute(Qt::WA_DeleteOnClose);
1932     view->setWindowTitle("Debug NetworkModel View");
1933     view->setModel(Client::networkModel());
1934     view->setColumnWidth(0, 250);
1935     view->setColumnWidth(1, 250);
1936     view->setColumnWidth(2, 80);
1937     view->resize(610, 300);
1938     view->show();
1939 }
1940
1941
1942 void MainWin::on_actionDebugHotList_triggered()
1943 {
1944     _bufferHotList->invalidate();
1945     _bufferHotList->sort(0, Qt::DescendingOrder);
1946
1947     QTreeView *view = new QTreeView;
1948     view->setAttribute(Qt::WA_DeleteOnClose);
1949     view->setModel(_bufferHotList);
1950     view->show();
1951 }
1952
1953
1954 void MainWin::on_actionDebugBufferViewOverlay_triggered()
1955 {
1956     DebugBufferViewOverlay *overlay = new DebugBufferViewOverlay(0);
1957     overlay->setAttribute(Qt::WA_DeleteOnClose);
1958     overlay->show();
1959 }
1960
1961
1962 void MainWin::on_actionDebugMessageModel_triggered()
1963 {
1964     QTableView *view = new QTableView(0);
1965     DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
1966     filter->setSourceModel(Client::messageModel());
1967     view->setModel(filter);
1968     view->setAttribute(Qt::WA_DeleteOnClose, true);
1969     view->verticalHeader()->hide();
1970     view->horizontalHeader()->setStretchLastSection(true);
1971     view->show();
1972 }
1973
1974
1975 void MainWin::on_actionDebugLog_triggered()
1976 {
1977     DebugLogWidget *logWidget = new DebugLogWidget(0);
1978     logWidget->show();
1979 }
1980
1981
1982 void MainWin::showStatusBarMessage(const QString &message)
1983 {
1984     statusBar()->showMessage(message, 10000);
1985 }