[Xfce4-commits] <ristretto:ristretto-0.0> Sanitize navigator-iter design

Stephan Arts noreply at xfce.org
Sun Oct 23 19:14:36 CEST 2011


Updating branch refs/heads/ristretto-0.0
         to 4608a75c0edfe4e17b0b6ad2fe9be671ce576c4b (commit)
       from 73304079584fdced4a7f58b86daacdb58cb68e7d (commit)

commit 4608a75c0edfe4e17b0b6ad2fe9be671ce576c4b
Author: Stephan Arts <stephan at xfce.org>
Date:   Fri May 22 23:27:28 2009 +0200

    Sanitize navigator-iter design

 ChangeLog         |    5 ++
 src/main_window.c |  130 +++++++++++++++++++--------------------------
 src/navigator.c   |  151 +++++++++++++++++++++++++++++++++++-----------------
 3 files changed, 161 insertions(+), 125 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4ba0c47..ee56277 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-05-22  Stephan Arts <stephan at xfce.org>
+
+	* src/navigating.c,
+	  src/main_window.c: Sanitize navigator-iter design
+
 2009-05-21  Stephan Arts <stephan at xfce.org>
 
 	* src/main_window.c: if a file in the recent_menu does not exist, because
diff --git a/src/main_window.c b/src/main_window.c
index 251cc6c..a1bad99 100644
--- a/src/main_window.c
+++ b/src/main_window.c
@@ -56,7 +56,6 @@ struct _RsttoMainWindowPriv
         RsttoNavigator *navigator;
         gboolean        toolbar_visible;
     } props;
-    gboolean busy;
 
     guint show_fs_toolbar_timeout_id;
     gint window_save_geometry_timer_id;
@@ -127,7 +126,7 @@ cb_rstto_main_window_show_fs_toolbar_timeout (RsttoMainWindow *window);
 static void
 cb_rstto_main_window_navigator_new_image (RsttoNavigator *navigator, RsttoImage *image, RsttoMainWindow *window);
 static void
-cb_rstto_main_window_navigator_invalidate_iters (RsttoNavigator *navigator, RsttoMainWindow *window);
+cb_rstto_main_window_navigator_remove_image (RsttoNavigator *navigator, RsttoImage *image, RsttoMainWindow *window);
 
 static void
 cb_rstto_main_window_zoom_100 (GtkWidget *widget, RsttoMainWindow *window);
@@ -569,35 +568,32 @@ rstto_main_window_navigator_iter_changed (RsttoMainWindow *window)
 
     if (window->priv->props.navigator)
     {
-        if (window->priv->busy == FALSE)
+        position = rstto_navigator_iter_get_position (window->priv->iter);
+        count = rstto_navigator_get_n_images (navigator);
+        cur_image = rstto_navigator_iter_get_image (window->priv->iter);
+        if (cur_image)
         {
-            position = rstto_navigator_iter_get_position (window->priv->iter);
-            count = rstto_navigator_get_n_images (navigator);
-            cur_image = rstto_navigator_iter_get_image (window->priv->iter);
-            if (cur_image)
-            {
-                file = rstto_image_get_file (cur_image);
+            file = rstto_image_get_file (cur_image);
 
-                path = g_file_get_path (file);
-                basename = g_path_get_basename (path);
+            path = g_file_get_path (file);
+            basename = g_path_get_basename (path);
 
-                title = g_strdup_printf ("%s - %s [%d/%d]", RISTRETTO_APP_TITLE,  basename, position+1, count);
-                rstto_main_window_set_sensitive (window, TRUE);
+            title = g_strdup_printf ("%s - %s [%d/%d]", RISTRETTO_APP_TITLE,  basename, position+1, count);
+            rstto_main_window_set_sensitive (window, TRUE);
 
-                g_free (basename);
-                g_free (path);
-            }
-            else
-            {
-                title = g_strdup (RISTRETTO_APP_TITLE);
-                rstto_main_window_set_sensitive (window, FALSE);
-            }
+            g_free (basename);
+            g_free (path);
+        }
+        else
+        {
+            title = g_strdup (RISTRETTO_APP_TITLE);
+            rstto_main_window_set_sensitive (window, FALSE);
+        }
 
-            gtk_window_set_title (GTK_WINDOW (window), title);
-            rstto_picture_viewer_set_image (RSTTO_PICTURE_VIEWER (window->priv->picture_viewer), cur_image);
+        gtk_window_set_title (GTK_WINDOW (window), title);
+        rstto_picture_viewer_set_image (RSTTO_PICTURE_VIEWER (window->priv->picture_viewer), cur_image);
 
-            g_free (title);
-        }
+        g_free (title);
     }
 
 }
