PolkitQt-1

polkitqt1-agent-listener.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 "polkitqt1-agent-listener.h"
9 
10 #include <QDebug>
11 
12 #include "polkitqtlistener_p.h"
13 
14 #include "polkitqt1-authority.h"
15 #include "polkitqt1-subject.h"
16 #include "polkitqt1-identity.h"
17 #include "polkitqt1-details.h"
18 
19 namespace PolkitQt1
20 {
21 
22 namespace Agent
23 {
24 
25 class ListenerPrivate
26 {
27 public:
28  PolkitAgentListener *listener;
29 #ifndef POLKIT_QT_1_COMPATIBILITY_MODE
30  void *registeredHandle;
31 #endif
32 };
33 
35  : QObject(parent), d(new ListenerPrivate)
36 {
37  d->listener = polkit_qt_listener_new();
38 
39  qDebug() << "New PolkitAgentListener " << d->listener;
40 
41  ListenerAdapter::instance()->addListener(this);
42 }
43 
44 Listener::Listener(PolkitAgentListener *listener, QObject *parent)
45  : QObject(parent), d(new ListenerPrivate)
46 {
47  d->listener = listener;
48 
49  if (d->listener != nullptr) {
50  g_object_ref(d->listener);
51  }
52 }
53 
54 Listener::~Listener()
55 {
56  qDebug("Destroying listener");
57 
58  ListenerAdapter::instance()->removeListener(this);
59 #ifndef POLKIT_QT_1_COMPATIBILITY_MODE
60  if (d->registeredHandle) {
61  polkit_agent_listener_unregister(d->registeredHandle);
62  }
63 #endif
64  if (d->listener != nullptr) {
65  g_object_unref(d->listener);
66  }
67 }
68 
69 bool Listener::registerListener(const PolkitQt1::Subject &subject, const QString &objectPath)
70 {
71  GError *error = nullptr;
72 
73 #ifndef POLKIT_QT_1_COMPATIBILITY_MODE
74  d->registeredHandle = polkit_agent_listener_register(d->listener,
75  POLKIT_AGENT_REGISTER_FLAGS_NONE,
76 #else
77  bool r = polkit_agent_register_listener(d->listener,
78 #endif
79  subject.subject(),
80  objectPath.toLatin1().data(),
81 #ifndef POLKIT_QT_1_COMPATIBILITY_MODE
82  nullptr,
83 #endif
84  &error);
85 
86  if (error != nullptr) {
87  qWarning() << QString("Cannot register authentication agent: %1").arg(error->message);
88  g_error_free(error);
89  return false;
90  }
91 #ifndef POLKIT_QT_1_COMPATIBILITY_MODE
92  if (d->registeredHandle == nullptr) {
93  qWarning() << QString("Cannot register authentication agent!");
94  return false;
95  }
96  return true;
97 #else
98  return r;
99 #endif
100 }
101 
102 const PolkitAgentListener *Listener::listener()
103 {
104  return d->listener;
105 }
106 
107 }
108 
109 }
This class represents PolicyKit subjects.
bool registerListener(const PolkitQt1::Subject &subject, const QString &objectPath)
Registers listener with polkit daemon as an authentication agent for subject.
const PolkitAgentListener * listener()
Returns pointer to the PolkitAgentListener.
QByteArray toLatin1() const const
PolkitSubject * subject() const
Gets PolkitSubject object.
Namespace wrapping Polkit-Qt Agent classes.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Listener(QObject *parent=nullptr)
Constructor of Listener class.
Namespace wrapping Polkit-Qt classes.
char * data()
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.