PolkitQt-1

listeneradapter.cpp
1 /*
2  This file is part of the Polkit-qt project
3  SPDX-FileCopyrightText: 2009 Jaroslav Reznik <[email protected]>
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 
13 namespace PolkitQt1
14 {
15 
16 namespace Agent
17 {
18 
19 class ListenerAdapterHelper
20 {
21 public:
22  ListenerAdapterHelper() : q(nullptr) {}
23  ~ListenerAdapterHelper() {
24  delete q;
25  }
26  ListenerAdapter *q;
27 };
28 
29 Q_GLOBAL_STATIC(ListenerAdapterHelper, s_globalListenerAdapter)
30 
31 ListenerAdapter *ListenerAdapter::instance()
32 {
33  if (!s_globalListenerAdapter()->q) {
34  new ListenerAdapter();
35  }
36 
37  return s_globalListenerAdapter()->q;
38 }
39 
40 ListenerAdapter::ListenerAdapter(QObject *parent)
41  : QObject(parent)
42 {
43  Q_ASSERT(!s_globalListenerAdapter()->q);
44  s_globalListenerAdapter()->q = this;
45 }
46 
47 Listener* 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 
62 void 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 
75  PolkitQt1::Details dets(details);
76 
77  Listener *list = findListener(listener);
78 
79  for (GList *identity = g_list_first(identities); identity != nullptr; identity = g_list_next(identity)) {
80  idents.append(PolkitQt1::Identity((PolkitIdentity *)identity->data));
81  }
82 
83  list->initiateAuthentication(QString::fromUtf8(action_id),
85  QString::fromUtf8(icon_name),
86  dets,
87  QString::fromUtf8(cookie),
88  idents,
89  new AsyncResult(result));
90 }
91 
92 gboolean ListenerAdapter::polkit_qt_listener_initiate_authentication_finish(PolkitAgentListener *listener,
93  GAsyncResult *res,
94  GError **error)
95 {
96  qDebug() << "polkit_qt_listener_initiate_authentication_finish callback for " << listener;
97 
98  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res);
99  if (g_simple_async_result_propagate_error(simple, error)) {
100  return false;
101  }
102  return true;
103 }
104 
105 void ListenerAdapter::cancelled_cb(PolkitAgentListener *listener)
106 {
107  qDebug() << "cancelled_cb for " << listener;
108 
109  Listener *list = findListener(listener);
110 
111  list->cancelAuthentication();
112 }
113 
114 void ListenerAdapter::addListener(Listener *listener)
115 {
116  qDebug() << "Adding new listener " << listener << "for " << listener->listener();
117 
118  m_listeners.append(listener);
119 }
120 
121 void ListenerAdapter::removeListener(Listener *listener)
122 {
123  qDebug() << "Removing listener " << listener;
124 
125  // should be safe as we don't have more than one same listener registered in one time
126  m_listeners.removeOne(listener);
127 }
128 
129 }
130 
131 }
void append(const T &value)
QString fromUtf8(const char *str, int size)
Abstract class representing identities.
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
Namespace wrapping Polkit-Qt Agent classes.
Class used for passing details around.
Namespace wrapping Polkit-Qt classes.
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:06:09 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.