File indexing completed on 2024-06-16 04:29:30
0001 /* 0002 SnoreNotify is a Notification Framework based on Qt 0003 Copyright (C) 2013-2015 Hannah von Reth <vonreth@kde.org> 0004 0005 SnoreNotify is free software: you can redistribute it and/or modify 0006 it under the terms of the GNU Lesser General Public License as published by 0007 the Free Software Foundation, either version 3 of the License, or 0008 (at your option) any later version. 0009 0010 SnoreNotify is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 GNU Lesser General Public License for more details. 0014 0015 You should have received a copy of the GNU Lesser General Public License 0016 along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>. 0017 */ 0018 0019 #include "freedesktopnotificationfrontend.h" 0020 #include "notificationsadaptor.h" 0021 0022 #include "plugins/backends/freedesktop_backend/fredesktopnotification.h" 0023 #include "libsnore/snore.h" 0024 #include "libsnore/version.h" 0025 #include "libsnore/notification/notification_p.h" 0026 0027 #include <QImage> 0028 #include <QIcon> 0029 0030 using namespace Snore; 0031 0032 FreedesktopFrontend::FreedesktopFrontend() 0033 { 0034 connect(this, &FreedesktopFrontend::enabledChanged, [this](bool enabled) { 0035 if (enabled) { 0036 m_adaptor = new NotificationsAdaptor(this); 0037 QDBusConnection dbus = QDBusConnection::sessionBus(); 0038 if (dbus.registerService(QStringLiteral("org.freedesktop.Notifications"))) { 0039 if (!dbus.registerObject(QStringLiteral("/org/freedesktop/Notifications"), this)) { 0040 setErrorString(tr("Failed to register dbus object.")); 0041 } 0042 } else { 0043 setErrorString(tr("Failed to register dbus service.")); 0044 } 0045 } else { 0046 QDBusConnection dbus = QDBusConnection::sessionBus(); 0047 dbus.unregisterService(QStringLiteral("org.freedesktop.Notifications")); 0048 dbus.unregisterObject(QStringLiteral("/org/freedesktop/Notifications")); 0049 m_adaptor->deleteLater(); 0050 m_adaptor = nullptr; 0051 } 0052 }); 0053 } 0054 0055 void FreedesktopFrontend::slotActionInvoked(Notification notification) 0056 { 0057 if (notification.isActiveIn(this)) { 0058 if (notification.actionInvoked().isValid()) { 0059 emit ActionInvoked(notification.id(), QString::number(notification.actionInvoked().id())); 0060 } 0061 } 0062 } 0063 0064 void FreedesktopFrontend::slotNotificationClosed(Notification notification) 0065 { 0066 if (notification.removeActiveIn(this)) { 0067 emit NotificationClosed(notification.id(), notification.closeReason()); 0068 } 0069 } 0070 0071 uint FreedesktopFrontend::Notify(const QString &app_name, uint replaces_id, 0072 const QString &app_icon, const QString &summary, const QString &body, 0073 const QStringList &actions, const QVariantMap &hints, int timeout) 0074 { 0075 Application app; 0076 Notification::Prioritys priotity = Notification::Normal; 0077 0078 if (!SnoreCore::instance().aplications().contains(app_name)) { 0079 Icon appIcon(QIcon::fromTheme(app_icon, QIcon(QStringLiteral(":/root/snore.png")))); 0080 app = Application(app_name, appIcon); 0081 app.hints().setValue("use-markup", true); 0082 SnoreCore::instance().registerApplication(app); 0083 } else { 0084 app = SnoreCore::instance().aplications()[app_name]; 0085 } 0086 0087 Icon icon = app.icon(); 0088 if (hints.contains(QStringLiteral("image_data"))) { 0089 FreedesktopImageHint image; 0090 hints.value(QStringLiteral("image_data")).value<QDBusArgument>() >> image; 0091 icon = Icon(QPixmap::fromImage(image.toQImage())); 0092 } 0093 0094 if (hints.contains(QStringLiteral("urgency"))) { 0095 priotity = Notification::Prioritys(hints.value(QStringLiteral("urgency")).toInt() - 1); 0096 } 0097 0098 Notification noti; 0099 Notification toReplace = SnoreCore::instance().getActiveNotificationByID(replaces_id); 0100 if (replaces_id != 0 && toReplace.isValid()) { 0101 noti = Notification(toReplace, summary, body, icon, timeout == -1 ? Notification::defaultTimeout() : timeout / 1000, priotity); 0102 } else { 0103 noti = Notification(app, app.defaultAlert(), summary, body, icon, timeout == -1 ? Notification::defaultTimeout() : timeout / 1000, priotity); 0104 } 0105 for (int i = 0; i < actions.length(); i += 2) { 0106 noti.addAction(Action(actions.at(i).toInt(), actions.at(i + 1))); 0107 } 0108 0109 noti.addActiveIn(this); 0110 noti.data()->setSource(this); 0111 SnoreCore::instance().broadcastNotification(noti); 0112 return noti.id(); 0113 } 0114 0115 void FreedesktopFrontend::CloseNotification(uint id) 0116 { 0117 Notification noti = SnoreCore::instance().getActiveNotificationByID(id); 0118 if (noti.isValid()) { 0119 SnoreCore::instance().requestCloseNotification(noti, Notification::TimedOut); 0120 } 0121 } 0122 0123 QStringList FreedesktopFrontend::GetCapabilities() 0124 { 0125 return QStringList({ QStringLiteral("body"), 0126 QStringLiteral("urgency"), 0127 QStringLiteral("body-hyperlinks"), 0128 QStringLiteral("body-markup"), 0129 QStringLiteral("icon-static"), 0130 QStringLiteral("actions") 0131 }); 0132 } 0133 0134 QString FreedesktopFrontend::GetServerInformation(QString &vendor, QString &version, QString &specVersion) 0135 { 0136 vendor = QStringLiteral("SnoreNotify"); 0137 version = Version::version(); 0138 specVersion = QStringLiteral("0.9"); 0139 return vendor; 0140 }