[gnome-font-viewer] model: use a single IO scheduler job to load thumbnail images



commit 4f08812acbfb1e116f96de434047d79e3a04ad17
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Aug 20 15:24:07 2012 +0200

    model: use a single IO scheduler job to load thumbnail images
    
    Instead of firing a new thread for all files. Using
    g_io_scheduler_job_send_to_mainloop_async() we can load thumbnails into
    the model as they get created anyway.

 src/font-model.c |  128 +++++++++++++++++++++++++++---------------------------
 1 files changed, 64 insertions(+), 64 deletions(-)
---
diff --git a/src/font-model.c b/src/font-model.c
index 3721ba1..faeb6fd 100644
--- a/src/font-model.c
+++ b/src/font-model.c
@@ -147,7 +147,7 @@ load_thumbnail_data_free (LoadThumbnailData *data)
 }
 
 static gboolean
-ensure_thumbnail_job_done (gpointer user_data)
+one_thumbnail_done (gpointer user_data)
 {
     LoadThumbnailData *data = user_data;
 
@@ -206,83 +206,72 @@ create_thumbnail (LoadThumbnailData *data)
 }
 
 static gboolean
-ensure_thumbnail_job (GIOSchedulerJob *job,
-                      GCancellable *cancellable,
-                      gpointer user_data)
+ensure_thumbnails_job (GIOSchedulerJob *job,
+                       GCancellable *cancellable,
+                       gpointer user_data)
 {
-    LoadThumbnailData *data = user_data;
-    gboolean thumb_failed;
-    const gchar *thumb_path;
-
-    GError *error = NULL;
-    GFile *thumb_file = NULL;
-    GFileInputStream *is = NULL;
-    GFileInfo *info = NULL;
-
-    info = g_file_query_info (data->font_file,
-                              ATTRIBUTES_FOR_EXISTING_THUMBNAIL,
-                              G_FILE_QUERY_INFO_NONE,
-                              NULL, &error);
-
-    if (error != NULL) {
-        g_debug ("Can't query info for file %s: %s\n", data->font_path, error->message);
-        goto out;
-    }
+    GList *thumbnails = user_data, *l;
 
-    thumb_failed = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
-    if (thumb_failed)
-        goto out;
+    for (l = thumbnails; l != NULL; l = l->next) {
+        gboolean thumb_failed;
+        const gchar *thumb_path;
+        LoadThumbnailData *data = l->data;
 
-    thumb_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
+        GError *error = NULL;
+        GFile *thumb_file = NULL;
+        GFileInputStream *is = NULL;
+        GFileInfo *info = NULL;
 
-    if (thumb_path != NULL) {
-        thumb_file = g_file_new_for_path (thumb_path);
-        is = g_file_read (thumb_file, NULL, &error);
+        info = g_file_query_info (data->font_file,
+                                  ATTRIBUTES_FOR_EXISTING_THUMBNAIL,
+                                  G_FILE_QUERY_INFO_NONE,
+                                  NULL, &error);
 
         if (error != NULL) {
-            g_debug ("Can't read file %s: %s\n", thumb_path, error->message);
-            goto out;
+            g_debug ("Can't query info for file %s: %s\n", data->font_path, error->message);
+            goto next;
         }
 
-        data->pixbuf = gdk_pixbuf_new_from_stream_at_scale (G_INPUT_STREAM (is),
-                                                            128, 128, TRUE,
-                                                            NULL, &error);
+        thumb_failed = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
+        if (thumb_failed)
+            goto next;
 
-        if (error != NULL) {
-            g_debug ("Can't read thumbnail pixbuf %s: %s\n", thumb_path, error->message);
-            goto out;
-        }
-    } else {
-        data->pixbuf = create_thumbnail (data);
-    }
+        thumb_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
 
- out:
-    g_clear_error (&error);
-    g_clear_object (&is);
-    g_clear_object (&thumb_file);
-    g_clear_object (&info);
+        if (thumb_path != NULL) {
+            thumb_file = g_file_new_for_path (thumb_path);
+            is = g_file_read (thumb_file, NULL, &error);
 
-    g_io_scheduler_job_send_to_mainloop_async (job, ensure_thumbnail_job_done,
-                                               data, NULL);
+            if (error != NULL) {
+                g_debug ("Can't read file %s: %s\n", thumb_path, error->message);
+                goto next;
+            }
 
-    return FALSE;
-}
+            data->pixbuf = gdk_pixbuf_new_from_stream_at_scale (G_INPUT_STREAM (is),
+                                                                128, 128, TRUE,
+                                                                NULL, &error);
 
-static void
-ensure_thumbnail (FontViewModel *self,
-                  const gchar *path,
-                  GtkTreeIter *iter)
-{
-    LoadThumbnailData *data;
+            if (error != NULL) {
+                g_debug ("Can't read thumbnail pixbuf %s: %s\n", thumb_path, error->message);
+                goto next;
+            }
+        } else {
+            data->pixbuf = create_thumbnail (data);
+        }
 
-    data = g_slice_new0 (LoadThumbnailData);
-    data->self = g_object_ref (self);
-    data->font_file = g_file_new_for_path (path);
-    data->font_path = g_strdup (path);
-    data->iter = *iter;
+    next:
+        g_clear_error (&error);
+        g_clear_object (&is);
+        g_clear_object (&thumb_file);
+        g_clear_object (&info);
 
-    g_io_scheduler_push_job (ensure_thumbnail_job, data,
-                             NULL, G_PRIORITY_DEFAULT, NULL);
+        g_io_scheduler_job_send_to_mainloop_async (job, one_thumbnail_done,
+                                                   data, NULL);
+    }
+
+    g_list_free (thumbnails);
+
+    return FALSE;
 }
 
 /* make sure the font list is valid */
@@ -294,6 +283,7 @@ ensure_font_list (FontViewModel *self)
     gint i;
     FcChar8 *file;
     gchar *font_name, *collation_key;
+    GList *thumbnails = NULL;
 
     if (self->priv->font_list) {
             FcFontSetDestroy (self->priv->font_list);
@@ -319,6 +309,7 @@ ensure_font_list (FontViewModel *self)
 
     for (i = 0; i < self->priv->font_list->nfont; i++) {
         GtkTreeIter iter;
+        LoadThumbnailData *data;
 
 	FcPatternGetString (self->priv->font_list->fonts[i], FC_FILE, 0, &file);
         font_name = font_utils_get_font_name_for_file (self->priv->library, (const gchar *) file);
@@ -336,11 +327,20 @@ ensure_font_list (FontViewModel *self)
                                            COLUMN_COLLATION_KEY, collation_key,
                                            -1);
 
-        ensure_thumbnail (self, (const gchar *) file, &iter);
+        data = g_slice_new0 (LoadThumbnailData);
+        data->font_file = g_file_new_for_path (file);
+        data->font_path = g_strdup (file);
+        data->iter = iter;
+        data->self = g_object_ref (self);
+
+        thumbnails = g_list_prepend (thumbnails, data);
 
         g_free (font_name);
         g_free (collation_key);
     }
+
+    g_io_scheduler_push_job (ensure_thumbnails_job, thumbnails,
+                             NULL, G_PRIORITY_DEFAULT, NULL);
 }
 
 static gboolean



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