1 | /* |
2 | SPDX-FileCopyrightText: 2011 Ilia Kats <ilia-kats@gmx.net> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "secretagent.h" |
8 | #include "manager_p.h" |
9 | #include "nmdebug.h" |
10 | #include "secretagent_p.h" |
11 | |
12 | #undef signals |
13 | #include <NetworkManager.h> |
14 | #include <libnm/NetworkManager.h> |
15 | |
16 | #define signals Q_SIGNALS |
17 | |
18 | #include <QDBusConnection> |
19 | #include <QDBusMetaType> |
20 | |
21 | #include "agentmanagerinterface.h" |
22 | #include "secretagentadaptor.h" |
23 | |
24 | NetworkManager::SecretAgentPrivate::SecretAgentPrivate(const QString &id, |
25 | const NetworkManager::SecretAgent::Capabilities capabilities, |
26 | NetworkManager::SecretAgent *parent) |
27 | : q_ptr(parent) |
28 | , agent(parent) |
29 | #ifdef NMQT_STATIC |
30 | , agentManager(NetworkManagerPrivate::DBUS_SERVICE, QLatin1String(NM_DBUS_PATH_AGENT_MANAGER), QDBusConnection::sessionBus(), parent) |
31 | , watcher(NetworkManagerPrivate::DBUS_SERVICE, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, parent) |
32 | #else |
33 | , agentManager(NetworkManagerPrivate::DBUS_SERVICE, QLatin1String(NM_DBUS_PATH_AGENT_MANAGER), QDBusConnection::systemBus(), parent) |
34 | , watcher(NetworkManagerPrivate::DBUS_SERVICE, QDBusConnection::systemBus(), QDBusServiceWatcher::WatchForOwnerChange, parent) |
35 | #endif |
36 | , agentId(id) |
37 | , capabilities(capabilities) |
38 | { |
39 | Q_Q(SecretAgent); |
40 | |
41 | qRegisterMetaType<NMVariantMapMap>(typeName: "NMVariantMapMap" ); |
42 | qDBusRegisterMetaType<NMVariantMapMap>(); |
43 | |
44 | // Similarly to NetworkManagerPrivate(), we register two listeners here: |
45 | // one for ServiceRegistered and one for InterfacesAdded. |
46 | agentManager.connection().connect(service: NetworkManagerPrivate::DBUS_SERVICE, |
47 | path: "/org/freedesktop" , |
48 | interface: NetworkManagerPrivate::FDO_DBUS_OBJECT_MANAGER, |
49 | name: QLatin1String("InterfacesAdded" ), |
50 | receiver: q, |
51 | SLOT(dbusInterfacesAdded(QDBusObjectPath, QVariantMap))); |
52 | |
53 | QObject::connect(sender: &watcher, signal: &QDBusServiceWatcher::serviceRegistered, context: parent, slot: [this] { this->daemonRegistered(); }); |
54 | |
55 | agentManager.connection().registerObject(path: QLatin1String(NM_DBUS_PATH_SECRET_AGENT), object: &agent, options: QDBusConnection::ExportAllSlots); |
56 | |
57 | registerAgent(capabilities); |
58 | } |
59 | |
60 | NetworkManager::SecretAgentPrivate::~SecretAgentPrivate() |
61 | { |
62 | agentManager.Unregister(); |
63 | } |
64 | |
65 | void NetworkManager::SecretAgentPrivate::dbusInterfacesAdded(const QDBusObjectPath &path, const QVariantMap &interfaces) |
66 | { |
67 | Q_UNUSED(path); |
68 | if (!interfaces.contains(key: QString::fromLatin1(ba: agentManager.staticInterfaceName()))) { |
69 | return; |
70 | } |
71 | |
72 | registerAgent(capabilities); |
73 | } |
74 | |
75 | void NetworkManager::SecretAgentPrivate::daemonRegistered() |
76 | { |
77 | registerAgent(capabilities); |
78 | } |
79 | |
80 | void NetworkManager::SecretAgentPrivate::registerAgent() |
81 | { |
82 | registerAgent(capabilities: NetworkManager::SecretAgent::Capability::NoCapability); |
83 | } |
84 | |
85 | void NetworkManager::SecretAgentPrivate::registerAgent(NetworkManager::SecretAgent::Capabilities capabilities) |
86 | { |
87 | agentManager.RegisterWithCapabilities(identifier: agentId, capabilities); |
88 | } |
89 | |
90 | NetworkManager::SecretAgent::SecretAgent(const QString &id, QObject *parent) |
91 | : QObject(parent) |
92 | , QDBusContext() |
93 | , d_ptr(new NetworkManager::SecretAgentPrivate(id, NetworkManager::SecretAgent::Capability::NoCapability, this)) |
94 | { |
95 | } |
96 | |
97 | NetworkManager::SecretAgent::SecretAgent(const QString &id, NetworkManager::SecretAgent::Capabilities capabilities, QObject *parent) |
98 | : QObject(parent) |
99 | , QDBusContext() |
100 | , d_ptr(new NetworkManager::SecretAgentPrivate(id, capabilities, this)) |
101 | { |
102 | } |
103 | |
104 | NetworkManager::SecretAgent::~SecretAgent() |
105 | { |
106 | Q_D(SecretAgent); |
107 | delete d; |
108 | } |
109 | |
110 | void NetworkManager::SecretAgent::sendError(NetworkManager::SecretAgent::Error error, const QString &explanation, const QDBusMessage &callMessage) const |
111 | { |
112 | Q_D(const SecretAgent); |
113 | |
114 | QString errorString; |
115 | switch (error) { |
116 | case NotAuthorized: |
117 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".NotAuthorized" ); |
118 | break; |
119 | case InvalidConnection: |
120 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".InvalidConnection" ); |
121 | break; |
122 | case UserCanceled: |
123 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".UserCanceled" ); |
124 | break; |
125 | case AgentCanceled: |
126 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".AgentCanceled" ); |
127 | break; |
128 | case InternalError: |
129 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".InternalError" ); |
130 | break; |
131 | case NoSecrets: |
132 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".NoSecrets" ); |
133 | break; |
134 | default: |
135 | errorString = QStringLiteral(NM_DBUS_INTERFACE_SECRET_AGENT) % QStringLiteral(".Unknown" ); |
136 | break; |
137 | } |
138 | |
139 | QDBusMessage reply; |
140 | if (callMessage.type() == QDBusMessage::InvalidMessage) { |
141 | reply = message().createErrorReply(name: errorString, msg: explanation); |
142 | } else { |
143 | reply = callMessage.createErrorReply(name: errorString, msg: explanation); |
144 | } |
145 | |
146 | if (!d->agentManager.connection().send(message: reply)) { |
147 | qCDebug(NMQT) << "Failed to put error message on DBus queue" << errorString << explanation; |
148 | } |
149 | } |
150 | |
151 | #include "moc_secretagent.cpp" |
152 | |