2
This program is free software; you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation; either version 2 of the License, or
5
(at your option) any later version.
9
copyright: (C) 2004 by Peter Simonsson
13
#include "notificationhandler.h"
15
#include "chatwindow.h"
16
#include "application.h"
17
#include "mainwindow.h"
18
#include "viewcontainer.h"
21
#include "config-konversation.h"
23
#include <QTextDocument>
25
#include <KNotification>
26
#include <KWindowSystem>
28
#ifdef HAVE_INDICATEQT
29
#include <qindicateindicator.h>
30
#include <qindicateserver.h>
32
class Indicator : public QIndicate::Indicator
35
Indicator(QIndicate::Server* server)
36
: QIndicate::Indicator(server)
39
QPointer<ChatWindow> m_chatWin;
43
namespace Konversation
46
NotificationHandler::NotificationHandler(Application* parent)
49
m_mainWindow = parent->getMainWindow();
50
#ifdef HAVE_INDICATEQT
51
m_indicateServer = QIndicate::Server::defaultInstance();
52
m_indicateServer->setType("message.irc");
53
QString appName = KGlobal::mainComponent().componentName();
54
KService::Ptr service = KService::serviceByDesktopName(appName);
56
m_indicateServer->setDesktopFile(service->entryPath());
58
kWarning() << "Could not find desktop file for application";
60
connect(m_indicateServer, SIGNAL(serverDisplay()),
61
SLOT(slotIndicateServerDisplay()));
62
updateIndicateServer();
63
m_mainWindow->installEventFilter(this);
64
connect(Preferences::self(), SIGNAL(configChanged()),
65
SLOT(updateIndicateServer()));
69
NotificationHandler::~NotificationHandler()
73
void NotificationHandler::message(ChatWindow* chatWin, const QString& fromNick, const QString& message)
75
if (!chatWin || !chatWin->notificationsEnabled())
78
if (Preferences::self()->messageIndicatorShowChannel())
79
addIndicator(chatWin, fromNick);
81
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
84
QString cleanedMessage = removeIrcMarkup(message);
85
QString forKNotify = Qt::escape(cleanedMessage);
87
KNotification::event(QString::fromLatin1("message"), QString("<html><%1> %2</html>").arg(fromNick).arg(forKNotify), QPixmap(), m_mainWindow);
89
if(!Preferences::self()->trayNotifyOnlyOwnNick())
91
startTrayNotification(chatWin);
95
if(Preferences::self()->oSDShowChannel() &&
96
(!m_mainWindow->isActiveWindow() || (chatWin != m_mainWindow->getViewContainer()->getFrontView())))
98
Application* konvApp = static_cast<Application*>(kapp);
99
konvApp->osd->show('(' + chatWin->getName() + ") <" + fromNick + "> " + cleanedMessage);
104
void NotificationHandler::nick(ChatWindow* chatWin, const QString& fromNick, const QString& message)
106
if (!chatWin || !chatWin->notificationsEnabled())
109
if (Preferences::self()->messageIndicatorShowOwnNick())
110
addIndicator(chatWin, fromNick);
112
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
115
QString cleanedMessage = removeIrcMarkup(message);
116
QString forKNotify = Qt::escape(cleanedMessage);
118
KNotification::event(QString::fromLatin1("nick"), QString("<html><%1> %2</html>").arg(fromNick).arg(forKNotify), QPixmap(), m_mainWindow);
120
startTrayNotification(chatWin);
122
Application* konvApp = static_cast<Application*>(kapp);
124
if((Preferences::self()->oSDShowChannel() || Preferences::self()->oSDShowOwnNick()) &&
125
(!m_mainWindow->isActiveWindow() ||
126
(chatWin != m_mainWindow->getViewContainer()->getFrontView())))
128
konvApp->osd->show(i18n("[HighLight] (%1) <%2> %3",chatWin->getName(),fromNick,cleanedMessage));
132
void NotificationHandler::queryMessage(ChatWindow* chatWin,
133
const QString& fromNick, const QString& message)
135
if (!chatWin || !chatWin->notificationsEnabled())
138
if (Preferences::self()->messageIndicatorShowQuery())
139
addIndicator(chatWin, fromNick);
141
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
144
QString cleanedMessage = removeIrcMarkup(message);
145
QString forKNotify = Qt::escape(cleanedMessage);
147
KNotification::event(QString::fromLatin1("queryMessage"), QString("<html><%1> %2</html>").arg(fromNick).arg(forKNotify), QPixmap(), m_mainWindow);
149
startTrayNotification(chatWin);
151
Application* konvApp = static_cast<Application*>(kapp);
153
if(Preferences::self()->oSDShowQuery() && (!m_mainWindow->isActiveWindow() ||
154
(chatWin != m_mainWindow->getViewContainer()->getFrontView())))
156
konvApp->osd->show(i18n("[Query] <%1> %2",fromNick,cleanedMessage));
160
void NotificationHandler::startTrayNotification(ChatWindow* chatWin)
162
if (!chatWin || !chatWin->notificationsEnabled())
165
if (!chatWin->getServer() || (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer()->isAway()))
168
if (!m_mainWindow->isActiveWindow() && chatWin->getServer()->isConnected() && m_mainWindow->systemTrayIcon())
169
m_mainWindow->systemTrayIcon()->startNotification();
173
void NotificationHandler::join(ChatWindow* chatWin, const QString& nick)
175
if (!chatWin || !chatWin->notificationsEnabled())
178
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
181
KNotification::event(QString::fromLatin1("join"), i18n("%1 joined %2",nick, chatWin->getName()), QPixmap(), m_mainWindow);
184
if(Preferences::self()->oSDShowChannelEvent() &&
185
(!m_mainWindow->isActiveWindow() || (chatWin != m_mainWindow->getViewContainer()->getFrontView())))
187
Application* konvApp = static_cast<Application*>(kapp);
188
konvApp->osd->show(i18n("%1 joined %2",nick, chatWin->getName()));
192
void NotificationHandler::part(ChatWindow* chatWin, const QString& nick)
194
if (!chatWin || !chatWin->notificationsEnabled())
197
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
200
KNotification::event(QString::fromLatin1("part"), i18n("%1 parted %2",nick, chatWin->getName()), QPixmap(), m_mainWindow);
203
if(Preferences::self()->oSDShowChannelEvent() &&
204
(!m_mainWindow->isActiveWindow() || (chatWin != m_mainWindow->getViewContainer()->getFrontView())))
206
Application* konvApp = static_cast<Application*>(kapp);
207
konvApp->osd->show(i18n("%1 parted %2",nick, chatWin->getName()));
211
void NotificationHandler::quit(ChatWindow* chatWin, const QString& nick)
213
if (!chatWin || !chatWin->notificationsEnabled())
216
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
219
KNotification::event(QString::fromLatin1("part"), i18n("%1 quit %2",nick, chatWin->getServer()->getServerName()), QPixmap(), m_mainWindow);
222
void NotificationHandler::nickChange(ChatWindow* chatWin, const QString& oldNick, const QString& newNick)
224
if (!chatWin || !chatWin->notificationsEnabled())
227
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
230
KNotification::event(QString::fromLatin1("nickchange"), i18n("%1 changed nickname to %2",oldNick, newNick), QPixmap(), m_mainWindow);
233
void NotificationHandler::dccIncoming(ChatWindow* chatWin, const QString& fromNick)
235
if (!chatWin || !chatWin->notificationsEnabled())
238
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
241
KNotification::event(QString::fromLatin1("dcc_incoming"), i18n("%1 wants to send a file to you",fromNick), QPixmap(), m_mainWindow);
244
void NotificationHandler::dccError(ChatWindow* chatWin, const QString& error)
246
if (!chatWin || !chatWin->notificationsEnabled())
249
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
252
KNotification::event(QString::fromLatin1("dcc_error"), i18n("An Error has occurred in a DCC transfer: %1",error), QPixmap(), m_mainWindow);
255
void NotificationHandler::dccTransferDone(ChatWindow* chatWin, const QString& file)
257
if (!chatWin || !chatWin->notificationsEnabled())
260
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
263
KNotification::event(QString::fromLatin1("dcctransfer_done"), i18nc("%1 - filename","%1 File Transfer is complete",file), QPixmap(), m_mainWindow);
266
void NotificationHandler::mode(ChatWindow* chatWin, const QString& /*nick*/)
268
if (!chatWin || !chatWin->notificationsEnabled())
271
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
274
KNotification *ev=new KNotification("mode", m_mainWindow);
278
void NotificationHandler::query(ChatWindow* chatWin, const QString& fromNick)
280
if (!chatWin || !chatWin->notificationsEnabled())
283
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
286
startTrayNotification(chatWin);
288
KNotification *ev=new KNotification("query", m_mainWindow);
289
ev->setText(i18n("%1 has started a conversation (query) with you.",fromNick));
293
void NotificationHandler::nickOnline(ChatWindow* chatWin, const QString& nick)
295
if (!chatWin || !chatWin->notificationsEnabled())
298
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
301
KNotification *ev=new KNotification("notify", m_mainWindow);
302
ev->setText(i18n("%1 is online (%2).", nick, chatWin->getServer()->getServerName()));
307
void NotificationHandler::nickOffline(ChatWindow* chatWin, const QString& nick)
309
if (!chatWin || !chatWin->notificationsEnabled())
312
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
315
KNotification *ev=new KNotification("notify", m_mainWindow);
316
ev->setText(i18n("%1 went offline (%2).", nick, chatWin->getServer()->getServerName()));
321
void NotificationHandler::kick(ChatWindow* chatWin, const QString& channel,const QString& nick)
323
if (!chatWin || !chatWin->notificationsEnabled())
326
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
329
KNotification *ev=new KNotification("kick", m_mainWindow);
330
ev->setText(i18n("You are kicked by %1 from %2", nick, channel));
335
void NotificationHandler::dccChat(ChatWindow* chatWin, const QString& nick)
337
if (!chatWin || !chatWin->notificationsEnabled())
340
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
343
KNotification *ev=new KNotification("dccChat", m_mainWindow);
344
ev->setText(i18n("%1 started a dcc chat with you", nick));
349
void NotificationHandler::highlight(ChatWindow* chatWin, const QString& fromNick, const QString& message)
351
if (!chatWin || !chatWin->notificationsEnabled())
354
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
357
startTrayNotification(chatWin);
359
QString cleanedMessage = removeIrcMarkup(message);
360
QString forKNotify = Qt::escape(cleanedMessage);
362
if(fromNick.isEmpty())
363
KNotification::event(QString::fromLatin1("highlight"), QString("<html>(%1) *** %2</html>").arg(chatWin->getName()).arg(forKNotify), QPixmap(), m_mainWindow);
365
KNotification::event(QString::fromLatin1("highlight"), QString("<html>(%1) <%2> %3</html>").arg(chatWin->getName()).arg(fromNick).arg(forKNotify), QPixmap(), m_mainWindow);
367
if(Preferences::self()->oSDShowOwnNick() &&
368
(!m_mainWindow->isActiveWindow() || (chatWin != m_mainWindow->getViewContainer()->getFrontView())))
370
Application* konvApp = static_cast<Application*>(kapp);
371
// if there was no nick associated, this must be a command message, so don't try displaying
372
// an empty nick in <>
373
if(fromNick.isEmpty())
374
konvApp->osd->show(i18n("[HighLight] (%1) *** %2",chatWin->getName(),cleanedMessage));
375
// normal highlight message
377
konvApp->osd->show(i18n("[HighLight] (%1) <%2> %3",chatWin->getName(),fromNick,cleanedMessage));
381
void NotificationHandler::connectionFailure(ChatWindow* chatWin, const QString& server)
383
if (!chatWin || !chatWin->notificationsEnabled())
386
KNotification *ev=new KNotification("connectionFailure", m_mainWindow);
387
ev->setText(i18n("Failed to connect to %1", server));
392
void NotificationHandler::channelJoin(ChatWindow* chatWin, const QString& channel)
394
if (!chatWin || !chatWin->notificationsEnabled())
397
if (Preferences::self()->disableNotifyWhileAway() && chatWin->getServer() && chatWin->getServer()->isAway())
400
KNotification::event(QString::fromLatin1("channelJoin"), i18n("You have joined %1.",channel), QPixmap(), m_mainWindow);
403
void NotificationHandler::addIndicator(ChatWindow* chatWin, const QString& nick)
405
#ifdef HAVE_INDICATEQT
406
ViewContainer* viewContainer = m_mainWindow->getViewContainer();
407
if (m_mainWindow->isActiveWindow() && chatWin == viewContainer->getFrontView()) {
408
// Do not show indicator if the user is already using this chat
412
Indicator* indicator = m_indicatorForChatWindow.value(chatWin);
414
indicator = new Indicator(m_indicateServer);
415
indicator->m_chatWin = chatWin;
416
m_indicatorForChatWindow.insert(chatWin, indicator);
417
connect(chatWin, SIGNAL(closing(ChatWindow*)), SLOT(deleteIndicatorForChatWindow(ChatWindow*)));
418
connect(viewContainer, SIGNAL(viewChanged(ChatWindow*)), SLOT(deleteIndicatorForChatWindow(ChatWindow*)));
420
connect(indicator, SIGNAL(display(QIndicate::Indicator*)), SLOT(slotIndicatorDisplay(QIndicate::Indicator*)));
424
if (chatWin->getType() == ChatWindow::Query) {
427
name = QString("%1 (%2)").arg(chatWin->getName()).arg(nick);
429
indicator->setNameProperty(name);
430
indicator->setTimeProperty(QDateTime::currentDateTime());
431
indicator->setDrawAttentionProperty(true);
438
void NotificationHandler::slotIndicatorDisplay(QIndicate::Indicator* _indicator)
440
#ifdef HAVE_INDICATEQT
441
Indicator* indicator = static_cast<Indicator*>(_indicator);
442
ChatWindow* chatWin = indicator->m_chatWin;
443
m_indicatorForChatWindow.remove(chatWin);
446
m_mainWindow->show();
447
KWindowSystem::forceActiveWindow(m_mainWindow->winId());
449
m_mainWindow->getViewContainer()->showView(chatWin);
450
chatWin->adjustFocus();
453
Q_UNUSED(_indicator);
457
void NotificationHandler::slotIndicateServerDisplay()
459
m_mainWindow->show();
460
KWindowSystem::forceActiveWindow(m_mainWindow->winId());
463
void NotificationHandler::deleteIndicatorForChatWindow(ChatWindow* chatWin)
465
#ifdef HAVE_INDICATEQT
466
delete m_indicatorForChatWindow.take(chatWin);
472
bool NotificationHandler::eventFilter(QObject*, QEvent* event)
474
#ifdef HAVE_INDICATEQT
475
if (event->type() == QEvent::WindowActivate) {
476
ViewContainer* viewContainer = m_mainWindow->getViewContainer();
477
QPointer<ChatWindow> chatWin = viewContainer->getFrontView();
479
deleteIndicatorForChatWindow(chatWin);
488
void NotificationHandler::updateIndicateServer()
490
#ifdef HAVE_INDICATEQT
491
bool needServer = Preferences::self()->messageIndicatorShowOwnNick()
492
|| Preferences::self()->messageIndicatorShowChannel()
493
|| Preferences::self()->messageIndicatorShowQuery();
496
m_indicateServer->show();
498
m_indicateServer->hide();
504
#include "notificationhandler.moc"