ssl: Remove fallback code for missing SSL support
[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
471     // Navigation
472     coll = QtUi::actionCollection("Navigation", tr("Navigation"));
473
474     coll->addActions(
475         {{"JumpHotBuffer",
476           new Action(tr("Jump to hot chat"), coll, this, &MainWin::onJumpHotBufferTriggered, QKeySequence(Qt::META + Qt::Key_A))},
477          {"ActivateBufferFilter",
478           new Action(tr("Activate the buffer search"), coll, this, &MainWin::onBufferSearchTriggered, QKeySequence(Qt::CTRL + Qt::Key_S))}});
479
480     // Jump keys
481 #ifdef Q_OS_MAC
482     const int bindModifier = Qt::ControlModifier | Qt::AltModifier;
483     const int jumpModifier = Qt::ControlModifier;
484 #else
485     const int bindModifier = Qt::ControlModifier;
486     const int jumpModifier = Qt::AltModifier;
487 #endif
488
489     coll->addAction("BindJumpKey0",
490                     new Action(tr("Set Quick Access #0"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_0)))
491         ->setProperty("Index", 0);
492     coll->addAction("BindJumpKey1",
493                     new Action(tr("Set Quick Access #1"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_1)))
494         ->setProperty("Index", 1);
495     coll->addAction("BindJumpKey2",
496                     new Action(tr("Set Quick Access #2"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_2)))
497         ->setProperty("Index", 2);
498     coll->addAction("BindJumpKey3",
499                     new Action(tr("Set Quick Access #3"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_3)))
500         ->setProperty("Index", 3);
501     coll->addAction("BindJumpKey4",
502                     new Action(tr("Set Quick Access #4"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_4)))
503         ->setProperty("Index", 4);
504     coll->addAction("BindJumpKey5",
505                     new Action(tr("Set Quick Access #5"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_5)))
506         ->setProperty("Index", 5);
507     coll->addAction("BindJumpKey6",
508                     new Action(tr("Set Quick Access #6"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_6)))
509         ->setProperty("Index", 6);
510     coll->addAction("BindJumpKey7",
511                     new Action(tr("Set Quick Access #7"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_7)))
512         ->setProperty("Index", 7);
513     coll->addAction("BindJumpKey8",
514                     new Action(tr("Set Quick Access #8"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_8)))
515         ->setProperty("Index", 8);
516     coll->addAction("BindJumpKey9",
517                     new Action(tr("Set Quick Access #9"), coll, this, &MainWin::bindJumpKey, QKeySequence(bindModifier + Qt::Key_9)))
518         ->setProperty("Index", 9);
519
520     coll->addAction("JumpKey0", new Action(tr("Quick Access #0"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_0)))
521         ->setProperty("Index", 0);
522     coll->addAction("JumpKey1", new Action(tr("Quick Access #1"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_1)))
523         ->setProperty("Index", 1);
524     coll->addAction("JumpKey2", new Action(tr("Quick Access #2"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_2)))
525         ->setProperty("Index", 2);
526     coll->addAction("JumpKey3", new Action(tr("Quick Access #3"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_3)))
527         ->setProperty("Index", 3);
528     coll->addAction("JumpKey4", new Action(tr("Quick Access #4"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_4)))
529         ->setProperty("Index", 4);
530     coll->addAction("JumpKey5", new Action(tr("Quick Access #5"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_5)))
531         ->setProperty("Index", 5);
532     coll->addAction("JumpKey6", new Action(tr("Quick Access #6"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_6)))
533         ->setProperty("Index", 6);
534     coll->addAction("JumpKey7", new Action(tr("Quick Access #7"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_7)))
535         ->setProperty("Index", 7);
536     coll->addAction("JumpKey8", new Action(tr("Quick Access #8"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_8)))
537         ->setProperty("Index", 8);
538     coll->addAction("JumpKey9", new Action(tr("Quick Access #9"), coll, this, &MainWin::onJumpKey, QKeySequence(jumpModifier + Qt::Key_9)))
539         ->setProperty("Index", 9);
540
541     // Buffer navigation
542     coll->addAction("NextBufferView",
543                     new Action(icon::get("go-next-view"),
544                                tr("Activate Next Chat List"),
545                                coll,
546                                this,
547                                &MainWin::nextBufferView,
548                                QKeySequence(QKeySequence::Forward)));
549     coll->addAction("PreviousBufferView",
550                     new Action(icon::get("go-previous-view"),
551                                tr("Activate Previous Chat List"),
552                                coll,
553                                this,
554                                &MainWin::previousBufferView,
555                                QKeySequence::Back));
556     coll->addAction("NextBuffer",
557                     new Action(icon::get("go-down"),
558                                tr("Go to Next Chat"),
559                                coll,
560                                this,
561                                &MainWin::nextBuffer,
562                                QKeySequence(Qt::ALT + Qt::Key_Down)));
563     coll->addAction("PreviousBuffer",
564                     new Action(icon::get("go-up"),
565                                tr("Go to Previous Chat"),
566                                coll,
567                                this,
568                                &MainWin::previousBuffer,
569                                QKeySequence(Qt::ALT + Qt::Key_Up)));
570 }
571
572 void MainWin::setupMenus()
573 {
574     ActionCollection* coll = QtUi::actionCollection("General");
575
576     _fileMenu = menuBar()->addMenu(tr("&File"));
577
578     static const QStringList coreActions = QStringList() << "ConnectCore"
579                                                          << "DisconnectCore"
580                                                          << "ChangePassword"
581                                                          << "CoreInfo";
582
583     QAction* coreAction;
584     foreach (QString actionName, coreActions) {
585         coreAction = coll->action(actionName);
586         _fileMenu->addAction(coreAction);
587         flagRemoteCoreOnly(coreAction);
588     }
589     flagRemoteCoreOnly(_fileMenu->addSeparator());
590
591     _networksMenu = _fileMenu->addMenu(tr("&Networks"));
592     _networksMenu->addAction(coll->action("ConfigureNetworks"));
593     _networksMenu->addSeparator();
594     _fileMenu->addSeparator();
595     _fileMenu->addAction(coll->action("Quit"));
596
597     _viewMenu = menuBar()->addMenu(tr("&View"));
598     _bufferViewsMenu = _viewMenu->addMenu(tr("&Chat Lists"));
599     _bufferViewsMenu->addAction(coll->action("ConfigureBufferViews"));
600     _toolbarMenu = _viewMenu->addMenu(tr("&Toolbars"));
601     _viewMenu->addSeparator();
602
603     _viewMenu->addAction(coll->action("ToggleMenuBar"));
604     _viewMenu->addAction(coll->action("ToggleStatusBar"));
605     _viewMenu->addAction(coll->action("ToggleSearchBar"));
606
607     coreAction = coll->action("ShowAwayLog");
608     flagRemoteCoreOnly(coreAction);
609     _viewMenu->addAction(coreAction);
610
611     _viewMenu->addSeparator();
612     _viewMenu->addAction(coll->action("LockLayout"));
613
614     _settingsMenu = menuBar()->addMenu(tr("&Settings"));
615 #ifdef HAVE_KDE
616 #    if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 23, 0)
617     _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
618     _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
619 #    else
620     _settingsMenu->addAction(KStandardAction::configureNotifications(this, &MainWin::showNotificationsDlg, this));
621     _settingsMenu->addAction(KStandardAction::keyBindings(this, &MainWin::showShortcutsDlg, this));
622 #    endif
623 #else
624     _settingsMenu->addAction(coll->action("ConfigureShortcuts"));
625 #endif
626     _settingsMenu->addAction(coll->action("ConfigureQuassel"));
627
628     _helpMenu = menuBar()->addMenu(tr("&Help"));
629
630     _helpMenu->addAction(coll->action("AboutQuassel"));
631 #ifndef HAVE_KDE
632     _helpMenu->addAction(coll->action("AboutQt"));
633 #else
634 #    if KCONFIGWIDGETS_VERSION < QT_VERSION_CHECK(5, 23, 0)
635     _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
636 #    else
637     _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, &KHelpMenu::aboutKDE, this));
638 #    endif
639 #endif
640     _helpMenu->addSeparator();
641     _helpDebugMenu = _helpMenu->addMenu(icon::get("tools-report-bug"), tr("Debug"));
642     _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
643     _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
644     _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
645     _helpDebugMenu->addAction(coll->action("DebugHotList"));
646     _helpDebugMenu->addAction(coll->action("DebugLog"));
647     _helpDebugMenu->addAction(coll->action("ShowResourceTree"));
648     _helpDebugMenu->addSeparator();
649     _helpDebugMenu->addAction(coll->action("ReloadStyle"));
650
651     // Toggle visibility
652     QAction* showMenuBar = QtUi::actionCollection("General")->action("ToggleMenuBar");
653
654     QtUiSettings uiSettings;
655     bool enabled = uiSettings.value("ShowMenuBar", QVariant(true)).toBool();
656     showMenuBar->setChecked(enabled);
657     enabled ? menuBar()->show() : menuBar()->hide();
658
659     connect(showMenuBar, &QAction::toggled, menuBar(), &QMenuBar::setVisible);
660     connect(showMenuBar, &QAction::toggled, this, &MainWin::saveMenuBarStatus);
661 }
662
663 void MainWin::setupBufferWidget()
664 {
665     _bufferWidget = new BufferWidget(this);
666     _bufferWidget->setModel(Client::bufferModel());
667     _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
668     setCentralWidget(_bufferWidget);
669 }
670
671 void MainWin::addBufferView(int bufferViewConfigId)
672 {
673     addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
674 }
675
676 void MainWin::addBufferView(ClientBufferViewConfig* config)
677 {
678     if (!config)
679         return;
680
681     config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
682     auto* dock = new BufferViewDock(config, this);
683
684     // create the view and initialize it's filter
685     auto* view = new BufferView(dock);
686     view->setFilteredModel(Client::bufferModel(), config);
687     view->installEventFilter(_inputWidget);  // for key presses
688
689     Client::bufferModel()->synchronizeView(view);
690
691     dock->setLocked(QtUiSettings().value("LockLayout", false).toBool());
692
693     dock->setWidget(view);
694     dock->setVisible(_layoutLoaded);  // don't show before state has been restored
695
696     addDockWidget(Qt::LeftDockWidgetArea, dock);
697     _bufferViewsMenu->addAction(dock->toggleViewAction());
698
699     connect(dock->toggleViewAction(), &QAction::toggled, this, &MainWin::bufferViewToggled);
700     connect(dock, &QDockWidget::visibilityChanged, this, &MainWin::bufferViewVisibilityChanged);
701     _bufferViews.append(dock);
702
703     if (!activeBufferView())
704         nextBufferView();
705 }
706
707 void MainWin::removeBufferView(int bufferViewConfigId)
708 {
709     QVariant actionData;
710     BufferViewDock* dock;
711     foreach (QAction* action, _bufferViewsMenu->actions()) {
712         actionData = action->data();
713         if (!actionData.isValid())
714             continue;
715
716         dock = qobject_cast<BufferViewDock*>(action->parent());
717         if (dock && actionData.toInt() == bufferViewConfigId) {
718             removeAction(action);
719             Client::bufferViewOverlay()->removeView(dock->bufferViewId());
720             _bufferViews.removeAll(dock);
721
722             if (dock->isActive()) {
723                 dock->setActive(false);
724                 _activeBufferViewIndex = -1;
725                 nextBufferView();
726             }
727
728             dock->deleteLater();
729         }
730     }
731 }
732
733 void MainWin::bufferViewToggled(bool enabled)
734 {
735     if (!enabled && !isMinimized()) {
736         // hiding the mainwindow triggers a toggle of the bufferview (which pretty much sucks big time)
737         // since this isn't our fault and we can't do anything about it, we suppress the resulting calls
738         return;
739     }
740     auto* action = qobject_cast<QAction*>(sender());
741     Q_ASSERT(action);
742     auto* dock = qobject_cast<BufferViewDock*>(action->parent());
743     Q_ASSERT(dock);
744
745     // Make sure we don't toggle backlog fetch for a view we've already removed
746     if (!_bufferViews.contains(dock))
747         return;
748
749     if (enabled)
750         Client::bufferViewOverlay()->addView(dock->bufferViewId());
751     else
752         Client::bufferViewOverlay()->removeView(dock->bufferViewId());
753 }
754
755 void MainWin::bufferViewVisibilityChanged(bool visible)
756 {
757     Q_UNUSED(visible);
758     auto* dock = qobject_cast<BufferViewDock*>(sender());
759     Q_ASSERT(dock);
760     if ((!dock->isHidden() && !activeBufferView()) || (dock->isHidden() && dock->isActive()))
761         nextBufferView();
762 }
763
764 BufferView* MainWin::allBuffersView() const
765 {
766     // "All Buffers" is always the first dock created
767     if (_bufferViews.count() > 0)
768         return _bufferViews[0]->bufferView();
769     return nullptr;
770 }
771
772 BufferView* MainWin::activeBufferView() const
773 {
774     if (_activeBufferViewIndex < 0 || _activeBufferViewIndex >= _bufferViews.count())
775         return nullptr;
776     BufferViewDock* dock = _bufferViews.at(_activeBufferViewIndex);
777     return dock->isActive() ? dock->bufferView() : nullptr;
778 }
779
780 void MainWin::changeActiveBufferView(int bufferViewId)
781 {
782     if (bufferViewId < 0)
783         return;
784
785     if (_activeBufferViewIndex >= 0 && _activeBufferViewIndex < _bufferViews.count()) {
786         _bufferViews[_activeBufferViewIndex]->setActive(false);
787         _activeBufferViewIndex = -1;
788     }
789
790     for (int i = 0; i < _bufferViews.count(); i++) {
791         BufferViewDock* dock = _bufferViews.at(i);
792         if (dock->bufferViewId() == bufferViewId && !dock->isHidden()) {
793             _activeBufferViewIndex = i;
794             dock->setActive(true);
795             return;
796         }
797     }
798
799     nextBufferView();  // fallback
800 }
801
802 void MainWin::showPasswordChangeDlg()
803 {
804     if (Client::isCoreFeatureEnabled(Quassel::Feature::PasswordChange)) {
805         PasswordChangeDlg{}.exec();
806     }
807     else {
808         QMessageBox box(QMessageBox::Warning,
809                         tr("Feature Not Supported"),
810                         tr("<b>Your Quassel Core does not support this feature</b>"),
811                         QMessageBox::Ok);
812         box.setInformativeText(tr("You need a Quassel Core v0.12.0 or newer in order to be able to remotely change your password."));
813         box.exec();
814     }
815 }
816
817 void MainWin::showMigrationWarning(bool show)
818 {
819     if (show && !_migrationWarning) {
820         _migrationWarning = new QMessageBox(QMessageBox::Information,
821                                             tr("Upgrading..."),
822                                             "<b>" + tr("Your database is being upgraded") + "</b>",
823                                             QMessageBox::NoButton,
824                                             this);
825         _migrationWarning->setInformativeText(
826             "<p>" + tr("In order to support new features, we need to make changes to your backlog database. This may take a long while.")
827             + "</p><p>" + tr("Do not exit Quassel until the upgrade is complete!") + "</p>");
828         _migrationWarning->setStandardButtons(QMessageBox::NoButton);
829         _migrationWarning->show();
830     }
831     else if (!show && _migrationWarning) {
832         _migrationWarning->close();
833         _migrationWarning->deleteLater();
834         _migrationWarning = nullptr;
835     }
836 }
837
838 void MainWin::onExitRequested(const QString& reason)
839 {
840     if (!reason.isEmpty()) {
841         QMessageBox box(QMessageBox::Critical,
842                         tr("Fatal error"),
843                         "<b>" + tr("Quassel encountered a fatal error and is terminated.") + "</b>",
844                         QMessageBox::Ok);
845         box.setInformativeText("<p>" + tr("Reason:<em>") + " " + reason + "</em>");
846         box.exec();
847     }
848 }
849
850 void MainWin::changeActiveBufferView(bool backwards)
851 {
852     if (_activeBufferViewIndex >= 0 && _activeBufferViewIndex < _bufferViews.count()) {
853         _bufferViews[_activeBufferViewIndex]->setActive(false);
854     }
855
856     if (!_bufferViews.count())
857         return;
858
859     int c = _bufferViews.count();
860     while (c--) {  // yes, this will reactivate the current active one if all others fail
861         if (backwards) {
862             if (--_activeBufferViewIndex < 0)
863                 _activeBufferViewIndex = _bufferViews.count() - 1;
864         }
865         else {
866             if (++_activeBufferViewIndex >= _bufferViews.count())
867                 _activeBufferViewIndex = 0;
868         }
869
870         BufferViewDock* dock = _bufferViews.at(_activeBufferViewIndex);
871         if (dock->isHidden())
872             continue;
873
874         dock->setActive(true);
875         return;
876     }
877
878     _activeBufferViewIndex = -1;
879 }
880
881 void MainWin::nextBufferView()
882 {
883     changeActiveBufferView(false);
884 }
885
886 void MainWin::previousBufferView()
887 {
888     changeActiveBufferView(true);
889 }
890
891 void MainWin::nextBuffer()
892 {
893     BufferView* view = activeBufferView();
894     if (view)
895         view->nextBuffer();
896 }
897
898 void MainWin::previousBuffer()
899 {
900     BufferView* view = activeBufferView();
901     if (view)
902         view->previousBuffer();
903 }
904
905 void MainWin::hideCurrentBuffer()
906 {
907     BufferView* view = activeBufferView();
908     if (view)
909         view->hideCurrentBuffer();
910 }
911
912 void MainWin::showNotificationsDlg()
913 {
914     SettingsPageDlg{new NotificationsSettingsPage{}}.exec();
915 }
916
917 void MainWin::onConfigureNetworksTriggered()
918 {
919     SettingsPageDlg{new NetworksSettingsPage{}}.exec();
920 }
921
922 void MainWin::onConfigureViewsTriggered()
923 {
924     SettingsPageDlg{new BufferViewSettingsPage{}}.exec();
925 }
926
927 void MainWin::onLockLayoutToggled(bool lock)
928 {
929     QList<VerticalDock*> docks = findChildren<VerticalDock*>();
930     foreach (VerticalDock* dock, docks) {
931         dock->showTitle(!lock);
932     }
933
934     QList<NickListDock*> nickdocks = findChildren<NickListDock*>();
935     foreach (NickListDock* nickdock, nickdocks) {
936         nickdock->setLocked(lock);
937     }
938
939     QList<BufferViewDock*> bufferdocks = findChildren<BufferViewDock*>();
940     foreach (BufferViewDock* bufferdock, bufferdocks) {
941         bufferdock->setLocked(lock);
942     }
943
944     if (Client::bufferViewManager()) {
945         foreach (ClientBufferViewConfig* config, Client::bufferViewManager()->clientBufferViewConfigs()) {
946             config->setLocked(lock);
947         }
948     }
949
950     _mainToolBar->setMovable(!lock);
951     _nickToolBar->setMovable(!lock);
952
953     QtUiSettings().setValue("LockLayout", lock);
954 }
955
956 void MainWin::setupNickWidget()
957 {
958     // create nick dock
959     NickListDock* nickDock = new NickListDock(tr("Nicks"), this);
960     nickDock->setObjectName("NickDock");
961     nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
962     nickDock->setLocked(QtUiSettings().value("LockLayout", false).toBool());
963
964     _nickListWidget = new NickListWidget(nickDock);
965     nickDock->setWidget(_nickListWidget);
966
967     addDockWidget(Qt::RightDockWidgetArea, nickDock);
968     _viewMenu->addAction(nickDock->toggleViewAction());
969     nickDock->toggleViewAction()->setText(tr("Show Nick List"));
970
971     // See NickListDock::NickListDock();
972     // connect(nickDock->toggleViewAction(), &NickListDock::triggered, nickListWidget, &QWidget::showWidget);
973
974     // attach the NickListWidget to the BufferModel and the default selection
975     _nickListWidget->setModel(Client::bufferModel());
976     _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
977
978     _nickListWidget->setVisible(false);
979 }
980
981 void MainWin::setupChatMonitor()
982 {
983     VerticalDock* dock = new VerticalDock(tr("Chat Monitor"), this);
984     dock->setObjectName("ChatMonitorDock");
985
986     auto* filter = new ChatMonitorFilter(Client::messageModel(), this);
987     _chatMonitorView = new ChatMonitorView(filter, this);
988     _chatMonitorView->show();
989     dock->setWidget(_chatMonitorView);
990     dock->hide();
991
992     addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
993     _viewMenu->addAction(dock->toggleViewAction());
994     dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
995 }
996
997 void MainWin::setupInputWidget()
998 {
999     VerticalDock* dock = new VerticalDock(tr("Inputline"), this);
1000     dock->setObjectName("InputDock");
1001
1002     _inputWidget = new InputWidget(dock);
1003     dock->setWidget(_inputWidget);
1004
1005     addDockWidget(Qt::BottomDockWidgetArea, dock);
1006
1007     _viewMenu->addAction(dock->toggleViewAction());
1008     dock->toggleViewAction()->setText(tr("Show Input Line"));
1009
1010     _inputWidget->setModel(Client::bufferModel());
1011     _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
1012
1013     _inputWidget->inputLine()->installEventFilter(_bufferWidget);
1014 }
1015
1016 void MainWin::setupTopicWidget()
1017 {
1018     VerticalDock* dock = new VerticalDock(tr("Topic"), this);
1019     dock->setObjectName("TopicDock");
1020     _topicWidget = new TopicWidget(dock);
1021
1022     dock->setWidget(_topicWidget);
1023
1024     _topicWidget->setModel(Client::bufferModel());
1025     _topicWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
1026
1027     connect(_topicWidget, &TopicWidget::switchedPlain, _bufferWidget, selectOverload<>(&QWidget::setFocus));
1028
1029     addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
1030
1031     _viewMenu->addAction(dock->toggleViewAction());
1032     dock->toggleViewAction()->setText(tr("Show Topic Line"));
1033 }
1034
1035 void MainWin::setupTransferWidget()
1036 {
1037     auto dock = new QDockWidget(tr("Transfers"), this);
1038     dock->setObjectName("TransferDock");
1039     dock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1040
1041     auto view = new QTableView(dock);  // to be replaced by the real thing
1042     view->setModel(Client::transferModel());
1043     dock->setWidget(view);
1044     dock->hide();  // hidden by default
1045     addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
1046
1047     auto action = dock->toggleViewAction();
1048     action->setText(tr("Show File Transfers"));
1049     action->setIcon(icon::get("download"));
1050     action->setShortcut(QKeySequence(Qt::Key_F6));
1051     QtUi::actionCollection("General")->addAction("ShowTransferWidget", action);
1052     _viewMenu->addAction(action);
1053 }
1054
1055 void MainWin::setupViewMenuTail()
1056 {
1057     _viewMenu->addSeparator();
1058     _viewMenu->addAction(_fullScreenAction);
1059 }
1060
1061 void MainWin::setupTitleSetter()
1062 {
1063     _titleSetter.setModel(Client::bufferModel());
1064     _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
1065 }
1066
1067 void MainWin::setupStatusBar()
1068 {
1069     // MessageProcessor progress
1070     statusBar()->addPermanentWidget(_msgProcessorStatusWidget);
1071
1072     // Connection state
1073     _coreConnectionStatusWidget->update();
1074     statusBar()->addPermanentWidget(_coreConnectionStatusWidget);
1075
1076     QAction* showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
1077
1078     QtUiSettings uiSettings;
1079
1080     bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
1081     showStatusbar->setChecked(enabled);
1082     enabled ? statusBar()->show() : statusBar()->hide();
1083
1084     connect(showStatusbar, &QAction::toggled, statusBar(), &QWidget::setVisible);
1085     connect(showStatusbar, &QAction::toggled, this, &MainWin::saveStatusBarStatus);
1086
1087     connect(Client::coreConnection(), &CoreConnection::connectionMsg, statusBar(), [statusBar = statusBar()](auto&& message) {
1088         statusBar->showMessage(message);
1089     });
1090 }
1091
1092 void MainWin::setupHotList()
1093 {
1094     auto* flatProxy = new FlatProxyModel(this);
1095     flatProxy->setSourceModel(Client::bufferModel());
1096     _bufferHotList = new BufferHotListFilter(flatProxy);
1097 }
1098
1099 void MainWin::saveMenuBarStatus(bool enabled)
1100 {
1101     QtUiSettings uiSettings;
1102     uiSettings.setValue("ShowMenuBar", enabled);
1103 }
1104
1105 void MainWin::saveStatusBarStatus(bool enabled)
1106 {
1107     QtUiSettings uiSettings;
1108     uiSettings.setValue("ShowStatusBar", enabled);
1109 }
1110
1111 void MainWin::setupSystray()
1112 {
1113 #ifdef HAVE_DBUS
1114     _systemTray = new StatusNotifierItem(this);
1115 #elif !defined QT_NO_SYSTEMTRAYICON
1116     _systemTray = new LegacySystemTray(this);
1117 #else
1118     _systemTray = new SystemTray(this);  // dummy
1119 #endif
1120 }
1121
1122 void MainWin::setupToolBars()
1123 {
1124     connect(_bufferWidget,
1125             selectOverload<const QModelIndex&>(&AbstractBufferContainer::currentChanged),
1126             QtUi::toolBarActionProvider(),
1127             &ToolBarActionProvider::onCurrentBufferChanged);
1128     connect(_nickListWidget,
1129             &NickListWidget::nickSelectionChanged,
1130             QtUi::toolBarActionProvider(),
1131             &ToolBarActionProvider::onNickSelectionChanged);
1132
1133 #ifdef Q_OS_MAC
1134     setUnifiedTitleAndToolBarOnMac(true);
1135 #endif
1136
1137 #ifdef HAVE_KDE
1138     _mainToolBar = new KToolBar("MainToolBar", this, Qt::TopToolBarArea, false, true, true);
1139 #else
1140     _mainToolBar = new QToolBar(this);
1141     _mainToolBar->setObjectName("MainToolBar");
1142 #endif
1143     _mainToolBar->setWindowTitle(tr("Main Toolbar"));
1144     addToolBar(_mainToolBar);
1145
1146     if (Quassel::runMode() != Quassel::Monolithic) {
1147         ActionCollection* coll = QtUi::actionCollection("General");
1148         _mainToolBar->addAction(coll->action("ConnectCore"));
1149         _mainToolBar->addAction(coll->action("DisconnectCore"));
1150     }
1151
1152     _mainToolBar->setMovable(!QtUiSettings().value("LockLayout", false).toBool());
1153
1154     QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
1155     _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
1156
1157 #ifdef HAVE_KDE
1158     _nickToolBar = new KToolBar("NickToolBar", this, Qt::TopToolBarArea, false, true, true);
1159 #else
1160     _nickToolBar = new QToolBar(this);
1161     _nickToolBar->setObjectName("NickToolBar");
1162 #endif
1163     _nickToolBar->setWindowTitle(tr("Nick Toolbar"));
1164     _nickToolBar->setVisible(false);  // default: not visible
1165     addToolBar(_nickToolBar);
1166     _nickToolBar->setMovable(!QtUiSettings().value("LockLayout", false).toBool());
1167
1168     QtUi::toolBarActionProvider()->addActions(_nickToolBar, ToolBarActionProvider::NickToolBar);
1169     _toolbarMenu->addAction(_nickToolBar->toggleViewAction());
1170
1171 #ifdef Q_OS_MAC
1172     QtUiSettings uiSettings;
1173
1174     bool visible = uiSettings.value("ShowMainToolBar", QVariant(true)).toBool();
1175     _mainToolBar->setVisible(visible);
1176     connect(_mainToolBar, &QToolBar::visibilityChanged, this, &MainWin::saveMainToolBarStatus);
1177 #endif
1178 }
1179
1180 void MainWin::saveMainToolBarStatus(bool enabled)
1181 {
1182 #ifdef Q_OS_MAC
1183     QtUiSettings uiSettings;
1184     uiSettings.setValue("ShowMainToolBar", enabled);
1185 #else
1186     Q_UNUSED(enabled);
1187 #endif
1188 }
1189
1190 void MainWin::doAutoConnect()
1191 {
1192     int accountId = Quassel::optionValue("account").toInt();
1193     if (!Client::coreConnection()->connectToCore(accountId)) {
1194         // No autoconnect selected (or no accounts)
1195         showCoreConnectionDlg();
1196     }
1197 }
1198
1199 void MainWin::connectedToCore()
1200 {
1201     Q_CHECK_PTR(Client::bufferViewManager());
1202     connect(Client::bufferViewManager(), &BufferViewManager::bufferViewConfigAdded, this, selectOverload<int>(&MainWin::addBufferView));
1203     connect(Client::bufferViewManager(), &BufferViewManager::bufferViewConfigDeleted, this, &MainWin::removeBufferView);
1204     connect(Client::bufferViewManager(), &SyncableObject::initDone, this, &MainWin::loadLayout);
1205
1206     if (Client::transferManager()) {
1207         connect(Client::transferManager(), &TransferManager::transferAdded, this, &MainWin::showNewTransferDlg);
1208     }
1209
1210     setConnectedState();
1211 }
1212
1213 void MainWin::setConnectedState()
1214 {
1215     ActionCollection* coll = QtUi::actionCollection("General");
1216
1217     coll->action("ConnectCore")->setEnabled(false);
1218     coll->action("DisconnectCore")->setEnabled(true);
1219     coll->action("ChangePassword")->setEnabled(true);
1220     coll->action("CoreInfo")->setEnabled(true);
1221
1222     foreach (QAction* action, _fileMenu->actions()) {
1223         if (isRemoteCoreOnly(action))
1224             action->setVisible(!Client::internalCore());
1225     }
1226
1227     disconnect(Client::backlogManager(),
1228                &ClientBacklogManager::updateProgress,
1229                _msgProcessorStatusWidget,
1230                &MsgProcessorStatusWidget::setProgress);
1231     disconnect(Client::backlogManager(), &ClientBacklogManager::messagesRequested, this, &MainWin::showStatusBarMessage);
1232     disconnect(Client::backlogManager(), &ClientBacklogManager::messagesProcessed, this, &MainWin::showStatusBarMessage);
1233     if (!Client::internalCore()) {
1234         connect(Client::backlogManager(),
1235                 &ClientBacklogManager::updateProgress,
1236                 _msgProcessorStatusWidget,
1237                 &MsgProcessorStatusWidget::setProgress);
1238         connect(Client::backlogManager(), &ClientBacklogManager::messagesRequested, this, &MainWin::showStatusBarMessage);
1239         connect(Client::backlogManager(), &ClientBacklogManager::messagesProcessed, this, &MainWin::showStatusBarMessage);
1240     }
1241
1242     // _viewMenu->setEnabled(true);
1243     if (!Client::internalCore())
1244         statusBar()->showMessage(tr("Connected to core."));
1245     else
1246         statusBar()->clearMessage();
1247
1248     _coreConnectionStatusWidget->setVisible(!Client::internalCore());
1249     updateIcon();
1250     systemTray()->setState(SystemTray::Active);
1251
1252     if (Client::networkIds().isEmpty()) {
1253         IrcConnectionWizard* wizard = new IrcConnectionWizard(this, Qt::Sheet);
1254         wizard->show();
1255     }
1256     else {
1257         // Monolithic always preselects last used buffer - Client only if the connection died
1258         if (Client::coreConnection()->wasReconnect() || Quassel::runMode() == Quassel::Monolithic) {
1259             QtUiSettings s;
1260             BufferId lastUsedBufferId(s.value("LastUsedBufferId").toInt());
1261             if (lastUsedBufferId.isValid())
1262                 Client::bufferModel()->switchToBuffer(lastUsedBufferId);
1263         }
1264     }
1265 }
1266
1267 void MainWin::loadLayout()
1268 {
1269     QtUiSettings s;
1270     int accountId = Client::currentCoreAccount().accountId().toInt();
1271     QByteArray state = s.value(QString("MainWinState-%1").arg(accountId)).toByteArray();
1272     _nickListWidget->setVisible(true);
1273     if (state.isEmpty()) {
1274         foreach (BufferViewDock* view, _bufferViews)
1275             view->show();
1276         _layoutLoaded = true;
1277         return;
1278     }
1279     restoreState(state, accountId);
1280     int bufferViewId = s.value(QString("ActiveBufferView-%1").arg(accountId), -1).toInt();
1281     if (bufferViewId >= 0)
1282         changeActiveBufferView(bufferViewId);
1283
1284     _layoutLoaded = true;
1285 }
1286
1287 void MainWin::saveLayout()
1288 {
1289     QtUiSettings s;
1290     int accountId = _bufferViews.count() ? Client::currentCoreAccount().accountId().toInt() : 0;  // only save if we still have a layout!
1291     if (accountId > 0) {
1292         s.setValue(QString("MainWinState-%1").arg(accountId), saveState(accountId));
1293         BufferView* view = activeBufferView();
1294         s.setValue(QString("ActiveBufferView-%1").arg(accountId), view ? view->config()->bufferViewId() : -1);
1295     }
1296 }
1297
1298 void MainWin::disconnectedFromCore()
1299 {
1300     // save core specific layout and remove bufferviews;
1301     saveLayout();
1302     _layoutLoaded = false;
1303
1304     QVariant actionData;
1305     BufferViewDock* dock;
1306     foreach (QAction* action, _bufferViewsMenu->actions()) {
1307         actionData = action->data();
1308         if (!actionData.isValid())
1309             continue;
1310
1311         dock = qobject_cast<BufferViewDock*>(action->parent());
1312         if (dock && actionData.toInt() != -1) {
1313             removeAction(action);
1314             _bufferViews.removeAll(dock);
1315             dock->deleteLater();
1316         }
1317     }
1318
1319     // store last active buffer
1320     QtUiSettings s;
1321     BufferId lastBufId = _bufferWidget->currentBuffer();
1322     if (lastBufId.isValid()) {
1323         s.setValue("LastUsedBufferId", lastBufId.toInt());
1324         // clear the current selection
1325         Client::bufferModel()->standardSelectionModel()->clearSelection();
1326     }
1327     restoreState(s.value("MainWinState").toByteArray());
1328     setDisconnectedState();
1329 }
1330
1331 void MainWin::setDisconnectedState()
1332 {
1333     ActionCollection* coll = QtUi::actionCollection("General");
1334     // ui.menuCore->setEnabled(false);
1335     coll->action("ConnectCore")->setEnabled(true);
1336     coll->action("DisconnectCore")->setEnabled(false);
1337     coll->action("CoreInfo")->setEnabled(false);
1338     coll->action("ChangePassword")->setEnabled(false);
1339     //_viewMenu->setEnabled(false);
1340     statusBar()->showMessage(tr("Not connected to core."));
1341     if (_msgProcessorStatusWidget)
1342         _msgProcessorStatusWidget->setProgress(0, 0);
1343     updateIcon();
1344     systemTray()->setState(SystemTray::Passive);
1345     _nickListWidget->setVisible(false);
1346 }
1347
1348 void MainWin::userAuthenticationRequired(CoreAccount* account, bool* valid, const QString& errorMessage)
1349 {
1350     Q_UNUSED(errorMessage)
1351     CoreConnectAuthDlg dlg(account);
1352     *valid = (dlg.exec() == QDialog::Accepted);
1353 }
1354
1355 void MainWin::handleNoSslInClient(bool* accepted)
1356 {
1357     QMessageBox box(QMessageBox::Warning,
1358                     tr("Unencrypted Connection"),
1359                     tr("<b>Your client does not support SSL encryption</b>"),
1360                     QMessageBox::Ignore | QMessageBox::Cancel);
1361     box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
1362     box.setDefaultButton(QMessageBox::Ignore);
1363     *accepted = (box.exec() == QMessageBox::Ignore);
1364 }
1365
1366 void MainWin::handleNoSslInCore(bool* accepted)
1367 {
1368     QMessageBox box(QMessageBox::Warning,
1369                     tr("Unencrypted Connection"),
1370                     tr("<b>Your core does not support SSL encryption</b>"),
1371                     QMessageBox::Ignore | QMessageBox::Cancel);
1372     box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
1373     box.setDefaultButton(QMessageBox::Ignore);
1374     *accepted = (box.exec() == QMessageBox::Ignore);
1375 }
1376
1377 void MainWin::handleSslErrors(const QSslSocket* socket, bool* accepted, bool* permanently)
1378 {
1379     QString errorString = "<ul>";
1380     foreach (const QSslError error, socket->sslErrors())
1381         errorString += QString("<li>%1</li>").arg(error.errorString());
1382     errorString += "</ul>";
1383
1384     QMessageBox box(QMessageBox::Warning,
1385                     tr("Untrusted Security Certificate"),
1386                     tr("<b>The SSL certificate provided by the core at %1 is untrusted for the following reasons:</b>").arg(socket->peerName()),
1387                     QMessageBox::Cancel);
1388     box.setInformativeText(errorString);
1389     box.addButton(tr("Continue"), QMessageBox::AcceptRole);
1390     box.setDefaultButton(box.addButton(tr("Show Certificate"), QMessageBox::HelpRole));
1391
1392     QMessageBox::ButtonRole role;
1393     do {
1394         box.exec();
1395         role = box.buttonRole(box.clickedButton());
1396         if (role == QMessageBox::HelpRole) {
1397             SslInfoDlg dlg(socket);
1398             dlg.exec();
1399         }
1400     } while (role == QMessageBox::HelpRole);
1401
1402     *accepted = role == QMessageBox::AcceptRole;
1403     if (*accepted) {
1404         QMessageBox box2(QMessageBox::Warning,
1405                          tr("Untrusted Security Certificate"),
1406                          tr("Would you like to accept this certificate forever without being prompted?"),
1407                          QMessageBox::NoButton);
1408         box2.setDefaultButton(box2.addButton(tr("Current Session Only"), QMessageBox::NoRole));
1409         box2.addButton(tr("Forever"), QMessageBox::YesRole);
1410         box2.exec();
1411         *permanently = (box2.buttonRole(box2.clickedButton()) == QMessageBox::YesRole);
1412     }
1413 }
1414
1415 void MainWin::handleCoreConnectionError(const QString& error)
1416 {
1417     QMessageBox::critical(this, tr("Core Connection Error"), error, QMessageBox::Ok);
1418 }
1419
1420 void MainWin::showCoreConnectionDlg()
1421 {
1422     CoreConnectDlg dlg;
1423     if (dlg.exec() == QDialog::Accepted) {
1424         AccountId accId = dlg.selectedAccount();
1425         if (accId.isValid())
1426             Client::coreConnection()->connectToCore(accId);
1427     }
1428 }
1429
1430 void MainWin::showCoreConfigWizard(const QVariantList& backends, const QVariantList& authenticators)
1431 {
1432     auto* wizard = new CoreConfigWizard(Client::coreConnection(), backends, authenticators, this);
1433
1434     wizard->show();
1435 }
1436
1437 void MainWin::showChannelList(NetworkId netId, const QString& channelFilters, bool listImmediately)
1438 {
1439     if (!netId.isValid()) {
1440         auto* action = qobject_cast<QAction*>(sender());
1441         if (action)
1442             netId = action->data().value<NetworkId>();
1443         if (!netId.isValid()) {
1444             // We still haven't found a valid network, probably no network selected, e.g. "/list"
1445             // on the client homescreen when no networks are connected.
1446             QMessageBox box(QMessageBox::Information,
1447                             tr("No network selected"),
1448                             QString("<b>%1</b>").arg(tr("No network selected")),
1449                             QMessageBox::Ok);
1450             box.setInformativeText(tr("Select a network before trying to view the channel list."));
1451             box.exec();
1452             return;
1453         }
1454     }
1455
1456     auto* channelListDlg = new ChannelListDlg(this);
1457     channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
1458     channelListDlg->setNetwork(netId);
1459     if (!channelFilters.isEmpty()) {
1460         channelListDlg->setChannelFilters(channelFilters);
1461     }
1462     if (listImmediately) {
1463         channelListDlg->requestSearch();
1464     }
1465     channelListDlg->show();
1466 }
1467
1468 void MainWin::showNetworkConfig(NetworkId netId)
1469 {
1470     SettingsPageDlg dlg{new NetworksSettingsPage{}};
1471     if (netId.isValid())
1472         qobject_cast<NetworksSettingsPage*>(dlg.currentPage())->bufferList_Open(netId);
1473     dlg.exec();
1474 }
1475
1476 void MainWin::showIgnoreList(QString newRule)
1477 {
1478     SettingsPageDlg dlg{new IgnoreListSettingsPage{}};
1479     // prepare config dialog for new rule
1480     if (!newRule.isEmpty())
1481         qobject_cast<IgnoreListSettingsPage*>(dlg.currentPage())->editIgnoreRule(newRule);
1482     dlg.exec();
1483 }
1484
1485 void MainWin::showCoreInfoDlg()
1486 {
1487     CoreInfoDlg{}.exec();
1488 }
1489
1490 void MainWin::showAwayLog()
1491 {
1492     if (_awayLog)
1493         return;
1494     auto* filter = new AwayLogFilter(Client::messageModel());
1495     _awayLog = new AwayLogView(filter, nullptr);
1496     filter->setParent(_awayLog);
1497     connect(_awayLog, &QObject::destroyed, this, &MainWin::awayLogDestroyed);
1498     _awayLog->setAttribute(Qt::WA_DeleteOnClose);
1499     _awayLog->show();
1500 }
1501
1502 void MainWin::awayLogDestroyed()
1503 {
1504     _awayLog = nullptr;
1505 }
1506
1507 void MainWin::showSettingsDlg()
1508 {
1509     auto dlg = new SettingsDlg();
1510
1511     // Category: Interface
1512     dlg->registerSettingsPage(new AppearanceSettingsPage(dlg));
1513     dlg->registerSettingsPage(new ChatViewSettingsPage(dlg));
1514     dlg->registerSettingsPage(new ChatViewColorSettingsPage(dlg));
1515     dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
1516     dlg->registerSettingsPage(new ItemViewSettingsPage(dlg));
1517     dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
1518     dlg->registerSettingsPage(new InputWidgetSettingsPage(dlg));
1519     dlg->registerSettingsPage(new TopicWidgetSettingsPage(dlg));
1520 #ifdef HAVE_SONNET
1521     dlg->registerSettingsPage(new SonnetSettingsPage(dlg));
1522 #endif
1523     dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
1524     dlg->registerSettingsPage(new CoreHighlightSettingsPage(dlg));
1525     dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
1526     dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
1527
1528     // Category: IRC
1529     dlg->registerSettingsPage(new ConnectionSettingsPage(dlg));
1530     dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
1531     dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
1532     dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
1533     dlg->registerSettingsPage(new IgnoreListSettingsPage(dlg));
1534     // dlg->registerSettingsPage(new DccSettingsPage(dlg)); not ready yet
1535
1536     // Category: Remote Cores
1537     if (Quassel::runMode() != Quassel::Monolithic) {
1538         dlg->registerSettingsPage(new CoreAccountSettingsPage(dlg));
1539         dlg->registerSettingsPage(new CoreConnectionSettingsPage(dlg));
1540     }
1541
1542     dlg->show();
1543 }
1544
1545 void MainWin::showAboutDlg()
1546 {
1547     AboutDlg{}.exec();
1548 }
1549
1550 void MainWin::showShortcutsDlg()
1551 {
1552 #ifdef HAVE_KDE
1553     KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed);
1554     foreach (KActionCollection* coll, QtUi::actionCollections()) {
1555         dlg.addCollection(coll, coll->property("Category").toString());
1556     }
1557     dlg.configure(true);
1558 #else
1559     SettingsPageDlg{new ShortcutsSettingsPage{QtUi::actionCollections()}}.exec();
1560 #endif
1561 }
1562
1563 void MainWin::showNewTransferDlg(const QUuid& transferId)
1564 {
1565     auto transfer = Client::transferManager()->transfer(transferId);
1566     if (transfer) {
1567         if (transfer->status() == Transfer::Status::New) {
1568             auto* dlg = new ReceiveFileDlg(transfer, this);
1569             dlg->show();
1570         }
1571     }
1572     else {
1573         qWarning() << "Unknown transfer ID" << transferId;
1574     }
1575 }
1576
1577 void MainWin::onFullScreenToggled()
1578 {
1579     // Relying on QWidget::isFullScreen is discouraged, see the KToggleFullScreenAction docs
1580     // Also, one should not use showFullScreen() or showNormal(), as those reset all other window flags
1581
1582 #ifdef HAVE_KDE
1583     static_cast<KToggleFullScreenAction*>(_fullScreenAction)->setFullScreen(this, _fullScreenAction->isChecked());
1584 #else
1585     if (_fullScreenAction->isChecked())
1586         setWindowState(windowState() | Qt::WindowFullScreen);
1587     else
1588         setWindowState(windowState() & ~Qt::WindowFullScreen);
1589 #endif
1590 }
1591
1592 /********************************************************************************************************/
1593
1594 bool MainWin::event(QEvent* event)
1595 {
1596     switch (event->type()) {
1597     case QEvent::WindowActivate: {
1598         BufferId bufferId = Client::bufferModel()->currentBuffer();
1599         if (bufferId.isValid())
1600             Client::instance()->markBufferAsRead(bufferId);
1601         break;
1602     }
1603     case QEvent::WindowDeactivate:
1604         if (bufferWidget()->autoMarkerLineOnLostFocus())
1605             bufferWidget()->setMarkerLine();
1606         break;
1607     default:
1608         break;
1609     }
1610     return QMainWindow::event(event);
1611 }
1612
1613 void MainWin::moveEvent(QMoveEvent* event)
1614 {
1615     if (!(windowState() & Qt::WindowMaximized))
1616         _normalPos = event->pos();
1617
1618     QMainWindow::moveEvent(event);
1619 }
1620
1621 void MainWin::resizeEvent(QResizeEvent* event)
1622 {
1623     if (!(windowState() & Qt::WindowMaximized))
1624         _normalSize = event->size();
1625
1626     QMainWindow::resizeEvent(event);
1627 }
1628
1629 void MainWin::closeEvent(QCloseEvent* event)
1630 {
1631     QtUiSettings s;
1632     auto* app = qobject_cast<QtUiApplication*> qApp;
1633     Q_ASSERT(app);
1634     // On OSX it can happen that the closeEvent occurs twice. (Especially if packaged with Frameworks)
1635     // This messes up MainWinState/MainWinHidden save/restore.
1636     // It's a bug in Qt: https://bugreports.qt.io/browse/QTBUG-43344
1637     if (!_aboutToQuit && !app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
1638         QtUi::hideMainWidget();
1639         event->ignore();
1640     }
1641     else if (!_aboutToQuit) {
1642         _aboutToQuit = true;
1643         event->accept();
1644         Quassel::instance()->quit();
1645     }
1646     else {
1647         event->ignore();
1648     }
1649 }
1650
1651 void MainWin::messagesInserted(const QModelIndex& parent, int start, int end)
1652 {
1653     Q_UNUSED(parent);
1654
1655     bool hasFocus = QApplication::activeWindow() != nullptr;
1656
1657     for (int i = start; i <= end; i++) {
1658         QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
1659         if (!idx.isValid()) {
1660             qDebug() << "MainWin::messagesInserted(): Invalid model index!";
1661             continue;
1662         }
1663         Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
1664         if (flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
1665             continue;
1666
1667         BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
1668         BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
1669
1670         // check if bufferId belongs to the shown chatlists
1671         if (!(Client::bufferViewOverlay()->bufferIds().contains(bufId) || Client::bufferViewOverlay()->tempRemovedBufferIds().contains(bufId)))
1672             continue;
1673
1674         // check if it's the buffer currently displayed
1675         if (hasFocus && bufId == Client::bufferModel()->currentBuffer())
1676             continue;
1677
1678         // only show notifications for higlights or queries
1679         if (bufType != BufferInfo::QueryBuffer && !(flags & Message::Highlight))
1680             continue;
1681
1682         // and of course: don't notify for ignored messages
1683         if (Client::ignoreListManager()
1684             && Client::ignoreListManager()->match(idx.data(MessageModel::MessageRole).value<Message>(),
1685                                                   Client::networkModel()->networkName(bufId)))
1686             continue;
1687
1688         // seems like we have a legit notification candidate!
1689         QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
1690         QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
1691         QString contents = idx.data(ChatLineModel::DisplayRole).toString();
1692         AbstractNotificationBackend::NotificationType type;
1693
1694         if (bufType == BufferInfo::QueryBuffer && !hasFocus)
1695             type = AbstractNotificationBackend::PrivMsg;
1696         else if (bufType == BufferInfo::QueryBuffer && hasFocus)
1697             type = AbstractNotificationBackend::PrivMsgFocused;
1698         else if (flags & Message::Highlight && !hasFocus)
1699             type = AbstractNotificationBackend::Highlight;
1700         else
1701             type = AbstractNotificationBackend::HighlightFocused;
1702
1703         QtUi::instance()->invokeNotification(bufId, type, sender, contents);
1704     }
1705 }
1706
1707 void MainWin::currentBufferChanged(BufferId buffer)
1708 {
1709     if (buffer.isValid())
1710         Client::instance()->markBufferAsRead(buffer);
1711 }
1712
1713 void MainWin::clientNetworkCreated(NetworkId id)
1714 {
1715     const Network* net = Client::network(id);
1716     auto* act = new QAction(net->networkName(), this);
1717     act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
1718     act->setData(QVariant::fromValue(id));
1719     connect(net, &SyncableObject::updatedRemotely, this, &MainWin::clientNetworkUpdated);
1720     connect(act, &QAction::triggered, this, &MainWin::connectOrDisconnectFromNet);
1721
1722     QAction* beforeAction = nullptr;
1723     foreach (QAction* action, _networksMenu->actions()) {
1724         if (!action->data().isValid())  // ignore stock actions
1725             continue;
1726         if (net->networkName().localeAwareCompare(action->text()) < 0) {
1727             beforeAction = action;
1728             break;
1729         }
1730     }
1731     _networksMenu->insertAction(beforeAction, act);
1732 }
1733
1734 void MainWin::clientNetworkUpdated()
1735 {
1736     const auto* net = qobject_cast<const Network*>(sender());
1737     if (!net)
1738         return;
1739
1740     auto* action = findChild<QAction*>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1741     if (!action)
1742         return;
1743
1744     action->setText(net->networkName());
1745
1746     switch (net->connectionState()) {
1747     case Network::Initialized:
1748         action->setIcon(icon::get("network-connect"));
1749         // if we have no currently selected buffer, jump to the first connecting statusbuffer
1750         if (!bufferWidget()->currentBuffer().isValid()) {
1751             QModelIndex idx = Client::networkModel()->networkIndex(net->networkId());
1752             if (idx.isValid()) {
1753                 BufferId statusBufferId = idx.data(NetworkModel::BufferIdRole).value<BufferId>();
1754                 Client::bufferModel()->switchToBuffer(statusBufferId);
1755             }
1756         }
1757         break;
1758     case Network::Disconnected:
1759         action->setIcon(icon::get("network-disconnect"));
1760         break;
1761     default:
1762         action->setIcon(icon::get("network-wired"));
1763     }
1764 }
1765
1766 void MainWin::clientNetworkRemoved(NetworkId id)
1767 {
1768     auto* action = findChild<QAction*>(QString("NetworkAction-%1").arg(id.toInt()));
1769     if (!action)
1770         return;
1771
1772     action->deleteLater();
1773 }
1774
1775 void MainWin::connectOrDisconnectFromNet()
1776 {
1777     auto* act = qobject_cast<QAction*>(sender());
1778     if (!act)
1779         return;
1780     const Network* net = Client::network(act->data().value<NetworkId>());
1781     if (!net)
1782         return;
1783     if (net->connectionState() == Network::Disconnected)
1784         net->requestConnect();
1785     else
1786         net->requestDisconnect();
1787 }
1788
1789 void MainWin::onFormatApplyColorTriggered()
1790 {
1791     if (!_inputWidget)
1792         return;
1793
1794     _inputWidget->applyFormatActiveColor();
1795 }
1796
1797 void MainWin::onFormatApplyColorFillTriggered()
1798 {
1799     if (!_inputWidget)
1800         return;
1801
1802     _inputWidget->applyFormatActiveColorFill();
1803 }
1804
1805 void MainWin::onFormatClearTriggered()
1806 {
1807     if (!_inputWidget)
1808         return;
1809
1810     _inputWidget->clearFormat();
1811 }
1812
1813 void MainWin::onFormatBoldTriggered()
1814 {
1815     if (!_inputWidget)
1816         return;
1817
1818     _inputWidget->toggleFormatBold();
1819 }
1820
1821 void MainWin::onFormatItalicTriggered()
1822 {
1823     if (!_inputWidget)
1824         return;
1825
1826     _inputWidget->toggleFormatItalic();
1827 }
1828
1829 void MainWin::onFormatUnderlineTriggered()
1830 {
1831     if (!_inputWidget)
1832         return;
1833
1834     _inputWidget->toggleFormatUnderline();
1835 }
1836
1837 void MainWin::onJumpHotBufferTriggered()
1838 {
1839     if (!_bufferHotList->rowCount())
1840         return;
1841
1842     Client::bufferModel()->switchToBuffer(_bufferHotList->hottestBuffer());
1843 }
1844
1845 void MainWin::onBufferSearchTriggered()
1846 {
1847     if (_activeBufferViewIndex < 0 || _activeBufferViewIndex >= _bufferViews.count()) {
1848         qWarning() << "Tried to activate filter on invalid bufferview:" << _activeBufferViewIndex;
1849         return;
1850     }
1851
1852     _bufferViews[_activeBufferViewIndex]->activateFilter();
1853 }
1854
1855 void MainWin::onJumpKey()
1856 {
1857     auto* action = qobject_cast<QAction*>(sender());
1858     if (!action || !Client::bufferModel())
1859         return;
1860     int idx = action->property("Index").toInt();
1861
1862     if (_jumpKeyMap.isEmpty())
1863         _jumpKeyMap = CoreAccountSettings().jumpKeyMap();
1864
1865     if (!_jumpKeyMap.contains(idx))
1866         return;
1867
1868     BufferId buffer = _jumpKeyMap.value(idx);
1869     if (buffer.isValid())
1870         Client::bufferModel()->switchToBuffer(buffer);
1871 }
1872
1873 void MainWin::bindJumpKey()
1874 {
1875     auto* action = qobject_cast<QAction*>(sender());
1876     if (!action || !Client::bufferModel())
1877         return;
1878     int idx = action->property("Index").toInt();
1879
1880     _jumpKeyMap[idx] = Client::bufferModel()->currentBuffer();
1881     CoreAccountSettings().setJumpKeyMap(_jumpKeyMap);
1882 }
1883
1884 void MainWin::onDebugNetworkModelTriggered()
1885 {
1886     auto* view = new QTreeView;
1887     view->setAttribute(Qt::WA_DeleteOnClose);
1888     view->setWindowTitle("Debug NetworkModel View");
1889     view->setModel(Client::networkModel());
1890     view->setColumnWidth(0, 250);
1891     view->setColumnWidth(1, 250);
1892     view->setColumnWidth(2, 80);
1893     view->resize(610, 300);
1894     view->show();
1895 }
1896
1897 void MainWin::onDebugHotListTriggered()
1898 {
1899     _bufferHotList->invalidate();
1900     _bufferHotList->sort(0, Qt::DescendingOrder);
1901
1902     auto* view = new QTreeView;
1903     view->setAttribute(Qt::WA_DeleteOnClose);
1904     view->setModel(_bufferHotList);
1905     view->show();
1906 }
1907
1908 void MainWin::onDebugBufferViewOverlayTriggered()
1909 {
1910     auto* overlay = new DebugBufferViewOverlay(nullptr);
1911     overlay->setAttribute(Qt::WA_DeleteOnClose);
1912     overlay->show();
1913 }
1914
1915 void MainWin::onDebugMessageModelTriggered()
1916 {
1917     auto* view = new QTableView(nullptr);
1918     auto* filter = new DebugMessageModelFilter(view);
1919     filter->setSourceModel(Client::messageModel());
1920     view->setModel(filter);
1921     view->setAttribute(Qt::WA_DeleteOnClose, true);
1922     view->verticalHeader()->hide();
1923     view->horizontalHeader()->setStretchLastSection(true);
1924     view->show();
1925 }
1926
1927 void MainWin::onDebugLogTriggered()
1928 {
1929     auto dlg = new DebugLogDlg(this);  // will be deleted on close
1930     dlg->show();
1931 }
1932
1933 void MainWin::onShowResourceTreeTriggered()
1934 {
1935     auto dlg = new ResourceTreeDlg(this);  // will be deleted on close
1936     dlg->show();
1937 }
1938
1939 void MainWin::showStatusBarMessage(const QString& message)
1940 {
1941     statusBar()->showMessage(message, 10000);
1942 }