[gnome-software] Add support for the AppStream <languages> tag



commit e48f6fe7a54c9cad3b0cd0b8a349b1042639d116
Author: Richard Hughes <richard hughsie com>
Date:   Wed Feb 26 21:16:14 2014 +0000

    Add support for the AppStream <languages> tag

 src/plugins/appstream-app.c       |   33 ++++++++++++++++++++++++-
 src/plugins/appstream-app.h       |    8 +++++-
 src/plugins/appstream-cache.c     |   48 ++++++++++++++++++++++++++++++++++++-
 src/plugins/appstream-common.c    |   10 +++++++-
 src/plugins/appstream-common.h    |    4 ++-
 src/plugins/gs-plugin-appstream.c |    2 +-
 6 files changed, 99 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/appstream-app.c b/src/plugins/appstream-app.c
index 0774243..f131b9e 100644
--- a/src/plugins/appstream-app.c
+++ b/src/plugins/appstream-app.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -41,6 +41,7 @@ struct AppstreamApp
        gchar                   *description;
        gchar                   *description_lang;
        GHashTable              *urls;
+       GHashTable              *languages;
        gchar                   *project_license;
        gchar                   *project_group;
        gchar                   *icon;
@@ -83,6 +84,7 @@ appstream_app_free (AppstreamApp *app)
        g_free (app->id);
        g_ptr_array_unref (app->pkgnames);
        g_hash_table_unref (app->urls);
+       g_hash_table_unref (app->languages);
        g_free (app->project_license);
        g_free (app->project_group);
        g_free (app->icon);
@@ -140,6 +142,7 @@ appstream_app_new (void)
        app->screenshots = g_ptr_array_new_with_free_func ((GDestroyNotify) appstream_screenshot_free);
        app->token_cache = g_ptr_array_new_with_free_func ((GDestroyNotify) appstream_app_token_item_free);
        app->urls = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+       app->languages = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
        app->name_value = G_MAXUINT;
        app->summary_value = G_MAXUINT;
        app->icon_kind = APPSTREAM_APP_ICON_KIND_UNKNOWN;
@@ -483,6 +486,34 @@ appstream_app_add_mimetype (AppstreamApp *app,
 }
 
 /**
+ * appstream_app_add_locale:
+ */
+void
+appstream_app_add_locale (AppstreamApp *app,
+                         const gchar *locale,
+                         gsize length,
+                         guint percentage)
+{
+       if (percentage < 50)
+               return;
+       g_hash_table_insert (app->languages,
+                            g_strndup (locale, length),
+                            GUINT_TO_POINTER (percentage));
+}
+
+/**
+ * appstream_app_has_locale:
+ */
+guint
+appstream_app_has_locale (AppstreamApp *app,
+                         const gchar *locale)
+{
+       gpointer found;
+       found = g_hash_table_lookup (app->languages, locale);
+       return GPOINTER_TO_UINT (found);
+}
+
+/**
  * appstream_app_set_icon_kind:
  */
 void
