[gnome-software] Show the best locale when using AppData files for the long description



commit a7eced7e36a87b856f2a2dc5106c45b86c2f196b
Author: Richard Hughes <richard hughsie com>
Date:   Sat Oct 5 19:58:22 2013 +0100

    Show the best locale when using AppData files for the long description
    
    Resolves: https://bugzilla.gnome.org/show_bug.cgi?id=709436

 src/plugins/gs-plugin-appdata.c |   59 ++++++++++++++++++++------------------
 1 files changed, 31 insertions(+), 28 deletions(-)
---
diff --git a/src/plugins/gs-plugin-appdata.c b/src/plugins/gs-plugin-appdata.c
index be2df07..eb30f66 100644
--- a/src/plugins/gs-plugin-appdata.c
+++ b/src/plugins/gs-plugin-appdata.c
@@ -134,25 +134,36 @@ typedef struct {
        GString                 *string;
        AppStreamDescriptionTag  description_tag;
        gchar                   *lang;
+       guint                    locale_value;
 } AppstreamCacheHelper;
 
 /**
  * appstream_description_build:
  */
 static void
-appstream_description_build (GString *string,
-                            const gchar *lang,
+appstream_description_build (AppstreamCacheHelper *helper,
                             AppStreamDescriptionTag tag,
                             const gchar *text)
 {
+       guint locale_value;
+
        /* we are not interested */
-       if (string == NULL)
+       if (helper->string == NULL)
                return;
 
-       /* ignore anything that's not C */
-       if (g_strcmp0 (lang, "C") != 0)
+       /* is this worse than the locale we're already showing */
+       locale_value = appstream_get_locale_value (helper->lang);
+       if (locale_value > helper->locale_value)
                return;
 
+       /* is this better than the previous locale */
+       if (locale_value < helper->locale_value) {
+               g_debug ("Dumping existing string for locale %s!", helper->lang);
+               g_string_set_size (helper->string, 0);
+               helper->locale_value = locale_value;
+       }
+
+
        /* format markup in the same way as the distro pre-processor */
        switch (tag) {
        case APPSTREAM_DESCRIPTION_TAG_START:
@@ -166,18 +177,18 @@ appstream_description_build (GString *string,
                break;
        case APPSTREAM_DESCRIPTION_TAG_END:
                /* remove trailing newline */
-               g_string_truncate (string, string->len - 1);
+               g_string_truncate (helper->string, helper->string->len - 1);
                break;
                break;
        case APPSTREAM_DESCRIPTION_TAG_P_CONTENT:
-               g_string_append_printf (string, "%s\n", text);
+               g_string_append_printf (helper->string, "%s\n", text);
                break;
        case APPSTREAM_DESCRIPTION_TAG_LI_CONTENT:
-               g_string_append_printf (string, " • %s\n", text);
+               g_string_append_printf (helper->string, " • %s\n", text);
                break;
        case APPSTREAM_DESCRIPTION_TAG_P_START:
-               if (string->len > 0)
-                       g_string_append (string, "\n");
+               if (helper->string->len > 0)
+                       g_string_append (helper->string, "\n");
                break;
        default:
                break;
@@ -218,20 +229,17 @@ appdata_parse_start_element_cb (GMarkupParseContext *context,
 
                /* build string */
                if (g_strcmp0 (element_name, "p") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_P_START,
                                                     NULL);
                        helper->description_tag = APPSTREAM_DESCRIPTION_TAG_P_CONTENT;
                } else if (g_strcmp0 (element_name, "ul") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_UL_START,
                                                     NULL);
                        helper->description_tag = APPSTREAM_DESCRIPTION_TAG_UL_CONTENT;
                } else if (g_strcmp0 (element_name, "li") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_LI_START,
                                                     NULL);
                        helper->description_tag = APPSTREAM_DESCRIPTION_TAG_LI_CONTENT;
@@ -247,8 +255,7 @@ appdata_parse_start_element_cb (GMarkupParseContext *context,
                 * doing all this string munging is moderately expensive */
                if (gs_app_get_description (helper->app) == NULL)
                        helper->string = g_string_new ("");
-               appstream_description_build (helper->string,
-                                            helper->lang,
+               appstream_description_build (helper,
                                             APPSTREAM_DESCRIPTION_TAG_START,
                                             NULL);
                break;
@@ -273,23 +280,19 @@ appdata_parse_end_element_cb (GMarkupParseContext *context,
        AppstreamCacheHelper *helper = (AppstreamCacheHelper *) user_data;
        if (helper->tag == APPSTREAM_TAG_DESCRIPTION) {
                if (g_strcmp0 (element_name, "p") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_P_END,
                                                     NULL);
                } else if (g_strcmp0 (element_name, "ul") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_UL_END,
                                                     NULL);
                } else if (g_strcmp0 (element_name, "li") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_LI_END,
                                                     NULL);
                } else if (g_strcmp0 (element_name, "description") == 0) {
-                       appstream_description_build (helper->string,
-                                                    helper->lang,
+                       appstream_description_build (helper,
                                                     APPSTREAM_DESCRIPTION_TAG_END,
                                                     NULL);
                        if (helper->string != NULL) {
@@ -373,8 +376,7 @@ appdata_parse_text_cb (GMarkupParseContext *context,
                tmp = appdata_xml_unmunge (text, text_len);
                if (tmp == NULL)
                        break;
-               appstream_description_build (helper->string,
-                                            helper->lang,
+               appstream_description_build (helper,
                                             helper->description_tag,
                                             tmp);
                break;
@@ -451,6 +453,7 @@ gs_plugin_refine_by_local_appdata (GsApp *app,
        /* parse file */
        helper = g_new0 (AppstreamCacheHelper, 1);
        helper->app = app;
+       helper->locale_value = G_MAXUINT;
        ctx = g_markup_parse_context_new (&parser,
                                          G_MARKUP_PREFIX_ERROR_POSITION,
                                          helper,


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