GAppInfo: Add async API to get default Application for content type

Make possible to fetch the Application for default content type in a
thread without using blocking I/O.
This commit is contained in:
Marco Trevisan (Treviño)
2022-06-01 21:45:58 +02:00
parent abae8c1f05
commit 44dbd43170
4 changed files with 225 additions and 0 deletions

View File

@@ -778,6 +778,106 @@ g_app_info_should_show (GAppInfo *appinfo)
return (* iface->should_show) (appinfo);
}
typedef struct {
char *content_type;
gboolean must_support_uris;
} DefaultForTypeData;
static void
default_for_type_data_free (DefaultForTypeData *data)
{
g_free (data->content_type);
g_free (data);
}
static void
get_default_for_type_thread (GTask *task,
gpointer object,
gpointer task_data,
GCancellable *cancellable)
{
DefaultForTypeData *data = task_data;
GAppInfo *info;
info = g_app_info_get_default_for_type (data->content_type,
data->must_support_uris);
if (!info)
{
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("Failed to find default application for "
"content type %s"), data->content_type);
return;
}
g_task_return_pointer (task, g_steal_pointer (&info), g_object_unref);
}
/**
* g_app_info_get_default_for_type_async:
* @content_type: the content type to find a #GAppInfo for
* @must_support_uris: if %TRUE, the #GAppInfo is expected to
* support URIs
* @cancellable: optional #GCancellable object, %NULL to ignore
* @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
* @user_data: (nullable): data to pass to @callback
*
* Asynchronously gets the default #GAppInfo for a given content type.
*
* Since: 2.74
*/
void
g_app_info_get_default_for_type_async (const char *content_type,
gboolean must_support_uris,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
DefaultForTypeData *data;
g_return_if_fail (content_type != NULL && *content_type != '\0');
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
data = g_new0 (DefaultForTypeData, 1);
data->content_type = g_strdup (content_type);
data->must_support_uris = must_support_uris;
task = g_task_new (NULL, cancellable, callback, user_data);
g_task_set_source_tag (task, g_app_info_get_default_for_type_async);
g_task_set_task_data (task, data, (GDestroyNotify) default_for_type_data_free);
g_task_set_check_cancellable (task, TRUE);
g_task_run_in_thread (task, get_default_for_type_thread);
g_object_unref (task);
}
/**
* g_app_info_get_default_for_type_finish:
* @result: a #GAsyncResult
* @error: (nullable): a #GError
*
* Finishes a default #GAppInfo lookup started by
* g_app_info_get_default_for_type_async().
*
* If no #GAppInfo is found, then @error will be set to %G_IO_ERROR_NOT_FOUND.
*
* Returns: (transfer full): #GAppInfo for given @content_type or
* %NULL on error.
*
* Since: 2.74
*/
GAppInfo *
g_app_info_get_default_for_type_finish (GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
g_app_info_get_default_for_type_async, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
return g_task_propagate_pointer (G_TASK (result), error);
}
/**
* g_app_info_launch_default_for_uri:
* @uri: the uri to show