cli: port mmcli_get_call to GTask

This commit is contained in:
Aleksander Morgado
2017-10-18 23:17:25 +02:00
parent 6f9e2003e0
commit 4f0a796ece
2 changed files with 135 additions and 116 deletions

View File

@@ -1142,63 +1142,66 @@ mmcli_get_sms_sync (GDBusConnection *connection,
return found; return found;
} }
const gchar * /******************************************************************************/
mmcli_get_state_reason_string (MMModemStateChangeReason reason) /* Call */
{
switch (reason) {
case MM_MODEM_STATE_CHANGE_REASON_UNKNOWN:
return "None or unknown";
case MM_MODEM_STATE_CHANGE_REASON_USER_REQUESTED:
return "User request";
case MM_MODEM_STATE_CHANGE_REASON_SUSPEND:
return "Suspend";
case MM_MODEM_STATE_CHANGE_REASON_FAILURE:
return "Failure";
}
g_warn_if_reached ();
return NULL;
}
typedef struct { typedef struct {
GSimpleAsyncResult *result; gchar *call_path;
GCancellable *cancellable;
gchar *call_path;
MMManager *manager; MMManager *manager;
GList *modems; GList *modems;
MMObject *current; MMObject *current;
MMCall *call; } GetCallContext;
} GetVoiceContext;
typedef struct {
MMManager *manager;
MMObject *object;
MMCall *call;
} GetCallResults;
static void static void
get_voice_context_free (GetVoiceContext *ctx) get_call_results_free (GetCallResults *results)
{
g_object_unref (results->manager);
g_object_unref (results->object);
g_object_unref (results->call);
g_free (results);
}
static void
get_call_context_free (GetCallContext *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->call)
g_object_unref (ctx->call);
g_list_free_full (ctx->modems, g_object_unref); g_list_free_full (ctx->modems, g_object_unref);
g_free (ctx->call_path); g_free (ctx->call_path);
g_free (ctx); g_free (ctx);
} }
static void MMCall *
get_voice_context_complete (GetVoiceContext *ctx) mmcli_get_call_finish (GAsyncResult *res,
MMManager **o_manager,
MMObject **o_object)
{ {
g_simple_async_result_complete (ctx->result); GetCallResults *results;
g_object_unref (ctx->result); MMCall *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->call);
get_call_results_free (results);
return obj;
} }
static void look_for_call_in_modem (GTask *task);
static void look_for_call_in_modem (GetVoiceContext *ctx);
static MMCall * static MMCall *
find_call_in_list (GList *list, find_call_in_list (GList *list,
const gchar *call_path) const gchar *call_path)
{ {
GList *l; GList *l;
@@ -1216,12 +1219,17 @@ find_call_in_list (GList *list,
} }
static void static void
list_call_ready (MMModemVoice *modem, list_calls_ready (MMModemVoice *modem,
GAsyncResult *res, GAsyncResult *res,
GetVoiceContext *ctx) GTask *task)
{ {
GList *call_list; GetCallContext *ctx;
GError *error = NULL; GetCallResults *results;
MMCall *found;
GList *call_list;
GError *error = NULL;
ctx = g_task_get_task_data (task);
call_list = mm_modem_voice_list_calls_finish (modem, res, &error); call_list = mm_modem_voice_list_calls_finish (modem, res, &error);
if (error) { if (error) {
@@ -1231,27 +1239,31 @@ list_call_ready (MMModemVoice *modem,
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
ctx->call = find_call_in_list (call_list, ctx->call_path); found = find_call_in_list (call_list, ctx->call_path);
g_list_free_full (call_list, g_object_unref); g_list_free_full (call_list, g_object_unref);
/* Found! */ if (!found) {
if (ctx->call) { /* Not found, try with next modem */
g_simple_async_result_set_op_res_gpointer ( look_for_call_in_modem (task);
ctx->result,
ctx,
(GDestroyNotify)get_voice_context_free);
get_voice_context_complete (ctx);
return; return;
} }
/* Not found, try with next modem */ /* Found! */
look_for_call_in_modem (ctx); results = g_new (GetCallResults, 1);
results->manager = g_object_ref (ctx->manager);
results->object = g_object_ref (ctx->current);
results->call = found;
g_task_return_pointer (task, results, (GDestroyNotify) get_call_results_free);
g_object_unref (task);
} }
static void static void
look_for_call_in_modem (GetVoiceContext *ctx) look_for_call_in_modem (GTask *task)
{ {
MMModemVoice *modem; GetCallContext *ctx;
MMModemVoice *modem;
ctx = g_task_get_task_data (task);
if (!ctx->modems) { if (!ctx->modems) {
g_printerr ("error: couldn't find call at '%s': 'not found in any modem'\n", g_printerr ("error: couldn't find call at '%s': 'not found in any modem'\n",
@@ -1264,27 +1276,31 @@ look_for_call_in_modem (GetVoiceContext *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_voice (ctx->current); modem = mm_object_get_modem_voice (ctx->current);
if (modem) { if (!modem) {
g_debug ("Looking for call '%s' in modem '%s'...", /* Current modem has no messaging capabilities, try with next modem */
ctx->call_path, look_for_call_in_modem (task);
mm_object_get_path (ctx->current));
mm_modem_voice_list_calls (modem,
ctx->cancellable,
(GAsyncReadyCallback)list_call_ready,
ctx);
g_object_unref (modem);
return; return;
} }
/* Current modem has no messaging capabilities, try with next modem */ g_debug ("Looking for call '%s' in modem '%s'...",
look_for_call_in_modem (ctx); ctx->call_path,
mm_object_get_path (ctx->current));
mm_modem_voice_list_calls (modem,
g_task_get_cancellable (task),
(GAsyncReadyCallback)list_calls_ready,
task);
g_object_unref (modem);
} }
static void static void
get_voice_manager_ready (GDBusConnection *connection, get_call_manager_ready (GDBusConnection *connection,
GAsyncResult *res, GAsyncResult *res,
GetVoiceContext *ctx) GTask *task)
{ {
GetCallContext *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) {
@@ -1293,7 +1309,7 @@ get_voice_manager_ready (GDBusConnection *connection,
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
look_for_call_in_modem (ctx); look_for_call_in_modem (task);
} }
static gchar * static gchar *
@@ -1324,49 +1340,33 @@ get_call_path (const gchar *path_or_index)
return call_path; return call_path;
} }
MMCall *
mmcli_get_call_finish (GAsyncResult *res,
MMManager **o_manager,
MMObject **o_object)
{
GetVoiceContext *ctx;
ctx = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
if (o_manager)
*o_manager = g_object_ref (ctx->manager);
if (o_object)
*o_object = g_object_ref (ctx->current);
return g_object_ref (ctx->call);
}
void void
mmcli_get_call (GDBusConnection *connection, mmcli_get_call (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)
{ {
GetVoiceContext *ctx; GTask *task;
GetCallContext *ctx;
ctx = g_new0 (GetVoiceContext, 1); task = g_task_new (connection, cancellable, callback, user_data);
ctx = g_new0 (GetCallContext, 1);
ctx->call_path = get_call_path (path_or_index); ctx->call_path = get_call_path (path_or_index);
if (cancellable) g_task_set_task_data (task, ctx, (GDestroyNotify) get_call_context_free);
ctx->cancellable = g_object_ref (cancellable);
ctx->result = g_simple_async_result_new (G_OBJECT (connection),
callback,
user_data,
mmcli_get_call);
mmcli_get_manager (connection, mmcli_get_manager (connection,
cancellable, cancellable,
(GAsyncReadyCallback)get_voice_manager_ready, (GAsyncReadyCallback)get_call_manager_ready,
ctx); task);
} }
MMCall * MMCall *
mmcli_get_call_sync (GDBusConnection *connection, mmcli_get_call_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;
@@ -1431,6 +1431,26 @@ mmcli_get_call_sync (GDBusConnection *connection,
return found; return found;
} }
/******************************************************************************/
const gchar *
mmcli_get_state_reason_string (MMModemStateChangeReason reason)
{
switch (reason) {
case MM_MODEM_STATE_CHANGE_REASON_UNKNOWN:
return "None or unknown";
case MM_MODEM_STATE_CHANGE_REASON_USER_REQUESTED:
return "User request";
case MM_MODEM_STATE_CHANGE_REASON_SUSPEND:
return "Suspend";
case MM_MODEM_STATE_CHANGE_REASON_FAILURE:
return "Failure";
}
g_warn_if_reached ();
return NULL;
}
/* Common options */ /* Common options */
static gchar *modem_str; static gchar *modem_str;
static gchar *bearer_str; static gchar *bearer_str;

View File

@@ -83,19 +83,18 @@ MMSms *mmcli_get_sms_sync (GDBusConnection *connection,
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,
GCancellable *cancellable, GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data); gpointer user_data);
MMCall *mmcli_get_call_finish (GAsyncResult *res, MMCall *mmcli_get_call_finish (GAsyncResult *res,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
MMCall *mmcli_get_call_sync (GDBusConnection *connection, MMCall *mmcli_get_call_sync (GDBusConnection *connection,
const gchar *path_or_index, const gchar *path_or_index,
MMManager **manager, MMManager **manager,
MMObject **object); MMObject **object);
const gchar *mmcli_get_state_reason_string (MMModemStateChangeReason reason); const gchar *mmcli_get_state_reason_string (MMModemStateChangeReason reason);