|
Packit Service |
b23acc |
// SPDX-License-Identifier: GPL-2.0+
|
|
Packit Service |
b23acc |
/*
|
|
Packit Service |
b23acc |
* Copyright (C) 2013 Red Hat, Inc.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* SECTION:nmt-utils
|
|
Packit Service |
b23acc |
* @short_description: Miscellaneous nmtui-specific utilities
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-default.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nmt-utils.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* NmtSyncOp:
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* A helper object used when synchronously waiting for an asynchronous
|
|
Packit Service |
b23acc |
* operation to complete.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* The caller first does:
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* |[
|
|
Packit Service |
b23acc |
* NmtSyncOp op;
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* nmt_sync_op_init (&op);
|
|
Packit Service |
b23acc |
* ]|
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* It then passes the op as the user_data to the async operation's
|
|
Packit Service |
b23acc |
* callback function, and then calls nmt_sync_op_wait_boolean() or
|
|
Packit Service |
b23acc |
* nmt_sync_op_wait_pointer() to wait for the result.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* When the async callback is invoked, it should call
|
|
Packit Service |
b23acc |
* nmt_sync_op_complete_boolean() or nmt_sync_op_complete_pointer() to
|
|
Packit Service |
b23acc |
* return a result or an error to the caller.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* There is no free/clear function; any memory that needs to be freed
|
|
Packit Service |
b23acc |
* will have been returned to the caller from
|
|
Packit Service |
b23acc |
* nmt_sync_op_wait_boolean() or nmt_sync_op_wait_pointer(), so there
|
|
Packit Service |
b23acc |
* is nothing left that needs to be freed.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
typedef struct {
|
|
Packit Service |
b23acc |
gpointer result;
|
|
Packit Service |
b23acc |
GError *error;
|
|
Packit Service |
b23acc |
gpointer complete;
|
|
Packit Service |
b23acc |
} NmtSyncOpReal;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* nmt_sync_op_init:
|
|
Packit Service |
b23acc |
* @op: pointer to a stack-allocated #NmtSyncOp
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Initializes @op before use.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
void
|
|
Packit Service |
b23acc |
nmt_sync_op_init (NmtSyncOp *op)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
memset (op, 0, sizeof (*op));
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* nmt_sync_op_wait_boolean:
|
|
Packit Service |
b23acc |
* @op: the #NmtSyncOp
|
|
Packit Service |
b23acc |
* @error: return location for a #GError
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* This runs the main loop until @op's operation returns, and then
|
|
Packit Service |
b23acc |
* returns the result or error.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Returns: the result of the operation.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
gboolean
|
|
Packit Service |
b23acc |
nmt_sync_op_wait_boolean (NmtSyncOp *op,
|
|
Packit Service |
b23acc |
GError **error)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
return GPOINTER_TO_UINT (nmt_sync_op_wait_pointer (op, error));
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* nmt_sync_op_complete_boolean:
|
|
Packit Service |
b23acc |
* @op: the #NmtSyncOp
|
|
Packit Service |
b23acc |
* @result: the result of the operation
|
|
Packit Service |
b23acc |
* @error: (allow-none): the error, or %NULL
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Completes @op and returns @result and/or @error to the caller.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
void
|
|
Packit Service |
b23acc |
nmt_sync_op_complete_boolean (NmtSyncOp *op,
|
|
Packit Service |
b23acc |
gboolean result,
|
|
Packit Service |
b23acc |
GError *error)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
nmt_sync_op_complete_pointer (op, GUINT_TO_POINTER (result), error);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* nmt_sync_op_wait_pointer:
|
|
Packit Service |
b23acc |
* @op: the #NmtSyncOp
|
|
Packit Service |
b23acc |
* @error: return location for a #GError
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* This runs the main loop until @op's operation returns, and then
|
|
Packit Service |
b23acc |
* returns the result or error.
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Returns: the result of the operation.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
gpointer
|
|
Packit Service |
b23acc |
nmt_sync_op_wait_pointer (NmtSyncOp *op,
|
|
Packit Service |
b23acc |
GError **error)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
NmtSyncOpReal *real = (NmtSyncOpReal *)op;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
while (!real->complete)
|
|
Packit Service |
b23acc |
g_main_context_iteration (NULL, TRUE);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (real->error)
|
|
Packit Service |
b23acc |
g_propagate_error (error, real->error);
|
|
Packit Service |
b23acc |
return real->result;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/**
|
|
Packit Service |
b23acc |
* nmt_sync_op_complete_pointer:
|
|
Packit Service |
b23acc |
* @op: the #NmtSyncOp
|
|
Packit Service |
b23acc |
* @result: the result of the operation
|
|
Packit Service |
b23acc |
* @error: (allow-none): the error, or %NULL
|
|
Packit Service |
b23acc |
*
|
|
Packit Service |
b23acc |
* Completes @op and returns @result and/or @error to the caller.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
void
|
|
Packit Service |
b23acc |
nmt_sync_op_complete_pointer (NmtSyncOp *op,
|
|
Packit Service |
b23acc |
gpointer result,
|
|
Packit Service |
b23acc |
GError *error)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
NmtSyncOpReal *real = (NmtSyncOpReal *)op;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
real->result = result;
|
|
Packit Service |
b23acc |
real->error = error ? g_error_copy (error) : NULL;
|
|
Packit Service |
b23acc |
real->complete = GUINT_TO_POINTER (TRUE);
|
|
Packit Service |
b23acc |
}
|