[gnome-software] Search all datadirs at startup so that local builds can use the system AppStream data



commit 19d9e0d6d0451388cc63f0c0b89e2ee9b64c76cb
Author: Richard Hughes <richard hughsie com>
Date:   Fri Sep 6 16:08:39 2013 +0100

    Search all datadirs at startup so that local builds can use the system AppStream data

 src/plugins/appstream-cache.c     |   15 +++++++++++
 src/plugins/appstream-cache.h     |    1 +
 src/plugins/gs-plugin-appstream.c |   51 ++++++++++++++++++++++++------------
 3 files changed, 50 insertions(+), 17 deletions(-)
---
diff --git a/src/plugins/appstream-cache.c b/src/plugins/appstream-cache.c
index d77a16d..b497c6b 100644
--- a/src/plugins/appstream-cache.c
+++ b/src/plugins/appstream-cache.c
@@ -46,6 +46,7 @@ typedef enum {
 struct AppstreamCachePrivate
 {
        GPtrArray               *array;         /* of AppstreamApp */
+       GPtrArray               *icon_path_array;
        GHashTable              *hash_id;       /* of AppstreamApp{id} */
        GHashTable              *hash_pkgname;  /* of AppstreamApp{pkgname} */
 };
@@ -181,6 +182,7 @@ appstream_cache_icon_kind_from_string (const gchar *kind_str)
 }
 
 typedef struct {
+       const gchar             *path_icons;
        AppstreamApp            *item_temp;
        AppstreamCache          *cache;
        AppstreamCacheSection    section;
@@ -221,6 +223,9 @@ appstream_cache_start_element_cb (GMarkupParseContext *context,
                        return;
                }
                helper->item_temp = appstream_app_new ();
+               appstream_app_set_userdata (helper->item_temp,
+                                           (gpointer) helper->path_icons,
+                                           NULL);
                break;
 
        case APPSTREAM_CACHE_SECTION_ICON:
@@ -448,12 +453,14 @@ appstream_cache_text_cb (GMarkupParseContext *context,
 gboolean
 appstream_cache_parse_file (AppstreamCache *cache,
                            GFile *file,
+                           const gchar *path_icons,
                            GCancellable *cancellable,
                            GError **error)
 {
        const gchar *content_type = NULL;
        gboolean ret = TRUE;
        gchar *data = NULL;
+       gchar *icon_path_tmp = NULL;
        GConverter *converter = NULL;
        GFileInfo *info = NULL;
        GInputStream *file_stream;
@@ -502,8 +509,14 @@ appstream_cache_parse_file (AppstreamCache *cache,
                             "cannot process file of type %s",
                             content_type);
        }
+
+       /* add to array to maintain a ref for the lifetime of the AppstreamApp */
+       icon_path_tmp = g_strdup (path_icons);
+       g_ptr_array_add (cache->priv->icon_path_array, icon_path_tmp);
+
        helper = g_new0 (AppstreamCacheHelper, 1);
        helper->cache = cache;
+       helper->path_icons = icon_path_tmp;
        ctx = g_markup_parse_context_new (&parser,
                                          G_MARKUP_PREFIX_ERROR_POSITION,
                                          helper,
@@ -553,6 +566,7 @@ appstream_cache_init (AppstreamCache *cache)
        AppstreamCachePrivate *priv;
        priv = cache->priv = APPSTREAM_CACHE_GET_PRIVATE (cache);
        priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) appstream_app_free);
+       priv->icon_path_array = g_ptr_array_new_with_free_func (g_free);
        priv->hash_id = g_hash_table_new_full (g_str_hash,
                                               g_str_equal,
                                               NULL,
@@ -573,6 +587,7 @@ appstream_cache_finalize (GObject *object)
        AppstreamCachePrivate *priv = cache->priv;
 
        g_ptr_array_unref (priv->array);
+       g_ptr_array_unref (priv->icon_path_array);
        g_hash_table_unref (priv->hash_id);
        g_hash_table_unref (priv->hash_pkgname);
 
diff --git a/src/plugins/appstream-cache.h b/src/plugins/appstream-cache.h
index e079707..15f0e1b 100644
--- a/src/plugins/appstream-cache.h
+++ b/src/plugins/appstream-cache.h
@@ -61,6 +61,7 @@ GType          appstream_cache_get_type               (void);
 AppstreamCache *appstream_cache_new                    (void);
 gboolean        appstream_cache_parse_file             (AppstreamCache *cache,
                                                         GFile          *file,
+                                                        const gchar    *path_icons,
                                                         GCancellable   *cancellable,
                                                         GError         **error);
 guint           appstream_cache_get_size               (AppstreamCache *cache);
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 5ab9e21..f17528b 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -48,6 +48,7 @@ static gboolean
 gs_plugin_parse_xml_file (GsPlugin *plugin,
                          const gchar *parent_dir,
                          const gchar *filename,
+                         const gchar *path_icons,
                          GError **error)
 {
        gboolean ret;
@@ -56,9 +57,13 @@ gs_plugin_parse_xml_file (GsPlugin *plugin,
 
        /* load this specific file */
        path  = g_build_filename (parent_dir, filename, NULL);
-       g_debug ("Loading AppStream XML %s", path);
+       g_debug ("Loading AppStream XML %s with icon path %s", path, path_icons);
        file = g_file_new_for_path (path);
-       ret = appstream_cache_parse_file (plugin->priv->cache, file, NULL, error);
+       ret = appstream_cache_parse_file (plugin->priv->cache,
+                                         file,
+                                         path_icons,
+                                         NULL,
+                                         error);
        if (!ret)
                goto out;
 out:
@@ -72,7 +77,8 @@ out:
  */
 static gboolean
 gs_plugin_parse_xml_dir (GsPlugin *plugin,
-                        const gchar *path,
+                        const gchar *path_xml,
+                        const gchar *path_icons,
                         GError **error)
 {
        const gchar *tmp;
@@ -80,15 +86,15 @@ gs_plugin_parse_xml_dir (GsPlugin *plugin,
        GDir *dir = NULL;
 
        /* search all files */
-       if (!g_file_test (path, G_FILE_TEST_EXISTS))
+       if (!g_file_test (path_xml, G_FILE_TEST_EXISTS))
                goto out;
-       dir = g_dir_open (path, 0, error);
+       dir = g_dir_open (path_xml, 0, error);
        if (dir == NULL) {
                ret = FALSE;
                goto out;
        }
        while ((tmp = g_dir_read_name (dir)) != NULL) {
-               ret = gs_plugin_parse_xml_file (plugin, path, tmp, error);
+               ret = gs_plugin_parse_xml_file (plugin, path_xml, tmp, path_icons, error);
                if (!ret)
                        goto out;
        }
@@ -104,22 +110,31 @@ out:
 static gboolean
 gs_plugin_parse_xml (GsPlugin *plugin, GError **error)
 {
+       const gchar * const * data_dirs;
        gboolean ret;
-       gchar *path_usr = NULL;
-       gchar *path_var = NULL;
+       gchar *path_xml = NULL;
+       gchar *path_icons = NULL;
+       guint i;
 
        /* search all files */
-       path_usr = g_build_filename (DATADIR, "app-info", "xmls", NULL);
-       ret = gs_plugin_parse_xml_dir (plugin, path_usr, error);
-       if (!ret)
-               goto out;
-       path_var = g_build_filename (LOCALSTATEDIR, "cache", "app-info", "xmls", NULL);
-       ret = gs_plugin_parse_xml_dir (plugin, path_var, error);
+       data_dirs = g_get_system_data_dirs ();
+       for (i = 0; data_dirs[i] != NULL; i++) {
+               path_xml = g_build_filename (data_dirs[i], "app-info", "xmls", NULL);
+               path_icons = g_build_filename (data_dirs[i], "app-info", "icons", NULL);
+               ret = gs_plugin_parse_xml_dir (plugin, path_xml, path_icons, error);
+               g_free (path_xml);
+               g_free (path_icons);
+               if (!ret)
+                       goto out;
+       }
+       path_xml = g_build_filename (LOCALSTATEDIR, "cache", "app-info", "xmls", NULL);
+       path_icons = g_build_filename (LOCALSTATEDIR, "cache", "app-info", "icons", NULL);
+       ret = gs_plugin_parse_xml_dir (plugin, path_xml, path_icons, error);
+       g_free (path_xml);
+       g_free (path_icons);
        if (!ret)
                goto out;
 out:
-       g_free (path_usr);
-       g_free (path_var);
        return ret;
 }
 
@@ -195,6 +210,7 @@ gs_plugin_refine_item (GsPlugin *plugin,
                       AppstreamApp *item,
                       GError **error)
 {
+       const gchar *icon_dir;
        gboolean ret = TRUE;
        gchar *icon_path = NULL;
        GdkPixbuf *pixbuf = NULL;
@@ -236,8 +252,9 @@ gs_plugin_refine_item (GsPlugin *plugin,
                                                           GTK_ICON_LOOKUP_FORCE_SIZE,
                                                           error);
                } else if (appstream_app_get_icon_kind (item) == APPSTREAM_APP_ICON_KIND_CACHED) {
+                       icon_dir = appstream_app_get_userdata (item);
                        icon_path = g_strdup_printf ("%s/%s.png",
-                                                    plugin->priv->cachedir,
+                                                    icon_dir,
                                                     appstream_app_get_icon (item));
                        pixbuf = gdk_pixbuf_new_from_file_at_size (icon_path,
                                                                   plugin->pixbuf_size,


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