diff --git a/src/plugins/appstream-app.h b/src/plugins/appstream-app.h
index c6f43bd..3f776dc 100644
--- a/src/plugins/appstream-app.h
+++ b/src/plugins/appstream-app.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -112,6 +112,12 @@ void                appstream_app_add_keyword              (AppstreamApp   *app,
 void            appstream_app_add_mimetype             (AppstreamApp   *app,
                                                         const gchar    *mimetype,
                                                         gsize           length);
+void            appstream_app_add_locale               (AppstreamApp   *app,
+                                                        const gchar    *locale,
+                                                        gsize           length,
+                                                        guint           percentage);
+guint           appstream_app_has_locale               (AppstreamApp   *app,
+                                                        const gchar    *locale);
 void            appstream_app_add_desktop_core         (AppstreamApp   *app,
                                                         const gchar    *desktop,
                                                         gsize           length);
diff --git a/src/plugins/appstream-cache.c b/src/plugins/appstream-cache.c
index 112a12d..d996777 100644
--- a/src/plugins/appstream-cache.c
+++ b/src/plugins/appstream-cache.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -200,6 +200,7 @@ appstream_cache_start_element_cb (GMarkupParseContext *context,
        case APPSTREAM_TAG_MIMETYPES:
        case APPSTREAM_TAG_MIMETYPE:
        case APPSTREAM_TAG_PRIORITY:
+       case APPSTREAM_TAG_LANGUAGES:
                /* ignore */
                break;
        case APPSTREAM_TAG_ID:
@@ -260,6 +261,32 @@ appstream_cache_start_element_cb (GMarkupParseContext *context,
                        appstream_screenshot_set_kind (helper->screenshot, kind);
                }
                break;
+
+       case APPSTREAM_TAG_LANG:
+               if (helper->tag != APPSTREAM_TAG_LANGUAGES) {
+                       g_set_error (error,
+                                    APPSTREAM_CACHE_ERROR,
+                                    APPSTREAM_CACHE_ERROR_FAILED,
+                                    "XML start %s invalid, tag %s",
+                                    element_name,
+                                    appstream_tag_to_string (helper->tag));
+                       return;
+               }
+
+               /* get the lang percentage */
+               for (i = 0; attribute_names[i] != NULL; i++) {
+                       if (g_strcmp0 (attribute_names[i], "percentage") == 0) {
+                               tmp = attribute_values[i];
+                               break;
+                       }
+               }
+
+               /* save language percentage */
+               if (tmp == NULL)
+                       helper->priority = 100;
+               else
+                       helper->priority = atoi (tmp);
+               break;
        case APPSTREAM_TAG_CAPTION:
                if (helper->screenshot == NULL ||
                    helper->tag != APPSTREAM_TAG_SCREENSHOT) {
@@ -564,6 +591,12 @@ appstream_cache_end_element_cb (GMarkupParseContext *context,
                helper->screenshot = NULL;
                helper->tag = APPSTREAM_TAG_SCREENSHOTS;
                break;
+       case APPSTREAM_TAG_LANG:
+               helper->tag = APPSTREAM_TAG_LANGUAGES;
+               break;
+       case APPSTREAM_TAG_LANGUAGES:
+               helper->tag = APPSTREAM_TAG_APPLICATION;
+               break;
        case APPSTREAM_TAG_ID:
        case APPSTREAM_TAG_PKGNAME:
        case APPSTREAM_TAG_APPCATEGORIES:
@@ -781,6 +814,19 @@ appstream_cache_text_cb (GMarkupParseContext *context,
                                                  text,
                                                  text_len);
                break;
+       case APPSTREAM_TAG_LANG:
+               if (helper->item_temp == NULL) {
+                       g_set_error_literal (error,
+                                            APPSTREAM_CACHE_ERROR,
+                                            APPSTREAM_CACHE_ERROR_FAILED,
+                                            "lang not started");
+                       return;
+               }
+               appstream_app_add_locale (helper->item_temp,
+                                         text,
+                                         text_len,
+                                         helper->priority);
+               break;
        default:
                /* ignore unknown entries */
                break;
diff --git a/src/plugins/appstream-common.c b/src/plugins/appstream-common.c
index 549efbd..3291e80 100644
--- a/src/plugins/appstream-common.c
+++ b/src/plugins/appstream-common.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -82,6 +82,10 @@ appstream_tag_from_string (const gchar *element_name)
                return APPSTREAM_TAG_PRIORITY;
        if (g_strcmp0 (element_name, "caption") == 0)
                return APPSTREAM_TAG_CAPTION;
+       if (g_strcmp0 (element_name, "languages") == 0)
+               return APPSTREAM_TAG_LANGUAGES;
+       if (g_strcmp0 (element_name, "lang") == 0)
+               return APPSTREAM_TAG_LANG;
        return APPSTREAM_TAG_UNKNOWN;
 }
 
@@ -139,6 +143,10 @@ appstream_tag_to_string (AppstreamTag tag)
                return "priority";
        if (tag == APPSTREAM_TAG_CAPTION)
                return "caption";
+       if (tag == APPSTREAM_TAG_LANGUAGES)
+               return "languages";
+       if (tag == APPSTREAM_TAG_LANG)
+               return "lang";
        return NULL;
 }
 
diff --git a/src/plugins/appstream-common.h b/src/plugins/appstream-common.h
index eb2d06c..f2188f9 100644
--- a/src/plugins/appstream-common.h
+++ b/src/plugins/appstream-common.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *
@@ -52,6 +52,8 @@ typedef enum {
        APPSTREAM_TAG_COMPULSORY_FOR_DESKTOP,
        APPSTREAM_TAG_PRIORITY,
        APPSTREAM_TAG_CAPTION,
+       APPSTREAM_TAG_LANGUAGES,
+       APPSTREAM_TAG_LANG,
        APPSTREAM_TAG_LAST
 } AppstreamTag;
 
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 2977f06..27f708a 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  *
- * Copyright (C) 2013 Richard Hughes <richard hughsie com>
+ * Copyright (C) 2013-2014 Richard Hughes <richard hughsie com>
  *
  * Licensed under the GNU General Public License Version 2
  *


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