@@ -703,7 +699,11 @@ rstto_main_window_set_property (GObject      *object,
             if (window->priv->props.navigator)
             {
                 g_signal_handlers_disconnect_by_func (window->priv->props.navigator, cb_rstto_main_window_navigator_new_image, window);
+                g_signal_handlers_disconnect_by_func (window->priv->props.navigator, cb_rstto_main_window_navigator_remove_image, window);
                 g_object_unref (window->priv->props.navigator);
+
+                rstto_navigator_iter_free (window->priv->iter);
+                window->priv->iter = NULL;
             }
 
             window->priv->props.navigator = g_value_get_object (value);
@@ -712,7 +712,8 @@ rstto_main_window_set_property (GObject      *object,
             {
                 g_object_ref (window->priv->props.navigator);
                 g_signal_connect (G_OBJECT (window->priv->props.navigator), "new-image", G_CALLBACK (cb_rstto_main_window_navigator_new_image), window);
-                g_signal_connect (G_OBJECT (window->priv->props.navigator), "iter-changed", G_CALLBACK (cb_rstto_main_window_navigator_invalidate_iters), window);
+                g_signal_connect (G_OBJECT (window->priv->props.navigator), "remove-image", G_CALLBACK (cb_rstto_main_window_navigator_remove_image), window);
+
                 window->priv->iter = rstto_navigator_get_iter (window->priv->props.navigator);
             }
             break;
@@ -988,7 +989,6 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
     gtk_widget_hide (dialog);
     if(response == GTK_RESPONSE_OK)
     {
-        window->priv->busy = TRUE;
         files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (dialog));
         _files_iter = files;
         while (_files_iter)
@@ -1016,7 +1016,8 @@ cb_rstto_main_window_open_image (GtkWidget *widget, RsttoMainWindow *window)
         g_value_set_string (&current_uri_val, gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dialog)));
         g_object_set_property (G_OBJECT(window->priv->settings_manager), "current-uri", &current_uri_val);
 
-        window->priv->busy = FALSE;
+        if (window->priv->iter == NULL)
+            window->priv->iter = rstto_navigator_get_iter (window->priv->props.navigator);
         rstto_main_window_navigator_iter_changed (window);
     }
 
@@ -1065,7 +1066,6 @@ cb_rstto_main_window_open_folder (GtkWidget *widget, RsttoMainWindow *window)
     if(response == GTK_RESPONSE_OK)
     {
         gtk_widget_hide(dialog);
-        window->priv->busy = TRUE;
         file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
 
         file_enumarator = g_file_enumerate_children (file, "standard::*", 0, NULL, NULL);
@@ -1093,7 +1093,8 @@ cb_rstto_main_window_open_folder (GtkWidget *widget, RsttoMainWindow *window)
         g_value_set_string (&current_uri_val, gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dialog)));
         g_object_set_property (G_OBJECT(window->priv->settings_manager), "current-uri", &current_uri_val);
 
-        window->priv->busy = FALSE;
+        if (window->priv->iter == NULL)
+            window->priv->iter = rstto_navigator_get_iter (window->priv->props.navigator);
         rstto_main_window_navigator_iter_changed (window);
     }
 
