[gnome-software: 3/11] gs-plugin: Drop custom locale handling




commit 2e071fdee7b6db27f79059d19834e5630a21cea8
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed May 19 14:12:20 2021 +0100

    gs-plugin: Drop custom locale handling
    
    Rather than keeping a copy of `setlocale (LC_MESSAGES, NULL)` or `getenv
    ("GS_SELF_TEST_LOCALE")` around in each plugin and the plugin loader,
    just query `setlocale()` whenever the locale is needed.
    
    This reduces dependencies on the `GsPlugin` or `GsPluginLoader`.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 lib/gs-plugin-loader.c             | 24 ++++----------------
 lib/gs-plugin-loader.h             |  2 --
 lib/gs-plugin-private.h            |  2 --
 lib/gs-plugin.c                    | 45 --------------------------------------
 lib/gs-plugin.h                    |  1 -
 plugins/core/gs-plugin-appstream.c | 13 +++--------
 plugins/dummy/gs-self-test.c       |  3 ++-
 src/gs-update-monitor.c            |  3 ++-
 8 files changed, 11 insertions(+), 82 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index e3d7f9218..a4e8a5ad7 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -41,7 +41,6 @@ struct _GsPluginLoader
 
        GPtrArray               *plugins;
        GPtrArray               *locations;
-       gchar                   *locale;
        gchar                   *language;
        gboolean                 plugin_dir_dirty;
        SoupSession             *soup_session;
@@ -2189,7 +2188,6 @@ gs_plugin_loader_open_plugin (GsPluginLoader *plugin_loader,
                          G_CALLBACK (gs_plugin_loader_repository_changed_cb),
                          plugin_loader);
        gs_plugin_set_soup_session (plugin, plugin_loader->soup_session);
-       gs_plugin_set_locale (plugin, plugin_loader->locale);
        gs_plugin_set_language (plugin, plugin_loader->language);
        gs_plugin_set_scale (plugin, gs_plugin_loader_get_scale (plugin_loader));
        gs_plugin_set_network_monitor (plugin, plugin_loader->network_monitor);
@@ -2753,7 +2751,6 @@ gs_plugin_loader_finalize (GObject *object)
 
        g_strfreev (plugin_loader->compatible_projects);
        g_ptr_array_unref (plugin_loader->locations);
-       g_free (plugin_loader->locale);
        g_free (plugin_loader->language);
        g_ptr_array_unref (plugin_loader->file_monitors);
        g_hash_table_unref (plugin_loader->events_by_id);
@@ -2867,6 +2864,7 @@ gs_plugin_loader_init (GsPluginLoader *plugin_loader)
        g_autoptr(GError) local_error = NULL;
        const guint64 odrs_review_max_cache_age_secs = 237000;  /* 1 week */
        const guint odrs_review_n_results_max = 20;
+       const gchar *locale;
 
 #ifdef HAVE_SYSPROF
        plugin_loader->sysprof_writer = sysprof_capture_writer_new_from_env (0);
@@ -2895,15 +2893,6 @@ gs_plugin_loader_init (GsPluginLoader *plugin_loader)
                                                            SOUP_SESSION_TIMEOUT, 10,
                                                            NULL);
 
-       /* get the locale */
-       tmp = g_getenv ("GS_SELF_TEST_LOCALE");
-       if (tmp != NULL) {
-               g_debug ("using self test locale of %s", tmp);
-               plugin_loader->locale = g_strdup (tmp);
-       } else {
-               plugin_loader->locale = g_strdup (setlocale (LC_MESSAGES, NULL));
-       }
-
        /* get the category manager */
        plugin_loader->category_manager = gs_category_manager_new ();
 
@@ -2950,12 +2939,13 @@ gs_plugin_loader_init (GsPluginLoader *plugin_loader)
 
        /* get the language from the locale (i.e. strip the territory, codeset
         * and modifier) */
-       plugin_loader->language = g_strdup (plugin_loader->locale);
+       locale = setlocale (LC_MESSAGES, NULL);
+       plugin_loader->language = g_strdup (locale);
        match = strpbrk (plugin_loader->language, "._@");
        if (match != NULL)
                *match = '\0';
 
-       g_debug ("Using locale = %s, language = %s", plugin_loader->locale, plugin_loader->language);
+       g_debug ("Using locale = %s, language = %s", locale, plugin_loader->language);
 
        g_mutex_init (&plugin_loader->pending_apps_mutex);
        g_mutex_init (&plugin_loader->events_by_id_mutex);
@@ -3969,12 +3959,6 @@ gs_plugin_loader_set_max_parallel_ops (GsPluginLoader *plugin_loader,
                           error->message);
 }
 
