cli: port mmcli_get_sim to GTask

This commit is contained in:
Aleksander Morgado
2017-10-18 23:04:41 +02:00
parent 050eb46f51
commit 6f9e2003e0
2 changed files with 95 additions and 81 deletions

View File

@@ -613,61 +613,73 @@ mmcli_get_bearer_sync (GDBusConnection *connection,
return found; return found;
} }
/******************************************************************************/
/* SIM */
typedef struct { typedef struct {
GSimpleAsyncResult *result; gchar *sim_path;
GCancellable *cancellable;
gchar *sim_path;
MMManager *manager; MMManager *manager;
MMObject *modem; MMObject *current;
MMSim *sim;
} GetSimContext; } GetSimContext;
typedef struct {
MMManager *manager;
MMObject *object;
MMSim *sim;
} GetSimResults;
static void
get_sim_results_free (GetSimResults *results)
{
g_object_unref (results->manager);
g_object_unref (results->object);
g_object_unref (results->sim);
g_free (results);
}
static void static void
get_sim_context_free (GetSimContext *ctx) get_sim_context_free (GetSimContext *ctx)
{ {
if (ctx->modem) if (ctx->current)
g_object_unref (ctx->modem); g_object_unref (ctx->current);
if (ctx->cancellable)
g_object_unref (ctx->cancellable);
if (ctx->manager) if (ctx->manager)
g_object_unref (ctx->manager); g_object_unref (ctx->manager);
if (ctx->sim)
g_object_unref (ctx->sim);
g_free (ctx->sim_path); g_free (ctx->sim_path);
g_free (ctx); g_free (ctx);
} }
static void MMSim *
get_sim_context_complete (GetSimContext *ctx) mmcli_get_sim_finish (GAsyncResult *res,
MMManager **o_manager,
MMObject **o_object)
{ {
g_simple_async_result_complete (ctx->result); GetSimResults *results;
g_object_unref (ctx->result); MMSim *obj;
ctx->result = NULL;
results = g_task_propagate_pointer (G_TASK (res), NULL);
g_assert (results);
if (o_manager)
*o_manager = g_object_ref (results->manager);
if (o_object)
*o_object = g_object_ref (results->object);
obj = g_object_ref (results->sim);
get_sim_results_free (results);
return obj;
} }
MMSim * static void
mmcli_get_sim_finish (GAsyncResult *res, get_sim_ready (MMModem *modem,
MMManager **o_manager, GAsyncResult *res,
MMObject **o_object) GTask *task)
{ {
GetSimContext *ctx; GetSimContext *ctx;
GetSimResults *results;
MMSim *sim;
GError *error = NULL;
ctx = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)); ctx = g_task_get_task_data (task);
if (o_manager)
*o_manager = g_object_ref (ctx->manager);
if (o_object)
*o_object = g_object_ref (ctx->modem);
return g_object_ref (ctx->sim);
}
static void sim = mm_modem_get_sim_finish (modem, res, &error);
get_sim_ready (MMModem *modem,
GAsyncResult *res,
GetSimContext *ctx)
{
GError *error = NULL;
ctx->sim = mm_modem_get_sim_finish (modem, res, &error);
if (error) { if (error) {
g_printerr ("error: couldn't get sim '%s' at '%s': '%s'\n", g_printerr ("error: couldn't get sim '%s' at '%s': '%s'\n",
ctx->sim_path, ctx->sim_path,
@@ -676,20 +688,25 @@ get_sim_ready (MMModem *modem,
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
g_simple_async_result_set_op_res_gpointer ( /* Found! */
ctx->result, results = g_new (GetSimResults, 1);
ctx, results->manager = g_object_ref (ctx->manager);
(GDestroyNotify)get_sim_context_free); results->object = g_object_ref (ctx->current);
get_sim_context_complete (ctx); results->sim = sim;
g_task_return_pointer (task, results, (GDestroyNotify) get_sim_results_free);
g_object_unref (task);
} }
static void static void
get_sim_manager_ready (GDBusConnection *connection, get_sim_manager_ready (GDBusConnection *connection,
GAsyncResult *res, GAsyncResult *res,
GetSimContext *ctx) GTask *task)
{ {
GList *l; GetSimContext *ctx;
GList *modems; GList *l;
GList *modems;
ctx = g_task_get_task_data (task);
ctx->manager = mmcli_get_manager_finish (res); ctx->manager = mmcli_get_manager_finish (res);
@@ -700,30 +717,28 @@ get_sim_manager_ready (GDBusConnection *connection,
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
for (l = modems; l; l = g_list_next (l)) { for (l = modems; l && !ctx->current; l = g_list_next (l)) {
MMObject *object; MMObject *object;
MMModem *modem; MMModem *modem;
object = MM_OBJECT (l->data); object = MM_OBJECT (l->data);
modem = mm_object_get_modem (object); modem = mm_object_get_modem (object);
if (g_str_equal (ctx->sim_path, mm_modem_get_sim_path (modem))) { if (g_str_equal (ctx->sim_path, mm_modem_get_sim_path (modem))) {
ctx->modem = g_object_ref (object); ctx->current = g_object_ref (object);
mm_modem_get_sim (modem, mm_modem_get_sim (modem,
ctx->cancellable, g_task_get_cancellable (task),
(GAsyncReadyCallback)get_sim_ready, (GAsyncReadyCallback)get_sim_ready,
ctx); task);
break;
} }
g_object_unref (modem); g_object_unref (modem);
} }
g_list_free_full (modems, g_object_unref);
if (!ctx->modem) { if (!ctx->current) {
g_printerr ("error: couldn't find sim at '%s'\n", g_printerr ("error: couldn't find sim at '%s'\n",
ctx->sim_path); ctx->sim_path);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
g_list_free_full (modems, g_object_unref);
} }
static gchar * static gchar *
@@ -755,33 +770,32 @@ get_sim_path (const gchar *path_or_index)
} }
void void
mmcli_get_sim (GDBusConnection *connection, mmcli_get_sim (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GTask *task;
GetSimContext *ctx; GetSimContext *ctx;
task = g_task_new (connection, cancellable, callback, user_data);
ctx = g_new0 (GetSimContext, 1); ctx = g_new0 (GetSimContext, 1);
ctx->sim_path = get_sim_path (path_or_index); ctx->sim_path = get_sim_path (path_or_index);
if (cancellable) g_task_set_task_data (task, ctx, (GDestroyNotify) get_sim_context_free);
ctx->cancellable = g_object_ref (cancellable);
ctx->result = g_simple_async_result_new (G_OBJECT (connection),
callback,
user_data,
mmcli_get_sim);
mmcli_get_manager (connection, mmcli_get_manager (connection,
cancellable, cancellable,
(GAsyncReadyCallback)get_sim_manager_ready, (GAsyncReadyCallback)get_sim_manager_ready,
ctx); task);
} }
MMSim * MMSim *
mmcli_get_sim_sync (GDBusConnection *connection, mmcli_get_sim_sync (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,
MMManager **o_manager, MMManager **o_manager,
MMObject **o_object) MMObject **o_object)
{ {
MMManager *manager; MMManager *manager;
GList *modems; GList *modems;

View File

@@ -57,18 +57,18 @@ MMBearer *mmcli_get_bearer_sync (GDBusConnection *connection,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
void mmcli_get_sim (GDBusConnection *connection, void mmcli_get_sim (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data); gpointer user_data);
MMSim *mmcli_get_sim_finish (GAsyncResult *res, MMSim *mmcli_get_sim_finish (GAsyncResult *res,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
MMSim *mmcli_get_sim_sync (GDBusConnection *connection, MMSim *mmcli_get_sim_sync (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
void mmcli_get_sms (GDBusConnection *connection, void mmcli_get_sms (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,