[Xfce4-commits] <ristretto:master> Implement the folder-concept introduced in 0.2.0 in the image-list.

Stephan Arts noreply at xfce.org
Sun Nov 13 23:18:07 CET 2011


Updating branch refs/heads/master
         to 91ac881276b68c2e50874c2c0d7de01fee443913 (commit)
       from bd84c942674c5522ea94f6c49fbc27c08e390eda (commit)

commit 91ac881276b68c2e50874c2c0d7de01fee443913
Author: Stephan Arts <stephan at xfce.org>
Date:   Sat Nov 12 08:24:39 2011 +0100

    Implement the folder-concept introduced in 0.2.0 in the image-list.
    
    This removed the need for code-duplication, and reduces the chances of
    bugs being introduced.

 src/image_list.c  |   90 +++++++++++++++++++++++++++++++++++++++++++----------
 src/image_list.h  |   22 ++++++++-----
 src/main_window.c |   80 +++++++++-------------------------------------
 3 files changed, 102 insertions(+), 90 deletions(-)

diff --git a/src/image_list.c b/src/image_list.c
index 5484082..2a5eb89 100644
--- a/src/image_list.c
+++ b/src/image_list.c
@@ -55,14 +55,25 @@ rstto_image_list_iter_class_init(RsttoImageListIterClass *);
 static void
 rstto_image_list_iter_dispose(GObject *object);
 
+static void
+rstto_image_list_monitor_dir (
+        RsttoImageList *image_list,
+        GFile *dir );
+
+static void
+rstto_image_list_remove_all (
+        RsttoImageList *image_list);
+
 static gboolean
 iter_next (
         RsttoImageListIter *iter,
         gboolean sticky);
+
 static gboolean
 iter_previous (
         RsttoImageListIter *iter,
         gboolean sticky);
+
 static void 
 iter_set_position (
         RsttoImageListIter *iter,
@@ -232,7 +243,6 @@ rstto_image_list_add_file (RsttoImageList *image_list, RsttoFile *file, GError *
             {
                 if (FALSE == RSTTO_IMAGE_LIST_ITER(iter->data)->priv->sticky)
                 {
-                    g_debug("find file");
                     rstto_image_list_iter_find_file (iter->data, file);
                 }
                 iter = g_slist_next (iter);
@@ -331,7 +341,7 @@ rstto_image_list_remove_file (RsttoImageList *image_list, RsttoFile *file)
     }
 }
 
-void
+static void
 rstto_image_list_remove_all (RsttoImageList *image_list)
 {
     GSList *iter = NULL;
@@ -348,7 +358,52 @@ rstto_image_list_remove_all (RsttoImageList *image_list)
     g_signal_emit (G_OBJECT (image_list), rstto_image_list_signals[RSTTO_IMAGE_LIST_SIGNAL_REMOVE_ALL], 0, NULL);
 }
 
-void
+gboolean
+rstto_image_list_set_directory (
+        RsttoImageList *image_list,
+        GFile *dir,
+        GError **error )
+{
+    /* Declare variables */
+    GFileEnumerator *file_enumerator = NULL;
+    GFileInfo *file_info;
+    const gchar *filename;
+    const gchar *content_type;
+    GFile *child_file;
+
+    /* Source code block */
+    rstto_image_list_remove_all (image_list);
+
+    /* Allow all images to be removed by providing NULL to dir */
+    if ( NULL != dir )
+    {
+        file_enumerator = g_file_enumerate_children (dir, "standard::*", 0, NULL, NULL);
+
+        if (NULL != file_enumerator)
+        {
+            for(file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL);
+                NULL != file_info;
+                file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL))
+            {
+                filename = g_file_info_get_name (file_info);
+                content_type  = g_file_info_get_content_type (file_info);
+                child_file = g_file_get_child (dir, filename);
+                if (strncmp (content_type, "image/", 6) == 0)
+                {
+                    rstto_image_list_add_file (image_list, rstto_file_new (child_file), NULL);
+                }
+            }
+            g_object_unref (file_enumerator);
+            file_enumerator = NULL;
+        }
+    }
+
+    rstto_image_list_monitor_dir ( image_list, dir );
+
+    return TRUE;
+}
+
+static void
 rstto_image_list_monitor_dir (
         RsttoImageList *image_list,
         GFile *dir )
@@ -361,17 +416,21 @@ rstto_image_list_monitor_dir (
         image_list->priv->monitor = NULL;
     }
 