-const gchar *
-gs_plugin_loader_get_locale (GsPluginLoader *plugin_loader)
-{
-       return plugin_loader->locale;
-}
-
 /**
  * gs_plugin_loader_get_category_manager:
  * @plugin_loader: a #GsPluginLoader
diff --git a/lib/gs-plugin-loader.h b/lib/gs-plugin-loader.h
index e4fc28103..8949d1709 100644
--- a/lib/gs-plugin-loader.h
+++ b/lib/gs-plugin-loader.h
@@ -84,8 +84,6 @@ GsPlugin      *gs_plugin_loader_find_plugin           (GsPluginLoader *plugin_loader,
 void            gs_plugin_loader_set_max_parallel_ops  (GsPluginLoader *plugin_loader,
                                                         guint           max_ops);
 
-const gchar    *gs_plugin_loader_get_locale            (GsPluginLoader *plugin_loader);
-
 GsCategoryManager *gs_plugin_loader_get_category_manager (GsPluginLoader *plugin_loader);
 
 G_END_DECLS
diff --git a/lib/gs-plugin-private.h b/lib/gs-plugin-private.h
index d785ed86d..d958371b2 100644
--- a/lib/gs-plugin-private.h
+++ b/lib/gs-plugin-private.h
@@ -35,8 +35,6 @@ void           gs_plugin_set_priority                 (GsPlugin       *plugin,
                                                         guint           priority);
 void            gs_plugin_set_name                     (GsPlugin       *plugin,
                                                         const gchar    *name);
-void            gs_plugin_set_locale                   (GsPlugin       *plugin,
-                                                        const gchar    *locale);
 void            gs_plugin_set_language                 (GsPlugin       *plugin,
                                                         const gchar    *language);
 void            gs_plugin_set_auth_array               (GsPlugin       *plugin,
diff --git a/lib/gs-plugin.c b/lib/gs-plugin.c
index 7a6729e1c..2c954b76b 100644
--- a/lib/gs-plugin.c
+++ b/lib/gs-plugin.c
@@ -61,7 +61,6 @@ typedef struct
        gboolean                 enabled;
        guint                    interactive_cnt;
        GMutex                   interactive_mutex;
-       gchar                   *locale;                /* allow-none */
        gchar                   *language;              /* allow-none */
        gchar                   *name;
        gchar                   *appstream_id;
@@ -210,7 +209,6 @@ gs_plugin_finalize (GObject *object)
        g_free (priv->name);
        g_free (priv->appstream_id);
        g_free (priv->data);
-       g_free (priv->locale);
        g_free (priv->language);
        if (priv->soup_session != NULL)
                g_object_unref (priv->soup_session);
@@ -531,32 +529,6 @@ gs_plugin_set_priority (GsPlugin *plugin, guint priority)
        priv->priority = priority;
 }
 
