Fixing backlog timestamps when merging from sqlite.
[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 #endif
30
31 #include "aboutdlg.h"
32 #include "awaylogfilter.h"
33 #include "awaylogview.h"
34 #include "action.h"
35 #include "actioncollection.h"
36 #include "buffermodel.h"
37 #include "bufferview.h"
38 #include "bufferviewoverlay.h"
39 #include "bufferviewoverlayfilter.h"
40 #include "bufferwidget.h"
41 #include "channellistdlg.h"
42 #include "chatlinemodel.h"
43 #include "chatmonitorfilter.h"
44 #include "chatmonitorview.h"
45 #include "chatview.h"
46 #include "client.h"
47 #include "clientsyncer.h"
48 #include "clientbacklogmanager.h"
49 #include "clientbufferviewconfig.h"
50 #include "clientbufferviewmanager.h"
51 #include "coreinfodlg.h"
52 #include "coreconnectdlg.h"
53 #include "contextmenuactionprovider.h"
54 #include "debuglogwidget.h"
55 #include "debugmessagemodelfilter.h"
56 #include "iconloader.h"
57 #include "inputwidget.h"
58 #include "inputline.h"
59 #include "irclistmodel.h"
60 #include "ircconnectionwizard.h"
61 #include "jumpkeyhandler.h"
62 #include "msgprocessorstatuswidget.h"
63 #include "nicklistwidget.h"
64 #include "qtuiapplication.h"
65 #include "qtuimessageprocessor.h"
66 #include "qtuisettings.h"
67 #include "sessionsettings.h"
68 #include "settingsdlg.h"
69 #include "settingspagedlg.h"
70 #include "systemtray.h"
71 #include "toolbaractionprovider.h"
72 #include "topicwidget.h"
73 #include "verticaldock.h"
74
75 #ifndef HAVE_KDE
76 #  ifdef HAVE_DBUS
77 #    include "desktopnotificationbackend.h"
78 #  endif
79 #  ifdef HAVE_PHONON
80 #    include "phononnotificationbackend.h"
81 #  endif
82 #  include "systraynotificationbackend.h"
83 #  include "taskbarnotificationbackend.h"
84 #else /* HAVE_KDE */
85 #  include "knotificationbackend.h"
86 #endif /* HAVE_KDE */
87
88 #include "settingspages/aliasessettingspage.h"
89 #include "settingspages/appearancesettingspage.h"
90 #include "settingspages/backlogsettingspage.h"
91 #include "settingspages/bufferviewsettingspage.h"
92 #include "settingspages/chatmonitorsettingspage.h"
93 #include "settingspages/colorsettingspage.h"
94 #include "settingspages/generalsettingspage.h"
95 #include "settingspages/highlightsettingspage.h"
96 #include "settingspages/identitiessettingspage.h"
97 #include "settingspages/networkssettingspage.h"
98 #include "settingspages/notificationssettingspage.h"
99
100 MainWin::MainWin(QWidget *parent)
101 #ifdef HAVE_KDE
102   : KMainWindow(parent),
103   _kHelpMenu(new KHelpMenu(this, KGlobal::mainComponent().aboutData())),
104 #else
105   : QMainWindow(parent),
106 #endif
107     coreLagLabel(new QLabel()),
108     sslLabel(new QLabel()),
109     msgProcessorStatusWidget(new MsgProcessorStatusWidget()),
110     _titleSetter(this),
111     _awayLog(0)
112 {
113   QtUiSettings uiSettings;
114   QString style = uiSettings.value("Style", QString()).toString();
115   if(!style.isEmpty()) {
116     QApplication::setStyle(style);
117   }
118
119   QApplication::setQuitOnLastWindowClosed(false);
120
121   setWindowTitle("Quassel IRC");
122   setWindowIconText("Quassel IRC");
123   updateIcon();
124
125   installEventFilter(new JumpKeyHandler(this));
126
127   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
128   connect(app, SIGNAL(saveStateToSession(const QString&)), SLOT(saveStateToSession(const QString&)));
129   connect(app, SIGNAL(saveStateToSessionSettings(SessionSettings&)), SLOT(saveStateToSessionSettings(SessionSettings&)));
130 }
131
132 void MainWin::init() {
133   QtUiSettings s;
134   if(s.value("MainWinSize").isValid())
135     resize(s.value("MainWinSize").toSize());
136   else
137     resize(QSize(800, 500));
138
139   connect(QApplication::instance(), SIGNAL(aboutToQuit()), SLOT(saveLayout()));
140   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
141   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
142   connect(Client::messageModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
143            SLOT(messagesInserted(const QModelIndex &, int, int)));
144   connect(GraphicalUi::contextMenuActionProvider(), SIGNAL(showChannelList(NetworkId)), SLOT(showChannelList(NetworkId)));
145
146   // Setup Dock Areas
147   setDockNestingEnabled(true);
148   setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
149   setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
150   setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
151   setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
152
153   // Order is sometimes important
154   setupActions();
155   setupBufferWidget();
156   setupMenus();
157   setupTopicWidget();
158   setupChatMonitor();
159   setupNickWidget();
160   setupInputWidget();
161   setupStatusBar();
162   setupToolBars();
163   setupSystray();
164   setupTitleSetter();
165
166 #ifndef HAVE_KDE
167   QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
168   QtUi::registerNotificationBackend(new SystrayNotificationBackend(this));
169 #  ifdef HAVE_PHONON
170   QtUi::registerNotificationBackend(new PhononNotificationBackend(this));
171 #  endif
172 #  ifdef HAVE_DBUS
173   QtUi::registerNotificationBackend(new DesktopNotificationBackend(this));
174 #  endif
175
176 #else /* HAVE_KDE */
177   QtUi::registerNotificationBackend(new KNotificationBackend(this));
178 #endif /* HAVE_KDE */
179
180   // restore mainwin state
181   restoreState(s.value("MainWinState").toByteArray());
182
183   // restore locked state of docks
184   QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
185
186   setDisconnectedState();  // Disable menus and stuff
187
188   show();
189
190   if(Quassel::runMode() != Quassel::Monolithic) {
191     showCoreConnectionDlg(true); // autoconnect if appropriate
192   } else {
193     startInternalCore();
194   }
195 }
196
197 MainWin::~MainWin() {
198   QtUiSettings s;
199   s.setValue("MainWinSize", size());
200   s.setValue("MainWinPos", pos());
201   s.setValue("MainWinState", saveState());
202 }
203
204 void MainWin::updateIcon() {
205 #ifdef Q_WS_MAC
206   const int size = 128;
207 #else
208   const int size = 48;
209 #endif
210
211   QPixmap icon;
212   if(Client::isConnected())
213     icon = DesktopIcon("quassel", size);
214   else
215     icon = DesktopIcon("quassel_inactive", size);
216   setWindowIcon(icon);
217   qApp->setWindowIcon(icon);
218 }
219
220 void MainWin::setupActions() {
221   ActionCollection *coll = QtUi::actionCollection("General");
222   // File
223   coll->addAction("ConnectCore", new Action(SmallIcon("network-connect"), tr("&Connect to Core..."), coll,
224                                              this, SLOT(showCoreConnectionDlg())));
225   coll->addAction("DisconnectCore", new Action(SmallIcon("network-disconnect"), tr("&Disconnect from Core"), coll,
226                                                 Client::instance(), SLOT(disconnectFromCore())));
227   coll->addAction("CoreInfo", new Action(SmallIcon("help-about"), tr("Core &Info..."), coll,
228                                           this, SLOT(showCoreInfoDlg())));
229   coll->addAction("ConfigureNetworks", new Action(SmallIcon("configure"), tr("Configure &Networks..."), coll,
230                                               this, SLOT(on_actionConfigureNetworks_triggered())));
231   coll->addAction("Quit", new Action(SmallIcon("application-exit"), tr("&Quit"), coll,
232                                       qApp, SLOT(quit()), tr("Ctrl+Q")));
233
234   // View
235   coll->addAction("ConfigureBufferViews", new Action(tr("&Configure Buffer Views..."), coll,
236                                              this, SLOT(on_actionConfigureViews_triggered())));
237
238   QAction *lockAct = coll->addAction("LockLayout", new Action(tr("&Lock Layout"), coll));
239   lockAct->setCheckable(true);
240   connect(lockAct, SIGNAL(toggled(bool)), SLOT(on_actionLockLayout_toggled(bool)));
241
242   coll->addAction("ToggleSearchBar", new Action(SmallIcon("edit-find"), tr("Show &Search Bar"), coll,
243                                                 0, 0, QKeySequence::Find))->setCheckable(true);
244   coll->addAction("ShowAwayLog", new Action(tr("Show Away Log"), coll,
245                                             this, SLOT(showAwayLog())));
246   coll->addAction("ToggleStatusBar", new Action(tr("Show Status &Bar"), coll,
247                                                  0, 0))->setCheckable(true);
248
249   // Settings
250   coll->addAction("ConfigureQuassel", new Action(SmallIcon("configure"), tr("&Configure Quassel..."), coll,
251                                                   this, SLOT(showSettingsDlg()), tr("F7")));
252
253   // Help
254   coll->addAction("AboutQuassel", new Action(SmallIcon("quassel"), tr("&About Quassel"), coll,
255                                               this, SLOT(showAboutDlg())));
256   coll->addAction("AboutQt", new Action(QIcon(":/pics/qt-logo.png"), tr("About &Qt"), coll,
257                                          qApp, SLOT(aboutQt())));
258   coll->addAction("DebugNetworkModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &NetworkModel"), coll,
259                                        this, SLOT(on_actionDebugNetworkModel_triggered())));
260   coll->addAction("DebugBufferViewOverlay", new Action(SmallIcon("tools-report-bug"), tr("Debug &BufferViewOverlay"), coll,
261                                        this, SLOT(on_actionDebugBufferViewOverlay_triggered())));
262   coll->addAction("DebugMessageModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &MessageModel"), coll,
263                                        this, SLOT(on_actionDebugMessageModel_triggered())));
264   coll->addAction("DebugLog", new Action(SmallIcon("tools-report-bug"), tr("Debug &Log"), coll,
265                                        this, SLOT(on_actionDebugLog_triggered())));
266 }
267
268 void MainWin::setupMenus() {
269   ActionCollection *coll = QtUi::actionCollection("General");
270
271   _fileMenu = menuBar()->addMenu(tr("&File"));
272
273   static const QStringList coreActions = QStringList()
274     << "ConnectCore" << "DisconnectCore" << "CoreInfo";
275
276   QAction *coreAction;
277   foreach(QString actionName, coreActions) {
278     coreAction = coll->action(actionName);
279     _fileMenu->addAction(coreAction);
280     flagRemoteCoreOnly(coreAction);
281   }
282   flagRemoteCoreOnly(_fileMenu->addSeparator());
283
284   _networksMenu = _fileMenu->addMenu(tr("&Networks"));
285   _networksMenu->addAction(coll->action("ConfigureNetworks"));
286   _networksMenu->addSeparator();
287   _fileMenu->addSeparator();
288   _fileMenu->addAction(coll->action("Quit"));
289
290   _viewMenu = menuBar()->addMenu(tr("&View"));
291   _bufferViewsMenu = _viewMenu->addMenu(tr("&Buffer Views"));
292   _bufferViewsMenu->addAction(coll->action("ConfigureBufferViews"));
293   _toolbarMenu = _viewMenu->addMenu(tr("&Toolbars"));
294   _viewMenu->addSeparator();
295   _viewMenu->addAction(coll->action("ToggleSearchBar"));
296
297   coreAction = coll->action("ShowAwayLog");
298   flagRemoteCoreOnly(coreAction);
299   _viewMenu->addAction(coreAction);
300
301   _viewMenu->addAction(coll->action("ToggleStatusBar"));
302   _viewMenu->addSeparator();
303   _viewMenu->addAction(coll->action("LockLayout"));
304
305   _settingsMenu = menuBar()->addMenu(tr("&Settings"));
306 #ifdef HAVE_KDE
307   _settingsMenu->addAction(KStandardAction::keyBindings(this, SLOT(showShortcutsDlg()), this));
308   _settingsMenu->addAction(KStandardAction::configureNotifications(this, SLOT(showNotificationsDlg()), this));
309 #endif
310   _settingsMenu->addAction(coll->action("ConfigureQuassel"));
311
312   _helpMenu = menuBar()->addMenu(tr("&Help"));
313   _helpMenu->addAction(coll->action("AboutQuassel"));
314 #ifndef HAVE_KDE
315   _helpMenu->addAction(coll->action("AboutQt"));
316 #else
317   _helpMenu->addAction(KStandardAction::aboutKDE(_kHelpMenu, SLOT(aboutKDE()), this));
318 #endif
319   _helpMenu->addSeparator();
320   _helpDebugMenu = _helpMenu->addMenu(SmallIcon("tools-report-bug"), tr("Debug"));
321   _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
322   _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
323   _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
324   _helpDebugMenu->addAction(coll->action("DebugLog"));
325 }
326
327 void MainWin::setupBufferWidget() {
328   _bufferWidget = new BufferWidget(this);
329   _bufferWidget->setModel(Client::bufferModel());
330   _bufferWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
331   setCentralWidget(_bufferWidget);
332 }
333
334 void MainWin::addBufferView(int bufferViewConfigId) {
335   addBufferView(Client::bufferViewManager()->clientBufferViewConfig(bufferViewConfigId));
336 }
337
338 void MainWin::addBufferView(ClientBufferViewConfig *config) {
339   if(!config)
340     return;
341
342   config->setLocked(QtUiSettings().value("LockLayout", false).toBool());
343   BufferViewDock *dock = new BufferViewDock(config, this);
344
345   //create the view and initialize it's filter
346   BufferView *view = new BufferView(dock);
347   view->setFilteredModel(Client::bufferModel(), config);
348   view->installEventFilter(_inputWidget->inputLine()); // for key presses
349   view->show();
350
351   Client::bufferModel()->synchronizeView(view);
352
353   dock->setWidget(view);
354   dock->show();
355
356   addDockWidget(Qt::LeftDockWidgetArea, dock);
357   _bufferViewsMenu->addAction(dock->toggleViewAction());
358
359   connect(dock->toggleViewAction(), SIGNAL(toggled(bool)), this, SLOT(bufferViewToggled(bool)));
360   _bufferViews.append(dock);
361 }
362
363 void MainWin::removeBufferView(int bufferViewConfigId) {
364   QVariant actionData;
365   BufferViewDock *dock;
366   foreach(QAction *action, _bufferViewsMenu->actions()) {
367     actionData = action->data();
368     if(!actionData.isValid())
369       continue;
370
371     dock = qobject_cast<BufferViewDock *>(action->parent());
372     if(dock && actionData.toInt() == bufferViewConfigId) {
373       removeAction(action);
374       dock->deleteLater();
375     }
376   }
377 }
378
379 void MainWin::bufferViewToggled(bool enabled) {
380   QAction *action = qobject_cast<QAction *>(sender());
381   Q_ASSERT(action);
382   BufferViewDock *dock = qobject_cast<BufferViewDock *>(action->parent());
383   Q_ASSERT(dock);
384   if(enabled) {
385     Client::bufferViewOverlay()->addView(dock->bufferViewId());
386     BufferViewConfig *config = dock->config();
387     if(config && config->isInitialized()) {
388       BufferIdList buffers;
389       if(config->networkId().isValid()) {
390         foreach(BufferId bufferId, config->bufferList()) {
391           if(Client::networkModel()->networkId(bufferId) == config->networkId())
392             buffers << bufferId;
393         }
394         foreach(BufferId bufferId, config->temporarilyRemovedBuffers().toList()) {
395           if(Client::networkModel()->networkId(bufferId) == config->networkId())
396             buffers << bufferId;
397         }
398       } else {
399         buffers = BufferIdList::fromSet(config->bufferList().toSet() + config->temporarilyRemovedBuffers());
400       }
401       Client::backlogManager()->checkForBacklog(buffers);
402     }
403   } else {
404     Client::bufferViewOverlay()->removeView(dock->bufferViewId());
405   }
406 }
407
408 BufferView *MainWin::allBuffersView() const {
409   // "All Buffers" is always the first dock created
410   if(_bufferViews.count() > 0)
411     return _bufferViews[0]->bufferView();
412   return 0;
413 }
414
415 void MainWin::showNotificationsDlg() {
416   SettingsPageDlg dlg(new NotificationsSettingsPage(this), this);
417   dlg.exec();
418 }
419
420 void MainWin::on_actionConfigureNetworks_triggered() {
421   SettingsPageDlg dlg(new NetworksSettingsPage(this), this);
422   dlg.exec();
423 }
424
425 void MainWin::on_actionConfigureViews_triggered() {
426   SettingsPageDlg dlg(new BufferViewSettingsPage(this), this);
427   dlg.exec();
428 }
429
430 void MainWin::on_actionLockLayout_toggled(bool lock) {
431   QList<VerticalDock *> docks = findChildren<VerticalDock *>();
432   foreach(VerticalDock *dock, docks) {
433     dock->showTitle(!lock);
434   }
435   if(Client::bufferViewManager()) {
436     foreach(ClientBufferViewConfig *config, Client::bufferViewManager()->clientBufferViewConfigs()) {
437       config->setLocked(lock);
438     }
439   }
440   QtUiSettings().setValue("LockLayout", lock);
441 }
442
443 void MainWin::setupNickWidget() {
444   // create nick dock
445   NickListDock *nickDock = new NickListDock(tr("Nicks"), this);
446   nickDock->setObjectName("NickDock");
447   nickDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
448
449   _nickListWidget = new NickListWidget(nickDock);
450   nickDock->setWidget(_nickListWidget);
451
452   addDockWidget(Qt::RightDockWidgetArea, nickDock);
453   _viewMenu->addAction(nickDock->toggleViewAction());
454   nickDock->toggleViewAction()->setText(tr("Show Nick List"));
455
456   // See NickListDock::NickListDock();
457   // connect(nickDock->toggleViewAction(), SIGNAL(triggered(bool)), nickListWidget, SLOT(showWidget(bool)));
458
459   // attach the NickListWidget to the BufferModel and the default selection
460   _nickListWidget->setModel(Client::bufferModel());
461   _nickListWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
462 }
463
464 void MainWin::setupChatMonitor() {
465   VerticalDock *dock = new VerticalDock(tr("Chat Monitor"), this);
466   dock->setObjectName("ChatMonitorDock");
467
468   ChatMonitorFilter *filter = new ChatMonitorFilter(Client::messageModel(), this);
469   ChatMonitorView *chatView = new ChatMonitorView(filter, this);
470   chatView->show();
471   dock->setWidget(chatView);
472   dock->hide();
473
474   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
475   _viewMenu->addAction(dock->toggleViewAction());
476   dock->toggleViewAction()->setText(tr("Show Chat Monitor"));
477 }
478
479 void MainWin::setupInputWidget() {
480   VerticalDock *dock = new VerticalDock(tr("Inputline"), this);
481   dock->setObjectName("InputDock");
482
483   _inputWidget = new InputWidget(dock);
484   dock->setWidget(_inputWidget);
485
486   addDockWidget(Qt::BottomDockWidgetArea, dock);
487
488   _viewMenu->addAction(dock->toggleViewAction());
489   dock->toggleViewAction()->setText(tr("Show Input Line"));
490
491   _inputWidget->setModel(Client::bufferModel());
492   _inputWidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
493
494   _bufferWidget->setFocusProxy(_inputWidget);
495
496   _inputWidget->inputLine()->installEventFilter(_bufferWidget);
497 }
498
499 void MainWin::setupTopicWidget() {
500   VerticalDock *dock = new VerticalDock(tr("Topic"), this);
501   dock->setObjectName("TopicDock");
502   TopicWidget *topicwidget = new TopicWidget(dock);
503
504   dock->setWidget(topicwidget);
505
506   topicwidget->setModel(Client::bufferModel());
507   topicwidget->setSelectionModel(Client::bufferModel()->standardSelectionModel());
508
509   addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
510
511   _viewMenu->addAction(dock->toggleViewAction());
512   dock->toggleViewAction()->setText(tr("Show Topic Line"));
513 }
514
515 void MainWin::setupTitleSetter() {
516   _titleSetter.setModel(Client::bufferModel());
517   _titleSetter.setSelectionModel(Client::bufferModel()->standardSelectionModel());
518 }
519
520 void MainWin::setupStatusBar() {
521   // MessageProcessor progress
522   statusBar()->addPermanentWidget(msgProcessorStatusWidget);
523
524   // Core Lag:
525   updateLagIndicator();
526   statusBar()->addPermanentWidget(coreLagLabel);
527   coreLagLabel->hide();
528   connect(Client::signalProxy(), SIGNAL(lagUpdated(int)), this, SLOT(updateLagIndicator(int)));
529
530   // SSL indicator
531   sslLabel->setPixmap(QPixmap());
532   statusBar()->addPermanentWidget(sslLabel);
533   sslLabel->hide();
534
535   QAction *showStatusbar = QtUi::actionCollection("General")->action("ToggleStatusBar");
536
537   QtUiSettings uiSettings;
538
539   bool enabled = uiSettings.value("ShowStatusBar", QVariant(true)).toBool();
540   showStatusbar->setChecked(enabled);
541   enabled ? statusBar()->show() : statusBar()->hide();
542
543   connect(showStatusbar, SIGNAL(toggled(bool)), statusBar(), SLOT(setVisible(bool)));
544   connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
545 }
546
547 void MainWin::saveStatusBarStatus(bool enabled) {
548   QtUiSettings uiSettings;
549   uiSettings.setValue("ShowStatusBar", enabled);
550 }
551
552 void MainWin::setupSystray() {
553   _systemTray = new SystemTray(this);
554
555 #ifndef Q_WS_MAC
556   connect(systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systrayActivated(QSystemTrayIcon::ActivationReason)));
557 #endif
558
559 }
560
561 void MainWin::setupToolBars() {
562   connect(_bufferWidget, SIGNAL(currentChanged(QModelIndex)),
563           QtUi::toolBarActionProvider(), SLOT(currentBufferChanged(QModelIndex)));
564   connect(_nickListWidget, SIGNAL(nickSelectionChanged(QModelIndexList)),
565           QtUi::toolBarActionProvider(), SLOT(nickSelectionChanged(QModelIndexList)));
566
567 #ifdef Q_WS_MAC
568   setUnifiedTitleAndToolBarOnMac(true);
569 #endif
570   _mainToolBar = addToolBar(tr("Main Toolbar"));
571   _mainToolBar->setObjectName("MainToolBar");
572
573   QtUi::toolBarActionProvider()->addActions(_mainToolBar, ToolBarActionProvider::MainToolBar);
574   _toolbarMenu->addAction(_mainToolBar->toggleViewAction());
575
576   //_nickToolBar = addToolBar("User");
577   //_nickToolBar->setObjectName("NickToolBar");
578   //QtUi::toolBarActionProvider()->addActions(_nickToolBar, ToolBarActionProvider::NickToolBar);
579
580 #ifdef HAVE_KDE
581   _mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
582   //_nickToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
583 #endif
584 }
585
586 void MainWin::changeEvent(QEvent *event) {
587   if(event->type() == QEvent::WindowStateChange) {
588     if(windowState() & Qt::WindowMinimized) {
589       QtUiSettings s;
590       if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
591         hideToTray();
592         event->accept();
593       }
594     }
595   }
596 }
597
598 void MainWin::connectedToCore() {
599   Q_CHECK_PTR(Client::bufferViewManager());
600   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigAdded(int)), this, SLOT(addBufferView(int)));
601   connect(Client::bufferViewManager(), SIGNAL(bufferViewConfigDeleted(int)), this, SLOT(removeBufferView(int)));
602   connect(Client::bufferViewManager(), SIGNAL(initDone()), this, SLOT(loadLayout()));
603
604   setConnectedState();
605 }
606
607 void MainWin::setConnectedState() {
608   ActionCollection *coll = QtUi::actionCollection("General");
609
610   coll->action("ConnectCore")->setEnabled(false);
611   coll->action("DisconnectCore")->setEnabled(true);
612   coll->action("CoreInfo")->setEnabled(true);
613
614   foreach(QAction *action, _fileMenu->actions()) {
615     if(isRemoteCoreOnly(action))
616       action->setVisible(!Client::internalCore());
617   }
618
619   disconnect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
620   disconnect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
621   disconnect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
622   if(!Client::internalCore()) {
623     connect(Client::backlogManager(), SIGNAL(updateProgress(int, int)), msgProcessorStatusWidget, SLOT(setProgress(int, int)));
624     connect(Client::backlogManager(), SIGNAL(messagesRequested(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
625     connect(Client::backlogManager(), SIGNAL(messagesProcessed(const QString &)), this, SLOT(showStatusBarMessage(const QString &)));
626   }
627
628   // _viewMenu->setEnabled(true);
629   if(!Client::internalCore())
630     statusBar()->showMessage(tr("Connected to core."));
631   else
632     statusBar()->clearMessage();
633
634   if(Client::signalProxy()->isSecure()) {
635     sslLabel->setPixmap(SmallIcon("security-high"));
636   } else {
637     sslLabel->setPixmap(SmallIcon("security-low"));
638   }
639
640   sslLabel->setVisible(!Client::internalCore());
641   coreLagLabel->setVisible(!Client::internalCore());
642   updateIcon();
643   systemTray()->setState(SystemTray::Active);
644
645   if(Client::networkIds().isEmpty()) {
646     IrcConnectionWizard *wizard = new IrcConnectionWizard(this, Qt::Sheet);
647     wizard->show();
648   }
649 }
650
651 void MainWin::loadLayout() {
652   QtUiSettings s;
653   int accountId = Client::currentCoreAccount().toInt();
654   restoreState(s.value(QString("MainWinState-%1").arg(accountId)).toByteArray(), accountId);
655 }
656
657 void MainWin::saveLayout() {
658   QtUiSettings s;
659   int accountId = Client::currentCoreAccount().toInt();
660   if(accountId > 0) s.setValue(QString("MainWinState-%1").arg(accountId) , saveState(accountId));
661 }
662
663 void MainWin::updateLagIndicator(int lag) {
664   QString text = tr("Core Lag: %1");
665   if(lag == -1)
666     text = text.arg('-');
667   else
668     text = text.arg("%1 msec").arg(lag);
669   coreLagLabel->setText(text);
670 }
671
672 void MainWin::disconnectedFromCore() {
673   // save core specific layout and remove bufferviews;
674   saveLayout();
675   QVariant actionData;
676   BufferViewDock *dock;
677   foreach(QAction *action, _bufferViewsMenu->actions()) {
678     actionData = action->data();
679     if(!actionData.isValid())
680       continue;
681
682     dock = qobject_cast<BufferViewDock *>(action->parent());
683     if(dock && actionData.toInt() != -1) {
684       removeAction(action);
685       dock->deleteLater();
686     }
687   }
688   QtUiSettings s;
689   restoreState(s.value("MainWinState").toByteArray());
690   setDisconnectedState();
691 }
692
693 void MainWin::setDisconnectedState() {
694   ActionCollection *coll = QtUi::actionCollection("General");
695   //ui.menuCore->setEnabled(false);
696   coll->action("ConnectCore")->setEnabled(true);
697   coll->action("DisconnectCore")->setEnabled(false);
698   coll->action("CoreInfo")->setEnabled(false);
699   //_viewMenu->setEnabled(false);
700   statusBar()->showMessage(tr("Not connected to core."));
701   sslLabel->setPixmap(QPixmap());
702   sslLabel->hide();
703   updateLagIndicator();
704   coreLagLabel->hide();
705   if(msgProcessorStatusWidget)
706     msgProcessorStatusWidget->setProgress(0, 0);
707   updateIcon();
708   systemTray()->setState(SystemTray::Inactive);
709 }
710
711 void MainWin::startInternalCore() {
712   ClientSyncer *syncer = new ClientSyncer();
713   Client::registerClientSyncer(syncer);
714   connect(syncer, SIGNAL(syncFinished()), syncer, SLOT(deleteLater()), Qt::QueuedConnection);
715   syncer->useInternalCore();
716 }
717
718 void MainWin::showCoreConnectionDlg(bool autoConnect) {
719   CoreConnectDlg(autoConnect, this).exec();
720 }
721
722 void MainWin::showChannelList(NetworkId netId) {
723   ChannelListDlg *channelListDlg = new ChannelListDlg();
724
725   if(!netId.isValid()) {
726     QAction *action = qobject_cast<QAction *>(sender());
727     if(action)
728       netId = action->data().value<NetworkId>();
729   }
730
731   channelListDlg->setAttribute(Qt::WA_DeleteOnClose);
732   channelListDlg->setNetwork(netId);
733   channelListDlg->show();
734 }
735
736 void MainWin::showCoreInfoDlg() {
737   CoreInfoDlg(this).exec();
738 }
739
740 void MainWin::showAwayLog() {
741   if(_awayLog)
742     return;
743   AwayLogFilter *filter = new AwayLogFilter(Client::messageModel());
744   _awayLog = new AwayLogView(filter, 0);
745   filter->setParent(_awayLog);
746   connect(_awayLog, SIGNAL(destroyed()), this, SLOT(awayLogDestroyed()));
747   _awayLog->setAttribute(Qt::WA_DeleteOnClose);
748   _awayLog->show();
749 }
750
751 void MainWin::awayLogDestroyed() {
752   _awayLog = 0;
753 }
754
755 void MainWin::showSettingsDlg() {
756   SettingsDlg *dlg = new SettingsDlg();
757
758   //Category: Appearance
759   dlg->registerSettingsPage(new AppearanceSettingsPage(dlg)); //General
760   dlg->registerSettingsPage(new ColorSettingsPage(dlg));
761   dlg->registerSettingsPage(new HighlightSettingsPage(dlg));
762   dlg->registerSettingsPage(new NotificationsSettingsPage(dlg));
763   dlg->registerSettingsPage(new BacklogSettingsPage(dlg));
764   dlg->registerSettingsPage(new BufferViewSettingsPage(dlg));
765   dlg->registerSettingsPage(new ChatMonitorSettingsPage(dlg));
766
767   //Category: Misc
768   dlg->registerSettingsPage(new GeneralSettingsPage(dlg));
769   dlg->registerSettingsPage(new IdentitiesSettingsPage(dlg));
770   dlg->registerSettingsPage(new NetworksSettingsPage(dlg));
771   dlg->registerSettingsPage(new AliasesSettingsPage(dlg));
772
773   dlg->show();
774 }
775
776 void MainWin::showAboutDlg() {
777   AboutDlg(this).exec();
778 }
779
780 #ifdef HAVE_KDE
781 void MainWin::showShortcutsDlg() {
782   KShortcutsDialog::configure(QtUi::actionCollection("General"), KShortcutsEditor::LetterShortcutsDisallowed);
783 }
784 #endif
785
786 void MainWin::closeEvent(QCloseEvent *event) {
787   QtUiSettings s;
788   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
789   Q_ASSERT(app);
790   if(!app->aboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
791     toggleMinimizedToTray();
792     event->ignore();
793   } else {
794     event->accept();
795     QApplication::quit();
796   }
797 }
798
799 void MainWin::systrayActivated(QSystemTrayIcon::ActivationReason activationReason) {
800   if(activationReason == QSystemTrayIcon::Trigger) {
801     toggleMinimizedToTray();
802   }
803 }
804
805 void MainWin::hideToTray() {
806   if(!systemTray()->isSystemTrayAvailable()) {
807     qWarning() << Q_FUNC_INFO << "was called with no SystemTray available!";
808     return;
809   }
810   clearFocus();
811   hide();
812   systemTray()->setIconVisible();
813 }
814
815 void MainWin::toggleMinimizedToTray() {
816   if(windowState() & Qt::WindowMinimized) {
817     // restore
818     setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
819     show();
820     activateWindow();
821     raise();
822   } else {
823     setWindowState((windowState() & ~Qt::WindowActive) | Qt::WindowMinimized);
824     hideToTray();
825   }
826 }
827
828 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
829   Q_UNUSED(parent);
830
831   if(QApplication::activeWindow() != 0)
832     return;
833
834   for(int i = start; i <= end; i++) {
835     QModelIndex idx = Client::messageModel()->index(i, ChatLineModel::ContentsColumn);
836     if(!idx.isValid()) {
837       qDebug() << "MainWin::messagesInserted(): Invalid model index!";
838       continue;
839     }
840     Message::Flags flags = (Message::Flags)idx.data(ChatLineModel::FlagsRole).toInt();
841     if(flags.testFlag(Message::Backlog) || flags.testFlag(Message::Self)) continue;
842     flags |= Message::Backlog;  // we only want to trigger a highlight once!
843     Client::messageModel()->setData(idx, (int)flags, ChatLineModel::FlagsRole);
844
845     BufferId bufId = idx.data(ChatLineModel::BufferIdRole).value<BufferId>();
846     BufferInfo::Type bufType = Client::networkModel()->bufferType(bufId);
847
848     if(flags & Message::Highlight || bufType == BufferInfo::QueryBuffer) {
849       QModelIndex senderIdx = Client::messageModel()->index(i, ChatLineModel::SenderColumn);
850       QString sender = senderIdx.data(ChatLineModel::EditRole).toString();
851       QString contents = idx.data(ChatLineModel::DisplayRole).toString();
852       QtUi::invokeNotification(bufId, sender, contents);
853     }
854   }
855 }
856
857 bool MainWin::event(QEvent *event) {
858   if(event->type() == QEvent::WindowActivate)
859     QtUi::closeNotifications();
860   return QMainWindow::event(event);
861 }
862
863 void MainWin::clientNetworkCreated(NetworkId id) {
864   const Network *net = Client::network(id);
865   QAction *act = new QAction(net->networkName(), this);
866   act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
867   act->setData(QVariant::fromValue<NetworkId>(id));
868   connect(net, SIGNAL(updatedRemotely()), this, SLOT(clientNetworkUpdated()));
869   connect(act, SIGNAL(triggered()), this, SLOT(connectOrDisconnectFromNet()));
870
871   QAction *beforeAction = 0;
872   foreach(QAction *action, _networksMenu->actions()) {
873     if(!action->data().isValid())  // ignore stock actions
874       continue;
875     if(net->networkName().localeAwareCompare(action->text()) < 0) {
876       beforeAction = action;
877       break;
878     }
879   }
880   _networksMenu->insertAction(beforeAction, act);
881 }
882
883 void MainWin::clientNetworkUpdated() {
884   const Network *net = qobject_cast<const Network *>(sender());
885   if(!net)
886     return;
887
888   QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(net->networkId().toInt()));
889   if(!action)
890     return;
891
892   action->setText(net->networkName());
893
894   switch(net->connectionState()) {
895   case Network::Initialized:
896     action->setIcon(SmallIcon("network-connect"));
897     break;
898   case Network::Disconnected:
899     action->setIcon(SmallIcon("network-disconnect"));
900     break;
901   default:
902     action->setIcon(SmallIcon("network-wired"));
903   }
904 }
905
906 void MainWin::clientNetworkRemoved(NetworkId id) {
907   QAction *action = findChild<QAction *>(QString("NetworkAction-%1").arg(id.toInt()));
908   if(!action)
909     return;
910
911   action->deleteLater();
912 }
913
914 void MainWin::connectOrDisconnectFromNet() {
915   QAction *act = qobject_cast<QAction *>(sender());
916   if(!act) return;
917   const Network *net = Client::network(act->data().value<NetworkId>());
918   if(!net) return;
919   if(net->connectionState() == Network::Disconnected) net->requestConnect();
920   else net->requestDisconnect();
921 }
922
923 void MainWin::on_actionDebugNetworkModel_triggered() {
924   QTreeView *view = new QTreeView;
925   view->setAttribute(Qt::WA_DeleteOnClose);
926   view->setWindowTitle("Debug NetworkModel View");
927   view->setModel(Client::networkModel());
928   view->setColumnWidth(0, 250);
929   view->setColumnWidth(1, 250);
930   view->setColumnWidth(2, 80);
931   view->resize(610, 300);
932   view->show();
933 }
934
935 void MainWin::on_actionDebugBufferViewOverlay_triggered() {
936   QTreeView *view = new QTreeView;
937   view->setAttribute(Qt::WA_DeleteOnClose);
938   view->setWindowTitle("Debug BufferViewOverlay View");
939   BufferViewOverlayFilter *filter = new BufferViewOverlayFilter(Client::bufferModel(), Client::bufferViewOverlay());
940   filter->setParent(view);
941   view->setModel(filter);
942   view->setColumnWidth(0, 250);
943   view->setColumnWidth(1, 250);
944   view->setColumnWidth(2, 80);
945   view->resize(610, 300);
946   view->show();
947 }
948
949 void MainWin::on_actionDebugMessageModel_triggered() {
950   QTableView *view = new QTableView(0);
951   DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
952   filter->setSourceModel(Client::messageModel());
953   view->setModel(filter);
954   view->setAttribute(Qt::WA_DeleteOnClose, true);
955   view->verticalHeader()->hide();
956   view->horizontalHeader()->setStretchLastSection(true);
957   view->show();
958 }
959
960 void MainWin::on_actionDebugLog_triggered() {
961   DebugLogWidget *logWidget = new DebugLogWidget(0);
962   logWidget->show();
963 }
964
965 void MainWin::saveStateToSession(const QString &sessionId) {
966   return;
967   SessionSettings s(sessionId);
968
969   s.setValue("MainWinSize", size());
970   s.setValue("MainWinPos", pos());
971   s.setValue("MainWinState", saveState());
972 }
973
974 void MainWin::saveStateToSessionSettings(SessionSettings & s)
975 {
976   s.setValue("MainWinSize", size());
977   s.setValue("MainWinPos", pos());
978   s.setValue("MainWinState", saveState());
979 }
980
981 void MainWin::showStatusBarMessage(const QString &message) {
982   statusBar()->showMessage(message, 10000);
983 }
984