1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QVBoxLayout>
23 #include <KNotification>
24 #include <KNotifyConfigWidget>
25 #include <QTextDocument>
27 #include "knotificationbackend.h"
31 #include "iconloader.h"
32 #include "networkmodel.h"
34 #include "systemtray.h"
36 KNotificationBackend::KNotificationBackend(QObject *parent)
37 : AbstractNotificationBackend(parent),
38 _lastNotificationId(0)
40 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
41 SLOT(notificationActivated(QSystemTrayIcon::ActivationReason)));
44 void KNotificationBackend::notify(const Notification &n) {
48 type = "Highlight"; break;
49 case HighlightFocused:
50 type = "HighlightFocused"; break;
52 type = "PrivMsg"; break;
54 type = "PrivMsgFocused"; break;
57 QString message = QString("<b><%1></b> %2").arg(n.sender, Qt::escape(n.message));
58 KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
59 KNotification::RaiseWidgetOnActivation
60 |KNotification::CloseWhenWidgetActivated
61 |KNotification::CloseOnTimeout);
62 connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
63 connect(notification, SIGNAL(closed()), SLOT(notificationClosed()));
64 notification->setActions(QStringList("View"));
65 _notificationIds[notification] = _lastNotificationId = n.notificationId;
67 QtUi::mainWindow()->systemTray()->setAlert(true);
70 void KNotificationBackend::removeNotificationById(uint notificationId) {
71 QHash<KNotification *, uint>::iterator i = _notificationIds.begin();
72 while(i != _notificationIds.end()) {
73 if(i.value() == notificationId)
74 i = _notificationIds.erase(i);
78 if(_lastNotificationId == notificationId)
79 _lastNotificationId = 0;
82 void KNotificationBackend::close(uint notificationId) {
83 removeNotificationById(notificationId);
84 if(!_notificationIds.count())
85 QtUi::mainWindow()->systemTray()->setAlert(false);
88 void KNotificationBackend::notificationActivated() {
90 KNotification *n = qobject_cast<KNotification *>(sender());
91 if(n && _notificationIds.contains(n))
92 id = _notificationIds.value(n);
94 notificationActivated(id);
97 void KNotificationBackend::notificationActivated(QSystemTrayIcon::ActivationReason reason) {
98 if(reason == QSystemTrayIcon::Trigger && _lastNotificationId > 0) {
99 notificationActivated(_lastNotificationId); // most recent one
103 void KNotificationBackend::notificationActivated(uint notificationId) {
104 removeNotificationById(notificationId);
106 QtUi::mainWindow()->systemTray()->setInhibitActivation();
107 emit activated(notificationId);
109 if(!_notificationIds.count())
110 QtUi::mainWindow()->systemTray()->setAlert(false);
114 void KNotificationBackend::notificationClosed() {
115 //KNotification *n = qobject_cast<KNotification *>(sender());
116 //if(n && _notificationIds.contains(n))
117 // _notificationIds.remove(n);
120 SettingsPage *KNotificationBackend::createConfigWidget() const {
121 return new ConfigWidget();
124 /***************************************************************************/
126 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
127 _widget = new KNotifyConfigWidget(this);
128 _widget->setApplication("quassel");
130 QVBoxLayout *layout = new QVBoxLayout(this);
131 layout->addWidget(_widget);
133 connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
136 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
137 setChangedState(changed);
140 void KNotificationBackend::ConfigWidget::load() {
141 setChangedState(false);
144 void KNotificationBackend::ConfigWidget::save() {