[libsoup/wip/remove-deprecations: 33/49] Move accept_languages_from_system() to soup-misc
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsoup/wip/remove-deprecations: 33/49] Move accept_languages_from_system() to soup-misc
- Date: Tue, 30 Jun 2020 21:57:04 +0000 (UTC)
commit eb9b6b5923a3f7f26aa1e1e25b5957e0d0ec8c99
Author: Patrick Griffis <pgriffis igalia com>
Date: Thu Mar 12 17:46:32 2020 -0700
Move accept_languages_from_system() to soup-misc
libsoup/soup-misc.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++
libsoup/soup-misc.h | 2 ++
libsoup/soup-session.c | 82 +-------------------------------------------------
3 files changed, 83 insertions(+), 81 deletions(-)
---
diff --git a/libsoup/soup-misc.c b/libsoup/soup-misc.c
index f1e954ac..95f7b48d 100644
--- a/libsoup/soup-misc.c
+++ b/libsoup/soup-misc.c
@@ -276,3 +276,83 @@ soup_host_matches_host (const gchar *host, const gchar *compare_with)
return TRUE;
return g_str_has_suffix (compare_with, host);
}
+
+/* Converts a language in POSIX format and to be RFC2616 compliant */
+/* Based on code from epiphany-webkit (ephy_langs_append_languages()) */
+static gchar *
+posix_lang_to_rfc2616 (const gchar *language)
+{
+ /* Don't include charset variants, etc */
+ if (strchr (language, '.') || strchr (language, '@'))
+ return NULL;
+
+ /* Ignore "C" locale, which g_get_language_names() always
+ * includes as a fallback.
+ */
+ if (!strcmp (language, "C"))
+ return NULL;
+
+ return g_strdelimit (g_ascii_strdown (language, -1), "_", '-');
+}
+
+/* Converts @quality from 0-100 to 0.0-1.0 and appends to @str */
+static gchar *
+add_quality_value (const gchar *str, int quality)
+{
+ g_return_val_if_fail (str != NULL, NULL);
+
+ if (quality >= 0 && quality < 100) {
+ /* We don't use %.02g because of "." vs "," locale issues */
+ if (quality % 10)
+ return g_strdup_printf ("%s;q=0.%02d", str, quality);
+ else
+ return g_strdup_printf ("%s;q=0.%d", str, quality / 10);
+ } else
+ return g_strdup (str);
+}
+
+/* Returns a RFC2616 compliant languages list from system locales */
+gchar *
+soup_get_accept_languages_from_system (void)
+{
+ const char * const * lang_names;
+ GPtrArray *langs = NULL;
+ char *lang, *langs_str;
+ int delta;
+ guint i;
+
+ lang_names = g_get_language_names ();
+ g_return_val_if_fail (lang_names != NULL, NULL);
+
+ /* Build the array of languages */
+ langs = g_ptr_array_new_with_free_func (g_free);
+ for (i = 0; lang_names[i] != NULL; i++) {
+ lang = posix_lang_to_rfc2616 (lang_names[i]);
+ if (lang)
+ g_ptr_array_add (langs, lang);
+ }
+
+ /* Add quality values */
+ if (langs->len < 10)
+ delta = 10;
+ else if (langs->len < 20)
+ delta = 5;
+ else
+ delta = 1;
+
+ for (i = 0; i < langs->len; i++) {
+ lang = langs->pdata[i];
+ langs->pdata[i] = add_quality_value (lang, 100 - i * delta);
+ g_free (lang);
+ }
+
+ /* Fallback: add "en" if list is empty */
+ if (langs->len == 0)
+ g_ptr_array_add (langs, g_strdup ("en"));
+
+ g_ptr_array_add (langs, NULL);
+ langs_str = g_strjoinv (", ", (char **)langs->pdata);
+ g_ptr_array_free (langs, TRUE);
+
+ return langs_str;
+}
diff --git a/libsoup/soup-misc.h b/libsoup/soup-misc.h
index 57800349..8aad3bdf 100644
--- a/libsoup/soup-misc.h
+++ b/libsoup/soup-misc.h
@@ -94,6 +94,8 @@ SoupAddress *soup_address_new_from_gsockaddr (GSocketAddress *addr);
gboolean soup_host_matches_host (const gchar *host,
const gchar *compare_with);
+gchar *soup_get_accept_languages_from_system (void);
+
G_END_DECLS
#endif /* __SOUP_MISC_H__ */
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index 687cbd9e..779b9349 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -367,86 +367,6 @@ ensure_socket_props (SoupSession *session)
priv->idle_timeout);
}
-/* Converts a language in POSIX format and to be RFC2616 compliant */
-/* Based on code from epiphany-webkit (ephy_langs_append_languages()) */
-static gchar *
-posix_lang_to_rfc2616 (const gchar *language)
-{
- /* Don't include charset variants, etc */
- if (strchr (language, '.') || strchr (language, '@'))
- return NULL;
-
- /* Ignore "C" locale, which g_get_language_names() always
- * includes as a fallback.
- */
- if (!strcmp (language, "C"))
- return NULL;
-
- return g_strdelimit (g_ascii_strdown (language, -1), "_", '-');
-}
-
-/* Converts @quality from 0-100 to 0.0-1.0 and appends to @str */
-static gchar *
-add_quality_value (const gchar *str, int quality)
-{
- g_return_val_if_fail (str != NULL, NULL);
-
- if (quality >= 0 && quality < 100) {
- /* We don't use %.02g because of "." vs "," locale issues */
- if (quality % 10)
- return g_strdup_printf ("%s;q=0.%02d", str, quality);
- else
- return g_strdup_printf ("%s;q=0.%d", str, quality / 10);
- } else
- return g_strdup (str);
-}
-
-/* Returns a RFC2616 compliant languages list from system locales */
-static gchar *
-accept_languages_from_system (void)
-{
- const char * const * lang_names;
- GPtrArray *langs = NULL;
- char *lang, *langs_str;
- int delta;
- guint i;
-
- lang_names = g_get_language_names ();
- g_return_val_if_fail (lang_names != NULL, NULL);
-
- /* Build the array of languages */
- langs = g_ptr_array_new_with_free_func (g_free);
- for (i = 0; lang_names[i] != NULL; i++) {
- lang = posix_lang_to_rfc2616 (lang_names[i]);
- if (lang)
- g_ptr_array_add (langs, lang);
- }
-
- /* Add quality values */
- if (langs->len < 10)
- delta = 10;
- else if (langs->len < 20)
- delta = 5;
- else
- delta = 1;
-
- for (i = 0; i < langs->len; i++) {
- lang = langs->pdata[i];
- langs->pdata[i] = add_quality_value (lang, 100 - i * delta);
- g_free (lang);
- }
-
- /* Fallback: add "en" if list is empty */
- if (langs->len == 0)
- g_ptr_array_add (langs, g_strdup ("en"));
-
- g_ptr_array_add (langs, NULL);
- langs_str = g_strjoinv (", ", (char **)langs->pdata);
- g_ptr_array_free (langs, TRUE);
-
- return langs_str;
-}
-
static void
set_tlsdb (SoupSession *session, GTlsDatabase *tlsdb)
{
@@ -636,7 +556,7 @@ soup_session_set_property (GObject *object, guint prop_id,
/* Get languages from system if needed */
if (priv->accept_language_auto)
- priv->accept_language = accept_languages_from_system ();
+ priv->accept_language = soup_get_accept_languages_from_system ();
break;
case PROP_IDLE_TIMEOUT:
priv->idle_timeout = g_value_get_uint (value);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]