cli: port mmcli_get_sms to GTask

This commit is contained in:
Aleksander Morgado
2017-10-18 22:44:16 +02:00
parent f94a93d66a
commit 050eb46f51
2 changed files with 104 additions and 85 deletions

View File

@@ -839,59 +839,66 @@ mmcli_get_sim_sync (GDBusConnection *connection,
return found; return found;
} }
/******************************************************************************/
/* SMS */
typedef struct { typedef struct {
GSimpleAsyncResult *result; gchar *sms_path;
GCancellable *cancellable;
gchar *sms_path;
MMManager *manager; MMManager *manager;
GList *modems; GList *modems;
MMObject *current; MMObject *current;
MMSms *sms;
} GetSmsContext; } GetSmsContext;
typedef struct {
MMManager *manager;
MMObject *object;
MMSms *sms;
} GetSmsResults;
static void
get_sms_results_free (GetSmsResults *results)
{
g_object_unref (results->manager);
g_object_unref (results->object);
g_object_unref (results->sms);
g_free (results);
}
static void static void
get_sms_context_free (GetSmsContext *ctx) get_sms_context_free (GetSmsContext *ctx)
{ {
if (ctx->current) if (ctx->current)
g_object_unref (ctx->current); 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->sms)
g_object_unref (ctx->sms);
g_list_free_full (ctx->modems, g_object_unref); g_list_free_full (ctx->modems, g_object_unref);
g_free (ctx->sms_path); g_free (ctx->sms_path);
g_free (ctx); g_free (ctx);
} }
static void
get_sms_context_complete (GetSmsContext *ctx)
{
g_simple_async_result_complete (ctx->result);
g_object_unref (ctx->result);
ctx->result = NULL;
}
MMSms * MMSms *
mmcli_get_sms_finish (GAsyncResult *res, mmcli_get_sms_finish (GAsyncResult *res,
MMManager **o_manager, MMManager **o_manager,
MMObject **o_object) MMObject **o_object)
{ {
GetSmsContext *ctx; GetSmsResults *results;
MMSms *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->current); *o_object = g_object_ref (results->object);
return g_object_ref (ctx->sms); obj = g_object_ref (results->sms);
get_sms_results_free (results);
return obj;
} }
static void look_for_sms_in_modem (GetSmsContext *ctx); static void look_for_sms_in_modem (GTask *task);
static MMSms * static MMSms *
find_sms_in_list (GList *list, find_sms_in_list (GList *list,
const gchar *sms_path) const gchar *sms_path)
{ {
GList *l; GList *l;
@@ -910,11 +917,16 @@ find_sms_in_list (GList *list,
static void static void
list_sms_ready (MMModemMessaging *modem, list_sms_ready (MMModemMessaging *modem,
GAsyncResult *res, GAsyncResult *res,
GetSmsContext *ctx) GTask *task)
{ {
GList *sms_list; GetSmsContext *ctx;
GError *error = NULL; GetSmsResults *results;
MMSms *found;
GList *sms_list;
GError *error = NULL;
ctx = g_task_get_task_data (task);
sms_list = mm_modem_messaging_list_finish (modem, res, &error); sms_list = mm_modem_messaging_list_finish (modem, res, &error);
if (error) { if (error) {
@@ -924,28 +936,32 @@ list_sms_ready (MMModemMessaging *modem,
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
ctx->sms = find_sms_in_list (sms_list, ctx->sms_path); found = find_sms_in_list (sms_list, ctx->sms_path);
g_list_free_full (sms_list, g_object_unref); g_list_free_full (sms_list, g_object_unref);
/* Found! */ if (!found) {
if (ctx->sms) { /* Not found, try with next modem */
g_simple_async_result_set_op_res_gpointer ( look_for_sms_in_modem (task);
ctx->result,
ctx,
(GDestroyNotify)get_sms_context_free);
get_sms_context_complete (ctx);
return; return;
} }
/* Not found, try with next modem */ /* Found! */
look_for_sms_in_modem (ctx); results = g_new (GetSmsResults, 1);
results->manager = g_object_ref (ctx->manager);
results->object = g_object_ref (ctx->current);
results->sms = found;
g_task_return_pointer (task, results, (GDestroyNotify) get_sms_results_free);
g_object_unref (task);
} }
static void static void
look_for_sms_in_modem (GetSmsContext *ctx) look_for_sms_in_modem (GTask *task)
{ {
GetSmsContext *ctx;
MMModemMessaging *modem; MMModemMessaging *modem;
ctx = g_task_get_task_data (task);
if (!ctx->modems) { if (!ctx->modems) {
g_printerr ("error: couldn't find SMS at '%s': 'not found in any modem'\n", g_printerr ("error: couldn't find SMS at '%s': 'not found in any modem'\n",
ctx->sms_path); ctx->sms_path);
@@ -957,27 +973,31 @@ look_for_sms_in_modem (GetSmsContext *ctx)
ctx->modems = g_list_delete_link (ctx->modems, ctx->modems); ctx->modems = g_list_delete_link (ctx->modems, ctx->modems);
modem = mm_object_get_modem_messaging (ctx->current); modem = mm_object_get_modem_messaging (ctx->current);
if (modem) { if (!modem) {
g_debug ("Looking for sms '%s' in modem '%s'...", /* Current modem has no messaging capabilities, try with next modem */
ctx->sms_path, look_for_sms_in_modem (task);
mm_object_get_path (ctx->current));
mm_modem_messaging_list (modem,
ctx->cancellable,
(GAsyncReadyCallback)list_sms_ready,
ctx);
g_object_unref (modem);
return; return;
} }
/* Current modem has no messaging capabilities, try with next modem */ g_debug ("Looking for sms '%s' in modem '%s'...",
look_for_sms_in_modem (ctx); ctx->sms_path,
mm_object_get_path (ctx->current));
mm_modem_messaging_list (modem,
g_task_get_cancellable (task),
(GAsyncReadyCallback)list_sms_ready,
task);
g_object_unref (modem);
} }
static void static void
get_sms_manager_ready (GDBusConnection *connection, get_sms_manager_ready (GDBusConnection *connection,
GAsyncResult *res, GAsyncResult *res,
GetSmsContext *ctx) GTask *task)
{ {
GetSmsContext *ctx;
ctx = g_task_get_task_data (task);
ctx->manager = mmcli_get_manager_finish (res); ctx->manager = mmcli_get_manager_finish (res);
ctx->modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager)); ctx->modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager));
if (!ctx->modems) { if (!ctx->modems) {
@@ -986,7 +1006,7 @@ get_sms_manager_ready (GDBusConnection *connection,
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
look_for_sms_in_modem (ctx); look_for_sms_in_modem (task);
} }
static gchar * static gchar *
@@ -1018,33 +1038,32 @@ get_sms_path (const gchar *path_or_index)
} }
void void
mmcli_get_sms (GDBusConnection *connection, mmcli_get_sms (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;
GetSmsContext *ctx; GetSmsContext *ctx;
task = g_task_new (connection, cancellable, callback, user_data);
ctx = g_new0 (GetSmsContext, 1); ctx = g_new0 (GetSmsContext, 1);
ctx->sms_path = get_sms_path (path_or_index); ctx->sms_path = get_sms_path (path_or_index);
if (cancellable) g_task_set_task_data (task, ctx, (GDestroyNotify) get_sms_context_free);
ctx->cancellable = g_object_ref (cancellable);
ctx->result = g_simple_async_result_new (G_OBJECT (connection),
callback,
user_data,
mmcli_get_sms);
mmcli_get_manager (connection, mmcli_get_manager (connection,
cancellable, cancellable,
(GAsyncReadyCallback)get_sms_manager_ready, (GAsyncReadyCallback)get_sms_manager_ready,
ctx); task);
} }
MMSms * MMSms *
mmcli_get_sms_sync (GDBusConnection *connection, mmcli_get_sms_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

@@ -70,18 +70,18 @@ MMSim *mmcli_get_sim_sync (GDBusConnection *connection,
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,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data); gpointer user_data);
MMSms *mmcli_get_sms_finish (GAsyncResult *res, MMSms *mmcli_get_sms_finish (GAsyncResult *res,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
MMSms *mmcli_get_sms_sync (GDBusConnection *connection, MMSms *mmcli_get_sms_sync (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
void mmcli_get_call (GDBusConnection *connection, void mmcli_get_call (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,