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