PolkitQt-1

listeneradapter.cpp
1/*
2 This file is part of the Polkit-qt project
3 SPDX-FileCopyrightText: 2009 Jaroslav Reznik <jreznik@redhat.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "listeneradapter_p.h"
9#include <QDebug>
10#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1
11#include <polkitagent/polkitagent.h>
12
13namespace PolkitQt1
14{
15
16namespace Agent
17{
18
19class ListenerAdapterHelper
20{
21public:
22 ListenerAdapterHelper() : q(nullptr) {}
23 ~ListenerAdapterHelper() {
24 delete q;
25 }
26 ListenerAdapter *q;
27};
28
29Q_GLOBAL_STATIC(ListenerAdapterHelper, s_globalListenerAdapter)
30
31ListenerAdapter *ListenerAdapter::instance()
32{
33 if (!s_globalListenerAdapter()->q) {
34 new ListenerAdapter();
35 }
36
37 return s_globalListenerAdapter()->q;
38}
39
40ListenerAdapter::ListenerAdapter(QObject *parent)
41 : QObject(parent)
42{
43 Q_ASSERT(!s_globalListenerAdapter()->q);
44 s_globalListenerAdapter()->q = this;
45}
46
47Listener* ListenerAdapter::findListener(PolkitAgentListener *listener)
48{
49 Listener *listItem;
50
51 Q_FOREACH(listItem, m_listeners) {
52 Q_ASSERT(listItem);
53
54 if (listItem->listener() == listener) {
55 return listItem;
56 }
57 }
58
59 return nullptr;
60}
61
62void ListenerAdapter::polkit_qt_listener_initiate_authentication(PolkitAgentListener *listener,
63 const gchar *action_id,
64 const gchar *message,
65 const gchar *icon_name,
66 PolkitDetails *details,
67 const gchar *cookie,
68 GList *identities,
69 GCancellable *cancellable,
70 GSimpleAsyncResult *result)
71{
72 qDebug() << "polkit_qt_listener_initiate_authentication callback for " << listener;
73
74 PolkitQt1::Details dets(details);
75
76 Listener *list = findListener(listener);
77
78 // Polkit enumerates identities without regard for their hash value, potentially leading to duplicated entries.
79 // Unique the identities on our end.
80 // https://github.com/polkit-org/polkit/issues/542
81 QHash<guint, PolkitQt1::Identity> uniqueIdentities;
82 for (GList *entry = g_list_first(identities); entry != nullptr; entry = g_list_next(entry)) {
83 auto identity = static_cast<PolkitIdentity *>(entry->data);
84 uniqueIdentities.insert(polkit_identity_hash(identity), PolkitQt1::Identity(identity));
85 }
86
87 list->initiateAuthentication(QString::fromUtf8(action_id),
88 QString::fromUtf8(message),
89 QString::fromUtf8(icon_name),
90 dets,
91 QString::fromUtf8(cookie),
92 uniqueIdentities.values(),
93 new AsyncResult(result));
94}
95
96gboolean ListenerAdapter::polkit_qt_listener_initiate_authentication_finish(PolkitAgentListener *listener,
97 GAsyncResult *res,
98 GError **error)
99{
100 qDebug() << "polkit_qt_listener_initiate_authentication_finish callback for " << listener;
101
102 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res);
103 if (g_simple_async_result_propagate_error(simple, error)) {
104 return false;
105 }
106 return true;
107}
108
109void ListenerAdapter::cancelled_cb(PolkitAgentListener *listener)
110{
111 qDebug() << "cancelled_cb for " << listener;
112
113 Listener *list = findListener(listener);
114
115 list->cancelAuthentication();
116}
117
118void ListenerAdapter::addListener(Listener *listener)
119{
120 qDebug() << "Adding new listener " << listener << "for " << listener->listener();
121
122 m_listeners.append(listener);
123}
124
125void ListenerAdapter::removeListener(Listener *listener)
126{
127 qDebug() << "Removing listener " << listener;
128
129 // should be safe as we don't have more than one same listener registered in one time
130 m_listeners.removeOne(listener);
131}
132
133}
134
135}
Class used for passing details around.
Abstract class representing identities.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
iterator insert(const Key &key, const T &value)
QList< T > values() const const
void append(QList< T > &&value)
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:54:55 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.