-    monitor = g_file_monitor_directory (
-            dir,
-            G_FILE_MONITOR_NONE,
-            NULL,
-            NULL);
-
-    g_signal_connect (
-            G_OBJECT(monitor),
-            "changed",
-            G_CALLBACK (cb_file_monitor_changed),
-            image_list);
+    /* Allow a monitor to be removed by providing NULL to dir */
+    if ( NULL != dir )
+    {
+        monitor = g_file_monitor_directory (
+                dir,
+                G_FILE_MONITOR_NONE,
+                NULL,
+                NULL);
+
+        g_signal_connect (
+                G_OBJECT(monitor),
+                "changed",
+                G_CALLBACK (cb_file_monitor_changed),
+                image_list);
+    }
 
     image_list->priv->monitor = monitor;
 }
@@ -392,17 +451,14 @@ cb_file_monitor_changed (
     switch ( event_type )
     {
         case G_FILE_MONITOR_EVENT_DELETED:
-            g_debug("file deleted");
             rstto_image_list_remove_file ( image_list, r_file );
             r_file = NULL;
             break;
         case G_FILE_MONITOR_EVENT_CREATED:
-            g_debug("file created");
             rstto_image_list_add_file (image_list, r_file, NULL);
             r_file = NULL;
             break;
         case G_FILE_MONITOR_EVENT_MOVED:
-            g_debug("file moved");
             rstto_image_list_remove_file ( image_list, r_file );
             r_file = rstto_file_new (other_file);
             rstto_image_list_add_file (image_list, r_file, NULL);
diff --git a/src/image_list.h b/src/image_list.h
index 8b49818..e168629 100644
--- a/src/image_list.h
+++ b/src/image_list.h
@@ -98,10 +98,12 @@ RsttoImageList *rstto_image_list_new ();
 gint     rstto_image_list_get_n_images (RsttoImageList *image_list);
 gboolean rstto_image_list_add_file (RsttoImageList *image_list, RsttoFile *file, GError **);
 
-void
-rstto_image_list_monitor_dir (
+gboolean
+rstto_image_list_set_directory (
         RsttoImageList *image_list,
-        GFile *dir );
+        GFile *dir,
+        GError **);
+
 
 GCompareFunc rstto_image_list_get_compare_func (RsttoImageList *image_list);
 void         rstto_image_list_set_compare_func (RsttoImageList *image_list, GCompareFunc func);
@@ -112,10 +114,12 @@ rstto_image_list_set_sort_by_name (RsttoImageList *image_list);
 void
 rstto_image_list_set_sort_by_date (RsttoImageList *image_list);
 
-RsttoImageListIter *rstto_image_list_get_iter (RsttoImageList *image_list);
+RsttoImageListIter *
+rstto_image_list_get_iter (RsttoImageList *image_list);
 
 /** Iter functions */
-GType       rstto_image_list_iter_get_type ();
+GType
+rstto_image_list_iter_get_type ();
 
 RsttoFile *
 rstto_image_list_iter_get_file ( RsttoImageListIter *iter );
@@ -125,11 +129,11 @@ rstto_image_list_iter_previous (RsttoImageListIter *iter);
 gboolean
 rstto_image_list_iter_next (RsttoImageListIter *iter);
 
-gint        rstto_image_list_iter_get_position (RsttoImageListIter *iter);
-void        rstto_image_list_iter_set_position (RsttoImageListIter *iter, gint pos);
-
+gint
+rstto_image_list_iter_get_position (RsttoImageListIter *iter);
 void
-rstto_image_list_remove_all (RsttoImageList *image_list);
+rstto_image_list_iter_set_position (RsttoImageListIter *iter, gint pos);
+
 void
 rstto_image_list_remove_file (RsttoImageList *image_list, RsttoFile *file);
 gboolean
diff --git a/src/main_window.c b/src/main_window.c
index 43eb66c..9e971fe 100644
--- a/src/main_window.c
+++ b/src/main_window.c
@@ -1709,7 +1709,6 @@ cb_rstto_main_window_navigationtoolbar_button_press_event (GtkWidget *widget, Gd
 static void
 cb_rstto_main_window_image_list_iter_changed (RsttoImageListIter *iter, RsttoMainWindow *window)
 {
-    g_debug("Sticky: %d", rstto_image_list_iter_get_sticky (iter));
     rstto_main_window_image_list_iter_changed (window);
 }
 
@@ -2468,14 +2467,9 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
     gint response;
     GFile *file;
     GFile *p_file;
-    GFileEnumerator *file_enumerator = NULL;
     GSList *files = NULL, *_files_iter;
     GValue current_uri_val = {0, };
     GtkFileFilter *filter;
-    GFileInfo *file_info;
-    const gchar *filename;
-    const gchar *content_type;
-    GFile *child_file;
     RsttoFile *rfile;
 
     g_value_init (&current_uri_val, G_TYPE_STRING);
@@ -2556,38 +2550,17 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
                 {
                     rfile = rstto_file_new (files->data);
                     g_object_ref (rfile);
-                    if (rstto_image_list_add_file (window->priv->image_list, rfile, NULL) == TRUE )
-                    {
-                        rstto_image_list_remove_all (window->priv->image_list);
-                        rstto_image_list_add_file (window->priv->image_list, rfile, NULL);
-                    }
+
                     p_file = g_file_get_parent (files->data);
-                    file_enumerator = g_file_enumerate_children (p_file, "standard::*", 0, NULL, NULL);
-                    if (NULL != file_enumerator)
-                    {
-                        for(file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL);
-                            file_info != NULL;
-                            file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL))
-                        {
-                            filename = g_file_info_get_name (file_info);
-                            content_type  = g_file_info_get_content_type (file_info);
-                            child_file = g_file_get_child (p_file, filename);
-                            if (strncmp (content_type, "image/", 6) == 0)
-                            {
-                                rstto_image_list_add_file (window->priv->image_list, rstto_file_new (child_file), NULL);
-                            }
-                        }
-                        g_object_unref (file_enumerator);
-                        file_enumerator = NULL;
-                    }
+                    rstto_image_list_set_directory (
+                            window->priv->image_list,
+                            p_file,
+                            NULL );
                     rstto_image_list_iter_find_file (
                             window->priv->iter,
                             rfile );
                 }
             }
