summaryrefslogtreecommitdiff
path: root/clients/common/nm-polkit-listener.c
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-04-06 23:54:33 +0200
committerThomas Haller <thaller@redhat.com>2018-04-16 16:03:14 +0200
commit347118138dc6f12a058c9c86ddf29151f265cdb6 (patch)
treebf4da178a5fb3c01bb3e4d42953f432c3bc3045e /clients/common/nm-polkit-listener.c
parent86ab96989db89bd360bec7a5a63e5989d894c169 (diff)
downloadNetworkManager-347118138dc6f12a058c9c86ddf29151f265cdb6.tar.gz
cli: rework callbacks in NMPolkitListener to use one vtable structure
Instead of setting multiple callbacks, just let the user set one vtable with callbacks. Usually, GObject would implement this via signals. While that makes sense for public objects, for example to work better with GIR and allow intercepting the signal, this is overkill for our internal type. And NMPolkitListener already did not make use of signals, for good reason. Instead of passing multiple callbacks, must pass one structure with callback pointers. Also, extend the signature of the callbacks to always contain a @self argument and a @user_data.
Diffstat (limited to 'clients/common/nm-polkit-listener.c')
-rw-r--r--clients/common/nm-polkit-listener.c167
1 files changed, 57 insertions, 110 deletions
diff --git a/clients/common/nm-polkit-listener.c b/clients/common/nm-polkit-listener.c
index 5bba81e484..300cf11e8d 100644
--- a/clients/common/nm-polkit-listener.c
+++ b/clients/common/nm-polkit-listener.c
@@ -56,12 +56,8 @@ typedef struct {
char *icon_name;
char *identity;
- /* callbacks */
- NMPolkitListenerOnRequestFunc on_request_callback;
- NMPolkitListenerOnShowInfoFunc on_show_info_callback;
- NMPolkitListenerOnShowErrorFunc on_show_error_callback;
- NMPolkitListenerOnCompletedFunc on_completed_callback;
- gpointer request_callback_data;
+ const NMPolkitListenVtable *vtable;
+ gpointer vtable_user_data;
} NMPolkitListenerPrivate;
G_DEFINE_TYPE (NMPolkitListener, nm_polkit_listener, POLKIT_AGENT_TYPE_LISTENER)
@@ -70,26 +66,40 @@ G_DEFINE_TYPE (NMPolkitListener, nm_polkit_listener, POLKIT_AGENT_TYPE_LISTENER)
/*****************************************************************************/
+void
+nm_polkit_listener_set_vtable (NMPolkitListener *self,
+ const NMPolkitListenVtable *vtable,
+ gpointer user_data)
+{
+ NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
+
+ priv->vtable = vtable;
+ priv->vtable_user_data = user_data;
+}
+
+/*****************************************************************************/
+
static void
on_request (PolkitAgentSession *session,
const char *request,
gboolean echo_on,
gpointer user_data)
{
- NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data);
- char *response = NULL;
-
- if (priv->on_request_callback) {
- response = priv->on_request_callback (request, priv->action_id,
- priv->message, priv->icon_name,
- priv->identity, echo_on,
- priv->request_callback_data);
+ NMPolkitListener *self = NM_POLKIT_LISTENER (user_data);
+ NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
+ gs_free char *response = NULL;
+
+ if (priv->vtable && priv->vtable->on_request) {
+ response = priv->vtable->on_request (self,
+ request, priv->action_id,
+ priv->message, priv->icon_name,
+ priv->identity, echo_on,
+ priv->vtable_user_data);
}
- if (response) {
+ if (response)
polkit_agent_session_response (session, response);
- g_free (response);
- } else {
+ else {
//FIXME: polkit_agent_session_cancel() should emit "completed", but it doesn't work for me ???
//polkit_agent_session_cancel (session);
polkit_agent_session_response (session, "");
@@ -101,10 +111,13 @@ on_show_info (PolkitAgentSession *session,
const char *text,
gpointer user_data)
{
- NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data);
+ NMPolkitListener *self = NM_POLKIT_LISTENER (user_data);
+ NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
- if (priv->on_show_info_callback)
- priv->on_show_info_callback (text);
+ if (priv->vtable && priv->vtable->on_show_info) {
+ priv->vtable->on_show_info (self, text,
+ priv->vtable_user_data);
+ }
}
static void
@@ -112,10 +125,13 @@ on_show_error (PolkitAgentSession *session,
const char *text,
gpointer user_data)
{
- NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data);
+ NMPolkitListener *self = NM_POLKIT_LISTENER (user_data);
+ NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
- if (priv->on_show_error_callback)
- priv->on_show_error_callback (text);
+ if (priv->vtable && priv->vtable->on_show_error) {
+ priv->vtable->on_show_error (self, text,
+ priv->vtable_user_data);
+ }
}
static void
@@ -123,10 +139,13 @@ on_completed (PolkitAgentSession *session,
gboolean gained_authorization,
gpointer user_data)
{
- NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data);
+ NMPolkitListener *self = NM_POLKIT_LISTENER (user_data);
+ NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
- if (priv->on_completed_callback)
- priv->on_completed_callback (gained_authorization);
+ if (priv->vtable->on_completed) {
+ priv->vtable->on_completed (self, gained_authorization,
+ priv->vtable_user_data);
+ }
g_simple_async_result_complete_in_idle (priv->simple);
@@ -150,7 +169,8 @@ on_completed (PolkitAgentSession *session,
static void
on_cancelled (GCancellable *cancellable, gpointer user_data)
{
- NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data);
+ NMPolkitListener *self = NM_POLKIT_LISTENER (user_data);
+ NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
polkit_agent_session_cancel (priv->active_session);
}
@@ -271,83 +291,6 @@ initiate_authentication_finish (PolkitAgentListener *listener,
/*****************************************************************************/
-/**
- * nm_polkit_listener_set_request_callback:
- * @self: a #NMPolkitListener object
- * @request_callback: callback to install for polkit requests
- * @request_callback_data: usaer data passed to request_callback when it is called
- *
- * Set a callback for "request" signal. The callback will be invoked when polkit
- * requests an authorization.
- */
-void
-nm_polkit_listener_set_request_callback (NMPolkitListener *self,
- NMPolkitListenerOnRequestFunc request_callback,
- gpointer request_callback_data)
-{
- NMPolkitListenerPrivate *priv;
-
- g_return_if_fail (NM_IS_POLKIT_LISTENER (self));
-
- priv = NM_POLKIT_LISTENER_GET_PRIVATE (self);
-
- priv->on_request_callback = request_callback;
- priv->request_callback_data = request_callback_data;
-}
-
-/**
- * nm_polkit_listener_set_show_info_callback:
- * @self: a #NMPolkitListener object
- * @show_info_callback: callback to install for polkit show info trigger
- *
- * Set a callback for "show-info" signal. The callback will be invoked when polkit
- * has an info text to display.
- */
-void
-nm_polkit_listener_set_show_info_callback (NMPolkitListener *self,
- NMPolkitListenerOnShowInfoFunc show_info_callback)
-{
- g_return_if_fail (NM_IS_POLKIT_LISTENER (self));
-
- NM_POLKIT_LISTENER_GET_PRIVATE (self)->on_show_info_callback = show_info_callback;
-}
-
-/**
- * nm_polkit_listener_set_show_error_callback:
- * @self: a #NMPolkitListener object
- * @show_error_callback: callback to install for polkit show error trigger
- *
- * Set a callback for "show-error" signal. The callback will be invoked when polkit
- * has an error text to display.
- */
-void
-nm_polkit_listener_set_show_error_callback (NMPolkitListener *self,
- NMPolkitListenerOnShowErrorFunc show_error_callback)
-{
- g_return_if_fail (NM_IS_POLKIT_LISTENER (self));
-
- NM_POLKIT_LISTENER_GET_PRIVATE (self)->on_show_error_callback = show_error_callback;
-}
-
-/**
- * nm_polkit_listener_set_completed_callback:
- * @self: a #NMPolkitListener object
- * @completed_callback: callback to install for polkit completing authorization
- *
- * Set a callback for "completed" signal. The callback will be invoked when polkit
- * completed the request.
- */
-void
-nm_polkit_listener_set_completed_callback (NMPolkitListener *self,
- NMPolkitListenerOnCompletedFunc completed_callback)
-{
- g_return_if_fail (NM_IS_POLKIT_LISTENER (self));
-
- NM_POLKIT_LISTENER_GET_PRIVATE (self)->on_completed_callback = completed_callback;
-}
-
-/*****************************************************************************/
-
static void
nm_polkit_listener_init (NMPolkitListener *agent)
{
@@ -357,22 +300,26 @@ nm_polkit_listener_init (NMPolkitListener *agent)
* nm_polkit_listener_new:
* @for_session: %TRUE for registering the polkit agent for the user session,
* %FALSE for registering it for the running process
+ * @vtable: mandatory callbacks
+ * @user_data: user-data pointer for callbacks
* @error: location to store error, or %NULL
*
* Creates a new #NMPolkitListener and registers it as a polkit agent.
*
* Returns: a new #NMPolkitListener
*/
-PolkitAgentListener *
-nm_polkit_listener_new (gboolean for_session, GError **error)
+NMPolkitListener *
+nm_polkit_listener_new (gboolean for_session,
+ GError **error)
{
- PolkitAgentListener *listener;
+ NMPolkitListener *listener;
PolkitSubject* session;
NMPolkitListenerPrivate *priv;
- g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+ g_return_val_if_fail (!error || !*error, NULL);
listener = g_object_new (NM_TYPE_POLKIT_LISTENER, NULL);
+
priv = NM_POLKIT_LISTENER_GET_PRIVATE (listener);
if (for_session) {
@@ -382,7 +329,8 @@ nm_polkit_listener_new (gboolean for_session, GError **error)
} else
session = polkit_unix_process_new_for_owner (getpid (), 0, getuid ());
- priv->reg_handle = polkit_agent_listener_register (listener, POLKIT_AGENT_REGISTER_FLAGS_NONE,
+ priv->reg_handle = polkit_agent_listener_register (POLKIT_AGENT_LISTENER (listener),
+ POLKIT_AGENT_REGISTER_FLAGS_NONE,
session, NULL, NULL, error);
if (!priv->reg_handle) {
g_object_unref (listener);
@@ -393,7 +341,6 @@ nm_polkit_listener_new (gboolean for_session, GError **error)
return listener;
}
-
static void
nm_polkit_listener_finalize (GObject *object)
{