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