[Xfce4-commits] <thunar:master> Sort renamer list by clicking on name header (bug #2622).

Nick Schermer noreply at xfce.org
Sun Nov 4 12:06:01 CET 2012


Updating branch refs/heads/master
         to efc49a7b5f33e461f3d07a7276212f5e3ffc5453 (commit)
       from 12644513c79ba92aeb2129cb121e6c624bb20887 (commit)

commit efc49a7b5f33e461f3d07a7276212f5e3ffc5453
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Nov 4 12:03:54 2012 +0100

    Sort renamer list by clicking on name header (bug #2622).
    
    Allow sorting on the items by click on the tree header.

 thunar/thunar-renamer-dialog.c |   58 +++++++++++++++++++++++++++++++++++++---
 thunar/thunar-renamer-model.c  |   57 ++++++++++++++++++++++++++++++++++++---
 thunar/thunar-renamer-model.h  |    2 +
 3 files changed, 109 insertions(+), 8 deletions(-)

diff --git a/thunar/thunar-renamer-dialog.c b/thunar/thunar-renamer-dialog.c
index 4ca6d57..14f46f8 100644
--- a/thunar/thunar-renamer-dialog.c
+++ b/thunar/thunar-renamer-dialog.c
@@ -93,6 +93,8 @@ static void        thunar_renamer_dialog_action_about          (GtkAction
                                                                 ThunarRenamerDialog      *renamer_dialog);
 static void        thunar_renamer_dialog_action_properties     (GtkAction                *action,
                                                                 ThunarRenamerDialog      *renamer_dialog);
+static void        thunar_renamer_dialog_name_column_clicked   (GtkTreeViewColumn        *column,
+                                                                ThunarRenamerDialog      *renamer_dialog);
 static gboolean    thunar_renamer_dialog_button_press_event    (GtkWidget                *tree_view,
                                                                 GdkEventButton           *event,
                                                                 ThunarRenamerDialog      *renamer_dialog);
@@ -166,6 +168,8 @@ struct _ThunarRenamerDialog
   GtkWidget           *tree_view;
   GtkWidget           *progress;
 
+  GtkTreeViewColumn   *name_column;
+
   /* the current directory used for the "Add Files" dialog */
   ThunarFile          *current_directory;
 
@@ -287,7 +291,8 @@ static gint
 trd_renamer_compare (gconstpointer a,
                      gconstpointer b)
 {
-  return g_utf8_collate (thunarx_renamer_get_name (THUNARX_RENAMER (a)), thunarx_renamer_get_name (THUNARX_RENAMER (b)));
+  return g_utf8_collate (thunarx_renamer_get_name (THUNARX_RENAMER (a)),
+                         thunarx_renamer_get_name (THUNARX_RENAMER (b)));
 }
 
 
@@ -409,13 +414,15 @@ thunar_renamer_dialog_init (ThunarRenamerDialog *renamer_dialog)
   gtk_widget_show (renamer_dialog->tree_view);
 
   /* create the tree view column for the old file name */
-  column = gtk_tree_view_column_new ();
+  renamer_dialog->name_column = column = gtk_tree_view_column_new ();
   gtk_tree_view_column_set_spacing (column, 2);
   gtk_tree_view_column_set_min_width (column, 100);
   gtk_tree_view_column_set_title (column, _("Name"));
   gtk_tree_view_column_set_resizable (column, TRUE);
   gtk_tree_view_column_set_fixed_width (column, 250);
   gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
+  gtk_tree_view_column_set_clickable (column, TRUE);
+  g_signal_connect (G_OBJECT (column), "clicked", G_CALLBACK (thunar_renamer_dialog_name_column_clicked), renamer_dialog);
   renderer = g_object_new (THUNAR_TYPE_ICON_RENDERER, "size", 16, NULL);
   gtk_tree_view_column_pack_start (column, renderer, FALSE);
   gtk_tree_view_column_set_attributes (column, renderer, "file", THUNAR_RENAMER_MODEL_COLUMN_FILE, NULL);
@@ -1102,6 +1109,9 @@ thunar_renamer_dialog_action_add_files (GtkAction           *action,
               /* append the file to the renamer model */
               thunar_renamer_model_append (renamer_dialog->model, file);
 
+              /* unset sort order */
+              gtk_tree_view_column_set_sort_indicator (renamer_dialog->name_column, FALSE);
+
               /* release the file */
               g_object_unref (G_OBJECT (file));
             }
@@ -1230,6 +1240,37 @@ thunar_renamer_dialog_action_properties (GtkAction           *action,
 
 
 
+static void
+thunar_renamer_dialog_name_column_clicked (GtkTreeViewColumn   *column,
+                                           ThunarRenamerDialog *renamer_dialog)
+{
+  GtkSortType sort_order = GTK_SORT_ASCENDING;
+
+  _thunar_return_if_fail (renamer_dialog->name_column == column);
+
+  if (!gtk_tree_view_column_get_sort_indicator (column))
+    {
+      /* show sort order */
+      gtk_tree_view_column_set_sort_indicator (column, TRUE);
+    }
+  else
+    {
+      /* invert new sort order */
+      if (gtk_tree_view_column_get_sort_order (column) == GTK_SORT_ASCENDING)
+        sort_order = GTK_SORT_DESCENDING;
+      else
+        sort_order = GTK_SORT_ASCENDING;
+    }
+
+  /* set new sort direction */
+  gtk_tree_view_column_set_sort_order (column, sort_order);
+
+  /* sort the model */
+  thunar_renamer_model_sort (renamer_dialog->model, sort_order);
+}
+
+
+
 static gboolean
 thunar_renamer_dialog_button_press_event (GtkWidget           *tree_view,
                                           GdkEventButton      *event,
@@ -1341,6 +1382,9 @@ thunar_renamer_dialog_drag_data_received (GtkWidget           *tree_view,
               /* insert the file in the model */
               thunar_renamer_model_insert (renamer_dialog->model, file, position);
 
+              /* unset sort order */
+              gtk_tree_view_column_set_sort_indicator (renamer_dialog->name_column, FALSE);
+
               /* advance the offset if we do not append, so the drop order is consistent */
               if (position != -1)
                 position++;
@@ -1521,8 +1565,14 @@ thunar_renamer_dialog_drag_drop (GtkWidget           *tree_view,
             }
 
           /* perform the move */
-          thunar_renamer_model_reorder (renamer_dialog->model, rows, position);
-          g_list_free_full (rows, (GDestroyNotify) gtk_tree_path_free);
+          if (G_LIKELY (rows != NULL))
+            {
+              thunar_renamer_model_reorder (renamer_dialog->model, rows, position);
+              g_list_free_full (rows, (GDestroyNotify) gtk_tree_path_free);
+            }
+
+          /* unset sort column */
+          gtk_tree_view_column_set_sort_indicator (renamer_dialog->name_column, FALSE);
         }
 
       /* finish the dnd operation */
diff --git a/thunar/thunar-renamer-model.c b/thunar/thunar-renamer-model.c
index a85453d..05c9f79 100644
--- a/thunar/thunar-renamer-model.c
+++ b/thunar/thunar-renamer-model.c
@@ -1011,6 +1011,27 @@ thunar_renamer_model_cmp_array (gconstpointer pointer_a,
 
 
 
+static gint
+thunar_renamer_model_cmp_name (gconstpointer pointer_a,
+                               gconstpointer pointer_b,
+                               gpointer      user_data)
+{
+  const SortTuple              *tuple_a = pointer_a;
+  const SortTuple              *tuple_b = pointer_b;
+  const ThunarRenamerModelItem *a = tuple_a->item->data;
+  const ThunarRenamerModelItem *b = tuple_b->item->data;
+  GtkSortType                   sort_order = GPOINTER_TO_INT (user_data);
+  gint                          result;
+
+  /* sort files by their old filename */
+  result = thunar_file_compare_by_name (a->file, b->file, TRUE);
+
+  /* insert order for desc sorting */
+  return sort_order == GTK_SORT_ASCENDING ? result : -result;
+}
+
+
+
 /**
  * thunar_renamer_model_new:
  *
@@ -1313,14 +1334,19 @@ thunar_renamer_model_reorder (ThunarRenamerModel *renamer_model,
   gint         k, n, m;
   SortTuple   *sort_array;
 
+  _thunar_return_if_fail (THUNAR_IS_RENAMER_MODEL (renamer_model));
+
   /* leave when there is nothing to sort */
   n_items = g_list_length (renamer_model->items);
   if (G_UNLIKELY (n_items <= 1))
     return;
 
   /* correct the sort position to match the list items */
-  if (position == -1 || position > n_items)
-    position = n_items;
+  if (tree_paths != NULL)
+    {
+      if (position == -1 || position > n_items)
+        position = n_items;
+    }
 
   /* be sure to not overuse the stack */
   if (G_LIKELY (n_items < 500))
@@ -1332,7 +1358,8 @@ thunar_renamer_model_reorder (ThunarRenamerModel *renamer_model,
   for (lp = renamer_model->items, m = 0, n = 0; lp != NULL; lp = lp->next, ++n, ++m)
     {
       /* leave a hole in the sort position for the drop items */
-      if (G_UNLIKELY (m == position))
+      if (G_UNLIKELY (tree_paths != NULL
+          && m == position))
         m++;
 
       sort_array[n].offset = n;
@@ -1348,7 +1375,10 @@ thunar_renamer_model_reorder (ThunarRenamerModel *renamer_model,
     }
 
   /* sort the array using QuickSort */
-  g_qsort_with_data (sort_array, n_items, sizeof (SortTuple), thunar_renamer_model_cmp_array, NULL);
+  if (tree_paths != NULL)
+    g_qsort_with_data (sort_array, n_items, sizeof (SortTuple), thunar_renamer_model_cmp_array, NULL);
+  else
+    g_qsort_with_data (sort_array, n_items, sizeof (SortTuple), thunar_renamer_model_cmp_name, GINT_TO_POINTER (position));
 
   /* update our internals and generate the new order */
   new_order = g_newa (gint, n_items);
@@ -1388,6 +1418,25 @@ thunar_renamer_model_reorder (ThunarRenamerModel *renamer_model,
 
 
 /**
+ * thunar_renamer_model_sort:
+ * @renamer_model : a #ThunarRenamerModel.
+ * @sort_order    : sort direction of the model.
+ *
+ * Sort the entire model by the old filename.
+ **/
+void
+thunar_renamer_model_sort (ThunarRenamerModel *renamer_model,
+                           GtkSortType         sort_order)
+{
+  _thunar_return_if_fail (THUNAR_IS_RENAMER_MODEL (renamer_model));
+
+  /* abuse function above */
+  thunar_renamer_model_reorder (renamer_model, NULL, sort_order);
+}
+
+
+
+/**
  * thunar_renamer_model_clear:
  * @renamer_model : a #ThunarRenamerModel.
  *
diff --git a/thunar/thunar-renamer-model.h b/thunar/thunar-renamer-model.h
index ffc6763..2b97b17 100644
--- a/thunar/thunar-renamer-model.h
+++ b/thunar/thunar-renamer-model.h
@@ -69,6 +69,8 @@ void                 thunar_renamer_model_insert         (ThunarRenamerModel *re
 void                 thunar_renamer_model_reorder        (ThunarRenamerModel *renamer_model,
                                                           GList              *tree_paths,
                                                           gint                position);
+void                 thunar_renamer_model_sort           (ThunarRenamerModel *renamer_model,
+                                                          GtkSortType         sort_order);
 void                 thunar_renamer_model_clear          (ThunarRenamerModel *renamer_model);
 void                 thunar_renamer_model_remove         (ThunarRenamerModel *renamer_model,
                                                           GtkTreePath        *path);


More information about the Xfce4-commits mailing list