From dfba3853c7bd40d4d2fa9a20c291b169e0ff29e6 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Thu, 28 Jul 2022 14:22:15 +0200 Subject: [PATCH] dir: Use AccountsService to get system languages Use the new GetUsersLanguages() method from AccountsService to get the list of all the locales that each user is interested in. See https://gitlab.freedesktop.org/accountsservice/accountsservice/-/merge_requests/99 Closes: #5006 --- common/flatpak-dir.c | 65 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c index 2ce8ea5b..d19a5b57 100644 --- a/common/flatpak-dir.c +++ b/common/flatpak-dir.c @@ -16170,6 +16170,45 @@ get_accounts_dbus_proxy (void) NULL); } +static gboolean +get_all_langs_from_accounts_dbus (GDBusProxy *proxy, GPtrArray *langs) +{ + g_auto(GStrv) all_langs = NULL; + int i; + g_autoptr(GVariant) ret = NULL; + g_autoptr(GError) error = NULL; + + ret = g_dbus_proxy_call_sync (G_DBUS_PROXY (proxy), + "GetUsersLanguages", + g_variant_new ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (!ret) + { + g_debug ("Failed to get languages for all users: %s", error->message); + return FALSE; + } + + g_variant_get (ret, + "(^as)", + &all_langs); + + if (all_langs != NULL) + { + for (i = 0; all_langs[i] != NULL; i++) + { + g_autofree char *lang = NULL; + lang = flatpak_get_lang_from_locale (all_langs[i]); + if (lang != NULL && !flatpak_g_ptr_array_contains_string (langs, lang)) + g_ptr_array_add (langs, g_steal_pointer (&lang)); + } + } + + return TRUE; +} + static void get_locale_langs_from_accounts_dbus (GDBusProxy *proxy, GPtrArray *langs) { @@ -16268,20 +16307,24 @@ get_system_locales (FlatpakDir *self) g_autoptr(GDBusProxy) localed_proxy = NULL; g_autoptr(GDBusProxy) accounts_proxy = NULL; - /* Get the system default locales */ - localed_proxy = get_localed_dbus_proxy (); - if (localed_proxy != NULL) - get_locale_langs_from_localed_dbus (localed_proxy, langs); - - /* Now add the user account locales from AccountsService. If accounts_proxy is - * not NULL, it means that AccountsService exists */ accounts_proxy = get_accounts_dbus_proxy (); - if (accounts_proxy != NULL) - get_locale_langs_from_accounts_dbus (accounts_proxy, langs); + if (!get_all_langs_from_accounts_dbus (accounts_proxy, langs)) + { - g_ptr_array_add (langs, NULL); + /* Get the system default locales */ + localed_proxy = get_localed_dbus_proxy (); + if (localed_proxy != NULL) + get_locale_langs_from_localed_dbus (localed_proxy, langs); - g_once_init_leave (&cached, langs); + /* Now add the user account locales from AccountsService. If accounts_proxy is + * not NULL, it means that AccountsService exists */ + if (accounts_proxy != NULL) + get_locale_langs_from_accounts_dbus (accounts_proxy, langs); + + g_ptr_array_add (langs, NULL); + + g_once_init_leave (&cached, langs); + } } return (const GPtrArray *)cached;