1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 "knotificationbackend.h"
23 #include <QTextDocument>
24 #include <QVBoxLayout>
26 #include <KNotifications/KNotification>
27 #include <KNotifyConfig/KNotifyConfigWidget>
28 #include <knotifications_version.h>
33 #include "networkmodel.h"
37 KNotificationBackend::KNotificationBackend(QObject* parent)
38 : AbstractNotificationBackend(parent)
40 connect(QtUi::mainWindow()->systemTray(),
41 &SystemTray::activated,
43 selectOverload<SystemTray::ActivationReason>(&KNotificationBackend::notificationActivated));
48 void KNotificationBackend::notify(const Notification& n)
55 case HighlightFocused:
56 type = "HighlightFocused";
62 type = "PrivMsgFocused";
66 QString message = QString("<b><%1></b> %2").arg(n.sender, n.message.toHtmlEscaped());
67 KNotification* notification = KNotification::event(type,
69 QStringLiteral("dialog-information"),
71 KNotification::RaiseWidgetOnActivation | KNotification::CloseWhenWidgetActivated
72 | KNotification::CloseOnTimeout);
74 selectOverload<uint>(&KNotification::activated),
76 selectOverload<>(&KNotificationBackend::notificationActivated));
77 #if KNOTIFICATIONS_VERSION >= QT_VERSION_CHECK(5,31,0)
78 notification->setDefaultAction(tr("View"));
80 notification->setActions(QStringList{tr("View")});
82 notification->setProperty("notificationId", n.notificationId);
84 _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
89 void KNotificationBackend::removeNotificationById(uint notificationId)
91 QList<QPair<uint, QPointer<KNotification>>>::iterator i = _notifications.begin();
92 while (i != _notifications.end()) {
93 if (i->first == notificationId) {
96 i = _notifications.erase(i);
104 void KNotificationBackend::close(uint notificationId)
106 removeNotificationById(notificationId);
107 // if(!_notifications.count()) // FIXME make configurable
110 void KNotificationBackend::notificationActivated()
113 KNotification* n = qobject_cast<KNotification*>(sender());
115 id = n->property("notificationId").toUInt();
117 notificationActivated(id);
120 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason)
122 if (reason == SystemTray::Trigger) {
123 if (_notifications.count())
124 notificationActivated(_notifications.first().first); // oldest one
126 GraphicalUi::toggleMainWidget();
130 void KNotificationBackend::notificationActivated(uint notificationId)
132 emit activated(notificationId);
135 void KNotificationBackend::updateToolTip()
137 QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
138 _notifications.count() ? tr("%n pending highlight(s)", "", _notifications.count())
142 SettingsPage* KNotificationBackend::createConfigWidget() const
144 return new ConfigWidget();
147 /***************************************************************************/
149 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget* parent)
150 : SettingsPage("Internal", "KNotification", parent)
152 _widget = new KNotifyConfigWidget(this);
153 _widget->setApplication("quassel");
155 QVBoxLayout* layout = new QVBoxLayout(this);
156 layout->addWidget(_widget);
158 connect(_widget, &KNotifyConfigWidget::changed, this, &ConfigWidget::widgetChanged);
161 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed)
163 setChangedState(changed);
166 void KNotificationBackend::ConfigWidget::load()
168 setChangedState(false);
171 void KNotificationBackend::ConfigWidget::save()