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