From 57dae1b1c82ecc8b0e7cbbaf28e4f275172b84e2 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Mon, 20 Mar 2023 21:39:36 -0700 Subject: plugin-info: add resource methods to PeasPluginInfo Related #20 --- libpeas/peas-plugin-info-priv.h | 1 + libpeas/peas-plugin-info.c | 79 +++++++++++++++++++++++++++++++++++++++++ libpeas/peas-plugin-info.h | 8 +++++ 3 files changed, 88 insertions(+) diff --git a/libpeas/peas-plugin-info-priv.h b/libpeas/peas-plugin-info-priv.h index 1362a0d..ba2f0d6 100644 --- a/libpeas/peas-plugin-info-priv.h +++ b/libpeas/peas-plugin-info-priv.h @@ -54,6 +54,7 @@ struct _PeasPluginInfo { GHashTable *external_data; + GPtrArray *resources; GSettingsSchemaSource *schema_source; GError *error; diff --git a/libpeas/peas-plugin-info.c b/libpeas/peas-plugin-info.c index 2620145..e0d0ea8 100644 --- a/libpeas/peas-plugin-info.c +++ b/libpeas/peas-plugin-info.c @@ -107,6 +107,7 @@ peas_plugin_info_finalize (GObject *object) g_clear_pointer (&info->schema_source, g_settings_schema_source_unref); g_clear_pointer (&info->external_data, g_hash_table_unref); g_clear_pointer (&info->error, g_error_free); + g_clear_pointer (&info->resources, g_ptr_array_unref); G_OBJECT_CLASS (peas_plugin_info_parent_class)->finalize (object); } @@ -951,3 +952,81 @@ peas_plugin_info_get_external_data (const PeasPluginInfo *info, return g_hash_table_lookup (info->external_data, key); } + + /** + * peas_plugin_info_get_resource: + * @info: A #PeasPluginInfo. + * @filename: (allow-none): The filename of the resource, or %NULL. + * @error: a #GError for error reporting, or %NULL. + * + * Creates a new #GResource for the given @filename + * located in the module directory. If @filename is %NULL + * then "${module_name}.gresource" will be loaded. + * + * Returns: (transfer full): a new #GResource, or %NULL. + */ +GResource * +peas_plugin_info_get_resource (const PeasPluginInfo *info, + const char *filename, + GError **error) +{ + char *default_filename = NULL; + char *full_filename; + GResource *resource; + + g_return_val_if_fail (info != NULL, NULL); + g_return_val_if_fail (filename == NULL || filename[0] != '\0', NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + if (filename == NULL) + { + default_filename = g_strconcat (info->module_name, ".gresource", NULL); + filename = (const char *) default_filename; + } + + full_filename = g_build_filename (info->module_dir, filename, NULL); + resource = g_resource_load (full_filename, error); + + g_free (full_filename); + g_free (default_filename); + + return resource; +} + +/** + * peas_plugin_info_load_resource: + * @info: A #PeasPluginInfo. + * @filename: (allow-none): The filename of the resource, or %NULL. + * @error: a #GError for error reporting, or %NULL. + * + * Loads the resource using peas_plugin_info_get_resource() and + * registers it. The resource's lifetime will be automatically + * handled by @info. + * + * See peas_plugin_info_get_resource() for more information. + */ +void +peas_plugin_info_load_resource (const PeasPluginInfo *info, + const char *filename, + GError **error) +{ + GResource *resource; + PeasPluginInfo *info_ = (PeasPluginInfo *) info; + + resource = peas_plugin_info_get_resource (info, filename, error); + + if (resource == NULL) + return; + + if (info_->resources == NULL) + { + info_->resources = g_ptr_array_new (); + g_ptr_array_set_free_func (info_->resources, + (GDestroyNotify) g_resources_unregister); + } + + g_resources_register (resource); + + g_ptr_array_add (info_->resources, resource); + g_resource_unref (resource); +} diff --git a/libpeas/peas-plugin-info.h b/libpeas/peas-plugin-info.h index f5c4288..3313cd9 100644 --- a/libpeas/peas-plugin-info.h +++ b/libpeas/peas-plugin-info.h @@ -113,5 +113,13 @@ const char *peas_plugin_info_get_help_uri (const PeasPluginInfo * PEAS_AVAILABLE_IN_ALL const char *peas_plugin_info_get_external_data (const PeasPluginInfo *info, const char *key); +PEAS_AVAILABLE_IN_ALL +GResource *peas_plugin_info_get_resource (const PeasPluginInfo *info, + const char *filename, + GError **error); +PEAS_AVAILABLE_IN_ALL +void peas_plugin_info_load_resource (const PeasPluginInfo *info, + const char *filename, + GError **error); G_END_DECLS -- cgit v1.2.1