1 /***************************************************************************
2 * Copyright (C) 2005-2013 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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)));
45 void KNotificationBackend::notify(const Notification &n)
50 type = "Highlight"; break;
51 case HighlightFocused:
52 type = "HighlightFocused"; break;
54 type = "PrivMsg"; break;
56 type = "PrivMsgFocused"; break;
59 QString message = QString("<b><%1></b> %2").arg(n.sender, Qt::escape(n.message));
60 KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
61 KNotification::RaiseWidgetOnActivation
62 |KNotification::CloseWhenWidgetActivated
63 |KNotification::CloseOnTimeout);
64 connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
65 notification->setActions(QStringList("View"));
66 notification->setProperty("notificationId", n.notificationId);
68 _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
71 QtUi::mainWindow()->systemTray()->setAlert(true);
75 void KNotificationBackend::removeNotificationById(uint notificationId)
77 QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
78 while (i != _notifications.end()) {
79 if (i->first == notificationId) {
82 i = _notifications.erase(i);
91 void KNotificationBackend::close(uint notificationId)
93 removeNotificationById(notificationId);
94 //if(!_notifications.count()) // FIXME make configurable
95 QtUi::mainWindow()->systemTray()->setAlert(false);
99 void KNotificationBackend::notificationActivated()
102 KNotification *n = qobject_cast<KNotification *>(sender());
104 id = n->property("notificationId").toUInt();
106 notificationActivated(id);
110 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
112 if (reason == SystemTray::Trigger) {
113 if (_notifications.count())
114 notificationActivated(_notifications.first().first); // oldest one
116 GraphicalUi::toggleMainWidget();
121 void KNotificationBackend::notificationActivated(uint notificationId)
123 emit activated(notificationId);
127 void KNotificationBackend::updateToolTip()
129 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
130 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count()) : QString());
134 SettingsPage *KNotificationBackend::createConfigWidget() const
136 return new ConfigWidget();
140 /***************************************************************************/
142 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent)
144 _widget = new KNotifyConfigWidget(this);
145 _widget->setApplication("quassel");
147 QVBoxLayout *layout = new QVBoxLayout(this);
148 layout->addWidget(_widget);
150 connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
154 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed)
156 setChangedState(changed);
160 void KNotificationBackend::ConfigWidget::load()
162 setChangedState(false);
166 void KNotificationBackend::ConfigWidget::save()