File: server.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (155 lines) | stat: -rw-r--r-- 4,602 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
    SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "server.h"
#include "server_p.h"
#include "serverinfo.h"

#include "notification.h"
#include "notification_p.h"

#include "debug.h"

#include <KStartupInfo>
#include <KWindowSystem>
#include <QDebug>

using namespace NotificationManager;

Server::Server(QObject *parent)
    : QObject(parent)
    , d(new ServerPrivate(this))
{
    connect(d.data(), &ServerPrivate::validChanged, this, &Server::validChanged);
    connect(d.data(), &ServerPrivate::inhibitedChanged, this, [this] {
        Q_EMIT inhibitedChanged(inhibited());
    });
    connect(d.data(), &ServerPrivate::externalInhibitedChanged, this, [this] {
        Q_EMIT inhibitedByApplicationChanged(inhibitedByApplication());
    });
    connect(d.data(), &ServerPrivate::externalInhibitionsChanged, this, &Server::inhibitionApplicationsChanged);
    connect(d.data(), &ServerPrivate::serviceOwnershipLost, this, &Server::serviceOwnershipLost);
}

Server::~Server() = default;

Server &Server::self()
{
    static Server s_self;
    return s_self;
}

bool Server::init()
{
    return d->init();
}

bool Server::isValid() const
{
    return d->m_valid;
}

ServerInfo *Server::currentOwner() const
{
    return d->currentOwner();
}

void Server::closeNotification(uint notificationId, CloseReason reason)
{
    Q_EMIT notificationRemoved(notificationId, reason);
    Q_EMIT d->NotificationClosed(notificationId, static_cast<uint>(reason)); // tell on DBus
}

void Server::invokeAction(uint notificationId,
                          const QString &actionName,
                          const QString &xdgActivationAppId,
                          Notifications::InvokeBehavior behavior,
                          QWindow *window)
{
    if (KWindowSystem::isPlatformWayland()) {
        const quint32 launchedSerial = KWindowSystem::lastInputSerial(window);
        auto conn = QSharedPointer<QMetaObject::Connection>::create();
        *conn = connect(KWindowSystem::self(),
                        &KWindowSystem::xdgActivationTokenArrived,
                        this,
                        [this, actionName, notificationId, launchedSerial, conn, behavior](quint32 serial, const QString &token) {
                            if (serial == launchedSerial) {
                                disconnect(*conn);
                                Q_EMIT d->ActivationToken(notificationId, token);
                                Q_EMIT d->ActionInvoked(notificationId, actionName);

                                if (behavior & Notifications::Close) {
                                    Q_EMIT d->CloseNotification(notificationId);
                                }
                            }
                        });
        KWindowSystem::requestXdgActivationToken(window, launchedSerial, xdgActivationAppId);
    } else {
        KStartupInfoId startupId;
        startupId.initId();

        Q_EMIT d->ActivationToken(notificationId, QString::fromUtf8(startupId.id()));

        Q_EMIT d->ActionInvoked(notificationId, actionName);
        if (behavior & Notifications::Close) {
            Q_EMIT d->CloseNotification(notificationId);
        }
    }
}

void Server::reply(const QString &dbusService, uint notificationId, const QString &text, Notifications::InvokeBehavior behavior)
{
    d->sendReplyText(dbusService, notificationId, text, behavior);
}

uint Server::add(const Notification &notification)
{
    return d->add(notification);
}

bool Server::inhibited() const
{
    return d->inhibited();
}

void Server::setInhibited(bool inhibited)
{
    d->setInhibited(inhibited);
}

bool Server::inhibitedByApplication() const
{
    return d->externalInhibited();
}

QStringList Server::inhibitionApplications() const
{
    QStringList applications;
    const auto inhibitions = d->externalInhibitions();
    applications.reserve(inhibitions.count());
    for (const auto &inhibition : inhibitions) {
        applications.append(!inhibition.applicationName.isEmpty() ? inhibition.applicationName : inhibition.desktopEntry);
    }
    return applications;
}

QStringList Server::inhibitionReasons() const
{
    QStringList reasons;
    const auto inhibitions = d->externalInhibitions();
    reasons.reserve(inhibitions.count());
    for (const auto &inhibition : inhibitions) {
        reasons.append(inhibition.reason);
    }
    return reasons;
}

void Server::clearInhibitions()
{
    d->clearExternalInhibitions();
}

#include "moc_server.cpp"