File indexing completed on 2024-04-21 03:56:29
0001 /* 0002 SPDX-FileCopyrightText: 2014 Martin Klapetek <mklapetek@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <QTimer> 0008 #include <knotification.h> 0009 0010 #include <QDBusConnection> 0011 #include <QDBusMessage> 0012 #include <QDebug> 0013 #include <QGuiApplication> 0014 0015 void notificationDBusCall(const QString &iconName, const QString &title, const QString &body, const QStringList &actions, bool persistent = false) 0016 { 0017 QDBusMessage dbusNotificationMessage = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"), 0018 QStringLiteral("/org/freedesktop/Notifications"), 0019 QStringLiteral("org.freedesktop.Notifications"), 0020 QStringLiteral("Notify")); 0021 0022 QList<QVariant> args; 0023 0024 args.append(QString()); // app_name 0025 args.append((uint)0); // notification to update 0026 args.append(iconName); // app_icon 0027 args.append(title); // summary 0028 args.append(body); // body 0029 0030 QStringList actionList; 0031 int actId = 0; 0032 for (const QString &actionName : actions) { 0033 actId++; 0034 actionList.append(QString::number(actId)); 0035 actionList.append(actionName); 0036 } 0037 0038 args.append(actionList); // actions 0039 0040 args.append(QVariantMap()); // hints 0041 0042 // Persistent => 0 == infinite timeout 0043 // CloseOnTimeout => -1 == let the server decide 0044 int timeout = persistent ? 0 : -1; 0045 0046 args.append(timeout); // expire timeout 0047 0048 dbusNotificationMessage.setArguments(args); 0049 0050 QDBusMessage reply = QDBusConnection::sessionBus().call(dbusNotificationMessage, QDBus::Block, 4000); 0051 if (reply.type() == QDBusMessage::ErrorMessage) { 0052 qDebug() << "Error sending notification:" << reply.errorMessage(); 0053 } 0054 } 0055 0056 int main(int argc, char *argv[]) 0057 { 0058 QGuiApplication app(argc, argv); 0059 0060 notificationDBusCall(QStringLiteral("amarok"), 0061 QStringLiteral("Testing notification #1"), 0062 QStringLiteral("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In condimentum"), 0063 QStringList() << QStringLiteral("action1") << QStringLiteral("action2")); 0064 0065 // wait a little before sending another notification 0066 QEventLoop a; 0067 QTimer::singleShot(500, &a, &QEventLoop::quit); 0068 a.exec(); 0069 0070 notificationDBusCall(QStringLiteral("kwalletmanager"), 0071 QStringLiteral("Testing notification #2"), 0072 QStringLiteral("Praesent odio ipsum, posuere a magna ac, egestas vehicula lectus"), 0073 QStringList() << QStringLiteral("action1") << QStringLiteral("action2")); 0074 0075 QTimer::singleShot(1000, &a, &QEventLoop::quit); 0076 a.exec(); 0077 0078 notificationDBusCall(QStringLiteral("preferences-desktop-accessibility"), 0079 QStringLiteral("Testing notification #3"), 0080 QStringLiteral("Fusce hendrerit egestas pellentesque"), 0081 QStringList(), 0082 true); 0083 0084 QTimer::singleShot(2000, &app, SLOT(quit())); 0085 0086 return app.exec(); 0087 }