src: Yearly copyright bump
[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.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     if (!Client::coreConnection()->connectToCore()) {
1199         // No autoconnect selected (or no accounts)
1200         showCoreConnectionDlg();
1201     }
1202 }
1203
1204 void MainWin::connectedToCore()
1205 {
1206     Q_CHECK_PTR(Client::bufferViewManager());
1207     connect(Client::bufferViewManager(), &BufferViewManager::bufferViewConfigAdded, this, selectOverload<int>(&MainWin::addBufferView));
1208     connect(Client::bufferViewManager(), &BufferViewManager::bufferViewConfigDeleted, this, &MainWin::removeBufferView);
1209     connect(Client::bufferViewManager(), &SyncableObject::initDone, this, &MainWin::loadLayout);
1210
1211     if (Client::transferManager()) {
1212         connect(Client::transferManager(), &TransferManager::transferAdded, this, &MainWin::showNewTransferDlg);
1213     }
1214
1215     setConnectedState();
1216 }
1217
1218 void MainWin::setConnectedState()
1219 {
1220     ActionCollection* coll = QtUi::actionCollection("General");
1221
1222     coll->action("ConnectCore")->setEnabled(false);
1223     coll->action("DisconnectCore")->setEnabled(true);
1224     coll->action("ChangePassword")->setEnabled(true);
1225     coll->action("CoreInfo")->setEnabled(true);
1226
1227     foreach (QAction* action, _fileMenu->actions()) {
1228         if (isRemoteCoreOnly(action))
1229             action->setVisible(!Client::internalCore());
1230     }
1231
1232     disconnect(Client::backlogManager(),
1233                &ClientBacklogManager::updateProgress,
1234                _msgProcessorStatusWidget,
1235                &MsgProcessorStatusWidget::setProgress);
1236     disconnect(Client::backlogManager(), &ClientBacklogManager::messagesRequested, this, &MainWin::showStatusBarMessage);
1237     disconnect(Client::backlogManager(), &ClientBacklogManager::messagesProcessed, this, &MainWin::showStatusBarMessage);
1238     if (!Client::internalCore()) {
1239         connect(Client::backlogManager(),
1240                 &ClientBacklogManager::updateProgress,
1241                 _msgProcessorStatusWidget,
1242                 &MsgProcessorStatusWidget::setProgress);
1243         connect(Client::backlogManager(), &ClientBacklogManager::messagesRequested, this, &MainWin::showStatusBarMessage);
1244         connect(Client::backlogManager(), &ClientBacklogManager::messagesProcessed, this, &MainWin::showStatusBarMessage);
1245     }
1246
1247     // _viewMenu->setEnabled(true);
1248     if (!Client::internalCore())
1249         statusBar()->showMessage(tr("Connected to core."));
1250     else
1251         statusBar()->clearMessage();
1252
1253     _coreConnectionStatusWidget->setVisible(!Client::internalCore());
1254     updateIcon();
1255     systemTray()->setState(SystemTray::Active);
1256
1257     if (Client::networkIds().isEmpty()) {
1258         IrcConnectionWizard* wizard = new IrcConnectionWizard(this, Qt::Sheet);
1259         wizard->show();
1260     }
1261     else {
1262         // Monolithic always preselects last used buffer - Client only if the connection died
1263         if (Client::coreConnection()->wasReconnect() || Quassel::runMode() == Quassel::Monolithic) {
1264             QtUiSettings s;
1265             BufferId lastUsedBufferId(s.value("LastUsedBufferId").toInt());
1266             if (lastUsedBufferId.isValid())
1267                 Client::bufferModel()->switchToBuffer(lastUsedBufferId);
1268         }
1269     }
1270 }
1271
1272 void MainWin::loadLayout()
1273 {
1274     QtUiSettings s;
1275     int accountId = Client::currentCoreAccount().accountId().toInt();
1276     QByteArray state = s.value(QString("MainWinState-%1").arg(accountId)).toByteArray();
1277     if (state.isEmpty()) {
1278         foreach (BufferViewDock* view, _bufferViews)
1279             view->show();
1280         _layoutLoaded = true;
1281         return;
1282     }
1283     _nickListWidget->setVisible(true);
1284     restoreState(state, accountId);
1285     int bufferViewId = s.value(QString("ActiveBufferView-%1").arg(accountId), -1).toInt();
1286     if (bufferViewId >= 0)
1287         changeActiveBufferView(bufferViewId);
1288
1289     _layoutLoaded = true;
1290 }
1291
1292 void MainWin::saveLayout()
1293 {
1294     QtUiSettings s;
1295     int accountId = _bufferViews.count() ? Client::currentCoreAccount().accountId().toInt() : 0;  // only save if we still have a layout!
1296     if (accountId > 0) {
1297         s.setValue(QString("MainWinState-%1").arg(accountId), saveState(accountId));
1298         BufferView* view = activeBufferView();
1299         s.setValue(QString("ActiveBufferView-%1").arg(accountId), view ? view->config()->bufferViewId() : -1);
1300     }
1301 }
1302
1303 void MainWin::disconnectedFromCore()
1304 {
1305     // save core specific layout and remove bufferviews;
1306     saveLayout();
1307     _layoutLoaded = false;
1308
1309     QVariant actionData;
1310     BufferViewDock* dock;
1311     foreach (QAction* action, _bufferViewsMenu->actions()) {
1312         actionData = action->data();
1313         if (!actionData.isValid())
1314             continue;
1315
1316         dock = qobject_cast<BufferViewDock*>(action->parent());
1317         if (dock && actionData.toInt() != -1) {
1318             removeAction(action);
1319             _bufferViews.removeAll(dock);
1320             dock->deleteLater();
1321         }
1322     }
1323
1324     // store last active buffer
1325     QtUiSettings s;
1326     BufferId lastBufId = _bufferWidget->currentBuffer();
1327     if (lastBufId.isValid()) {
1328         s.setValue("LastUsedBufferId", lastBufId.toInt());
1329         // clear the current selection
1330         Client::bufferModel()->standardSelectionModel()->clearSelection();
1331     }
1332     restoreState(s.value("MainWinState").toByteArray());
1333     setDisconnectedState();
1334 }
1335
1336 void MainWin::setDisconnectedState()
1337 {
1338     ActionCollection* coll = QtUi::actionCollection("General");
1339     // ui.menuCore->setEnabled(false);
1340     coll->action("ConnectCore")->setEnabled(true);
1341     coll->action("DisconnectCore")->setEnabled(false);
1342     coll->action("CoreInfo")->setEnabled(false);
1343     coll->action("ChangePassword")->setEnabled(false);
1344     //_viewMenu->setEnabled(false);
1345     statusBar()->showMessage(tr("Not connected to core."));
1346     if (_msgProcessorStatusWidget)
1347         _msgProcessorStatusWidget->setProgress(0, 0);
1348     updateIcon();
1349     systemTray()->setState(SystemTray::Passive);
1350     _nickListWidget->setVisible(false);
1351 }
1352
1353 void MainWin::userAuthenticationRequired(CoreAccount* account, bool* valid, const QString& errorMessage)
1354 {
1355     Q_UNUSED(errorMessage)
1356     CoreConnectAuthDlg dlg(account);
1357     *valid = (dlg.exec() == QDialog::Accepted);
1358 }
1359
1360 void MainWin::handleNoSslInClient(bool* accepted)
1361 {
1362     QMessageBox box(QMessageBox::Warning,
1363                     tr("Unencrypted Connection"),
1364                     tr("<b>Your client does not support SSL encryption</b>"),
1365                     QMessageBox::Ignore | QMessageBox::Cancel);
1366     box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
1367     box.setDefaultButton(QMessageBox::Ignore);
1368     *accepted = (box.exec() == QMessageBox::Ignore);
1369 }
1370
1371 void MainWin::handleNoSslInCore(bool* accepted)
1372 {
1373     QMessageBox box(QMessageBox::Warning,
1374                     tr("Unencrypted Connection"),
1375                     tr("<b>Your core does not support SSL encryption</b>"),
1376                     QMessageBox::Ignore | QMessageBox::Cancel);
1377     box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
1378     box.setDefaultButton(QMessageBox::Ignore);
1379     *accepted = (box.exec() == QMessageBox::Ignore);
1380 }
1381
1382 #ifdef HAVE_SSL
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 #endif /* HAVE_SSL */
1423
1424 void MainWin::handleCoreConnectionError(const QString& error)
1425 {
1426     QMessageBox::critical(this, tr("Core Connection Error"), error, QMessageBox::Ok);
1427 }
1428
1429 void MainWin::showCoreConnectionDlg()
1430 {
1431     CoreConnectDlg dlg;
1432     if (dlg.exec() == QDialog::Accepted) {
1433         AccountId accId = dlg.selectedAccount();
1434         if (accId.isValid())
1435             Client::coreConnection()->connectToCore(accId);
1436     }
1437 }
1438
1439 void MainWin::showCoreConfigWizard(const QVariantList& backends, const QVariantList& authenticators)
1440 {
1441     auto* wizard = new CoreConfigWizard(Client::coreConnection(), backends, authenticators, this);
1442
1443     wizard->show();
1444 }
1445
1446 void MainWin::showChannelList(NetworkId netId, const QString& channelFilters, bool listImmediately)
1447 {
1448     if (!netId.isValid()) {
1449         auto* action = qobject_cast<QAction*>(sender());
1450         if (action)
1451             netId = action->data().value<NetworkId>();
1452         if (!netId.isValid()) {
1453             // We still haven't found a valid network, probably no network selected, e.g. "/list"
1454             // on the client homescreen when no networks are connected.
1455             QMessageBox box(QMessageBox::Information,
1456                             tr("No network selected"),
1457                             QString("<b>%1</b>").arg(tr("No network selected")),
1458                             QMessageBox::Ok);
1459             box.setInformativeText(tr("Select a network before trying to view the channel list."));
1460             box.exec();
1461             return;
1462         }
1463     }
1464
1465     auto* channelListDlg = new ChannelListDlg(this);
1466     channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
1467     channelListDlg->setNetwork(netId);
1468     if (!channelFilters.isEmpty()) {
1469         channelListDlg->setChannelFilters(channelFilters);
1470     }
1471     if (listImmediately) {
1472         channelListDlg->requestSearch();
1473     }
1474     channelListDlg->show();
1475 }
1476
1477 void MainWin::showNetworkConfig(NetworkId netId)
1478 {
1479     SettingsPageDlg dlg{new NetworksSettingsPage{}};
1480     if (netId.isValid())
1481         qobject_cast<NetworksSettingsPage*>(dlg.currentPage())->bufferList_Open(netId);
1482     dlg.exec();
1483 }
1484
1485 void MainWin::showIgnoreList(QString newRule)
1486 {
1487     SettingsPageDlg dlg{new IgnoreListSettingsPage{}};
1488     // prepare config dialog for new rule
1489     if (!newRule.isEmpty())
1490         qobject_cast<IgnoreListSettingsPage*>(dlg.currentPage())->editIgnoreRule(newRule);
1491     dlg.exec();
1492 }
1493
1494 void MainWin::showCoreInfoDlg()
1495 {
1496     CoreInfoDlg{}.exec();
1497 }
1498
1499 void MainWin::showAwayLog()
1500 {
1501     if (_awayLog)
1502         return;
1503     auto* filter = new AwayLogFilter(Client::messageModel());
1504     _awayLog = new AwayLogView(filter, nullptr);
1505     filter->setParent(_awayLog);
1506     connect(_awayLog, &QObject::destroyed, this, &MainWin::awayLogDestroyed);
1507     _awayLog->setAttribute(Qt::WA_DeleteOnClose);
1508     _awayLog->show();
1509 }
1510
1511 void MainWin::awayLogDestroyed()
1512 {
1513     _awayLog = nullptr;
1514 }
1515
1516 void MainWin::showSettingsDlg()
1517 {
1518     auto dlg = new SettingsDlg();
1519
1520     // Category: Interface
1521     dlg->registerSettingsPage(new AppearanceSettingsPage(dlg));
1522     dlg->registerSettingsPage(new ChatViewSettingsPage(dlg));
1523     dlg->registerSettingsPage(new ChatViewColorSettingsPage(dlg));
1524     dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
1525     dlg->registerSettingsPage(new ItemViewSettingsPage(dlg));
1526     dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
1527     dlg->registerSettingsPage(new InputWidgetSettingsPage(dlg));
1528     dlg->registerSettingsPage(new TopicWidgetSettingsPage(dlg));
1529 #ifdef HAVE_SONNET
1530     dlg->registerSettingsPage(new SonnetSettingsPage(dlg));
1531 #endif
1532     dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
1533     dlg->registerSettingsPage(new CoreHighlightSettingsPage(dlg));
1534     dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
1535     dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
1536
1537     // Category: IRC
1538     dlg->registerSettingsPage(new ConnectionSettingsPage(dlg));
1539     dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
1540     dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
1541     dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
1542     dlg->registerSettingsPage(new IgnoreListSettingsPage(dlg));
1543     // dlg->registerSettingsPage(new DccSettingsPage(dlg)); not ready yet
1544
1545     // Category: Remote Cores
1546     if (Quassel::runMode() != Quassel::Monolithic) {
1547         dlg->registerSettingsPage(new CoreAccountSettingsPage(dlg));
1548         dlg->registerSettingsPage(new CoreConnectionSettingsPage(dlg));
1549     }
1550
1551     dlg->show();
1552 }
1553
1554 void MainWin::showAboutDlg()
1555 {
1556     AboutDlg{}.exec();
1557 }
1558
1559 void MainWin::showShortcutsDlg()
1560 {
1561 #ifdef HAVE_KDE
1562     KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsDisallowed);
1563     foreach (KActionCollection* coll, QtUi::actionCollections()) {
1564         dlg.addCollection(coll, coll->property("Category").toString());
1565     }
1566     dlg.configure(true);
1567 #else
1568     SettingsPageDlg{new ShortcutsSettingsPage{QtUi::actionCollections()}}.exec();
1569 #endif
1570 }
1571
1572 void MainWin::showNewTransferDlg(const QUuid& transferId)
1573 {
1574     auto transfer = Client::transferManager()->transfer(transferId);
1575     if (transfer) {
1576         if (transfer->status() == Transfer::Status::New) {
1577             auto* dlg = new ReceiveFileDlg(transfer, this);
1578             dlg->show();
1579         }
1580     }
1581     else {
1582         qWarning() << "Unknown transfer ID" << transferId;
1583     }
1584 }
1585
1586 void MainWin::onFullScreenToggled()
1587 {
1588     // Relying on QWidget::isFullScreen is discouraged, see the KToggleFullScreenAction docs
1589     // Also, one should not use showFullScreen() or showNormal(), as those reset all other window flags
1590
1591 #ifdef HAVE_KDE
1592     static_cast<KToggleFullScreenAction*>(_fullScreenAction)->setFullScreen(this, _fullScreenAction->isChecked());
1593 #else
1594     if (_fullScreenAction->isChecked())
1595         setWindowState(windowState() | Qt::WindowFullScreen);
1596     else
1597         setWindowState(windowState() & ~Qt::WindowFullScreen);
1598 #endif
1599 }
1600
1601 /********************************************************************************************************/
1602
1603 bool MainWin::event(QEvent* event)
1604 {
1605     switch (event->type()) {
1606     case QEvent::WindowActivate: {
1607         BufferId bufferId = Client::bufferModel()->currentBuffer();
1608         if (bufferId.isValid())
1609             Client::instance()->markBufferAsRead(bufferId);
1610         break;
1611     }
1612     case QEvent::WindowDeactivate:
1613         if (bufferWidget()->autoMarkerLineOnLostFocus())
1614             bufferWidget()->setMarkerLine();
1615         break;
1616     default:
1617         break;
1618     }
1619     return QMainWindow::event(event);
1620 }
1621
1622 void MainWin::moveEvent(QMoveEvent* event)
1623 {
1624     if (!(windowState() & Qt::WindowMaximized))
1625         _normalPos = event->pos();
1626
1627     QMainWindow::moveEvent(event);
1628 }
1629
1630 void MainWin::resizeEvent(QResizeEvent* event)
1631 {
1632     if (!(windowState() & Qt::WindowMaximized))
1633         _normalSize = event->size();
1634
1635     QMainWindow::resizeEvent(event);
1636 }
1637
1638 void MainWin::closeEvent(QCloseEvent* event)
1639 {
1640     QtUiSettings s;
1641     auto* app = qobject_cast<QtUiApplication*> qApp;
1642     Q_ASSERT(app);
1643     // On OSX it can happen that the closeEvent occurs twice. (Especially if packaged with Frameworks)
1644     // This messes up MainWinState/MainWinHidden save/restore.
1645     // It's a bug in Qt: https://bugreports.qt.io/browse/QTBUG-43344
1646     if (!_aboutToQuit && !app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
1647         QtUi::hideMainWidget();
1648         event->ignore();
1649     }
1650     else if (!_aboutToQuit) {
1651         _aboutToQuit = true;
1652         event->accept();
1653         Quassel::instance()->quit();
1654     }
1655     else {
1656         event->ignore();
1657     }
1658 }
1659
1660 void MainWin::messagesInserted(const QModelIndex& parent, int start, int end)
1661 {
1662     Q_UNUSED(parent);
1663
1664     bool hasFocus = QApplication::activeWindow() != nullptr;
1665
1666     for (int i = start; i <= end; i++) {
1667         QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
1668         if (!idx.isValid()) {
1669             qDebug() << "MainWin::messagesInserted(): Invalid model index!";
1670             continue;
1671         }
1672         Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
1673         if (flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
1674             continue;
1675
1676         BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
1677         BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
1678
1679         // check if bufferId belongs to the shown chatlists
1680         if (!(Client::bufferViewOverlay()->bufferIds().contains(bufId) || Client::bufferViewOverlay()->tempRemovedBufferIds().contains(bufId)))
1681             continue;
1682
1683         // check if it's the buffer currently displayed
1684         if (hasFocus && bufId == Client::bufferModel()->currentBuffer())
1685             continue;
1686
1687         // only show notifications for higlights or queries
1688         if (bufType != BufferInfo::QueryBuffer && !(flags & Message::Highlight))
1689             continue;
1690
1691         // and of course: don't notify for ignored messages
1692         if (Client::ignoreListManager()
1693             && Client::ignoreListManager()->match(idx.data(MessageModel::MessageRole).value<Message>(),
1694                                                   Client::networkModel()->networkName(bufId)))
1695             continue;
1696
1697         // seems like we have a legit notification candidate!
1698         QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
1699         QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
1700         QString contents = idx.data(ChatLineModel::DisplayRole).toString();
1701         AbstractNotificationBackend::NotificationType type;
1702
1703         if (bufType == BufferInfo::QueryBuffer && !hasFocus)
1704             type = AbstractNotificationBackend::PrivMsg;
1705         else if (bufType == BufferInfo::QueryBuffer && hasFocus)
1706             type = AbstractNotificationBackend::PrivMsgFocused;
1707         else if (flags & Message::Highlight && !hasFocus)
1708             type = AbstractNotificationBackend::Highlight;
1709         else
1710             type = AbstractNotificationBackend::HighlightFocused;
1711
1712         QtUi::instance()->invokeNotification(bufId, type, sender, contents);
1713     }
1714 }
1715
1716 void MainWin::currentBufferChanged(BufferId buffer)
1717 {
1718     if (buffer.isValid())
1719         Client::instance()->markBufferAsRead(buffer);
1720 }
1721
1722 void MainWin::clientNetworkCreated(NetworkId id)
1723 {
1724     const Network* net = Client::network(id);
1725     auto* act = new QAction(net->networkName(), this);
1726     act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
1727     act->setData(QVariant::fromValue<NetworkId>(id));
1728     connect(net, &SyncableObject::updatedRemotely, this, &MainWin::clientNetworkUpdated);
1729     connect(act, &QAction::triggered, this, &MainWin::connectOrDisconnectFromNet);
1730
1731     QAction* beforeAction = nullptr;
1732     foreach (QAction* action, _networksMenu->actions()) {
1733         if (!action->data().isValid())  // ignore stock actions
1734             continue;
1735         if (net->networkName().localeAwareCompare(action->text()) < 0) {
1736             beforeAction = action;
1737             break;
1738         }
1739     }
1740     _networksMenu->insertAction(beforeAction, act);
1741 }
1742
1743 void MainWin::clientNetworkUpdated()
1744 {
1745     const auto* net = qobject_cast<const Network*>(sender());
1746     if (!net)
1747         return;
1748
1749     auto* action = findChild<QAction*>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1750     if (!action)
1751         return;
1752
1753     action->setText(net->networkName());
1754
1755     switch (net->connectionState()) {
1756     case Network::Initialized:
1757         action->setIcon(icon::get("network-connect"));
1758         // if we have no currently selected buffer, jump to the first connecting statusbuffer
1759         if (!bufferWidget()->currentBuffer().isValid()) {
1760             QModelIndex idx = Client::networkModel()->networkIndex(net->networkId());
1761             if (idx.isValid()) {
1762                 BufferId statusBufferId = idx.data(NetworkModel::BufferIdRole).value<BufferId>();
1763                 Client::bufferModel()->switchToBuffer(statusBufferId);
1764             }
1765         }
1766         break;
1767     case Network::Disconnected:
1768         action->setIcon(icon::get("network-disconnect"));
1769         break;
1770     default:
1771         action->setIcon(icon::get("network-wired"));
1772     }
1773 }
1774
1775 void MainWin::clientNetworkRemoved(NetworkId id)
1776 {
1777     auto* action = findChild<QAction*>(QString("NetworkAction-%1").arg(id.toInt()));
1778     if (!action)
1779         return;
1780
1781     action->deleteLater();
1782 }
1783
1784 void MainWin::connectOrDisconnectFromNet()
1785 {
1786     auto* act = qobject_cast<QAction*>(sender());
1787     if (!act)
1788         return;
1789     const Network* net = Client::network(act->data().value<NetworkId>());
1790     if (!net)
1791         return;
1792     if (net->connectionState() == Network::Disconnected)
1793         net->requestConnect();
1794     else
1795         net->requestDisconnect();
1796 }
1797
1798 void MainWin::onFormatApplyColorTriggered()
1799 {
1800     if (!_inputWidget)
1801         return;
1802
1803     _inputWidget->applyFormatActiveColor();
1804 }
1805
1806 void MainWin::onFormatApplyColorFillTriggered()
1807 {
1808     if (!_inputWidget)
1809         return;
1810
1811     _inputWidget->applyFormatActiveColorFill();
1812 }
1813
1814 void MainWin::onFormatClearTriggered()
1815 {
1816     if (!_inputWidget)
1817         return;
1818
1819     _inputWidget->clearFormat();
1820 }
1821
1822 void MainWin::onFormatBoldTriggered()
1823 {
1824     if (!_inputWidget)
1825         return;
1826
1827     _inputWidget->toggleFormatBold();
1828 }
1829
1830 void MainWin::onFormatItalicTriggered()
1831 {
1832     if (!_inputWidget)
1833         return;
1834
1835     _inputWidget->toggleFormatItalic();
1836 }
1837
1838 void MainWin::onFormatUnderlineTriggered()
1839 {
1840     if (!_inputWidget)
1841         return;
1842
1843     _inputWidget->toggleFormatUnderline();
1844 }
1845
1846 void MainWin::onJumpHotBufferTriggered()
1847 {
1848     if (!_bufferHotList->rowCount())
1849         return;
1850
1851     Client::bufferModel()->switchToBuffer(_bufferHotList->hottestBuffer());
1852 }
1853
1854 void MainWin::onBufferSearchTriggered()
1855 {
1856     if (_activeBufferViewIndex < 0 || _activeBufferViewIndex >= _bufferViews.count()) {
1857         qWarning() << "Tried to activate filter on invalid bufferview:" << _activeBufferViewIndex;
1858         return;
1859     }
1860
1861     _bufferViews[_activeBufferViewIndex]->activateFilter();
1862 }
1863
1864 void MainWin::onJumpKey()
1865 {
1866     auto* action = qobject_cast<QAction*>(sender());
1867     if (!action || !Client::bufferModel())
1868         return;
1869     int idx = action->property("Index").toInt();
1870
1871     if (_jumpKeyMap.isEmpty())
1872         _jumpKeyMap = CoreAccountSettings().jumpKeyMap();
1873
1874     if (!_jumpKeyMap.contains(idx))
1875         return;
1876
1877     BufferId buffer = _jumpKeyMap.value(idx);
1878     if (buffer.isValid())
1879         Client::bufferModel()->switchToBuffer(buffer);
1880 }
1881
1882 void MainWin::bindJumpKey()
1883 {
1884     auto* action = qobject_cast<QAction*>(sender());
1885     if (!action || !Client::bufferModel())
1886         return;
1887     int idx = action->property("Index").toInt();
1888
1889     _jumpKeyMap[idx] = Client::bufferModel()->currentBuffer();
1890     CoreAccountSettings().setJumpKeyMap(_jumpKeyMap);
1891 }
1892
1893 void MainWin::onDebugNetworkModelTriggered()
1894 {
1895     auto* view = new QTreeView;
1896     view->setAttribute(Qt::WA_DeleteOnClose);
1897     view->setWindowTitle("Debug NetworkModel View");
1898     view->setModel(Client::networkModel());
1899     view->setColumnWidth(0, 250);
1900     view->setColumnWidth(1, 250);
1901     view->setColumnWidth(2, 80);
1902     view->resize(610, 300);
1903     view->show();
1904 }
1905
1906 void MainWin::onDebugHotListTriggered()
1907 {
1908     _bufferHotList->invalidate();
1909     _bufferHotList->sort(0, Qt::DescendingOrder);
1910
1911     auto* view = new QTreeView;
1912     view->setAttribute(Qt::WA_DeleteOnClose);
1913     view->setModel(_bufferHotList);
1914     view->show();
1915 }
1916
1917 void MainWin::onDebugBufferViewOverlayTriggered()
1918 {
1919     auto* overlay = new DebugBufferViewOverlay(nullptr);
1920     overlay->setAttribute(Qt::WA_DeleteOnClose);
1921     overlay->show();
1922 }
1923
1924 void MainWin::onDebugMessageModelTriggered()
1925 {
1926     auto* view = new QTableView(nullptr);
1927     auto* filter = new DebugMessageModelFilter(view);
1928     filter->setSourceModel(Client::messageModel());
1929     view->setModel(filter);
1930     view->setAttribute(Qt::WA_DeleteOnClose, true);
1931     view->verticalHeader()->hide();
1932     view->horizontalHeader()->setStretchLastSection(true);
1933     view->show();
1934 }
1935
1936 void MainWin::onDebugLogTriggered()
1937 {
1938     auto dlg = new DebugLogDlg(this);  // will be deleted on close
1939     dlg->show();
1940 }
1941
1942 void MainWin::onShowResourceTreeTriggered()
1943 {
1944     auto dlg = new ResourceTreeDlg(this);  // will be deleted on close
1945     dlg->show();
1946 }
1947
1948 void MainWin::showStatusBarMessage(const QString& message)
1949 {
1950     statusBar()->showMessage(message, 10000);
1951 }