qa: Resolve Qt deprecation warnings - default-construct QFlags
[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{}.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{}}.exec();
922 }
923
924 void MainWin::onConfigureNetworksTriggered()
925 {
926     SettingsPageDlg{new NetworksSettingsPage{}}.exec();
927 }
928
929 void MainWin::onConfigureViewsTriggered()
930 {
931     SettingsPageDlg{new BufferViewSettingsPage{}}.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);
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     foreach (const QSslError error, socket->sslErrors())
1388         errorString += QString("<li>%1</li>").arg(error.errorString());
1389     errorString += "</ul>";
1390
1391     QMessageBox box(QMessageBox::Warning,
1392                     tr("Untrusted Security Certificate"),
1393                     tr("<b>The SSL certificate provided by the core at %1 is untrusted for the following reasons:</b>").arg(socket->peerName()),
1394                     QMessageBox::Cancel);
1395     box.setInformativeText(errorString);
1396     box.addButton(tr("Continue"), QMessageBox::AcceptRole);
1397     box.setDefaultButton(box.addButton(tr("Show Certificate"), QMessageBox::HelpRole));
1398
1399     QMessageBox::ButtonRole role;
1400     do {
1401         box.exec();
1402         role = box.buttonRole(box.clickedButton());
1403         if (role == QMessageBox::HelpRole) {
1404             SslInfoDlg dlg(socket);
1405             dlg.exec();
1406         }
1407     } while (role == QMessageBox::HelpRole);
1408
1409     *accepted = role == QMessageBox::AcceptRole;
1410     if (*accepted) {
1411         QMessageBox box2(QMessageBox::Warning,
1412                          tr("Untrusted Security Certificate"),
1413                          tr("Would you like to accept this certificate forever without being prompted?"),
1414                          QMessageBox::NoButton);
1415         box2.setDefaultButton(box2.addButton(tr("Current Session Only"), QMessageBox::NoRole));
1416         box2.addButton(tr("Forever"), QMessageBox::YesRole);
1417         box2.exec();
1418         *permanently = (box2.buttonRole(box2.clickedButton()) == QMessageBox::YesRole);
1419     }
1420 }
1421
1422 void MainWin::handleCoreConnectionError(const QString& error)
1423 {
1424     QMessageBox::critical(this, tr("Core Connection Error"), error, QMessageBox::Ok);
1425 }
1426
1427 void MainWin::showCoreConnectionDlg()
1428 {
1429     CoreConnectDlg dlg;
1430     if (dlg.exec() == QDialog::Accepted) {
1431         AccountId accId = dlg.selectedAccount();
1432         if (accId.isValid())
1433             Client::coreConnection()->connectToCore(accId);
1434     }
1435 }
1436
1437 void MainWin::showCoreConfigWizard(const QVariantList& backends, const QVariantList& authenticators)
1438 {
1439     auto* wizard = new CoreConfigWizard(Client::coreConnection(), backends, authenticators, this);
1440
1441     wizard->show();
1442 }
1443
1444 void MainWin::showChannelList(NetworkId netId, const QString& channelFilters, bool listImmediately)
1445 {
1446     if (!netId.isValid()) {
1447         auto* action = qobject_cast<QAction*>(sender());
1448         if (action)
1449             netId = action->data().value<NetworkId>();
1450         if (!netId.isValid()) {
1451             // We still haven't found a valid network, probably no network selected, e.g. "/list"
1452             // on the client homescreen when no networks are connected.
1453             QMessageBox box(QMessageBox::Information,
1454                             tr("No network selected"),
1455                             QString("<b>%1</b>").arg(tr("No network selected")),
1456                             QMessageBox::Ok);
1457             box.setInformativeText(tr("Select a network before trying to view the channel list."));
1458             box.exec();
1459             return;
1460         }
1461     }
1462
1463     auto* channelListDlg = new ChannelListDlg(this);
1464     channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
1465     channelListDlg->setNetwork(netId);
1466     if (!channelFilters.isEmpty()) {
1467         channelListDlg->setChannelFilters(channelFilters);
1468     }
1469     if (listImmediately) {
1470         channelListDlg->requestSearch();
1471     }
1472     channelListDlg->show();
1473 }
1474
1475 void MainWin::showNetworkConfig(NetworkId netId)
1476 {
1477     SettingsPageDlg dlg{new NetworksSettingsPage{}};
1478     if (netId.isValid())
1479         qobject_cast<NetworksSettingsPage*>(dlg.currentPage())->bufferList_Open(netId);
1480     dlg.exec();
1481 }
1482
1483 void MainWin::showIgnoreList(QString newRule)
1484 {
1485     SettingsPageDlg dlg{new IgnoreListSettingsPage{}};
1486     // prepare config dialog for new rule
1487     if (!newRule.isEmpty())
1488         qobject_cast<IgnoreListSettingsPage*>(dlg.currentPage())->editIgnoreRule(newRule);
1489     dlg.exec();
1490 }
1491
1492 void MainWin::showCoreInfoDlg()
1493 {
1494     CoreInfoDlg{}.exec();
1495 }
1496
1497 void MainWin::showAwayLog()
1498 {
1499     if (_awayLog)
1500         return;
1501     auto* filter = new AwayLogFilter(Client::messageModel());
1502     _awayLog = new AwayLogView(filter, nullptr);
1503     filter->setParent(_awayLog);
1504     connect(_awayLog, &QObject::destroyed, this, &MainWin::awayLogDestroyed);
1505     _awayLog->setAttribute(Qt::WA_DeleteOnClose);
1506     _awayLog->show();
1507 }
1508
1509 void MainWin::awayLogDestroyed()
1510 {
1511     _awayLog = nullptr;
1512 }
1513
1514 void MainWin::showSettingsDlg()
1515 {
1516     auto dlg = new SettingsDlg();
1517
1518     // Category: Interface
1519     dlg->registerSettingsPage(new AppearanceSettingsPage(dlg));
1520     dlg->registerSettingsPage(new ChatViewSettingsPage(dlg));
1521     dlg->registerSettingsPage(new ChatViewColorSettingsPage(dlg));
1522     dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
1523     dlg->registerSettingsPage(new ItemViewSettingsPage(dlg));
1524     dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
1525     dlg->registerSettingsPage(new InputWidgetSettingsPage(dlg));
1526     dlg->registerSettingsPage(new TopicWidgetSettingsPage(dlg));
1527 #ifdef HAVE_SONNET
1528     dlg->registerSettingsPage(new SonnetSettingsPage(dlg));
1529 #endif
1530     auto coreHighlightsPage = new CoreHighlightSettingsPage(dlg);
1531     auto localHighlightsPage = new HighlightSettingsPage(dlg);
1532     // Let CoreHighlightSettingsPage reload HighlightSettingsPage after doing an import with
1533     // cleaning up.  Otherwise, HighlightSettingsPage won't show that the local rules were deleted.
1534     connect(coreHighlightsPage, &CoreHighlightSettingsPage::localHighlightsChanged,
1535             localHighlightsPage, &HighlightSettingsPage::load);
1536     // Put core-side highlights before local/legacy highlights
1537     dlg->registerSettingsPage(coreHighlightsPage);
1538     dlg->registerSettingsPage(localHighlightsPage);
1539     dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
1540     dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
1541
1542     // Category: IRC
1543     dlg->registerSettingsPage(new ConnectionSettingsPage(dlg));
1544     dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
1545     dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
1546     dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
1547     dlg->registerSettingsPage(new IgnoreListSettingsPage(dlg));
1548     // dlg->registerSettingsPage(new DccSettingsPage(dlg)); not ready yet
1549
1550     // Category: Remote Cores
1551     if (Quassel::runMode() != Quassel::Monolithic) {
1552         dlg->registerSettingsPage(new CoreAccountSettingsPage(dlg));
1553         dlg->registerSettingsPage(new CoreConnectionSettingsPage(dlg));
1554     }
1555
1556     dlg->show();
1557 }
1558
1559 void MainWin::showAboutDlg()
1560 {
1561     AboutDlg{}.exec();
1562 }
1563
1564 void MainWin::showShortcutsDlg()
1565 {
1566 #ifdef HAVE_KDE
1567     KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed);
1568     foreach (KActionCollection* coll, QtUi::actionCollections()) {
1569         dlg.addCollection(coll, coll->property("Category").toString());
1570     }
1571     dlg.configure(true);
1572 #else
1573     SettingsPageDlg{new ShortcutsSettingsPage{QtUi::actionCollections()}}.exec();
1574 #endif
1575 }
1576
1577 void MainWin::showNewTransferDlg(const QUuid& transferId)
1578 {
1579     auto transfer = Client::transferManager()->transfer(transferId);
1580     if (transfer) {
1581         if (transfer->status() == Transfer::Status::New) {
1582             auto* dlg = new ReceiveFileDlg(transfer, this);
1583             dlg->show();
1584         }
1585     }
1586     else {
1587         qWarning() << "Unknown transfer ID" << transferId;
1588     }
1589 }
1590
1591 void MainWin::onFullScreenToggled()
1592 {
1593     // Relying on QWidget::isFullScreen is discouraged, see the KToggleFullScreenAction docs
1594     // Also, one should not use showFullScreen() or showNormal(), as those reset all other window flags
1595
1596 #ifdef HAVE_KDE
1597     static_cast<KToggleFullScreenAction*>(_fullScreenAction)->setFullScreen(this, _fullScreenAction->isChecked());
1598 #else
1599     if (_fullScreenAction->isChecked())
1600         setWindowState(windowState() | Qt::WindowFullScreen);
1601     else
1602         setWindowState(windowState() & ~Qt::WindowFullScreen);
1603 #endif
1604 }
1605
1606 /********************************************************************************************************/
1607
1608 bool MainWin::event(QEvent* event)
1609 {
1610     switch (event->type()) {
1611     case QEvent::WindowActivate: {
1612         BufferId bufferId = Client::bufferModel()->currentBuffer();
1613         if (bufferId.isValid())
1614             Client::instance()->markBufferAsRead(bufferId);
1615         break;
1616     }
1617     case QEvent::WindowDeactivate:
1618         if (bufferWidget()->autoMarkerLineOnLostFocus())
1619             bufferWidget()->setMarkerLine();
1620         break;
1621     default:
1622         break;
1623     }
1624     return QMainWindow::event(event);
1625 }
1626
1627 void MainWin::moveEvent(QMoveEvent* event)
1628 {
1629     if (!(windowState() & Qt::WindowMaximized))
1630         _normalPos = event->pos();
1631
1632     QMainWindow::moveEvent(event);
1633 }
1634
1635 void MainWin::resizeEvent(QResizeEvent* event)
1636 {
1637     if (!(windowState() & Qt::WindowMaximized))
1638         _normalSize = event->size();
1639
1640     QMainWindow::resizeEvent(event);
1641 }
1642
1643 void MainWin::closeEvent(QCloseEvent* event)
1644 {
1645     QtUiSettings s;
1646     auto* app = qobject_cast<QtUiApplication*> qApp;
1647     Q_ASSERT(app);
1648     // On OSX it can happen that the closeEvent occurs twice. (Especially if packaged with Frameworks)
1649     // This messes up MainWinState/MainWinHidden save/restore.
1650     // It's a bug in Qt: https://bugreports.qt.io/browse/QTBUG-43344
1651     if (!_aboutToQuit && !app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
1652         QtUi::hideMainWidget();
1653         event->ignore();
1654     }
1655     else if (!_aboutToQuit) {
1656         _aboutToQuit = true;
1657         event->accept();
1658         Quassel::instance()->quit();
1659     }
1660     else {
1661         event->ignore();
1662     }
1663 }
1664
1665 void MainWin::messagesInserted(const QModelIndex& parent, int start, int end)
1666 {
1667     Q_UNUSED(parent);
1668
1669     bool hasFocus = QApplication::activeWindow() != nullptr;
1670
1671     for (int i = start; i <= end; i++) {
1672         QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
1673         if (!idx.isValid()) {
1674             qDebug() << "MainWin::messagesInserted(): Invalid model index!";
1675             continue;
1676         }
1677         Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
1678         if (flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
1679             continue;
1680
1681         BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
1682         BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
1683
1684         // check if bufferId belongs to the shown chatlists
1685         if (!(Client::bufferViewOverlay()->bufferIds().contains(bufId) || Client::bufferViewOverlay()->tempRemovedBufferIds().contains(bufId)))
1686             continue;
1687
1688         // check if it's the buffer currently displayed
1689         if (hasFocus && bufId == Client::bufferModel()->currentBuffer())
1690             continue;
1691
1692         // only show notifications for higlights or queries
1693         if (bufType != BufferInfo::QueryBuffer && !(flags & Message::Highlight))
1694             continue;
1695
1696         // and of course: don't notify for ignored messages
1697         if (Client::ignoreListManager()
1698             && Client::ignoreListManager()->match(idx.data(MessageModel::MessageRole).value<Message>(),
1699                                                   Client::networkModel()->networkName(bufId)))
1700             continue;
1701
1702         // seems like we have a legit notification candidate!
1703         QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
1704         QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
1705         QString contents = idx.data(ChatLineModel::DisplayRole).toString();
1706         AbstractNotificationBackend::NotificationType type;
1707
1708         if (bufType == BufferInfo::QueryBuffer && !hasFocus)
1709             type = AbstractNotificationBackend::PrivMsg;
1710         else if (bufType == BufferInfo::QueryBuffer && hasFocus)
1711             type = AbstractNotificationBackend::PrivMsgFocused;
1712         else if (flags & Message::Highlight && !hasFocus)
1713             type = AbstractNotificationBackend::Highlight;
1714         else
1715             type = AbstractNotificationBackend::HighlightFocused;
1716
1717         QtUi::instance()->invokeNotification(bufId, type, sender, contents);
1718     }
1719 }
1720
1721 void MainWin::currentBufferChanged(BufferId buffer)
1722 {
1723     if (buffer.isValid())
1724         Client::instance()->markBufferAsRead(buffer);
1725 }
1726
1727 void MainWin::clientNetworkCreated(NetworkId id)
1728 {
1729     const Network* net = Client::network(id);
1730     auto* act = new QAction(net->networkName(), this);
1731     act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
1732     act->setData(QVariant::fromValue(id));
1733     connect(net, &SyncableObject::updatedRemotely, this, &MainWin::clientNetworkUpdated);
1734     connect(act, &QAction::triggered, this, &MainWin::connectOrDisconnectFromNet);
1735
1736     QAction* beforeAction = nullptr;
1737     foreach (QAction* action, _networksMenu->actions()) {
1738         if (!action->data().isValid())  // ignore stock actions
1739             continue;
1740         if (net->networkName().localeAwareCompare(action->text()) < 0) {
1741             beforeAction = action;
1742             break;
1743         }
1744     }
1745     _networksMenu->insertAction(beforeAction, act);
1746 }
1747
1748 void MainWin::clientNetworkUpdated()
1749 {
1750     const auto* net = qobject_cast<const Network*>(sender());
1751     if (!net)
1752         return;
1753
1754     auto* action = findChild<QAction*>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1755     if (!action)
1756         return;
1757
1758     action->setText(net->networkName());
1759
1760     switch (net->connectionState()) {
1761     case Network::Initialized:
1762         action->setIcon(icon::get("network-connect"));
1763         // if we have no currently selected buffer, jump to the first connecting statusbuffer
1764         if (!bufferWidget()->currentBuffer().isValid()) {
1765             QModelIndex idx = Client::networkModel()->networkIndex(net->networkId());
1766             if (idx.isValid()) {
1767                 BufferId statusBufferId = idx.data(NetworkModel::BufferIdRole).value<BufferId>();
1768                 Client::bufferModel()->switchToBuffer(statusBufferId);
1769             }
1770         }
1771         break;
1772     case Network::Disconnected:
1773         action->setIcon(icon::get("network-disconnect"));
1774         break;
1775     default:
1776         action->setIcon(icon::get("network-wired"));
1777     }
1778 }
1779
1780 void MainWin::clientNetworkRemoved(NetworkId id)
1781 {
1782     auto* action = findChild<QAction*>(QString("NetworkAction-%1").arg(id.toInt()));
1783     if (!action)
1784         return;
1785
1786     action->deleteLater();
1787 }
1788
1789 void MainWin::connectOrDisconnectFromNet()
1790 {
1791     auto* act = qobject_cast<QAction*>(sender());
1792     if (!act)
1793         return;
1794     const Network* net = Client::network(act->data().value<NetworkId>());
1795     if (!net)
1796         return;
1797     if (net->connectionState() == Network::Disconnected)
1798         net->requestConnect();
1799     else
1800         net->requestDisconnect();
1801 }
1802
1803 void MainWin::onFormatApplyColorTriggered()
1804 {
1805     if (!_inputWidget)
1806         return;
1807
1808     _inputWidget->applyFormatActiveColor();
1809 }
1810
1811 void MainWin::onFormatApplyColorFillTriggered()
1812 {
1813     if (!_inputWidget)
1814         return;
1815
1816     _inputWidget->applyFormatActiveColorFill();
1817 }
1818
1819 void MainWin::onFormatClearTriggered()
1820 {
1821     if (!_inputWidget)
1822         return;
1823
1824     _inputWidget->clearFormat();
1825 }
1826
1827 void MainWin::onFormatBoldTriggered()
1828 {
1829     if (!_inputWidget)
1830         return;
1831
1832     _inputWidget->toggleFormatBold();
1833 }
1834
1835 void MainWin::onFormatItalicTriggered()
1836 {
1837     if (!_inputWidget)
1838         return;
1839
1840     _inputWidget->toggleFormatItalic();
1841 }
1842
1843 void MainWin::onFormatUnderlineTriggered()
1844 {
1845     if (!_inputWidget)
1846         return;
1847
1848     _inputWidget->toggleFormatUnderline();
1849 }
1850
1851 void MainWin::onFormatStrikethroughTriggered()
1852 {
1853     if (!_inputWidget)
1854         return;
1855
1856     _inputWidget->toggleFormatStrikethrough();
1857 }
1858
1859
1860 void MainWin::onJumpHotBufferTriggered()
1861 {
1862     if (!_bufferHotList->rowCount())
1863         return;
1864
1865     Client::bufferModel()->switchToBuffer(_bufferHotList->hottestBuffer());
1866 }
1867
1868 void MainWin::onBufferSearchTriggered()
1869 {
1870     if (_activeBufferViewIndex < 0 || _activeBufferViewIndex >= _bufferViews.count()) {
1871         qWarning() << "Tried to activate filter on invalid bufferview:" << _activeBufferViewIndex;
1872         return;
1873     }
1874
1875     _bufferViews[_activeBufferViewIndex]->activateFilter();
1876 }
1877
1878 void MainWin::onJumpKey()
1879 {
1880     auto* action = qobject_cast<QAction*>(sender());
1881     if (!action || !Client::bufferModel())
1882         return;
1883     int idx = action->property("Index").toInt();
1884
1885     if (_jumpKeyMap.isEmpty())
1886         _jumpKeyMap = CoreAccountSettings().jumpKeyMap();
1887
1888     if (!_jumpKeyMap.contains(idx))
1889         return;
1890
1891     BufferId buffer = _jumpKeyMap.value(idx);
1892     if (buffer.isValid())
1893         Client::bufferModel()->switchToBuffer(buffer);
1894 }
1895
1896 void MainWin::bindJumpKey()
1897 {
1898     auto* action = qobject_cast<QAction*>(sender());
1899     if (!action || !Client::bufferModel())
1900         return;
1901     int idx = action->property("Index").toInt();
1902
1903     _jumpKeyMap[idx] = Client::bufferModel()->currentBuffer();
1904     CoreAccountSettings().setJumpKeyMap(_jumpKeyMap);
1905 }
1906
1907 void MainWin::onDebugNetworkModelTriggered()
1908 {
1909     auto* view = new QTreeView;
1910     view->setAttribute(Qt::WA_DeleteOnClose);
1911     view->setWindowTitle("Debug NetworkModel View");
1912     view->setModel(Client::networkModel());
1913     view->setColumnWidth(0, 250);
1914     view->setColumnWidth(1, 250);
1915     view->setColumnWidth(2, 80);
1916     view->resize(610, 300);
1917     view->show();
1918 }
1919
1920 void MainWin::onDebugHotListTriggered()
1921 {
1922     _bufferHotList->invalidate();
1923     _bufferHotList->sort(0, Qt::DescendingOrder);
1924
1925     auto* view = new QTreeView;
1926     view->setAttribute(Qt::WA_DeleteOnClose);
1927     view->setModel(_bufferHotList);
1928     view->show();
1929 }
1930
1931 void MainWin::onDebugBufferViewOverlayTriggered()
1932 {
1933     auto* overlay = new DebugBufferViewOverlay(nullptr);
1934     overlay->setAttribute(Qt::WA_DeleteOnClose);
1935     overlay->show();
1936 }
1937
1938 void MainWin::onDebugMessageModelTriggered()
1939 {
1940     auto* view = new QTableView(nullptr);
1941     auto* filter = new DebugMessageModelFilter(view);
1942     filter->setSourceModel(Client::messageModel());
1943     view->setModel(filter);
1944     view->setAttribute(Qt::WA_DeleteOnClose, true);
1945     view->verticalHeader()->hide();
1946     view->horizontalHeader()->setStretchLastSection(true);
1947     view->show();
1948 }
1949
1950 void MainWin::onDebugLogTriggered()
1951 {
1952     auto dlg = new DebugLogDlg(this);  // will be deleted on close
1953     dlg->show();
1954 }
1955
1956 void MainWin::onShowResourceTreeTriggered()
1957 {
1958     auto dlg = new ResourceTreeDlg(this);  // will be deleted on close
1959     dlg->show();
1960 }
1961
1962 void MainWin::showStatusBarMessage(const QString& message)
1963 {
1964     statusBar()->showMessage(message, 10000);
1965 }