@@ -1128,7 +1129,6 @@ cb_rstto_main_window_open_recent(GtkRecentChooser *chooser, RsttoMainWindow *win
     {
         if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
         {
-            window->priv->busy = TRUE;
             file_enumarator = g_file_enumerate_children (file, "standard::name", 0, NULL, NULL);
             while (child_file_info = g_file_enumerator_next_file (file_enumarator, NULL, NULL))
             {
@@ -1141,8 +1141,6 @@ cb_rstto_main_window_open_recent(GtkRecentChooser *chooser, RsttoMainWindow *win
                 g_object_unref (child_file_info);
             }
 
-            window->priv->busy = FALSE;
-            rstto_main_window_navigator_iter_changed (window);
         }
         else
         {
@@ -1169,6 +1167,8 @@ cb_rstto_main_window_open_recent(GtkRecentChooser *chooser, RsttoMainWindow *win
         gtk_widget_destroy (err_dialog);
     }
 
+    if (window->priv->iter == NULL)
+        window->priv->iter = rstto_navigator_get_iter (window->priv->props.navigator);
     rstto_main_window_navigator_iter_changed (window);
 
     g_object_unref (file);
@@ -1602,68 +1602,48 @@ static void
 cb_rstto_main_window_navigator_new_image (RsttoNavigator *navigator, RsttoImage *image, RsttoMainWindow *window)
 {
     if (window->priv->iter == NULL)
+    {
         window->priv->iter = rstto_navigator_get_iter (navigator);
-
-    rstto_navigator_iter_find_image (window->priv->iter, image);
-    rstto_main_window_navigator_iter_changed (window);
+        rstto_main_window_navigator_iter_changed (window);
+    }
 }
 
 /**
- * cb_rstto_main_window_navigator_invalidate_iters:
+ * cb_rstto_main_window_navigator_remove_image:
  * @navigator:
+ * @image:
  * @window:
  *
- * An image has been added to or removed from the image-list.
- * This means the iterator is no longer valid.
- *
  */
 static void
-cb_rstto_main_window_navigator_invalidate_iters (RsttoNavigator *navigator, RsttoMainWindow *window)
-{ 
-    RsttoImage *image = NULL;
-    gint pos = 0;
-
-    /* If we have an iterator, retrieve the image associated to it */
-    if (window->priv->iter)
+cb_rstto_main_window_navigator_remove_image (RsttoNavigator *navigator, RsttoImage *image, RsttoMainWindow *window)
+{
+    if (rstto_navigator_get_n_images (navigator) == 0)
     {
-        image = rstto_navigator_iter_get_image (window->priv->iter);
-        pos = rstto_navigator_iter_get_position (window->priv->iter);
+        if (window->priv->iter)
+        {
+            rstto_navigator_iter_free (window->priv->iter);
+            window->priv->iter = rstto_navigator_get_iter (navigator);
+        }
     }
-
-    /* If we have an image, add a reference to it to make sure we don't lose it */
-    if (image)
-        g_object_ref (image);
-
-    /* Free the invalid iter and get a new one */
-    if (window->priv->iter)
-        rstto_navigator_iter_free (window->priv->iter);
-    window->priv->iter = rstto_navigator_get_iter (navigator);
-
-
-    if (image)
+    else
     {
-
-        /* 
-         * Check if we can find the image inside the image-list, move the iter there.
-         */
-        if (rstto_navigator_iter_find_image (window->priv->iter, image) == FALSE)
+        if (rstto_navigator_iter_get_image (window->priv->iter) == image)
         {
-            /* If we cannot find the image, move the iter to the image after the original image */
-            if (rstto_navigator_iter_set_position (window->priv->iter, pos) == FALSE)
+            if (rstto_navigator_iter_get_position (window->priv->iter) > 0)
+            {
+                rstto_navigator_iter_previous (window->priv->iter);
+            }
+            else
             {
-                /* If this image does not exist, move it to the last image available */
-                rstto_navigator_iter_set_position (window->priv->iter, -1);
+                rstto_navigator_iter_set_position (window->priv->iter, 0);
             }
         }
-
-        /* we no longer need the image, release our reference */
-        g_object_unref (image);
-
     }
-
     rstto_main_window_navigator_iter_changed (window);
 }
 
+
 static gboolean
 cb_rstto_main_window_configure_event (GtkWidget *widget, GdkEventConfigure *event)
 {
diff --git a/src/navigator.c b/src/navigator.c
index 9a654fc..665e754 100644
--- a/src/navigator.c
+++ b/src/navigator.c
@@ -43,14 +43,15 @@ static GObjectClass *parent_class = NULL;
 enum
 {
     RSTTO_NAVIGATOR_SIGNAL_NEW_IMAGE = 0,
-    RSTTO_NAVIGATOR_SIGNAL_INVALIDATE_ITERS,
+    RSTTO_NAVIGATOR_SIGNAL_REMOVE_IMAGE,
     RSTTO_NAVIGATOR_SIGNAL_COUNT
 };
 
 struct _RsttoNavigatorIter
 {
-    GList *list;
-    GList *iter;
+    RsttoNavigator *navigator;
+    RsttoImage *image;
+    gint position;
 };
 
 struct _RsttoNavigatorPriv
@@ -114,15 +115,16 @@ rstto_navigator_class_init(RsttoNavigatorClass *nav_class)
             G_TYPE_OBJECT,
             NULL);
 
-    rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_INVALIDATE_ITERS] = g_signal_new("iter-changed",
+    rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_REMOVE_IMAGE] = g_signal_new("remove-image",
             G_TYPE_FROM_CLASS(nav_class),
             G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
             0,
             NULL,
             NULL,
-            g_cclosure_marshal_VOID__VOID,
+            g_cclosure_marshal_VOID__OBJECT,
             G_TYPE_NONE,
-            0,
+            1,
+            G_TYPE_OBJECT,
             NULL);
 }
 
@@ -151,7 +153,6 @@ rstto_navigator_add_file (RsttoNavigator *navigator, GFile *file, GError **error
         navigator->priv->images = g_list_insert_sorted (navigator->priv->images, image, (GCompareFunc)cb_rstto_navigator_image_name_compare_func);
         navigator->priv->n_images++;
 
-        g_signal_emit (G_OBJECT (navigator), rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_INVALIDATE_ITERS], 0, NULL);
         g_signal_emit (G_OBJECT (navigator), rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_NEW_IMAGE], 0, image, NULL);
         return TRUE;
     }
@@ -168,13 +169,15 @@ RsttoNavigatorIter *
 rstto_navigator_get_iter (RsttoNavigator *navigator)
 {
     RsttoNavigatorIter *iter = g_new0 (RsttoNavigatorIter, 1);
+    iter->navigator = navigator;
+    if (navigator->priv->images)
+        iter->image = navigator->priv->images->data;
 
-    iter->list = g_list_copy (navigator->priv->images);
-
-    iter->iter = iter->list;
+    if (iter->image)
+        g_object_ref (iter->image);
+    else
+        iter->position = -1;
 
-    if (iter->list)
-        g_list_foreach (iter->list, (GFunc)g_object_ref, NULL);
 
     return iter;
 }
@@ -182,70 +185,113 @@ rstto_navigator_get_iter (RsttoNavigator *navigator)
 gint
 rstto_navigator_iter_get_position (RsttoNavigatorIter *iter)
 {
-    return g_list_position (iter->list, iter->iter);
+    if (iter->image == NULL)
+    {
+        if ((iter->position == -1) && (rstto_navigator_get_n_images (iter->navigator) > 0))
+        {
+            rstto_navigator_iter_set_position (iter, 0);
+        }
+    }
+    return iter->position;
+}
+
+RsttoImage *
+rstto_navigator_iter_get_image (RsttoNavigatorIter *iter)
+{
+    if (iter->image == NULL)
+    {
+        if ((iter->position == -1) && (rstto_navigator_get_n_images (iter->navigator) > 0))
+        {
+            rstto_navigator_iter_set_position (iter, 0);
+        }
+    }
+    return RSTTO_IMAGE (iter->image);
 }
 
+
 gboolean
 rstto_navigator_iter_set_position (RsttoNavigatorIter *iter, gint pos)
 {
-    if (pos == -1)
+    if (iter->image)
     {
-        iter->iter = g_list_last (iter->list);
-        return TRUE;
+        g_object_unref (iter->image);
+        iter->image = NULL;
+    }
+
+    iter->image = g_list_nth_data (iter->navigator->priv->images, pos); 
+    if (iter->image)
+    {
+        iter->position = pos;
+        g_object_ref (iter->image);
     }
     else
     {
-        if (pos < g_list_length (iter->list))
-        {
-            iter->iter = g_list_nth (iter->list, pos);
-            return TRUE;
-        }
+        iter->position = -1;
     }
-    return FALSE;
 }
 
 gboolean
 rstto_navigator_iter_next (RsttoNavigatorIter *iter)
 {
-    if (g_list_next (iter->iter))
+    if (iter->image)
     {
-        iter->iter = g_list_next (iter->iter);
-        return TRUE;
+        g_object_unref (iter->image);
+        iter->image = NULL;
+    }
+
+    iter->image = g_list_nth_data (iter->navigator->priv->images, iter->position+1); 
+    if (iter->image)
+    {
+        iter->position++;
     }
     else
     {
-        iter->iter = g_list_first (iter->list);
-        return FALSE;
+        iter->position = 0;
+        iter->image = g_list_nth_data (iter->navigator->priv->images, 0); 
+        if (iter->image == NULL)
+        {
+            iter->position = -1;
+        }
     }
+    if (iter->image)
+        g_object_ref (iter->image);
 }
 
 gboolean
 rstto_navigator_iter_previous (RsttoNavigatorIter *iter)
 {
-    if (g_list_previous (iter->iter))
+    if (iter->image)
     {
-        iter->iter = g_list_previous (iter->iter);
-        return TRUE;
+        g_object_unref (iter->image);
+        iter->image = NULL;
+    }
+
+    iter->image = g_list_nth_data (iter->navigator->priv->images, iter->position-1); 
+    if (iter->image)
+    {
+        iter->position--;
     }
     else
     {
-        iter->iter = g_list_last (iter->list);
-        return FALSE;
+        iter->position = g_list_length (iter->navigator->priv->images)-1;
+        iter->image = g_list_nth_data (iter->navigator->priv->images, iter->position); 
+        if (iter->image == NULL)
+        {
+            iter->position = -1;
+        }
     }
-}
-
-RsttoImage *
-rstto_navigator_iter_get_image (RsttoNavigatorIter *iter)
-{
-    if (iter->iter)
-        return RSTTO_IMAGE (iter->iter->data);
-    return NULL;
+    if (iter->image)
+        g_object_ref (iter->image);
 }
 
 void
 rstto_navigator_iter_free (RsttoNavigatorIter *iter)
 {
-    g_list_foreach (iter->list, (GFunc)g_object_unref, NULL);
+    if (iter->image)
+    {
+        g_object_unref (iter->image);
+        iter->image = NULL;
+    }
     g_free (iter);
 }
 
@@ -254,9 +300,10 @@ rstto_navigator_remove_image (RsttoNavigator *navigator, RsttoImage *image)
 {
     if (g_list_find (navigator->priv->images, image))
     {
-        g_object_unref (image);
         navigator->priv->images = g_list_remove (navigator->priv->images, image);
-        g_signal_emit (G_OBJECT (navigator), rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_INVALIDATE_ITERS], 0, NULL);
+        g_signal_emit (G_OBJECT (navigator), rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_REMOVE_IMAGE], 0, image, NULL);
+
+        g_object_unref (image);
     }
 }
 
