From 6f0f7d1b40a8fa8d46a2d6f4317600001cdbbb18 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:57:27 +0200 Subject: Adding upstream version 43.5. Signed-off-by: Daniel Baumann --- .../fedora-langpacks/gs-plugin-fedora-langpacks.c | 140 +++++++++++++++++++++ .../fedora-langpacks/gs-plugin-fedora-langpacks.h | 22 ++++ plugins/fedora-langpacks/gs-self-test.c | 88 +++++++++++++ plugins/fedora-langpacks/meson.build | 34 +++++ 4 files changed, 284 insertions(+) create mode 100644 plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c create mode 100644 plugins/fedora-langpacks/gs-plugin-fedora-langpacks.h create mode 100644 plugins/fedora-langpacks/gs-self-test.c create mode 100644 plugins/fedora-langpacks/meson.build (limited to 'plugins/fedora-langpacks') diff --git a/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c new file mode 100644 index 0000000..cea7d6c --- /dev/null +++ b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.c @@ -0,0 +1,140 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2019 Sundeep Anand + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This plugin does following.. + * 1. locates the active locale, say, xx + * 2. identifies related langpacks-xx + * 3. tries to add langpack-xx in app list + * 4. logs install information; not to try again + */ + +#include + +#include "gs-plugin-fedora-langpacks.h" + +struct _GsPluginFedoraLangpacks { + GsPlugin parent; + + GHashTable *locale_langpack_map; +}; + +G_DEFINE_TYPE (GsPluginFedoraLangpacks, gs_plugin_fedora_langpacks, GS_TYPE_PLUGIN) + +static void +gs_plugin_fedora_langpacks_init (GsPluginFedoraLangpacks *self) +{ + GsPlugin *plugin = GS_PLUGIN (self); + + /* this plugin should be fedora specific */ + if (!gs_plugin_check_distro_id (plugin, "fedora")) { + gs_plugin_set_enabled (plugin, FALSE); + return; + } + + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream"); + + /* + * A few language code may have more than one language packs. + * Example: en {en_GB}, pt {pt_BR}, zh {zh_CN, zh_TW, zh_HK} + */ + self->locale_langpack_map = g_hash_table_new (g_str_hash, g_str_equal); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" + g_hash_table_insert (self->locale_langpack_map, "en_GB", "langpacks-en_GB"); + g_hash_table_insert (self->locale_langpack_map, "pt_BR", "langpacks-pt_BR"); + g_hash_table_insert (self->locale_langpack_map, "zh_CN", "langpacks-zh_CN"); + g_hash_table_insert (self->locale_langpack_map, "zh_TW", "langpacks-zh_TW"); + g_hash_table_insert (self->locale_langpack_map, "zh_HK", "langpacks-zh_HK"); +#pragma GCC diagnostic pop +} + +static void +gs_plugin_fedora_langpacks_dispose (GObject *object) +{ + GsPluginFedoraLangpacks *self = GS_PLUGIN_FEDORA_LANGPACKS (object); + + g_clear_pointer (&self->locale_langpack_map, g_hash_table_unref); + + G_OBJECT_CLASS (gs_plugin_fedora_langpacks_parent_class)->dispose (object); +} + +gboolean +gs_plugin_add_langpacks (GsPlugin *plugin, + GsAppList *list, + const gchar *locale, + GCancellable *cancellable, + GError **error) +{ + GsPluginFedoraLangpacks *self = GS_PLUGIN_FEDORA_LANGPACKS (plugin); + gchar *separator; + const gchar *language_code; + g_autofree gchar *cachefn = NULL; + g_autofree gchar *langpack_pkgname = NULL; + g_auto(GStrv) language_region = NULL; + + /* This plugin may receive user locale in the form as documented in `man 3 setlocale`: + * + * language[_territory][.codeset][@modifier] + * + * e.g. `ja_JP.UTF-8` or `en_GB.iso88591` or `uz_UZ.utf8@cyrillic` or `de_DE@euro` + * Get the locale without codeset and modifier as required for langpacks. + */ + separator = strpbrk (locale, ".@"); + if (separator != NULL) + *separator = '\0'; + + if (g_strrstr (locale, "_") != NULL && + !g_hash_table_lookup (self->locale_langpack_map, locale)) { + /* + * language_code should be the langpack_source_id + * if input language_code is a locale and it doesn't + * not found in locale_langpack_map + */ + language_region = g_strsplit (locale, "_", 2); + language_code = language_region[0]; + } else { + language_code = locale; + } + + /* per-user cache */ + langpack_pkgname = g_strconcat ("langpacks-", language_code, NULL); + cachefn = gs_utils_get_cache_filename ("langpacks", langpack_pkgname, + GS_UTILS_CACHE_FLAG_WRITEABLE | + GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY, + error); + if (cachefn == NULL) + return FALSE; + if (!g_file_test (cachefn, G_FILE_TEST_EXISTS)) { + g_autoptr(GsApp) app = gs_app_new (NULL); + gs_app_set_metadata (app, "GnomeSoftware::Creator", gs_plugin_get_name (plugin)); + gs_app_set_kind (app, AS_COMPONENT_KIND_LOCALIZATION); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_set_scope (app, AS_COMPONENT_SCOPE_SYSTEM); + gs_app_add_source (app, langpack_pkgname); + gs_app_list_add (list, app); + + /* ensure we do not keep trying to install the langpack */ + if (!g_file_set_contents (cachefn, language_code, -1, error)) + return FALSE; + } + + return TRUE; +} + +static void +gs_plugin_fedora_langpacks_class_init (GsPluginFedoraLangpacksClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->dispose = gs_plugin_fedora_langpacks_dispose; +} + +GType +gs_plugin_query_type (void) +{ + return GS_TYPE_PLUGIN_FEDORA_LANGPACKS; +} diff --git a/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.h b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.h new file mode 100644 index 0000000..2a6a428 --- /dev/null +++ b/plugins/fedora-langpacks/gs-plugin-fedora-langpacks.h @@ -0,0 +1,22 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2021 Endless OS Foundation LLC + * + * Author: Philip Withnall + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#pragma once + +#include +#include + +G_BEGIN_DECLS + +#define GS_TYPE_PLUGIN_FEDORA_LANGPACKS (gs_plugin_fedora_langpacks_get_type ()) + +G_DECLARE_FINAL_TYPE (GsPluginFedoraLangpacks, gs_plugin_fedora_langpacks, GS, PLUGIN_FEDORA_LANGPACKS, GsPlugin) + +G_END_DECLS diff --git a/plugins/fedora-langpacks/gs-self-test.c b/plugins/fedora-langpacks/gs-self-test.c new file mode 100644 index 0000000..c56058a --- /dev/null +++ b/plugins/fedora-langpacks/gs-self-test.c @@ -0,0 +1,88 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2019 Richard Hughes + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include + +#include "gnome-software-private.h" + +#include "gs-test.h" + +static void +gs_plugins_fedora_langpacks_func (GsPluginLoader *plugin_loader) +{ + g_autofree gchar *cachefn = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GsApp) app = NULL; + g_autoptr(GsAppList) list = NULL; + g_autoptr(GsPluginJob) plugin_job = NULL; + g_autoptr(GsOsRelease) os_release = NULL; + + os_release = gs_os_release_new (NULL); + if (g_strcmp0 (gs_os_release_get_id (os_release), "fedora") != 0) { + g_test_skip ("not on fedora"); + return; + } + + /* start with a clean slate */ + cachefn = gs_utils_get_cache_filename ("langpacks", "langpacks-pt_BR", + GS_UTILS_CACHE_FLAG_WRITEABLE | + GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY, + &error); + g_assert_no_error (error); + g_unlink (cachefn); + + /* get langpacks result based on locale */ + plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_GET_LANGPACKS, + "search", "pt_BR.UTF-8", + "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON, + NULL); + list = gs_plugin_loader_job_process (plugin_loader, plugin_job, NULL, &error); + g_assert_nonnull (list); + g_assert_no_error (error); + + /* check if we have just one app in the list */ + g_assert_cmpint (gs_app_list_length (list), ==, 1); + + /* check app's source and kind */ + app = gs_app_list_index (list, 0); + g_assert_cmpstr (gs_app_get_source_default (app), ==, "langpacks-pt_BR"); + g_assert_cmpint (gs_app_get_kind (app), ==, AS_COMPONENT_KIND_LOCALIZATION); +} + +int +main (int argc, char **argv) +{ + gboolean ret; + g_autoptr(GError) error = NULL; + g_autoptr(GsPluginLoader) plugin_loader = NULL; + const gchar * const allowlist[] = { + "fedora-langpacks", + NULL + }; + + gs_test_init (&argc, &argv); + + /* we can only load this once per process */ + plugin_loader = gs_plugin_loader_new (NULL, NULL); + gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR); + ret = gs_plugin_loader_setup (plugin_loader, + allowlist, + NULL, + NULL, + &error); + g_assert_no_error (error); + g_assert (ret); + + /* plugin tests go here */ + g_test_add_data_func ("/gnome-software/plugins/fedora-langpacks", + plugin_loader, + (GTestDataFunc) gs_plugins_fedora_langpacks_func); + return g_test_run (); +} diff --git a/plugins/fedora-langpacks/meson.build b/plugins/fedora-langpacks/meson.build new file mode 100644 index 0000000..c3dc057 --- /dev/null +++ b/plugins/fedora-langpacks/meson.build @@ -0,0 +1,34 @@ +cargs = ['-DG_LOG_DOMAIN="GsPluginFedoraLangpacks"'] + +shared_module( + 'gs_plugin_fedora-langpacks', + sources : 'gs-plugin-fedora-langpacks.c', + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : plugin_libs, +) + +if get_option('tests') + cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"'] + e = executable( + 'gs-self-test-fedora-langpacks', + compiled_schemas, + sources : [ + 'gs-self-test.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + dependencies : [ + plugin_libs, + ], + c_args : cargs, + ) + test('gs-self-test-fedora-langpacks', e, suite: ['plugins', 'fedora-langpacks'], env: test_env) +endif -- cgit v1.2.3