PolkitQt-1

polkitqt1-agent-session.cpp
1 /*
2  This file is part of the PolKit1-qt project
3  SPDX-FileCopyrightText: 2009 Radek Novacek <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "polkitqt1-agent-session.h"
9 
10 #include <QDebug>
11 
12 #include "polkitqt1-identity.h"
13 
14 #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1
15 #include <polkitagent/polkitagent.h>
16 
17 using namespace PolkitQt1::Agent;
18 
19 class Session::Private
20 {
21 public:
22  Private() {}
23  ~Private();
24 
25  static void completed(PolkitAgentSession *s, gboolean gained_authorization, gpointer user_data);
26  static void request(PolkitAgentSession *s, gchar *request, gboolean echo_on, gpointer user_data);
27  static void showError(PolkitAgentSession *s, gchar *text, gpointer user_data);
28  static void showInfo(PolkitAgentSession *s, gchar *text, gpointer user_data);
29 
31  PolkitAgentSession *polkitAgentSession;
32 };
33 
34 Session::Private::~Private()
35 {
36  // polkitAgentSession is freed in Session d'tor
37 }
38 
39 Session::Session(const PolkitQt1::Identity &identity, const QString &cookie, AsyncResult *result, QObject *parent)
40  : QObject(parent)
41  , d(new Private)
42 {
43  d->result = result;
44  d->polkitAgentSession = polkit_agent_session_new(identity.identity(), cookie.toUtf8().data());
45  g_signal_connect(G_OBJECT(d->polkitAgentSession), "completed", G_CALLBACK(Private::completed), this);
46  g_signal_connect(G_OBJECT(d->polkitAgentSession), "request", G_CALLBACK(Private::request), this);
47  g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-error", G_CALLBACK(Private::showError), this);
48  g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-info", G_CALLBACK(Private::showInfo), this);
49 }
50 
51 Session::Session(PolkitAgentSession *pkAgentSession, QObject *parent)
52  : QObject(parent)
53  , d(new Private)
54 {
55  d->polkitAgentSession = pkAgentSession;
56  if (d->polkitAgentSession) {
57  g_object_ref(d->polkitAgentSession);
58  }
59  g_signal_connect(G_OBJECT(d->polkitAgentSession), "completed", G_CALLBACK(Private::completed), this);
60  g_signal_connect(G_OBJECT(d->polkitAgentSession), "request", G_CALLBACK(Private::request), this);
61  g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-error", G_CALLBACK(Private::showError), this);
62  g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-info", G_CALLBACK(Private::showInfo), this);
63 }
64 
66 {
67  if (d->polkitAgentSession)
68  g_object_unref(d->polkitAgentSession);
69 
70  delete d;
71 }
72 
74 {
75  polkit_agent_session_initiate(d->polkitAgentSession);
76 }
77 
78 void Session::setResponse(const QString &response)
79 {
80  polkit_agent_session_response(d->polkitAgentSession, response.toUtf8().data());
81 }
82 
84 {
85  polkit_agent_session_cancel(d->polkitAgentSession);
86 }
87 
89 {
90  return d->result;
91 }
92 
93 void Session::Private::completed(PolkitAgentSession *s, gboolean gained_authorization, gpointer user_data)
94 {
95  qDebug() << "COMPLETED";
96  Session *session = (Session *)user_data;
97  Q_EMIT(session)->completed(gained_authorization);
98 
99  //free session here as polkit documentation asks
100  g_object_unref(session->d->polkitAgentSession);
101  session->d->polkitAgentSession = nullptr;
102 }
103 
104 void Session::Private::request(PolkitAgentSession *s, gchar *request, gboolean echo_on, gpointer user_data)
105 {
106  qDebug() << "REQUEST";
107  Q_EMIT((Session *)user_data)->request(QString::fromUtf8(request), echo_on);
108 }
109 
110 void Session::Private::showError(PolkitAgentSession *s, gchar *text, gpointer user_data)
111 {
112  qDebug() << "showError";
113  Q_EMIT((Session *)user_data)->showError(QString::fromUtf8(text));
114 }
115 
116 void Session::Private::showInfo(PolkitAgentSession *s, gchar *text, gpointer user_data)
117 {
118  qDebug() << "showInfo";
119  Q_EMIT((Session *)user_data)->showInfo(QString::fromUtf8(text));
120 }
121 
122 //
123 
124 class AsyncResult::Private
125 {
126 public:
127  Private(GSimpleAsyncResult *r) : result(r) {};
128 
129  GSimpleAsyncResult *result;
130 };
131 
132 AsyncResult::AsyncResult(GSimpleAsyncResult *result)
133  : d(new Private(result))
134 {
135 }
136 
137 AsyncResult::~AsyncResult()
138 {
139  if (d->result)
140  g_object_unref(d->result);
141 }
142 
144 {
145  if (d->result == nullptr)
146  return;
147  g_simple_async_result_complete(d->result);
148  // Assure that completed won't be called twice
149  g_object_unref(d->result);
150  d->result = nullptr;
151 }
152 
154 {
155  Q_ASSERT(d->result);
156  g_simple_async_result_set_error(d->result, POLKIT_ERROR, POLKIT_ERROR_FAILED, "%s", text.toUtf8().data());
157 }
void request(const QString &request, bool echo)
This signal will be emitted when user is requested to answer a question.
void completed(bool gainedAuthorization)
This signal will be emitted when the authentication polkitqt1-agent-session.has been completed or can...
QString fromUtf8(const char *str, int size)
~Session() override
Destroy authentication session.
Abstract class representing identities.
void setResponse(const QString &response)
Method for providing response to requests received via request signal.
Session(const PolkitQt1::Identity &identity, const QString &cookie, AsyncResult *result=nullptr, QObject *parent=nullptr)
Create a new authentication session.
Encapsulation of GSimpleAsyncResult to QObject class.
void setCompleted()
Mark the action that is tied to this result as completed.
QByteArray toUtf8() const const
void showError(const QString &text)
This signal will be emitted when there is information related to an error condition to be displayed t...
void cancel()
Cancel the authentication session.
AsyncResult * result()
Get AsyncResult that can be used to finish authentication operation.
void initiate()
Initiate the authentication session.
char * data()
void setError(const QString &text)
Sets an error for the asynchronous result.
void showInfo(const QString &text)
This signal will be emitted when there is information to be displayed to the user.
PolkitIdentity * identity() const
Gets PolkitIdentity object.
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.