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