[gtk+/filesystemmodel: 26/28] make icons only load when they are visible



commit 9032163b3c95afa2cd38b7864715f93f4a87d3b7
Author: Benjamin Otte <otte gnome org>
Date:   Tue Jun 23 10:30:45 2009 +0200

    make icons only load when they are visible
    
    The current code now checks for the visibility of the row in the tree
    model before issuing a load request.
    This includes an API change to the file system model, so the GetValue
    function can now return FALSE if it doesn't want the value to be cached
    and be called again later.

 gtk/gtkfilechooserdefault.c |   51 ++++++++++++++++++++++++++++++++----------
 gtk/gtkfilesystemmodel.c    |   32 ++++++++++++++++++--------
 gtk/gtkfilesystemmodel.h    |   12 +++++-----
 3 files changed, 67 insertions(+), 28 deletions(-)
---
diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c
index e24b4bb..94b9b4b 100644
--- a/gtk/gtkfilechooserdefault.c
+++ b/gtk/gtkfilechooserdefault.c
@@ -6745,7 +6745,7 @@ file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer da
   g_object_unref (info);
 }
 
-static void
+static gboolean
 file_system_model_set (GtkFileSystemModel *model,
 		       GFile              *file,
                        GFileInfo          *info,
@@ -6785,17 +6785,42 @@ file_system_model_set (GtkFileSystemModel *model,
             }
           else
             {
-              g_value_set_object (value, NULL);
-              g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
-              g_file_query_info_async (file,
-                                       G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
-                                       G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
-                                       G_FILE_ATTRIBUTE_STANDARD_ICON,
-                                       0,
-                                       G_PRIORITY_DEFAULT,
-                                       _gtk_file_system_model_get_cancellable (model),
-                                       file_system_model_got_thumbnail,
-                                       model);
+              GtkTreeModel *tree_model;
+              GtkTreePath *path, *start, *end;
+              GtkTreeIter iter;
+
+              if (impl->browse_files_tree_view == NULL)
+                return FALSE;
+
+              tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (impl->browse_files_tree_view));
+              if (tree_model != GTK_TREE_MODEL (model))
+                return FALSE;
+
+              if (!_gtk_file_system_model_get_iter_for_file (impl->browse_files_model,
+                                                              &iter,
+                                                              file))
+                g_assert_not_reached ();
+              if (!gtk_tree_view_get_visible_range (GTK_TREE_VIEW (impl->browse_files_tree_view), &start, &end))
+                return FALSE;
+              path = gtk_tree_model_get_path (tree_model, &iter);
+              if (gtk_tree_path_compare (start, path) != 1 &&
+                  gtk_tree_path_compare (path, end) != 1)
+                {
+                  g_file_info_set_attribute_boolean (info, "filechooser::queried", TRUE);
+                  g_file_query_info_async (file,
+                                           G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
+                                           G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
+                                           G_FILE_ATTRIBUTE_STANDARD_ICON,
+                                           0,
+                                           G_PRIORITY_DEFAULT,
+                                           _gtk_file_system_model_get_cancellable (model),
+                                           file_system_model_got_thumbnail,
+                                           model);
+                }
+              gtk_tree_path_free (path);
+              gtk_tree_path_free (start);
+              gtk_tree_path_free (end);
+              return FALSE;
             }
         }
       else
@@ -6832,6 +6857,8 @@ file_system_model_set (GtkFileSystemModel *model,
       g_assert_not_reached ();
       break;
     }
+
+  return TRUE;
 }
 
 /* Gets rid of the old list model and creates a new one for the current folder */
diff --git a/gtk/gtkfilesystemmodel.c b/gtk/gtkfilesystemmodel.c
index 8562d51..f2194da 100644
--- a/gtk/gtkfilesystemmodel.c
+++ b/gtk/gtkfilesystemmodel.c
@@ -356,8 +356,13 @@ gtk_file_system_model_get_value (GtkTreeModel *tree_model,
   g_return_if_fail (ITER_IS_VALID (model, iter));
 
   original = _gtk_file_system_model_get_value (model, iter, column);
-  g_value_init (value, G_VALUE_TYPE (original));
-  g_value_copy (original, value);
+  if (original)
+    {
+      g_value_init (value, G_VALUE_TYPE (original));
+      g_value_copy (original, value);
+    }
+  else
+    g_value_init (value, model->column_types[column]);
 }
 
 static gboolean
@@ -1223,13 +1228,16 @@ _gtk_file_system_model_get_file (GtkFileSystemModel *model,
  * @iter: a #GtkTreeIter pointing to a row of @model
  * @column: the column to get the value for
  *
- * Gets the value associated with the given row @iter and @column. 
+ * Gets the value associated with the given row @iter and @column.
+ * If no value is available yet and the default value should be used,
+ * %NULL is returned.
  * This is a performance optimization for the calls 
  * gtk_tree_model_get() or gtk_tree_model_get_value(), which copy 
  * the value and spend a considerable amount of time in iterator 
  * lookups. Both of which are slow.
  *
- * Returns: a pointer to the actual value as stored in @model.
+ * Returns: a pointer to the actual value as stored in @model or %NULL
+ *          if no value available yet.
  **/
 const GValue *
 _gtk_file_system_model_get_value (GtkFileSystemModel *model,
@@ -1246,12 +1254,16 @@ _gtk_file_system_model_get_value (GtkFileSystemModel *model,
   if (!G_VALUE_TYPE (&node->values[column]))
     {
       g_value_init (&node->values[column], model->column_types[column]);
-      model->get_func (model, 
-      node->file, 
-      node->info, 
-      column, 
-      &node->values[column],
-      model->get_data);
+      if (!model->get_func (model, 
+                            node->file, 
+                            node->info, 
+                            column, 
+                            &node->values[column],
+                            model->get_data))
+        {
+          g_value_unset (&node->values[column]);
+          return NULL;
+        }
     }
   
   return &node->values[column];
diff --git a/gtk/gtkfilesystemmodel.h b/gtk/gtkfilesystemmodel.h
index 50a8003..08d2bee 100644
--- a/gtk/gtkfilesystemmodel.h
+++ b/gtk/gtkfilesystemmodel.h
@@ -34,12 +34,12 @@ typedef struct _GtkFileSystemModel      GtkFileSystemModel;
 
 GType _gtk_file_system_model_get_type (void) G_GNUC_CONST;
 
-typedef void (*GtkFileSystemModelGetValue)   (GtkFileSystemModel *model,
-					      GFile              *file,
-					      GFileInfo          *info,
-                                              int                 column,
-                                              GValue             *value,
-					      gpointer            user_data);
+typedef gboolean (*GtkFileSystemModelGetValue)   (GtkFileSystemModel *model,
+                                                  GFile              *file,
+                                                  GFileInfo          *info,
+                                                  int                 column,
+                                                  GValue             *value,
+                                                  gpointer            user_data);
 
 GtkFileSystemModel *_gtk_file_system_model_new              (GFile *             dir,
                                                              const gchar *       attributes,



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