-            rstto_image_list_monitor_dir (
-                window->priv->image_list,
-                p_file );
         }
  
         g_value_set_string (&current_uri_val, gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dialog)));
@@ -2617,46 +2590,21 @@ cb_rstto_main_window_open_recent(GtkRecentChooser *chooser, RsttoMainWindow *win
 {
     GtkWidget *err_dialog;
     gchar *uri = gtk_recent_chooser_get_current_uri (chooser);
-    const gchar *filename;
-    const gchar *content_type = NULL;
     GError *error = NULL;
     GFile *file = g_file_new_for_uri (uri);
-    GFile *child_file, *p_file;
-    GFileEnumerator *file_enumerator = NULL;
-    GFileInfo *file_info = g_file_query_info (file, "standard::type", 0, NULL, &error);
+    GFile *p_file;
     RsttoFile *rfile;
 
     if (error == NULL)
     {
         rfile = rstto_file_new (file);
         g_object_ref (rfile);
-        if (rstto_image_list_add_file (window->priv->image_list, rfile, NULL) == TRUE )
-        {
-            rstto_image_list_remove_all (window->priv->image_list);
-            rstto_image_list_add_file (window->priv->image_list, rfile, NULL);
-        }
+
         p_file = g_file_get_parent (file);
-        file_enumerator = g_file_enumerate_children (p_file, "standard::*", 0, NULL, NULL);
-        if (NULL != file_enumerator)
-        {
-            for(file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL);
-                file_info != NULL;
-                file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL))
-            {
-                filename = g_file_info_get_name (file_info);
-                content_type  = g_file_info_get_content_type (file_info);
-                child_file = g_file_get_child (p_file, filename);
-                if (strncmp (content_type, "image/", 6) == 0)
-                {
-                    rstto_image_list_add_file (window->priv->image_list, rstto_file_new (child_file), NULL);
-                }
-            }
-            g_object_unref (file_enumerator);
-            file_enumerator = NULL;
-        }
-        rstto_image_list_monitor_dir (
-            window->priv->image_list,
-            p_file );
+        rstto_image_list_set_directory (
+                window->priv->image_list,
+                p_file,
+                NULL);
         rstto_image_list_iter_find_file (
                 window->priv->iter,
                 rfile );
@@ -2796,7 +2744,11 @@ cb_rstto_main_window_properties (GtkWidget *widget, RsttoMainWindow *window)
 static void
 cb_rstto_main_window_close (GtkWidget *widget, RsttoMainWindow *window)
 {
-    rstto_image_list_remove_all (window->priv->image_list);
+    rstto_image_list_set_directory (
+            window->priv->image_list,
+            NULL,
+            NULL);
+
     rstto_main_window_image_list_iter_changed (window);
 
     rstto_main_window_update_buttons (window);


More information about the Xfce4-commits mailing list