cli: port mmcli_get_modem to GTask

This commit is contained in:
Aleksander Morgado
2017-09-20 22:34:31 -07:00
parent 6bf046b6fc
commit 11e9b727b4
2 changed files with 44 additions and 48 deletions

View File

@@ -110,8 +110,11 @@ mmcli_get_manager_sync (GDBusConnection *connection)
return manager; return manager;
} }
/******************************************************************************/
/* Modem */
static MMObject * static MMObject *
find_modem (MMManager *manager, find_modem (MMManager *manager,
const gchar *modem_path, const gchar *modem_path,
const gchar *modem_uid) const gchar *modem_uid)
{ {
@@ -156,15 +159,13 @@ find_modem (MMManager *manager,
} }
typedef struct { typedef struct {
GSimpleAsyncResult *result;
GCancellable *cancellable;
gchar *modem_path; gchar *modem_path;
gchar *modem_uid; gchar *modem_uid;
} GetModemContext; } GetModemContext;
typedef struct { typedef struct {
MMManager *manager; MMManager *manager;
MMObject *object; MMObject *object;
} GetModemResults; } GetModemResults;
static void static void
@@ -176,48 +177,44 @@ get_modem_results_free (GetModemResults *results)
} }
static void static void
get_modem_context_complete_and_free (GetModemContext *ctx) get_modem_context_free (GetModemContext *ctx)
{ {
g_simple_async_result_complete (ctx->result);
g_object_unref (ctx->result);
if (ctx->cancellable)
g_object_unref (ctx->cancellable);
g_free (ctx->modem_path); g_free (ctx->modem_path);
g_free (ctx->modem_uid); g_free (ctx->modem_uid);
g_free (ctx); g_free (ctx);
} }
MMObject * MMObject *
mmcli_get_modem_finish (GAsyncResult *res, mmcli_get_modem_finish (GAsyncResult *res,
MMManager **o_manager) MMManager **o_manager)
{ {
GetModemResults *results; GetModemResults *results;
MMObject *obj;
results = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)); results = g_task_propagate_pointer (G_TASK (res), NULL);
g_assert (results);
if (o_manager) if (o_manager)
*o_manager = g_object_ref (results->manager); *o_manager = g_object_ref (results->manager);
obj = g_object_ref (results->object);
return g_object_ref (results->object); get_modem_results_free (results);
return obj;
} }
static void static void
get_manager_ready (GDBusConnection *connection, get_manager_ready (GDBusConnection *connection,
GAsyncResult *res, GAsyncResult *res,
GetModemContext *ctx) GTask *task)
{ {
GetModemResults *results; GetModemResults *results;
GetModemContext *ctx;
ctx = g_task_get_task_data (task);
results = g_new (GetModemResults, 1); results = g_new (GetModemResults, 1);
results->manager = mmcli_get_manager_finish (res); results->manager = mmcli_get_manager_finish (res);
results->object = find_modem (results->manager, ctx->modem_path, ctx->modem_uid); results->object = find_modem (results->manager, ctx->modem_path, ctx->modem_uid);
g_task_return_pointer (task, results, (GDestroyNotify)get_modem_results_free);
/* Set operation results */ g_object_unref (task);
g_simple_async_result_set_op_res_gpointer (
ctx->result,
results,
(GDestroyNotify)get_modem_results_free);
get_modem_context_complete_and_free (ctx);
} }
static void static void
@@ -269,32 +266,32 @@ get_modem_path_or_uid (const gchar *str,
} }
void void
mmcli_get_modem (GDBusConnection *connection, mmcli_get_modem (GDBusConnection *connection,
const gchar *modem_str, const gchar *modem_str,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GTask *task;
GetModemContext *ctx; GetModemContext *ctx;
task = g_task_new (connection, cancellable, callback, user_data);
ctx = g_new0 (GetModemContext, 1); ctx = g_new0 (GetModemContext, 1);
get_modem_path_or_uid (modem_str, &ctx->modem_path, &ctx->modem_uid); get_modem_path_or_uid (modem_str, &ctx->modem_path, &ctx->modem_uid);
g_assert (ctx->modem_path || ctx->modem_uid); g_assert (ctx->modem_path || ctx->modem_uid);
ctx->result = g_simple_async_result_new (G_OBJECT (connection), g_task_set_task_data (task, ctx, (GDestroyNotify) get_modem_context_free);
callback,
user_data,
mmcli_get_modem);
mmcli_get_manager (connection, mmcli_get_manager (connection,
cancellable, cancellable,
(GAsyncReadyCallback)get_manager_ready, (GAsyncReadyCallback)get_manager_ready,
ctx); task);
} }
MMObject * MMObject *
mmcli_get_modem_sync (GDBusConnection *connection, mmcli_get_modem_sync (GDBusConnection *connection,
const gchar *modem_str, const gchar *modem_str,
MMManager **o_manager) MMManager **o_manager)
{ {
MMManager *manager; MMManager *manager;
MMObject *found; MMObject *found;

View File

@@ -33,17 +33,16 @@ void mmcli_get_manager (GDBusConnection *connection,
MMManager *mmcli_get_manager_finish (GAsyncResult *res); MMManager *mmcli_get_manager_finish (GAsyncResult *res);
MMManager *mmcli_get_manager_sync (GDBusConnection *connection); MMManager *mmcli_get_manager_sync (GDBusConnection *connection);
void mmcli_get_modem (GDBusConnection *connection,
void mmcli_get_modem (GDBusConnection *connection, const gchar *modem_str,
const gchar *modem_str, GCancellable *cancellable,
GCancellable *cancellable, GAsyncReadyCallback callback,
GAsyncReadyCallback callback, gpointer user_data);
gpointer user_data); MMObject *mmcli_get_modem_finish (GAsyncResult *res,
MMObject *mmcli_get_modem_finish (GAsyncResult *res, MMManager **o_manager);
MMManager **o_manager); MMObject *mmcli_get_modem_sync (GDBusConnection *connection,
MMObject *mmcli_get_modem_sync (GDBusConnection *connection, const gchar *modem_str,
const gchar *modem_str, MMManager **o_manager);
MMManager **o_manager);
void mmcli_get_bearer (GDBusConnection *connection, void mmcli_get_bearer (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,