-/**
- * gs_plugin_get_locale:
- * @plugin: a #GsPlugin
- *
- * Gets the user locale. This is in the form documented in `man 3 setlocale`:
- * ```
- * language[_territory][.codeset][@modifier]
- * ```
- * where `language` is an
- * [ISO 639 language code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes),
- * `territory` is an
- * [ISO 3166 country code](https://en.wikipedia.org/wiki/ISO_3166-1), and
- * `codeset` is a character set or encoding identifier like `ISO-8859-1` or
- * `UTF-8`. For a list of all supported locales, run `locale -a`.
- *
- * Returns: the locale string, e.g. `en_GB` or `uz_UZ.utf8@cyrillic`
- *
- * Since: 3.22
- **/
-const gchar *
-gs_plugin_get_locale (GsPlugin *plugin)
-{
-       GsPluginPrivate *priv = gs_plugin_get_instance_private (plugin);
-       return priv->locale;
-}
-
 /**
  * gs_plugin_get_language:
  * @plugin: a #GsPlugin
@@ -578,23 +550,6 @@ gs_plugin_get_language (GsPlugin *plugin)
        return priv->language;
 }
 
-/**
- * gs_plugin_set_locale:
- * @plugin: a #GsPlugin
- * @locale: a locale string, e.g. "en_GB"
- *
- * Sets the plugin locale.
- *
- * Since: 3.22
- **/
-void
-gs_plugin_set_locale (GsPlugin *plugin, const gchar *locale)
-{
-       GsPluginPrivate *priv = gs_plugin_get_instance_private (plugin);
-       g_free (priv->locale);
-       priv->locale = g_strdup (locale);
-}
-
 /**
  * gs_plugin_set_language:
  * @plugin: a #GsPlugin
diff --git a/lib/gs-plugin.h b/lib/gs-plugin.h
index cb3256632..91a65488a 100644
--- a/lib/gs-plugin.h
+++ b/lib/gs-plugin.h
@@ -73,7 +73,6 @@ void           gs_plugin_add_flags                    (GsPlugin       *plugin,
 void            gs_plugin_remove_flags                 (GsPlugin       *plugin,
                                                         GsPluginFlags   flags);
 guint           gs_plugin_get_scale                    (GsPlugin       *plugin);
-const gchar    *gs_plugin_get_locale                   (GsPlugin       *plugin);
 const gchar    *gs_plugin_get_language                 (GsPlugin       *plugin);
 SoupSession    *gs_plugin_get_soup_session             (GsPlugin       *plugin);
 void            gs_plugin_set_soup_session             (GsPlugin       *plugin,
diff --git a/plugins/core/gs-plugin-appstream.c b/plugins/core/gs-plugin-appstream.c
index 121c9b7c7..fcdd9dc77 100644
--- a/plugins/core/gs-plugin-appstream.c
+++ b/plugins/core/gs-plugin-appstream.c
@@ -505,7 +505,6 @@ gs_plugin_appstream_check_silo (GsPlugin *plugin,
                                GError **error)
 {
        GsPluginData *priv = gs_plugin_get_data (plugin);
-       const gchar *locale;
        const gchar *test_xml;
        g_autofree gchar *blobfn = NULL;
        g_autoptr(XbBuilder) builder = xb_builder_new ();
@@ -515,7 +514,7 @@ gs_plugin_appstream_check_silo (GsPlugin *plugin,
        g_autoptr(GRWLockWriterLocker) writer_locker = NULL;
        g_autoptr(GPtrArray) parent_appdata = g_ptr_array_new_with_free_func (g_free);
        g_autoptr(GPtrArray) parent_appstream = g_ptr_array_new_with_free_func (g_free);
-
+       const gchar *const *locales = g_get_language_names ();
 
        reader_locker = g_rw_lock_reader_locker_new (&priv->silo_lock);
        /* everything is okay */
@@ -535,14 +534,8 @@ gs_plugin_appstream_check_silo (GsPlugin *plugin,
        }
 
        /* add current locales */
-       locale = g_getenv ("GS_SELF_TEST_LOCALE");
-       if (locale == NULL) {
-               const gchar *const *locales = g_get_language_names ();
-               for (guint i = 0; locales[i] != NULL; i++)
-                       xb_builder_add_locale (builder, locales[i]);
-       } else {
-               xb_builder_add_locale (builder, locale);
-       }
+       for (guint i = 0; locales[i] != NULL; i++)
+               xb_builder_add_locale (builder, locales[i]);
 
        /* only when in self test */
        test_xml = g_getenv ("GS_SELF_TEST_APPSTREAM_XML");
diff --git a/plugins/dummy/gs-self-test.c b/plugins/dummy/gs-self-test.c
index df5798cd2..7dba07053 100644
--- a/plugins/dummy/gs-self-test.c
+++ b/plugins/dummy/gs-self-test.c
@@ -9,6 +9,7 @@
 #include "config.h"
 
 #include <glib/gstdio.h>
+#include <locale.h>
 
 #include "gnome-software-private.h"
 
@@ -754,7 +755,7 @@ main (int argc, char **argv)
        g_setenv ("GS_XMLB_VERBOSE", "1", TRUE);
 
        /* set all the things required as a dummy test harness */
-       g_setenv ("GS_SELF_TEST_LOCALE", "en_GB", TRUE);
+       setlocale (LC_MESSAGES, "en_GB");
        g_setenv ("GS_SELF_TEST_DUMMY_ENABLE", "1", TRUE);
        g_setenv ("GS_SELF_TEST_PROVENANCE_SOURCES", "london*,boston", TRUE);
        g_setenv ("GS_SELF_TEST_PROVENANCE_LICENSE_SOURCES", "london*,boston", TRUE);
diff --git a/src/gs-update-monitor.c b/src/gs-update-monitor.c
index acf492aa5..cae30139a 100644
--- a/src/gs-update-monitor.c
+++ b/src/gs-update-monitor.c
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <glib/gi18n.h>
 #include <gsettings-desktop-schemas/gdesktop-enums.h>
+#include <locale.h>
 
 #include "gs-update-monitor.h"
 #include "gs-common.h"
@@ -855,7 +856,7 @@ check_language_pack (GsUpdateMonitor *monitor) {
        const gchar *locale;
        g_autoptr(GsPluginJob) plugin_job = NULL;
 
-       locale = gs_plugin_loader_get_locale (monitor->plugin_loader);
+       locale = setlocale (LC_MESSAGES, NULL);
        plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_GET_LANGPACKS,
                                         "search", locale,
                                         "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]