File: knotificationdbustest.cpp

package info (click to toggle)
knotifications 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,284 kB
  • sloc: cpp: 6,073; java: 299; xml: 199; sh: 14; makefile: 5
file content (87 lines) | stat: -rw-r--r-- 3,208 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
    SPDX-FileCopyrightText: 2014 Martin Klapetek <mklapetek@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include <QTimer>
#include <knotification.h>

#include <QApplication>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDebug>

void notificationDBusCall(const QString &iconName, const QString &title, const QString &body, const QStringList &actions, bool persistent = false)
{
    QDBusMessage dbusNotificationMessage = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.Notifications"),
                                                                          QStringLiteral("/org/freedesktop/Notifications"),
                                                                          QStringLiteral("org.freedesktop.Notifications"),
                                                                          QStringLiteral("Notify"));

    QList<QVariant> args;

    args.append(QString()); // app_name
    args.append((uint)0); // notification to update
    args.append(iconName); // app_icon
    args.append(title); // summary
    args.append(body); // body

    QStringList actionList;
    int actId = 0;
    for (const QString &actionName : actions) {
        actId++;
        actionList.append(QString::number(actId));
        actionList.append(actionName);
    }

    args.append(actionList); // actions

    args.append(QVariantMap()); // hints

    // Persistent     => 0  == infinite timeout
    // CloseOnTimeout => -1 == let the server decide
    int timeout = persistent ? 0 : -1;

    args.append(timeout); // expire timeout

    dbusNotificationMessage.setArguments(args);

    QDBusMessage reply = QDBusConnection::sessionBus().call(dbusNotificationMessage, QDBus::Block, 4000);
    if (reply.type() == QDBusMessage::ErrorMessage) {
        qDebug() << "Error sending notification:" << reply.errorMessage();
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    notificationDBusCall(QStringLiteral("amarok"),
                         QStringLiteral("Testing notification #1"),
                         QStringLiteral("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In condimentum"),
                         QStringList() << QStringLiteral("action1") << QStringLiteral("action2"));

    // wait a little before sending another notification
    QEventLoop a;
    QTimer::singleShot(500, &a, &QEventLoop::quit);
    a.exec();

    notificationDBusCall(QStringLiteral("kwalletmanager"),
                         QStringLiteral("Testing notification #2"),
                         QStringLiteral("Praesent odio ipsum, posuere a magna ac, egestas vehicula lectus"),
                         QStringList() << QStringLiteral("action1") << QStringLiteral("action2"));

    QTimer::singleShot(1000, &a, &QEventLoop::quit);
    a.exec();

    notificationDBusCall(QStringLiteral("preferences-desktop-accessibility"),
                         QStringLiteral("Testing notification #3"),
                         QStringLiteral("Fusce hendrerit egestas pellentesque"),
                         QStringList(),
                         true);

    QTimer::singleShot(2000, &app, SLOT(quit()));

    return app.exec();
}