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"
31 #include "networkmodel.h"
34 KNotificationBackend::KNotificationBackend(QObject *parent)
35 : AbstractNotificationBackend(parent)
37 connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
38 SLOT(notificationActivated(SystemTray::ActivationReason)));
41 void KNotificationBackend::notify(const Notification &n) {
45 type = "Highlight"; break;
46 case HighlightFocused:
47 type = "HighlightFocused"; break;
49 type = "PrivMsg"; break;
51 type = "PrivMsgFocused"; break;
54 QString message = QString("<b><%1></b> %2").arg(n.sender, Qt::escape(n.message));
55 KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
56 KNotification::RaiseWidgetOnActivation
57 |KNotification::CloseWhenWidgetActivated
58 |KNotification::CloseOnTimeout);
59 connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
60 notification->setActions(QStringList("View"));
61 notification->setProperty("notificationId", n.notificationId);
63 _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
65 QtUi::mainWindow()->systemTray()->setAlert(true);
68 void KNotificationBackend::removeNotificationById(uint notificationId) {
69 QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
70 while(i != _notifications.end()) {
71 if(i->first == notificationId) {
74 i = _notifications.erase(i);
80 void KNotificationBackend::close(uint notificationId) {
81 removeNotificationById(notificationId);
82 if(!_notifications.count())
83 QtUi::mainWindow()->systemTray()->setAlert(false);
86 void KNotificationBackend::notificationActivated() {
88 KNotification *n = qobject_cast<KNotification *>(sender());
90 id = n->property("notificationId").toUInt();
92 notificationActivated(id);
95 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
96 if(reason == SystemTray::Trigger && _notifications.count()) {
97 notificationActivated(_notifications.first().first); // oldest one
101 void KNotificationBackend::notificationActivated(uint notificationId) {
102 QtUi::mainWindow()->systemTray()->setInhibitActivation();
103 emit activated(notificationId);
107 SettingsPage *KNotificationBackend::createConfigWidget() const {
108 return new ConfigWidget();
111 /***************************************************************************/
113 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
114 _widget = new KNotifyConfigWidget(this);
115 _widget->setApplication("quassel");
117 QVBoxLayout *layout = new QVBoxLayout(this);
118 layout->addWidget(_widget);
120 connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
123 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
124 setChangedState(changed);
127 void KNotificationBackend::ConfigWidget::load() {
128 setChangedState(false);
131 void KNotificationBackend::ConfigWidget::save() {