1 /***************************************************************************
2 * Copyright (C) 2005-2010 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 <KNotification>
22 #include <KNotifyConfigWidget>
23 #include <QTextDocument>
24 #include <QVBoxLayout>
26 #include "knotificationbackend.h"
30 #include "iconloader.h"
32 #include "networkmodel.h"
35 KNotificationBackend::KNotificationBackend(QObject *parent)
36 : AbstractNotificationBackend(parent)
38 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
39 SLOT(notificationActivated(SystemTray::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 notification->setActions(QStringList("View"));
64 notification->setProperty("notificationId", n.notificationId);
66 _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
69 QtUi::mainWindow()->systemTray()->setAlert(true);
72 void KNotificationBackend::removeNotificationById(uint notificationId) {
73 QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
74 while(i != _notifications.end()) {
75 if(i->first == notificationId) {
78 i = _notifications.erase(i);
85 void KNotificationBackend::close(uint notificationId) {
86 removeNotificationById(notificationId);
87 if(!_notifications.count())
88 QtUi::mainWindow()->systemTray()->setAlert(false);
91 void KNotificationBackend::notificationActivated() {
93 KNotification *n = qobject_cast<KNotification *>(sender());
95 id = n->property("notificationId").toUInt();
97 notificationActivated(id);
100 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
101 if(reason == SystemTray::Trigger) {
102 if( _notifications.count())
103 notificationActivated(_notifications.first().first); // oldest one
105 GraphicalUi::toggleMainWidget();
109 void KNotificationBackend::notificationActivated(uint notificationId) {
110 emit activated(notificationId);
113 void KNotificationBackend::updateToolTip() {
114 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
115 _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
118 SettingsPage *KNotificationBackend::createConfigWidget() const {
119 return new ConfigWidget();
122 /***************************************************************************/
124 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
125 _widget = new KNotifyConfigWidget(this);
126 _widget->setApplication("quassel");
128 QVBoxLayout *layout = new QVBoxLayout(this);
129 layout->addWidget(_widget);
131 connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
134 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
135 setChangedState(changed);
138 void KNotificationBackend::ConfigWidget::load() {
139 setChangedState(false);
142 void KNotificationBackend::ConfigWidget::save() {