Integrate DesktopNotification into system tray/status notifier
[quassel.git] / src / qtui / mainwin.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "mainwin.h"
21
22 #ifdef HAVE_KDE
23 #  include <KAction>
24 #  include <KActionCollection>
25 #  include <KHelpMenu>
26 #  include <KMenuBar>
27 #  include <KShortcutsDialog>
28 #  include <KStatusBar>
29 #  include <KToolBar>
30 #  include <KWindowSystem>
31 #endif
32
33 #ifdef Q_WS_X11
34 #  include <QX11Info>
35 #endif
36
37 #include "aboutdlg.h"
38 #include "awaylogfilter.h"
39 #include "awaylogview.h"
40 #include "action.h"
41 #include "actioncollection.h"
42 #include "bufferhotlistfilter.h"
43 #include "buffermodel.h"
44 #include "bufferview.h"
45 #include "bufferviewoverlay.h"
46 #include "bufferviewoverlayfilter.h"
47 #include "bufferwidget.h"
48 #include "channellistdlg.h"
49 #include "chatlinemodel.h"
50 #include "chatmonitorfilter.h"
51 #include "chatmonitorview.h"
52 #include "chatview.h"
53 #include "client.h"
54 #include "clientbacklogmanager.h"
55 #include "clientbufferviewconfig.h"
56 #include "clientbufferviewmanager.h"
57 #include "clientignorelistmanager.h"
58 #include "coreconfigwizard.h"
59 #include "coreconnectdlg.h"
60 #include "coreconnection.h"
61 #include "coreconnectionstatuswidget.h"
62 #include "coreinfodlg.h"
63 #include "contextmenuactionprovider.h"
64 #include "debugbufferviewoverlay.h"
65 #include "debuglogwidget.h"
66 #include "debugmessagemodelfilter.h"
67 #include "flatproxymodel.h"
68 #include "iconloader.h"
69 #include "inputwidget.h"
70 #include "irclistmodel.h"
71 #include "ircconnectionwizard.h"
72 #include "jumpkeyhandler.h"
73 #include "legacysystemtray.h"
74 #include "msgprocessorstatuswidget.h"
75 #include "nicklistwidget.h"
76 #include "qtuiapplication.h"
77 #include "qtuimessageprocessor.h"
78 #include "qtuisettings.h"
79 #include "qtuistyle.h"
80 #include "settingsdlg.h"
81 #include "settingspagedlg.h"
82 #include "statusnotifieritem.h"
83 #include "toolbaractionprovider.h"
84 #include "topicwidget.h"
85 #include "verticaldock.h"
86
87 #ifndef HAVE_KDE
88 #  ifdef HAVE_PHONON
89 #    include "phononnotificationbackend.h"
90 #  endif
91 #  include "systraynotificationbackend.h"
92 #  include "taskbarnotificationbackend.h"
93 #else /* HAVE_KDE */
94 #  include "knotificationbackend.h"
95 #endif /* HAVE_KDE */
96
97 #ifdef HAVE_SSL
98 #  include "sslinfodlg.h"
99 #endif
100
101 #ifdef HAVE_INDICATEQT
102   #include "indicatornotificationbackend.h"
103 #endif
104
105 #include "settingspages/aliasessettingspage.h"
106 #include "settingspages/appearancesettingspage.h"
107 #include "settingspages/backlogsettingspage.h"
108 #include "settingspages/bufferviewsettingspage.h"
109 #include "settingspages/chatmonitorsettingspage.h"
110 #include "settingspages/chatviewsettingspage.h"
111 #include "settingspages/connectionsettingspage.h"
112 #include "settingspages/coreaccountsettingspage.h"
113 #include "settingspages/coreconnectionsettingspage.h"
114 #include "settingspages/highlightsettingspage.h"
115 #include "settingspages/identitiessettingspage.h"
116 #include "settingspages/ignorelistsettingspage.h"
117 #include "settingspages/inputwidgetsettingspage.h"
118 #include "settingspages/itemviewsettingspage.h"
119 #include "settingspages/networkssettingspage.h"
120 #include "settingspages/notificationssettingspage.h"
121 #include "settingspages/topicwidgetsettingspage.h"
122
123 MainWin::MainWin(QWidget *parent)
124 #ifdef HAVE_KDE
125   : KMainWindow(parent),
126   _kHelpMenu(new KHelpMenu(this, KGlobal::mainComponent().aboutData())),
127 #else
128   : QMainWindow(parent),
129 #endif
130     _msgProcessorStatusWidget(new MsgProcessorStatusWidget(this)),
131     _coreConnectionStatusWidget(new CoreConnectionStatusWidget(Client::coreConnection(), this)),
132     _titleSetter(this),
133     _awayLog(0),
134     _layoutLoaded(false)
135 {
136   setAttribute(Qt::WA_DeleteOnClose, false);  // we delete the mainwin manually
137
138   QtUiSettings uiSettings;
139   QString style = uiSettings.value("Style", QString()).toString();
140   if(!style.isEmpty()) {
141     QApplication::setStyle(style);
142   }
143
144   QApplication::setQuitOnLastWindowClosed(false);
145
146   setWindowTitle("Quassel IRC");
147   setWindowIconText("Quassel IRC");
148   updateIcon();
149
150   installEventFilter(new JumpKeyHandler(this));
151 }
152
153 void MainWin::init() {
154   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
155   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
156   connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
157            SLOT(messagesInserted(const QModelIndex &, int, int)));
158   connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
159   connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showIgnoreList(QString)), SLOT(showIgnoreList(QString)));
160
161   connect(Client::coreConnection(), SIGNAL(startCoreSetup(QVariantList)), SLOT(showCoreConfigWizard(QVariantList)));
162   connect(Client::coreConnection(), SIGNAL(connectionErrorPopup(QString)), SLOT(handleCoreConnectionError(QString)));
163   connect(Client::coreConnection(), SIGNAL(userAuthenticationRequired(CoreAccount *, bool *, QString)), SLOT(userAuthenticationRequired(CoreAccount *, bool *, QString)));
164   connect(Client::coreConnection(), SIGNAL(handleNoSslInClient(bool*)), SLOT(handleNoSslInClient(bool *)));
165   connect(Client::coreConnection(), SIGNAL(handleNoSslInCore(bool*)), SLOT(handleNoSslInCore(bool *)));
166 #ifdef HAVE_SSL
167   connect(Client::coreConnection(), SIGNAL(handleSslErrors(const QSslSocket *, bool *, bool *)), SLOT(handleSslErrors(const QSslSocket *, bool *, bool *)));
168 #endif
169
170   // Setup Dock Areas
171   setDockNestingEnabled(true);
172   setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
173   setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
174   setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
175   setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
176
177   // Order is sometimes important
178   setupActions();
179   setupBufferWidget();
180   setupMenus();
181   setupTopicWidget();
182   setupNickWidget();
183   setupInputWidget();
184   setupChatMonitor();
185   setupStatusBar();
186   setupToolBars();
187   setupSystray();
188   setupTitleSetter();
189   setupHotList();
190
191 #ifndef HAVE_KDE
192   QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
193 #  ifndef QT_NO_SYSTEMTRAYICON
194   QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
195 #  endif
196 #  ifdef HAVE_PHONON
197   QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
198 #  endif
199
200 #else /* HAVE_KDE */
201   QtUi::registerNotificationBackend(new KNotificationBackend(this));
202 #endif /* HAVE_KDE */
203
204 #ifdef HAVE_INDICATEQT
205   QtUi::registerNotificationBackend(new IndicatorNotificationBackend(this));
206 #endif
207
208   connect(bufferWidget(), SIGNAL(currentChanged(BufferId)), SLOT(currentBufferChanged(BufferId)));
209
210   setDisconnectedState();  // Disable menus and stuff
211
212 #ifdef HAVE_KDE
213   setAutoSaveSettings();
214 #endif
215
216   // restore mainwin state
217   QtUiSettings s;
218   restoreStateFromSettings(s);
219
220   // restore locked state of docks
221   QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
222
223   CoreConnection *conn = Client::coreConnection();
224   if(!conn->connectToCore()) {
225     // No autoconnect selected (or no accounts)
226     showCoreConnectionDlg();
227   }
228 }
229
230 MainWin::~MainWin() {
231
232 }
233
234 void MainWin::quit() {
235   QtUiSettings s;
236   saveStateToSettings(s);
237   saveLayout();
238   QApplication::quit();
239 }
240
241 void MainWin::saveStateToSettings(UiSettings &s) {
242   s.setValue("MainWinSize", _normalSize);
243   s.setValue("MainWinPos", _normalPos);
244   s.setValue("MainWinState", saveState());
245   s.setValue("MainWinGeometry", saveGeometry());
246   s.setValue("MainWinMinimized", isMinimized());
247   s.setValue("MainWinMaximized", isMaximized());
248   s.setValue("MainWinHidden", !isVisible());
249
250 #ifdef HAVE_KDE
251   saveAutoSaveSettings();
252 #endif
253 }
254
255 void MainWin::restoreStateFromSettings(UiSettings &s) {
256   _normalSize = s.value("MainWinSize", size()).toSize();
257   _normalPos = s.value("MainWinPos", pos()).toPoint();
258   bool maximized = s.value("MainWinMaximized", false).toBool();
259
260 #ifndef HAVE_KDE
261   restoreGeometry(s.value("MainWinGeometry").toByteArray());
262
263   if(maximized) {
264     // restoreGeometry() fails if the windows was maximized, so we resize and position explicitly
265     resize(_normalSize);
266     move(_normalPos);
267   }
268
269   restoreState(s.value("MainWinState").toByteArray());
270
271 #else
272   move(_normalPos);
273 #endif
274
275   if(s.value("MainWinHidden").toBool() && QtUi::haveSystemTray())
276     QtUi::hideMainWidget();
277   else if(s.value("MainWinMinimized").toBool())
278     showMinimized();
279   else if(maximized)
280     showMaximized();
281   else
282     show();
283 }
284
285 void MainWin::updateIcon() {
286 #ifdef Q_WS_MAC
287   const int size = 128;
288 #else
289   const int size = 48;
290 #endif
291
292   QPixmap icon;
293   if(Client::isConnected())
294     icon = DesktopIcon("quassel", size);
295   else
296     icon = DesktopIcon("quassel_inactive", size);
297   setWindowIcon(icon);
298   qApp->setWindowIcon(icon);
299 }
300
301 void MainWin::setupActions() {
302   ActionCollection *coll = QtUi::actionCollection("General");
303   // File
304   coll->addAction("ConnectCore", new Action(SmallIcon("network-connect"), tr("&Connect to Core..."), coll,
305                                              this, SLOT(showCoreConnectionDlg())));
306   coll->addAction("DisconnectCore", new Action(SmallIcon("network-disconnect"), tr("&Disconnect from Core"), coll,
307                                                 Client::instance(), SLOT(disconnectFromCore())));
308   coll->addAction("CoreInfo", new Action(SmallIcon("help-about"), tr("Core &Info..."), coll,
309                                           this, SLOT(showCoreInfoDlg())));
310   coll->addAction("ConfigureNetworks", new Action(SmallIcon("configure"), tr("Configure &Networks..."), coll,
311                                               this, SLOT(on_actionConfigureNetworks_triggered())));
312   coll->addAction("Quit", new Action(SmallIcon("application-exit"), tr("&Quit"), coll,
313                                       this, SLOT(quit()), tr("Ctrl+Q")));
314
315   // View
316   coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Chat Lists..."), coll,
317                                              this, SLOT(on_actionConfigureViews_triggered())));
318
319   QAction *lockAct = coll->addAction("LockLayout", new Action(tr("&Lock Layout"), coll));
320   lockAct->setCheckable(true);
321   connect(lockAct, SIGNAL(toggled(bool)), SLOT(on_actionLockLayout_toggled(bool)));
322
323   coll->addAction("ToggleSearchBar", new Action(SmallIcon("edit-find"), tr("Show &Search Bar"), coll,
324                                                 0, 0, QKeySequence::Find))->setCheckable(true);
325   coll->addAction("ShowAwayLog", new Action(tr("Show Away Log"), coll,
326                                             this, SLOT(showAwayLog())));
327   coll->addAction("ToggleMenuBar", new Action(SmallIcon("show-menu"), tr("Show &Menubar"), coll,
328                                                 0, 0, tr("Ctrl+M")))->setCheckable(true);
329
330   coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
331                                                 0, 0))->setCheckable(true);
332
333   // Settings
334   coll->addAction("ConfigureQuassel", new Action(SmallIcon("configure"), tr("&Configure Quassel..."), coll,
335                                                   this, SLOT(showSettingsDlg()), tr("F7")));
336
337   // Help
338   coll->addAction("AboutQuassel", new Action(SmallIcon("quassel"), tr("&About Quassel"), coll,
339                                               this, SLOT(showAboutDlg())));
340   coll->addAction("AboutQt", new Action(QIcon(":/pics/qt-logo.png"), tr("About &Qt"), coll,
341                                          qApp, SLOT(aboutQt())));
342   coll->addAction("DebugNetworkModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &NetworkModel"), coll,
343                                        this, SLOT(on_actionDebugNetworkModel_triggered())));
344   coll->addAction("DebugBufferViewOverlay", new Action(SmallIcon("tools-report-bug"), tr("Debug &BufferViewOverlay"), coll,
345                                        this, SLOT(on_actionDebugBufferViewOverlay_triggered())));
346   coll->addAction("DebugMessageModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &MessageModel"), coll,
347                                        this, SLOT(on_actionDebugMessageModel_triggered())));
348   coll->addAction("DebugHotList", new Action(SmallIcon("tools-report-bug"), tr("Debug &HotList"), coll,
349                                        this, SLOT(on_actionDebugHotList_triggered())));
350   coll->addAction("DebugLog", new Action(SmallIcon("tools-report-bug"), tr("Debug &Log"), coll,
351                                        this, SLOT(on_actionDebugLog_triggered())));
352   coll->addAction("ReloadStyle", new Action(SmallIcon("view-refresh"), tr("Reload Stylesheet"), coll,
353                                        QtUi::style(), SLOT(reload()), QKeySequence::Refresh));
354
355   // Navigation
356   coll->addAction("JumpHotBuffer", new Action(tr("Jump to hot chat"), coll,
357                                               this, SLOT(on_jumpHotBuffer_triggered()), QKeySequence(Qt::META + Qt::Key_A)));
358 }
359
360 void MainWin::setupMenus() {
361   ActionCollection *coll = QtUi::actionCollection("General");
362
363   _fileMenu = menuBar()->addMenu(tr("&File"));
364
365   static const QStringList coreActions = QStringList()
366     << "ConnectCore" << "DisconnectCore" << "CoreInfo";
367
368   QAction *coreAction;
369   foreach(QString actionName, coreActions) {
370     coreAction = coll->action(actionName);
371     _fileMenu->addAction(coreAction);
372     flagRemoteCoreOnly(coreAction);
373   }
374   flagRemoteCoreOnly(_fileMenu->addSeparator());
375
376   _networksMenu = _fileMenu->addMenu(tr("&Networks"));
377   _networksMenu->addAction(coll->action("ConfigureNetworks"));
378   _networksMenu->addSeparator();
379   _fileMenu->addSeparator();
380   _fileMenu->addAction(coll->action("Quit"));
381
382   _viewMenu = menuBar()->addMenu(tr("&View"));
383   _bufferViewsMenu = _viewMenu->addMenu(tr("&Chat Lists"));
384   _bufferViewsMenu->addAction(coll->action("ConfigureBufferViews"));
385   _toolbarMenu = _viewMenu->addMenu(tr("&Toolbars"));
386   _viewMenu->addSeparator();
387
388   _viewMenu->addAction(coll->action("ToggleMenuBar"));
389   _viewMenu->addAction(coll->action("ToggleStatusBar"));
390   _viewMenu->addAction(coll->action("ToggleSearchBar"));
391
392   coreAction = coll->action("ShowAwayLog");
393   flagRemoteCoreOnly(coreAction);
394   _viewMenu->addAction(coreAction);
395
396   _viewMenu->addSeparator();
397   _viewMenu->addAction(coll->action("LockLayout"));
398
399   _settingsMenu = menuBar()->addMenu(tr("&Settings"));
400 #ifdef HAVE_KDE
401   _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
402   _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
403 #endif
404   _settingsMenu->addAction(coll->action("ConfigureQuassel"));
405
406   _helpMenu = menuBar()->addMenu(tr("&Help"));
407   _helpMenu->addAction(coll->action("AboutQuassel"));
408 #ifndef HAVE_KDE
409   _helpMenu->addAction(coll->action("AboutQt"));
410 #else
411   _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
412 #endif
413   _helpMenu->addSeparator();
414   _helpDebugMenu = _helpMenu->addMenu(SmallIcon("tools-report-bug"), tr("Debug"));
415   _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
416   _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
417   _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
418   _helpDebugMenu->addAction(coll->action("DebugHotList"));
419   _helpDebugMenu->addAction(coll->action("DebugLog"));
420   _helpDebugMenu->addSeparator();
421   _helpDebugMenu->addAction(coll->action("ReloadStyle"));
422
423   // Toggle visibility
424   QAction *showMenuBar = QtUi::actionCollection("General")->action("ToggleMenuBar");
425
426   QtUiSettings uiSettings;
427   bool enabled = uiSettings.value("ShowMenuBar", QVariant(true)).toBool();
428   showMenuBar->setChecked(enabled);
429   enabled ? menuBar()->show() : menuBar()->hide();
430
431   connect(showMenuBar, SIGNAL(toggled(bool)), menuBar(), SLOT(setVisible(bool)));
432   connect(showMenuBar, SIGNAL(toggled(bool)), this, SLOT(saveMenuBarStatus(bool)));
433 }
434
435 void MainWin::setupBufferWidget() {
436   _bufferWidget = new BufferWidget(this);
437   _bufferWidget->setModel(Client::bufferModel());
438   _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
439   setCentralWidget(_bufferWidget);
440 }
441
442 void MainWin::addBufferView(int bufferViewConfigId) {
443   addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
444 }
445
446 void MainWin::addBufferView(ClientBufferViewConfig *config) {
447   if(!config)
448     return;
449
450   config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
451   BufferViewDock *dock = new BufferViewDock(config, this);
452
453   //create the view and initialize it's filter
454   BufferView *view = new BufferView(dock);
455   view->setFilteredModel(Client::bufferModel(), config);
456   view->installEventFilter(_inputWidget); // for key presses
457
458   Client::bufferModel()->synchronizeView(view);
459
460   dock->setWidget(view);
461   dock->setVisible(_layoutLoaded); // don't show before state has been restored
462
463   addDockWidget(Qt::LeftDockWidgetArea, dock);
464   _bufferViewsMenu->addAction(dock->toggleViewAction());
465
466   connect(dock->toggleViewAction(), SIGNAL(toggled(bool)), this, SLOT(bufferViewToggled(bool)));
467   _bufferViews.append(dock);
468 }
469
470 void MainWin::removeBufferView(int bufferViewConfigId) {
471   QVariant actionData;
472   BufferViewDock *dock;
473   foreach(QAction *action, _bufferViewsMenu->actions()) {
474     actionData = action->data();
475     if(!actionData.isValid())
476       continue;
477
478     dock = qobject_cast<BufferViewDock *>(action->parent());
479     if(dock && actionData.toInt() == bufferViewConfigId) {
480       removeAction(action);
481       _bufferViews.removeAll(dock);
482       Client::bufferViewOverlay()->removeView(dock->bufferViewId());
483       dock->deleteLater();
484     }
485   }
486 }
487
488 void MainWin::bufferViewToggled(bool enabled) {
489   QAction *action = qobject_cast<QAction *>(sender());
490   Q_ASSERT(action);
491   BufferViewDock *dock = qobject_cast<BufferViewDock *>(action->parent());
492   Q_ASSERT(dock);
493
494   // Make sure we don't toggle backlog fetch for a view we've already removed
495   if(!_bufferViews.contains(dock))
496     return;
497
498   if(enabled) {
499     Client::bufferViewOverlay()->addView(dock->bufferViewId());
500     BufferViewConfig *config = dock->config();
501     if(config && config->isInitialized()) {
502       BufferIdList buffers;
503       if(config->networkId().isValid()) {
504         foreach(BufferId bufferId, config->bufferList()) {
505           if(Client::networkModel()->networkId(bufferId) == config->networkId())
506             buffers << bufferId;
507         }
508         foreach(BufferId bufferId, config->temporarilyRemovedBuffers().toList()) {
509           if(Client::networkModel()->networkId(bufferId) == config->networkId())
510             buffers << bufferId;
511         }
512       } else {
513         buffers = BufferIdList::fromSet(config->bufferList().toSet() + config->temporarilyRemovedBuffers());
514       }
515       Client::backlogManager()->checkForBacklog(buffers);
516     }
517   } else {
518     Client::bufferViewOverlay()->removeView(dock->bufferViewId());
519   }
520 }
521
522 BufferView *MainWin::allBuffersView() const {
523   // "All Buffers" is always the first dock created
524   if(_bufferViews.count() > 0)
525     return _bufferViews[0]->bufferView();
526   return 0;
527 }
528
529 void MainWin::showNotificationsDlg() {
530   SettingsPageDlg dlg(new NotificationsSettingsPage(this), this);
531   dlg.exec();
532 }
533
534 void MainWin::on_actionConfigureNetworks_triggered() {
535   SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
536   dlg.exec();
537 }
538
539 void MainWin::on_actionConfigureViews_triggered() {
540   SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
541   dlg.exec();
542 }
543
544 void MainWin::on_actionLockLayout_toggled(bool lock) {
545   QList<VerticalDock *> docks = findChildren<VerticalDock *>();
546   foreach(VerticalDock *dock, docks) {
547     dock->showTitle(!lock);
548   }
549   if(Client::bufferViewManager()) {
550     foreach(ClientBufferViewConfig *config, Client::bufferViewManager()->clientBufferViewConfigs()) {
551       config->setLocked(lock);
552     }
553   }
554   QtUiSettings().setValue("LockLayout", lock);
555 }
556
557 void MainWin::setupNickWidget() {
558   // create nick dock
559   NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
560   nickDock->setObjectName("NickDock");
561   nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
562
563   _nickListWidget = new NickListWidget(nickDock);
564   nickDock->setWidget(_nickListWidget);
565
566   addDockWidget(Qt::RightDockWidgetArea, nickDock);
567   _viewMenu->addAction(nickDock->toggleViewAction());
568   nickDock->toggleViewAction()->setText(tr("Show Nick List"));
569
570   // See NickListDock::NickListDock();
571   // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
572
573   // attach the NickListWidget to the BufferModel and the default selection
574   _nickListWidget->setModel(Client::bufferModel());
575   _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
576 }
577
578 void MainWin::setupChatMonitor() {
579   VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
580   dock->setObjectName("ChatMonitorDock");
581
582   ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
583   _chatMonitorView = new ChatMonitorView(filter, this);
584   _chatMonitorView->setFocusProxy(_inputWidget);
585   _chatMonitorView->show();
586   dock->setWidget(_chatMonitorView);
587   dock->hide();
588
589   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
590   _viewMenu->addAction(dock->toggleViewAction());
591   dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
592 }
593
594 void MainWin::setupInputWidget() {
595   VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
596   dock->setObjectName("InputDock");
597
598   _inputWidget = new InputWidget(dock);
599   dock->setWidget(_inputWidget);
600
601   addDockWidget(Qt::BottomDockWidgetArea, dock);
602
603   _viewMenu->addAction(dock->toggleViewAction());
604   dock->toggleViewAction()->setText(tr("Show Input Line"));
605
606   _inputWidget->setModel(Client::bufferModel());
607   _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
608
609   _bufferWidget->setFocusProxy(_inputWidget);
610
611   _inputWidget->inputLine()->installEventFilter(_bufferWidget);
612
613   connect(_topicWidget, SIGNAL(switchedPlain()), _bufferWidget, SLOT(setFocus()));
614 }
615
616 void MainWin::setupTopicWidget() {
617   VerticalDock *dock = new VerticalDock(tr("Topic"), this);
618   dock->setObjectName("TopicDock");
619   _topicWidget = new TopicWidget(dock);
620
621   dock->setWidget(_topicWidget);
622
623   _topicWidget->setModel(Client::bufferModel());
624   _topicWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
625
626   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
627
628   _viewMenu->addAction(dock->toggleViewAction());
629   dock->toggleViewAction()->setText(tr("Show Topic Line"));
630 }
631
632 void MainWin::setupTitleSetter() {
633   _titleSetter.setModel(Client::bufferModel());
634   _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
635 }
636
637 void MainWin::setupStatusBar() {
638   // MessageProcessor progress
639   statusBar()->addPermanentWidget(_msgProcessorStatusWidget);
640
641   // Connection state
642   _coreConnectionStatusWidget->update();
643   statusBar()->addPermanentWidget(_coreConnectionStatusWidget);
644
645   QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
646
647   QtUiSettings uiSettings;
648
649   bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
650   showStatusbar->setChecked(enabled);
651   enabled ? statusBar()->show() : statusBar()->hide();
652
653   connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
654   connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
655
656   connect(Client::coreConnection(), SIGNAL(connectionMsg(QString)), statusBar(), SLOT(showMessage(QString)));
657 }
658
659 void MainWin::setupHotList() {
660   FlatProxyModel *flatProxy = new FlatProxyModel(this);
661   flatProxy->setSourceModel(Client::bufferModel());
662   _bufferHotList = new BufferHotListFilter(flatProxy);
663 }
664
665 void MainWin::saveMenuBarStatus(bool enabled) {
666   QtUiSettings uiSettings;
667   uiSettings.setValue("ShowMenuBar", enabled);
668 }
669
670 void MainWin::saveStatusBarStatus(bool enabled) {
671   QtUiSettings uiSettings;
672   uiSettings.setValue("ShowStatusBar", enabled);
673 }
674
675 void MainWin::setupSystray() {
676 #ifdef HAVE_DBUS
677   _systemTray = new StatusNotifierItem(this);
678 #elif !defined QT_NO_SYSTEMTRAYICON
679   _systemTray = new LegacySystemTray(this);
680 #else
681   _systemTray = new SystemTray(this); // dummy
682 #endif
683   _systemTray->init();
684 }
685
686 void MainWin::setupToolBars() {
687   connect(_bufferWidget, SIGNAL(currentChanged(QModelIndex)),
688           QtUi::toolBarActionProvider(), SLOT(currentBufferChanged(QModelIndex)));
689   connect(_nickListWidget, SIGNAL(nickSelectionChanged(QModelIndexList)),
690           QtUi::toolBarActionProvider(), SLOT(nickSelectionChanged(QModelIndexList)));
691
692 #ifdef Q_WS_MAC
693   setUnifiedTitleAndToolBarOnMac(true);
694 #endif
695
696 #ifdef HAVE_KDE
697   _mainToolBar = new KToolBar("MainToolBar", this, Qt::TopToolBarArea, false, true, true);
698 #else
699   _mainToolBar = new QToolBar(this);
700   _mainToolBar->setObjectName("MainToolBar");
701 #endif
702   _mainToolBar->setWindowTitle(tr("Main Toolbar"));
703   addToolBar(_mainToolBar);
704
705   QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
706   _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
707 }
708
709 void MainWin::connectedToCore() {
710   Q_CHECK_PTR(Client::bufferViewManager());
711   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
712   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
713   connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
714
715   setConnectedState();
716 }
717
718 void MainWin::setConnectedState() {
719   ActionCollection *coll = QtUi::actionCollection("General");
720
721   coll->action("ConnectCore")->setEnabled(false);
722   coll->action("DisconnectCore")->setEnabled(true);
723   coll->action("CoreInfo")->setEnabled(true);
724
725   foreach(QAction *action, _fileMenu->actions()) {
726     if(isRemoteCoreOnly(action))
727       action->setVisible(!Client::internalCore());
728   }
729
730   disconnect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), _msgProcessorStatusWidget, SLOT(setProgress(int, int)));
731   disconnect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
732   disconnect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
733   if(!Client::internalCore()) {
734     connect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), _msgProcessorStatusWidget, SLOT(setProgress(int, int)));
735     connect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
736     connect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
737   }
738
739   // _viewMenu->setEnabled(true);
740   if(!Client::internalCore())
741     statusBar()->showMessage(tr("Connected to core."));
742   else
743     statusBar()->clearMessage();
744
745   _coreConnectionStatusWidget->setVisible(!Client::internalCore());
746   updateIcon();
747   systemTray()->setState(SystemTray::Active);
748
749   if(Client::networkIds().isEmpty()) {
750     IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
751     wizard->show();
752   }
753 }
754
755 void MainWin::loadLayout() {
756   QtUiSettings s;
757   int accountId = Client::currentCoreAccount().accountId().toInt();
758   QByteArray state = s.value(QString("MainWinState-%1").arg(accountId)).toByteArray();
759   if(state.isEmpty()) {
760     // Make sure that the default bufferview is shown
761     if(_bufferViews.count())
762       _bufferViews.at(0)->show();
763     return;
764   }
765
766   restoreState(state, accountId);
767   _layoutLoaded = true;
768 }
769
770 void MainWin::saveLayout() {
771   QtUiSettings s;
772   int accountId = _bufferViews.count()? Client::currentCoreAccount().accountId().toInt() : 0; // only save if we still have a layout!
773   if(accountId > 0)
774     s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
775 }
776
777 void MainWin::disconnectedFromCore() {
778   // save core specific layout and remove bufferviews;
779   saveLayout();
780   _layoutLoaded = false;
781
782   QVariant actionData;
783   BufferViewDock *dock;
784   foreach(QAction *action, _bufferViewsMenu->actions()) {
785     actionData = action->data();
786     if(!actionData.isValid())
787       continue;
788
789     dock = qobject_cast<BufferViewDock *>(action->parent());
790     if(dock && actionData.toInt() != -1) {
791       removeAction(action);
792       _bufferViews.removeAll(dock);
793       Client::bufferViewOverlay()->removeView(dock->bufferViewId());
794       dock->deleteLater();
795     }
796   }
797
798   QtUiSettings s;
799   restoreState(s.value("MainWinState").toByteArray());
800   setDisconnectedState();
801 }
802
803 void MainWin::setDisconnectedState() {
804   ActionCollection *coll = QtUi::actionCollection("General");
805   //ui.menuCore->setEnabled(false);
806   coll->action("ConnectCore")->setEnabled(true);
807   coll->action("DisconnectCore")->setEnabled(false);
808   coll->action("CoreInfo")->setEnabled(false);
809   //_viewMenu->setEnabled(false);
810   statusBar()->showMessage(tr("Not connected to core."));
811   if(_msgProcessorStatusWidget)
812     _msgProcessorStatusWidget->setProgress(0, 0);
813   updateIcon();
814   systemTray()->setState(SystemTray::Passive);
815 }
816
817 void MainWin::userAuthenticationRequired(CoreAccount *account, bool *valid, const QString &errorMessage) {
818   Q_UNUSED(errorMessage)
819   CoreConnectAuthDlg dlg(account, this);
820   *valid = (dlg.exec() == QDialog::Accepted);
821 }
822
823 void MainWin::handleNoSslInClient(bool *accepted) {
824   QMessageBox box(QMessageBox::Warning, tr("Unencrypted Connection"), tr("<b>Your client does not support SSL encryption</b>"),
825                   QMessageBox::Ignore|QMessageBox::Cancel, this);
826   box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
827   box.setDefaultButton(QMessageBox::Ignore);
828   *accepted = box.exec() == QMessageBox::Ignore;
829 }
830
831 void MainWin::handleNoSslInCore(bool *accepted) {
832   QMessageBox box(QMessageBox::Warning, tr("Unencrypted Connection"), tr("<b>Your core does not support SSL encryption</b>"),
833                   QMessageBox::Ignore|QMessageBox::Cancel, this);
834   box.setInformativeText(tr("Sensitive data, like passwords, will be transmitted unencrypted to your Quassel core."));
835   box.setDefaultButton(QMessageBox::Ignore);
836   *accepted = box.exec() == QMessageBox::Ignore;
837
838 }
839
840 #ifdef HAVE_SSL
841
842 void MainWin::handleSslErrors(const QSslSocket *socket, bool *accepted, bool *permanently) {
843   QString errorString = "<ul>";
844   foreach(const QSslError error, socket->sslErrors())
845     errorString += QString("<li>%1</li>").arg(error.errorString());
846   errorString += "</ul>";
847
848   QMessageBox box(QMessageBox::Warning,
849                   tr("Untrusted Security Certificate"),
850                   tr("<b>The SSL certificate provided by the core at %1 is untrusted for the following reasons:</b>").arg(socket->peerName()),
851                   QMessageBox::Cancel, this);
852   box.setInformativeText(errorString);
853   box.addButton(tr("Continue"), QMessageBox::AcceptRole);
854   box.setDefaultButton(box.addButton(tr("Show Certificate"), QMessageBox::HelpRole));
855
856   QMessageBox::ButtonRole role;
857   do {
858     box.exec();
859     role = box.buttonRole(box.clickedButton());
860     if(role == QMessageBox::HelpRole) {
861       SslInfoDlg dlg(socket, this);
862       dlg.exec();
863     }
864   } while(role == QMessageBox::HelpRole);
865
866   *accepted = role == QMessageBox::AcceptRole;
867   if(*accepted) {
868     QMessageBox box2(QMessageBox::Warning,
869                      tr("Untrusted Security Certificate"),
870                      tr("Would you like to accept this certificate forever without being prompted?"),
871                      0, this);
872     box2.setDefaultButton(box2.addButton(tr("Current Session Only"), QMessageBox::NoRole));
873     box2.addButton(tr("Forever"), QMessageBox::YesRole);
874     box2.exec();
875     *permanently =  box2.buttonRole(box2.clickedButton()) == QMessageBox::YesRole;
876   }
877 }
878
879 #endif /* HAVE_SSL */
880
881 void MainWin::handleCoreConnectionError(const QString &error) {
882   QMessageBox::critical(this, tr("Core Connection Error"), error, QMessageBox::Ok);
883 }
884
885 void MainWin::showCoreConnectionDlg() {
886   CoreConnectDlg dlg(this);
887   if(dlg.exec() == QDialog::Accepted) {
888     AccountId accId = dlg.selectedAccount();
889     if(accId.isValid())
890       Client::coreConnection()->connectToCore(accId);
891   }
892 }
893
894 void MainWin::showCoreConfigWizard(const QVariantList &backends) {
895   CoreConfigWizard *wizard = new CoreConfigWizard(Client::coreConnection(), backends, this);
896
897   wizard->show();
898 }
899
900 void MainWin::showChannelList(NetworkId netId) {
901   ChannelListDlg *channelListDlg = new ChannelListDlg();
902
903   if(!netId.isValid()) {
904     QAction *action = qobject_cast<QAction *>(sender());
905     if(action)
906       netId = action->data().value<NetworkId>();
907   }
908
909   channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
910   channelListDlg->setNetwork(netId);
911   channelListDlg->show();
912 }
913
914 void MainWin::showIgnoreList(QString newRule) {
915   SettingsPageDlg dlg(new IgnoreListSettingsPage(this), this);
916   // prepare config dialog for new rule
917   if(!newRule.isEmpty())
918     qobject_cast<IgnoreListSettingsPage *>(dlg.currentPage())->editIgnoreRule(newRule);
919   dlg.exec();
920 }
921
922 void MainWin::showCoreInfoDlg() {
923   CoreInfoDlg(this).exec();
924 }
925
926 void MainWin::showAwayLog() {
927   if(_awayLog)
928     return;
929   AwayLogFilter *filter = new AwayLogFilter(Client::messageModel());
930   _awayLog = new AwayLogView(filter, 0);
931   filter->setParent(_awayLog);
932   connect(_awayLog, SIGNAL(destroyed()), this, SLOT(awayLogDestroyed()));
933   _awayLog->setAttribute(Qt::WA_DeleteOnClose);
934   _awayLog->show();
935 }
936
937 void MainWin::awayLogDestroyed() {
938   _awayLog = 0;
939 }
940
941 void MainWin::showSettingsDlg() {
942   SettingsDlg *dlg = new SettingsDlg();
943
944   //Category: Interface
945   dlg->registerSettingsPage(new AppearanceSettingsPage(dlg));
946   dlg->registerSettingsPage(new ChatViewSettingsPage(dlg));
947   dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
948   dlg->registerSettingsPage(new ItemViewSettingsPage(dlg));
949   dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
950   dlg->registerSettingsPage(new InputWidgetSettingsPage(dlg));
951   dlg->registerSettingsPage(new TopicWidgetSettingsPage(dlg));
952   dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
953   dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
954   dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
955
956   //Category: IRC
957   dlg->registerSettingsPage(new ConnectionSettingsPage(dlg));
958   dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
959   dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
960   dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
961   dlg->registerSettingsPage(new IgnoreListSettingsPage(dlg));
962
963   // Category: Remote Cores
964   if(Quassel::runMode() != Quassel::Monolithic) {
965     dlg->registerSettingsPage(new CoreAccountSettingsPage(dlg));
966     dlg->registerSettingsPage(new CoreConnectionSettingsPage(dlg));
967   }
968
969   dlg->show();
970 }
971
972 void MainWin::showAboutDlg() {
973   AboutDlg(this).exec();
974 }
975
976 #ifdef HAVE_KDE
977 void MainWin::showShortcutsDlg() {
978   KShortcutsDialog::configure(QtUi::actionCollection("General"), KShortcutsEditor::LetterShortcutsDisallowed);
979 }
980 #endif
981
982 /********************************************************************************************************/
983
984 bool MainWin::event(QEvent *event) {
985   if(event->type() == QEvent::WindowActivate) {
986     BufferId buffer = Client::bufferModel()->currentBuffer();
987     if(buffer.isValid())
988       QtUi::closeNotifications(buffer);
989   }
990   return QMainWindow::event(event);
991 }
992
993 void MainWin::moveEvent(QMoveEvent *event) {
994   if(!(windowState() & Qt::WindowMaximized))
995     _normalPos = event->pos();
996
997   QMainWindow::moveEvent(event);
998 }
999
1000 void MainWin::resizeEvent(QResizeEvent *event) {
1001   if(!(windowState() & Qt::WindowMaximized))
1002     _normalSize = event->size();
1003
1004   QMainWindow::resizeEvent(event);
1005 }
1006
1007 void MainWin::closeEvent(QCloseEvent *event) {
1008   QtUiSettings s;
1009   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
1010   Q_ASSERT(app);
1011   if(!app->isAboutToQuit() && QtUi::haveSystemTray() && s.value("MinimizeOnClose").toBool()) {
1012     QtUi::hideMainWidget();
1013     event->ignore();
1014   } else {
1015     event->accept();
1016     quit();
1017   }
1018 }
1019
1020 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
1021   Q_UNUSED(parent);
1022
1023   bool hasFocus = QApplication::activeWindow() != 0;
1024
1025   for(int i = start; i <= end; i++) {
1026     QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
1027     if(!idx.isValid()) {
1028       qDebug() << "MainWin::messagesInserted(): Invalid model index!";
1029       continue;
1030     }
1031     Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
1032     if(flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self))
1033       continue;
1034
1035     BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
1036     BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
1037
1038     if(hasFocus && bufId == Client::bufferModel()->currentBuffer())
1039       continue;
1040
1041     if((flags & Message::Highlight || bufType == BufferInfo::QueryBuffer)
1042       && !(Client::ignoreListManager() && Client::ignoreListManager()->match(idx.data(MessageModel::MessageRole).value<Message>(),
1043                                                                              Client::networkModel()->networkName(bufId))))
1044     {
1045       QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
1046       QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
1047       QString contents = idx.data(ChatLineModel::DisplayRole).toString();
1048       AbstractNotificationBackend::NotificationType type;
1049
1050       if(bufType == BufferInfo::QueryBuffer && !hasFocus)
1051         type = AbstractNotificationBackend::PrivMsg;
1052       else if(bufType == BufferInfo::QueryBuffer && hasFocus)
1053         type = AbstractNotificationBackend::PrivMsgFocused;
1054       else if(flags & Message::Highlight && !hasFocus)
1055         type = AbstractNotificationBackend::Highlight;
1056       else
1057         type = AbstractNotificationBackend::HighlightFocused;
1058
1059       QtUi::invokeNotification(bufId, type, sender, contents);
1060     }
1061   }
1062 }
1063
1064 void MainWin::currentBufferChanged(BufferId buffer) {
1065   if(buffer.isValid())
1066     QtUi::closeNotifications(buffer);
1067 }
1068
1069 void MainWin::clientNetworkCreated(NetworkId id) {
1070   const Network *net = Client::network(id);
1071   QAction *act = new QAction(net->networkName(), this);
1072   act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
1073   act->setData(QVariant::fromValue<NetworkId>(id));
1074   connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
1075   connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
1076
1077   QAction *beforeAction = 0;
1078   foreach(QAction *action, _networksMenu->actions()) {
1079     if(!action->data().isValid())  // ignore stock actions
1080       continue;
1081     if(net->networkName().localeAwareCompare(action->text()) < 0) {
1082       beforeAction = action;
1083       break;
1084     }
1085   }
1086   _networksMenu->insertAction(beforeAction, act);
1087 }
1088
1089 void MainWin::clientNetworkUpdated() {
1090   const Network *net = qobject_cast<const Network *>(sender());
1091   if(!net)
1092     return;
1093
1094   QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
1095   if(!action)
1096     return;
1097
1098   action->setText(net->networkName());
1099
1100   switch(net->connectionState()) {
1101   case Network::Initialized:
1102     action->setIcon(SmallIcon("network-connect"));
1103     break;
1104   case Network::Disconnected:
1105     action->setIcon(SmallIcon("network-disconnect"));
1106     break;
1107   default:
1108     action->setIcon(SmallIcon("network-wired"));
1109   }
1110 }
1111
1112 void MainWin::clientNetworkRemoved(NetworkId id) {
1113   QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
1114   if(!action)
1115     return;
1116
1117   action->deleteLater();
1118 }
1119
1120 void MainWin::connectOrDisconnectFromNet() {
1121   QAction *act = qobject_cast<QAction *>(sender());
1122   if(!act) return;
1123   const Network *net = Client::network(act->data().value<NetworkId>());
1124   if(!net) return;
1125   if(net->connectionState() == Network::Disconnected) net->requestConnect();
1126   else net->requestDisconnect();
1127 }
1128
1129 void MainWin::on_jumpHotBuffer_triggered() {
1130   if(!_bufferHotList->rowCount())
1131     return;
1132
1133   QModelIndex topIndex = _bufferHotList->index(0, 0);
1134   BufferId bufferId = _bufferHotList->data(topIndex, NetworkModel::BufferIdRole).value<BufferId>();
1135   Client::bufferModel()->switchToBuffer(bufferId);
1136 }
1137
1138 void MainWin::on_actionDebugNetworkModel_triggered() {
1139   QTreeView *view = new QTreeView;
1140   view->setAttribute(Qt::WA_DeleteOnClose);
1141   view->setWindowTitle("Debug NetworkModel View");
1142   view->setModel(Client::networkModel());
1143   view->setColumnWidth(0, 250);
1144   view->setColumnWidth(1, 250);
1145   view->setColumnWidth(2, 80);
1146   view->resize(610, 300);
1147   view->show();
1148 }
1149
1150 void MainWin::on_actionDebugHotList_triggered() {
1151   QTreeView *view = new QTreeView;
1152   view->setAttribute(Qt::WA_DeleteOnClose);
1153   view->setModel(_bufferHotList);
1154   view->show();
1155 }
1156
1157 void MainWin::on_actionDebugBufferViewOverlay_triggered() {
1158   DebugBufferViewOverlay *overlay = new DebugBufferViewOverlay(0);
1159   overlay->setAttribute(Qt::WA_DeleteOnClose);
1160   overlay->show();
1161 }
1162
1163 void MainWin::on_actionDebugMessageModel_triggered() {
1164   QTableView *view = new QTableView(0);
1165   DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
1166   filter->setSourceModel(Client::messageModel());
1167   view->setModel(filter);
1168   view->setAttribute(Qt::WA_DeleteOnClose, true);
1169   view->verticalHeader()->hide();
1170   view->horizontalHeader()->setStretchLastSection(true);
1171   view->show();
1172 }
1173
1174 void MainWin::on_actionDebugLog_triggered() {
1175   DebugLogWidget *logWidget = new DebugLogWidget(0);
1176   logWidget->show();
1177 }
1178
1179 void MainWin::showStatusBarMessage(const QString &message) {
1180   statusBar()->showMessage(message, 10000);
1181 }
1182