cli: port mmcli_get_sim to GTask
This commit is contained in:
@@ -613,61 +613,73 @@ mmcli_get_bearer_sync (GDBusConnection *connection,
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/* SIM */
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GSimpleAsyncResult *result;
|
|
||||||
GCancellable *cancellable;
|
|
||||||
gchar *sim_path;
|
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
|
|
||||||
get_sim_context_complete (GetSimContext *ctx)
|
|
||||||
{
|
|
||||||
g_simple_async_result_complete (ctx->result);
|
|
||||||
g_object_unref (ctx->result);
|
|
||||||
ctx->result = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
MMSim *
|
MMSim *
|
||||||
mmcli_get_sim_finish (GAsyncResult *res,
|
mmcli_get_sim_finish (GAsyncResult *res,
|
||||||
MMManager **o_manager,
|
MMManager **o_manager,
|
||||||
MMObject **o_object)
|
MMObject **o_object)
|
||||||
{
|
{
|
||||||
GetSimContext *ctx;
|
GetSimResults *results;
|
||||||
|
MMSim *obj;
|
||||||
|
|
||||||
ctx = 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 (ctx->manager);
|
*o_manager = g_object_ref (results->manager);
|
||||||
if (o_object)
|
if (o_object)
|
||||||
*o_object = g_object_ref (ctx->modem);
|
*o_object = g_object_ref (results->object);
|
||||||
return g_object_ref (ctx->sim);
|
obj = g_object_ref (results->sim);
|
||||||
|
get_sim_results_free (results);
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
get_sim_ready (MMModem *modem,
|
get_sim_ready (MMModem *modem,
|
||||||
GAsyncResult *res,
|
GAsyncResult *res,
|
||||||
GetSimContext *ctx)
|
GTask *task)
|
||||||
{
|
{
|
||||||
|
GetSimContext *ctx;
|
||||||
|
GetSimResults *results;
|
||||||
|
MMSim *sim;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
ctx->sim = mm_modem_get_sim_finish (modem, res, &error);
|
ctx = g_task_get_task_data (task);
|
||||||
|
|
||||||
|
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,21 +688,26 @@ 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)
|
||||||
{
|
{
|
||||||
|
GetSimContext *ctx;
|
||||||
GList *l;
|
GList *l;
|
||||||
GList *modems;
|
GList *modems;
|
||||||
|
|
||||||
|
ctx = g_task_get_task_data (task);
|
||||||
|
|
||||||
ctx->manager = mmcli_get_manager_finish (res);
|
ctx->manager = mmcli_get_manager_finish (res);
|
||||||
|
|
||||||
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager));
|
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager));
|
||||||
@@ -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 *
|
||||||
@@ -761,20 +776,19 @@ mmcli_get_sim (GDBusConnection *connection,
|
|||||||
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 *
|
||||||
|
Reference in New Issue
Block a user