[gnome-software] Support AppStream screenshot captions



commit 55d827e6d86bf87318796b8bc99902bbfb245c5e
Author: Richard Hughes <richard hughsie com>
Date:   Thu Oct 24 11:21:16 2013 +0100

    Support AppStream screenshot captions
    
    These are not yet used in the Fedora or SUSE metadata files.

 src/gs-app.c                       |    6 +++-
 src/gs-screenshot.c                |   23 ++++++++++++++++++++
 src/gs-screenshot.h                |    3 ++
 src/plugins/appstream-cache.c      |   40 ++++++++++++++++++++++++++++++++++++
 src/plugins/appstream-common.c     |    4 +++
 src/plugins/appstream-common.h     |    1 +
 src/plugins/appstream-screenshot.c |   33 +++++++++++++++++++++++++++++
 src/plugins/appstream-screenshot.h |    5 ++++
 src/plugins/gs-plugin-appstream.c  |    2 +
 9 files changed, 115 insertions(+), 2 deletions(-)
---
diff --git a/src/gs-app.c b/src/gs-app.c
index b8e5641..cfdfcf0 100644
--- a/src/gs-app.c
+++ b/src/gs-app.c
@@ -233,11 +233,13 @@ gs_app_to_string (GsApp *app)
                g_string_append_printf (str, "\tdescription:\t%s\n", priv->description);
        for (i = 0; i < priv->screenshots->len; i++) {
                ss = g_ptr_array_index (priv->screenshots, i);
-               g_string_append_printf (str, "\tscreenshot-%02i:\t%s\n",
+               tmp = gs_screenshot_get_caption (ss);
+               g_string_append_printf (str, "\tscreenshot-%02i:\t%s [%s]\n",
                                        i, gs_screenshot_get_url (ss,
                                                                  G_MAXUINT,
                                                                  G_MAXUINT,
-                                                                 NULL));
+                                                                 NULL),
+                                       tmp != NULL ? tmp : "<none>");
        }
        for (i = 0; i < priv->sources->len; i++) {
                tmp = g_ptr_array_index (priv->sources, i);
diff --git a/src/gs-screenshot.c b/src/gs-screenshot.c
index 4a744dc..01f78e2 100644
--- a/src/gs-screenshot.c
+++ b/src/gs-screenshot.c
@@ -34,6 +34,7 @@ struct GsScreenshotPrivate
 {
        GPtrArray               *array;
        gboolean                 is_default;
+       gchar                   *caption;
 };
 
 typedef struct {
@@ -155,6 +156,27 @@ gs_screenshot_get_url (GsScreenshot *screenshot,
 }
 
 /**
+ * gs_screenshot_get_caption:
+ **/
+const gchar *
+gs_screenshot_get_caption (GsScreenshot *screenshot)
+{
+       g_return_val_if_fail (GS_IS_SCREENSHOT (screenshot), NULL);
+       return screenshot->priv->caption;
+}
+
+/**
+ * gs_screenshot_set_caption:
+ **/
+void
+gs_screenshot_set_caption (GsScreenshot *screenshot, const gchar *caption)
+{
+       g_return_if_fail (GS_IS_SCREENSHOT (screenshot));
+       g_free (screenshot->priv->caption);
+       screenshot->priv->caption = g_strdup (caption);
+}
+
+/**
  * gs_screenshot_class_init:
  **/
 static void
@@ -184,6 +206,7 @@ gs_screenshot_finalize (GObject *object)
        GsScreenshot *screenshot = GS_SCREENSHOT (object);
        GsScreenshotPrivate *priv = screenshot->priv;
 
+       g_free (priv->caption);
        g_ptr_array_unref (priv->array);
 
        G_OBJECT_CLASS (gs_screenshot_parent_class)->finalize (object);
diff --git a/src/gs-screenshot.h b/src/gs-screenshot.h
index 33f0d2b..23348fc 100644
--- a/src/gs-screenshot.h
+++ b/src/gs-screenshot.h
@@ -59,6 +59,9 @@ GsScreenshot  *gs_screenshot_new              (void);
 gboolean        gs_screenshot_get_is_default   (GsScreenshot           *screenshot);
 void            gs_screenshot_set_is_default   (GsScreenshot           *screenshot,
                                                 gboolean                is_default);
+const gchar    *gs_screenshot_get_caption      (GsScreenshot           *screenshot);
+void            gs_screenshot_set_caption      (GsScreenshot           *screenshot,
+                                                const gchar            *caption);
 void            gs_screenshot_add_image        (GsScreenshot           *screenshot,
                                                 const gchar            *url,
                                                 guint                   width,
diff --git a/src/plugins/appstream-cache.c b/src/plugins/appstream-cache.c
index d2f4634..3d75f6e 100644
--- a/src/plugins/appstream-cache.c
+++ b/src/plugins/appstream-cache.c
@@ -257,6 +257,30 @@ appstream_cache_start_element_cb (GMarkupParseContext *context,
                        appstream_screenshot_set_kind (helper->screenshot, kind);
                }
                break;
+       case APPSTREAM_TAG_CAPTION:
+               if (helper->screenshot == NULL ||
+                   helper->tag != APPSTREAM_TAG_SCREENSHOT) {
+                       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 lang */
+               if (!g_markup_collect_attributes (element_name,
+                                                 attribute_names,
+                                                 attribute_values,
+                                                 error,
+                                                 G_MARKUP_COLLECT_STRDUP | G_MARKUP_COLLECT_OPTIONAL,
+                                                 "xml:lang", &helper->lang_temp,
+                                                 G_MARKUP_COLLECT_INVALID))
+                       return;
+               if (!helper->lang_temp)
+                       helper->lang_temp = g_strdup ("C");
+
        case APPSTREAM_TAG_IMAGE:
                if (helper->item_temp == NULL ||
                    helper->tag != APPSTREAM_TAG_SCREENSHOT) {
@@ -531,6 +555,9 @@ appstream_cache_end_element_cb (GMarkupParseContext *context,
        case APPSTREAM_TAG_ICON:
                helper->tag = APPSTREAM_TAG_APPLICATION;
                break;
+       case APPSTREAM_TAG_CAPTION:
+               helper->tag = APPSTREAM_TAG_SCREENSHOT;
+               break;
        case APPSTREAM_TAG_URL:
                g_free (helper->url_type_temp);
                helper->tag = APPSTREAM_TAG_APPLICATION;
@@ -722,6 +749,19 @@ appstream_cache_text_cb (GMarkupParseContext *context,
                }
                appstream_image_set_url (helper->image, text, text_len);
                break;
+       case APPSTREAM_TAG_CAPTION:
+               if (helper->screenshot == NULL) {
+                       g_set_error_literal (error,
+                                            APPSTREAM_CACHE_ERROR,
+                                            APPSTREAM_CACHE_ERROR_FAILED,
+                                            "screesnhot not started");
+                       return;
+               }
+               appstream_screenshot_set_caption (helper->screenshot,
+                                                 helper->lang_temp,
+                                                 text,
+                                                 text_len);
+               break;
        default:
                /* ignore unknown entries */
                break;
diff --git a/src/plugins/appstream-common.c b/src/plugins/appstream-common.c
index 8c1a649..a84aa1d 100644
--- a/src/plugins/appstream-common.c
+++ b/src/plugins/appstream-common.c
@@ -78,6 +78,8 @@ appstream_tag_from_string (const gchar *element_name)
                return APPSTREAM_TAG_COMPULSORY_FOR_DESKTOP;
        if (g_strcmp0 (element_name, "priority") == 0)
                return APPSTREAM_TAG_PRIORITY;
+       if (g_strcmp0 (element_name, "caption") == 0)
+               return APPSTREAM_TAG_CAPTION;
        return APPSTREAM_TAG_UNKNOWN;
 }
 
@@ -133,6 +135,8 @@ appstream_tag_to_string (AppstreamTag tag)
                return "compulsory_for_desktop";
        if (tag == APPSTREAM_TAG_PRIORITY)
                return "priority";
+       if (tag == APPSTREAM_TAG_CAPTION)
+               return "caption";
        return NULL;
 }
 
diff --git a/src/plugins/appstream-common.h b/src/plugins/appstream-common.h
index 44052b3..34de7a9 100644
--- a/src/plugins/appstream-common.h
+++ b/src/plugins/appstream-common.h
@@ -51,6 +51,7 @@ typedef enum {
        APPSTREAM_TAG_IMAGE,
        APPSTREAM_TAG_COMPULSORY_FOR_DESKTOP,
        APPSTREAM_TAG_PRIORITY,
+       APPSTREAM_TAG_CAPTION,
        APPSTREAM_TAG_LAST
 } AppstreamTag;
 
diff --git a/src/plugins/appstream-screenshot.c b/src/plugins/appstream-screenshot.c
index f28eef0..8b9bae2 100644
--- a/src/plugins/appstream-screenshot.c
+++ b/src/plugins/appstream-screenshot.c
@@ -21,12 +21,15 @@
 
 #include "config.h"
 
+#include "appstream-common.h"
 #include "appstream-screenshot.h"
 
 struct AppstreamScreenshot
 {
        AppstreamScreenshotKind  kind;
        GPtrArray               *array;
+       gchar                   *caption;
+       guint                    caption_value;
 };
 
 /**
@@ -37,6 +40,7 @@ appstream_screenshot_new (void)
 {
        AppstreamScreenshot *screenshot;
        screenshot = g_slice_new0 (AppstreamScreenshot);
+       screenshot->caption_value = G_MAXUINT;
        screenshot->kind = APPSTREAM_SCREENSHOT_KIND_NORMAL;
        screenshot->array = g_ptr_array_new_with_free_func ((GDestroyNotify) appstream_image_free);
        return screenshot;
@@ -48,6 +52,7 @@ appstream_screenshot_new (void)
 void
 appstream_screenshot_free (AppstreamScreenshot *screenshot)
 {
+       g_free (screenshot->caption);
        g_ptr_array_unref (screenshot->array);
        g_slice_free (AppstreamScreenshot, screenshot);
 }
@@ -91,6 +96,34 @@ appstream_screenshot_add_image (AppstreamScreenshot *screenshot,
 }
 
 /**
+ * appstream_screenshot_get_caption:
+ */
+const gchar *
+appstream_screenshot_get_caption (AppstreamScreenshot *app)
+{
+       return app->caption;
+}
+
+/**
+ * appstream_screenshot_set_caption:
+ */
+void
+appstream_screenshot_set_caption (AppstreamScreenshot *app,
+                                 const gchar *lang,
+                                 const gchar *caption,
+                                 gsize length)
+{
+       guint new_value;
+
+       new_value = appstream_get_locale_value (lang);
+       if (new_value < app->caption_value) {
+               g_free (app->caption);
+               app->caption = g_strndup (caption, length);
+               app->caption_value = new_value;
+       }
+}
+
+/**
  * appstream_screenshot_kind_from_string:
  */
 AppstreamScreenshotKind
diff --git a/src/plugins/appstream-screenshot.h b/src/plugins/appstream-screenshot.h
index 5996e47..524e9ee 100644
--- a/src/plugins/appstream-screenshot.h
+++ b/src/plugins/appstream-screenshot.h
@@ -47,6 +47,11 @@ void                  appstream_screenshot_set_kind  (AppstreamScreenshot    *screenshot,
                                                         AppstreamScreenshotKind kind);
 void                    appstream_screenshot_add_image (AppstreamScreenshot    *screenshot,
                                                         AppstreamImage         *image);
+const gchar            *appstream_screenshot_get_caption (AppstreamScreenshot  *app);
+void                    appstream_screenshot_set_caption (AppstreamScreenshot  *app,
+                                                        const gchar    *lang,
+                                                        const gchar    *caption,
+                                                        gsize           length);
 
 AppstreamScreenshotKind         appstream_screenshot_kind_from_string (const gchar     *kind);
 
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index a46db07..5796042 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -429,6 +429,8 @@ gs_plugin_refine_add_screenshots (GsApp *app, AppstreamApp *item)
                screenshot = gs_screenshot_new ();
                gs_screenshot_set_is_default (screenshot,
                                              ss_kind == APPSTREAM_SCREENSHOT_KIND_DEFAULT);
+               gs_screenshot_set_caption (screenshot,
+                                          appstream_screenshot_get_caption (ss));
                for (j = 0; j < images_as->len; j++) {
                        im = g_ptr_array_index (images_as, j);
                        gs_screenshot_add_image (screenshot,


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