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