@@ -266,18 +313,23 @@ rstto_navigator_remove_all (RsttoNavigator *navigator)
     g_list_foreach (navigator->priv->images, (GFunc)g_object_unref, NULL);
     g_list_free (navigator->priv->images);
     navigator->priv->images = NULL;
-    g_signal_emit (G_OBJECT (navigator), rstto_navigator_signals[RSTTO_NAVIGATOR_SIGNAL_INVALIDATE_ITERS], 0, NULL);
 }
 
 gboolean
 rstto_navigator_iter_find_image (RsttoNavigatorIter *iter, RsttoImage *image)
 {
-    GList *list = g_list_find (iter->list, image);
-    iter->iter = list;
-
-    if (list)
+    gint pos = g_list_index (iter->navigator->priv->images, image);
+    if (pos > -1)
+    {
+        if (iter->image)
+        {
+            g_object_unref (iter->image);
+            iter->image = NULL;
+        }
+        iter->image = image;
+        g_object_ref (iter->image);
         return TRUE;
-    
+    }
     return FALSE;
 }
 
@@ -298,7 +350,6 @@ cb_rstto_navigator_image_name_compare_func (RsttoImage *a, RsttoImage *b)
 
     result = g_strcasecmp (a_base, b_base);
 
-
     g_free (a_base);
     g_free (b_base);
     return result;


More information about the Xfce4-commits mailing list