PolkitQt-1

polkitqt1-agent-session.cpp
1/*
2 This file is part of the PolKit1-qt project
3 SPDX-FileCopyrightText: 2009 Radek Novacek <rnovacek@redhat.com>
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
17using namespace PolkitQt1::Agent;
18
19class Session::Private
20{
21public:
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
30 AsyncResult *result;
31 PolkitAgentSession *polkitAgentSession;
32};
33
34Session::Private::~Private()
35{
36 // polkitAgentSession is freed in Session d'tor
37}
38
39Session::Session(const PolkitQt1::Identity &identity, const QString &cookie, AsyncResult *result, 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
51Session::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
78void 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
93void 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
104void 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
110void 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
116void 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
124class AsyncResult::Private
125{
126public:
127 Private(GSimpleAsyncResult *r) : result(r) {};
128
129 GSimpleAsyncResult *result;
130};
131
132AsyncResult::AsyncResult(GSimpleAsyncResult *result)
133 : d(new Private(result))
134{
135}
136
137AsyncResult::~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}
Encapsulation of GSimpleAsyncResult to QObject class.
void setCompleted()
Mark the action that is tied to this result as completed.
void setError(const QString &text)
Sets an error for the asynchronous result.
~Session() override
Destroy authentication session.
void initiate()
Initiate the authentication session.
AsyncResult * result()
Get AsyncResult that can be used to finish authentication operation.
void completed(bool gainedAuthorization)
This signal will be emitted when the authentication polkitqt1-agent-session.has been completed or can...
Session(const PolkitQt1::Identity &identity, const QString &cookie, AsyncResult *result=nullptr, QObject *parent=nullptr)
Create a new authentication session.
void cancel()
Cancel the authentication session.
void setResponse(const QString &response)
Method for providing response to requests received via request signal.
Abstract class representing identities.
PolkitIdentity * identity() const
Gets PolkitIdentity object.
char * data()
QObject * parent() const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:57:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.