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