summaryrefslogtreecommitdiff
path: root/src/lib/xdgnotifier.cpp
blob: da52ec5a41f44275b4aa58aaaf42a225f9ffd2e6 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
#include "xdgnotifier.h"
#include "NotificationsIface.h"
#include <QGuiApplication>

XdgNotifier::XdgNotifier(QObject *parent)
    : QObject{parent}
{   
    QDBusConnection connection = QDBusConnection::sessionBus();

    if (!connection.isConnected()) {
        qWarning("Cannot connect to the D-Bus session bus.\n"
                 "Please check your system settings and try again.\n");
    }
    const QString service = "org.freedesktop.Notifications";
    const QString path = "/org/freedesktop/Notifications";

    m_iface = new OrgFreedesktopNotificationsInterface(service, path, connection);

    QObject::connect(m_iface, &OrgFreedesktopNotificationsInterface::ActionInvoked, [this](uint id, const QString &action_key){
        qDebug() << "Recive signal ActionInvoked(" << id << ", " << action_key << ")";
        emit actionInvoked(action_key);
    });

    QObject::connect(m_iface, &OrgFreedesktopNotificationsInterface::NotificationClosed, [this](uint id, uint reason){
        qDebug() << "Recive signal NotificationClosed(" << id << ", " << reason << ")";
        switch(reason)
        {
        case 1:
            emit notificationClosed("The notification expired.");
            break;
        case 2:
            emit notificationClosed("The notification was dismissed by the user.");
            break;
        case 3:
            emit notificationClosed(" The notification was closed by a call to CloseNotification.");
            break;
        default:
            emit notificationClosed("Undefined/reserved reasons.");
            break;
        }
    });
}

void XdgNotifier::notifyTextOnly(QString summary, QString body, bool replace)
{
    qDebug() << "notifyTextOnly(" << summary << ", " << body << ")";
    auto reply = m_iface->Notify(
                QGuiApplication::applicationDisplayName(),
                (replace)?(m_lastid):(0),
                QString(),
                summary,
                body,
                QStringList(),
                QVariantMap(),
                -1
                );
    reply.waitForFinished();
    if(!replace)
        m_lastid = reply.value();
}

void XdgNotifier::notifyWithActions(QString summary, QString body, bool replace, QStringList actions)
{
    auto reply = m_iface->Notify(
                QGuiApplication::applicationDisplayName(),
                (replace)?(m_lastid):(0),
                QString(),
                summary,
                body,
                actions,
                QVariantMap(),
                -1
                );
    reply.waitForFinished();
    if(!replace)
        m_lastid = reply.value();
}

void XdgNotifier::notify(const QString &summary, const QString &body, bool replace, const QStringList &actions, int timeout)
{
    auto reply = m_iface->Notify(
                QGuiApplication::applicationDisplayName(),
                (replace)?(m_lastid):(0),
                QString(),
                summary,
                body,
                actions,
                QVariantMap(),
                timeout
                );
    reply.waitForFinished();
    if(!replace)
        m_lastid = reply.value();
}

void XdgNotifier::closeNotification()
{
    auto reply = m_iface->CloseNotification(m_lastid);
}