[gnome-software] Parse screenshots from the AppStream data



commit 75f4152405afe0f35a76f4fa09ebe2af645c07b6
Author: Richard Hughes <richard hughsie com>
Date:   Fri Oct 4 10:35:59 2013 +0100

    Parse screenshots from the AppStream data

 src/plugins/appstream-app.c   |   21 +++++++++
 src/plugins/appstream-app.h   |    5 ++
 src/plugins/appstream-cache.c |   90 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 0 deletions(-)
---
diff --git a/src/plugins/appstream-app.c b/src/plugins/appstream-app.c
index b0802ea..6c28c1a 100644
--- a/src/plugins/appstream-app.c
+++ b/src/plugins/appstream-app.c
@@ -44,6 +44,7 @@ struct AppstreamApp
        GPtrArray               *keywords;
        gpointer                 userdata;
        GDestroyNotify           userdata_destroy_func;
+       GPtrArray               *screenshots; /* of AppstreamScreenshot */
 };
 
 /**
@@ -87,6 +88,7 @@ appstream_app_free (AppstreamApp *app)
        g_free (app->description);
        g_ptr_array_unref (app->appcategories);
        g_ptr_array_unref (app->keywords);
+       g_ptr_array_unref (app->screenshots);
        if (app->userdata_destroy_func != NULL)
                app->userdata_destroy_func (app->userdata);
        g_slice_free (AppstreamApp, app);
@@ -123,6 +125,7 @@ appstream_app_new (void)
        app = g_slice_new0 (AppstreamApp);
        app->appcategories = g_ptr_array_new_with_free_func (g_free);
        app->keywords = g_ptr_array_new_with_free_func (g_free);
+       app->screenshots = g_ptr_array_new_with_free_func ((GDestroyNotify) appstream_screenshot_free);
        app->name_value = G_MAXUINT;
        app->summary_value = G_MAXUINT;
        app->description_value = G_MAXUINT;
@@ -381,6 +384,24 @@ appstream_app_set_icon_kind (AppstreamApp *app,
 }
 
 /**
+ * appstream_app_add_screenshot:
+ */
+void
+appstream_app_add_screenshot (AppstreamApp *app, AppstreamScreenshot *screenshot)
+{
+       g_ptr_array_add (app->screenshots, screenshot);
+}
+
+/**
+ * appstream_app_get_screenshots:
+ */
+GPtrArray *
+appstream_app_get_screenshots (AppstreamApp *app)
+{
+       return app->screenshots;
+}
+
+/**
  * appstream_app_search_matches:
  */
 gboolean
diff --git a/src/plugins/appstream-app.h b/src/plugins/appstream-app.h
index e845233..0e38dc0 100644
--- a/src/plugins/appstream-app.h
+++ b/src/plugins/appstream-app.h
@@ -24,6 +24,8 @@
 
 #include <glib.h>
 
