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