+#include "appstream-screenshot.h"
+
 G_BEGIN_DECLS
 
 typedef enum {
@@ -90,6 +92,9 @@ gpointer       appstream_app_get_userdata             (AppstreamApp   *app);
 void            appstream_app_set_userdata             (AppstreamApp   *app,
                                                         gpointer        userdata,
                                                         GDestroyNotify  userdata_destroy_func);
+void            appstream_app_add_screenshot           (AppstreamApp   *app,
+                                                        AppstreamScreenshot *screenshot);
+GPtrArray      *appstream_app_get_screenshots          (AppstreamApp   *app);
 gboolean        appstream_app_search_matches           (AppstreamApp   *app,
                                                         const gchar    *search);
 
diff --git a/src/plugins/appstream-cache.c b/src/plugins/appstream-cache.c
index c512844..7f67495 100644
--- a/src/plugins/appstream-cache.c
+++ b/src/plugins/appstream-cache.c
@@ -21,8 +21,12 @@
 
 #include "config.h"
 
+#include <stdlib.h>
+
 #include "appstream-cache.h"
 #include "appstream-common.h"
+#include "appstream-image.h"
+#include "appstream-screenshot.h"
 
 static void    appstream_cache_finalize        (GObject        *object);
 
@@ -110,6 +114,8 @@ typedef struct {
        char                    *lang_temp;
        AppstreamCache          *cache;
        AppstreamTag             tag;
+       AppstreamImage          *image;
+       AppstreamScreenshot     *screenshot;
 } AppstreamCacheHelper;
 
 /**
@@ -127,7 +133,9 @@ appstream_cache_start_element_cb (GMarkupParseContext *context,
        AppstreamCacheHelper *helper = (AppstreamCacheHelper *) user_data;
        AppstreamTag section_new;
        const gchar *tmp = NULL;
+       guint height = 0;
        guint i;
+       guint width = 0;
 
        /* process tag start */
        section_new = appstream_tag_from_string (element_name);
@@ -139,6 +147,66 @@ appstream_cache_start_element_cb (GMarkupParseContext *context,
        case APPSTREAM_TAG_KEYWORD:
                /* ignore */
                break;
+       case APPSTREAM_TAG_SCREENSHOT:
+               if (helper->item_temp == NULL ||
+                   helper->tag != APPSTREAM_TAG_SCREENSHOTS) {
+                       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 screenshot kind */
+               for (i = 0; attribute_names[i] != NULL; i++) {
+                       if (g_strcmp0 (attribute_names[i], "type") == 0) {
+                               tmp = attribute_values[i];
+                               break;
+                       }
+               }
+
+               /* create screenshot */
+               helper->screenshot = appstream_screenshot_new ();
+               if (tmp != NULL) {
+                       AppstreamScreenshotKind kind;
+                       kind = appstream_screenshot_kind_from_string (tmp);
+                       appstream_screenshot_set_kind (helper->screenshot, kind);
+               }
+               break;
+       case APPSTREAM_TAG_IMAGE:
+               if (helper->item_temp == 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 the image attributes */
+               for (i = 0; attribute_names[i] != NULL; i++) {
+                       if (g_strcmp0 (attribute_names[i], "type") == 0)
+                               tmp = attribute_values[i];
+                       else if (g_strcmp0 (attribute_names[i], "width") == 0)
+                               width = atoi (attribute_values[i]);
+                       else if (g_strcmp0 (attribute_names[i], "height") == 0)
+                               height = atoi (attribute_values[i]);
+               }
+
+               /* create image */
+               helper->image = appstream_image_new ();
+               appstream_image_set_width (helper->image, width);
+               appstream_image_set_height (helper->image, height);
+               if (tmp != NULL) {
+                       AppstreamImageKind kind;
+                       kind = appstream_image_kind_from_string (tmp);
+                       appstream_image_set_kind (helper->image, kind);
+               }
+               break;
        case APPSTREAM_TAG_APPLICATION:
                if (helper->item_temp != NULL ||
                    helper->tag != APPSTREAM_TAG_APPLICATIONS) {
@@ -282,6 +350,18 @@ appstream_cache_end_element_cb (GMarkupParseContext *context,
                helper->item_temp = NULL;
                helper->tag = APPSTREAM_TAG_APPLICATIONS;
                break;
+       case APPSTREAM_TAG_IMAGE:
+               appstream_screenshot_add_image (helper->screenshot,
+                                               helper->image);
+               helper->image = NULL;
+               helper->tag = APPSTREAM_TAG_SCREENSHOT;
+               break;
+       case APPSTREAM_TAG_SCREENSHOT:
+               appstream_app_add_screenshot (helper->item_temp,
+                                             helper->screenshot);
+               helper->screenshot = NULL;
+               helper->tag = APPSTREAM_TAG_SCREENSHOTS;
+               break;
        case APPSTREAM_TAG_ID:
        case APPSTREAM_TAG_PKGNAME:
        case APPSTREAM_TAG_APPCATEGORIES:
@@ -429,6 +509,16 @@ appstream_cache_text_cb (GMarkupParseContext *context,
                }
                appstream_app_set_icon (helper->item_temp, text, text_len);
                break;
+       case APPSTREAM_TAG_IMAGE:
+               if (helper->image == NULL) {
+                       g_set_error_literal (error,
+                                            APPSTREAM_CACHE_ERROR,
+                                            APPSTREAM_CACHE_ERROR_FAILED,
+                                            "image not started");
+                       return;
+               }
+               appstream_image_set_url (helper->image, text, text_len);
+               break;
        default:
                /* ignore unknown entries */
                break;


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