[nautilus/wip/alexpandelea/batchRename] Add sorting options for files names



commit 5dcdb494df308926c9ce3ee861a11abfca5b27ce
Author: Alexandru Pandelea <alexandru pandelea gmail com>
Date:   Wed Jun 29 01:44:58 2016 +0300

    Add sorting options for files names
    
    Now the files names can be sorted by using criteria like: original name or
    last modified date. This is useful when there is used a numbering option and
    the user is not pleased with the default order of the files and wants to
    change it.
    
    In this commit some warnigns were also fixed.

 src/nautilus-batch-rename-utilities.c            |   67 +++++++++
 src/nautilus-batch-rename-utilities.h            |    3 +
 src/nautilus-batch-rename.c                      |  166 +++++++++++++++++-----
 src/nautilus-batch-rename.h                      |    9 ++
 src/resources/ui/nautilus-batch-rename-dialog.ui |   82 ++++++++----
 5 files changed, 265 insertions(+), 62 deletions(-)
---
diff --git a/src/nautilus-batch-rename-utilities.c b/src/nautilus-batch-rename-utilities.c
index 8668d6f..624f8be 100644
--- a/src/nautilus-batch-rename-utilities.c
+++ b/src/nautilus-batch-rename-utilities.c
@@ -1,6 +1,7 @@
 #include "nautilus-batch-rename.h"
 #include "nautilus-batch-rename-utilities.h"
 #include "nautilus-files-view.h"
+#include "nautilus-file.h"
 
 #include <glib.h>
 #include <gtk/gtk.h>
@@ -200,4 +201,70 @@ concat(gchar *s1, gchar *s2)
     memcpy(result + strlen(s1), s2, strlen(s2) + 1);
 
     return result;
+}
+
+gint
+compare_files_by_name_ascending (NautilusFile *f1,
+                                 NautilusFile *f2)
+{
+        if (f1 == f2)
+                return 0;
+
+        if (strcmp (nautilus_file_get_name (f1), nautilus_file_get_name (f2)) >= 0) {
+                return 1;
+        }
+        return -1;
+}
+
+gint
+compare_files_by_name_descending (NautilusFile *f1,
+                                  NautilusFile *f2)
+{
+        if (f1 == f2)
+                return 0;
+
+        if (strcmp (nautilus_file_get_name (f1), nautilus_file_get_name (f2)) >= 0) {
+                return -1;
+        }
+        return 1;
+}
+
+gint
+compare_files_by_first_modified (NautilusFile *f1,
+                                 NautilusFile *f2)
+{
+        return nautilus_file_compare_for_sort (f1,f2,
+                                                NAUTILUS_FILE_SORT_BY_MTIME,
+                                                FALSE, FALSE);
+}
+
+gint
+compare_files_by_last_modified (NautilusFile *f1,
+                                NautilusFile *f2)
+{
+        return nautilus_file_compare_for_sort (f1,f2,
+                                                NAUTILUS_FILE_SORT_BY_MTIME,
+                                                FALSE, TRUE);
+}
+
+GList*
+nautilus_batch_rename_sort (GList *selection,
+                            SortingMode mode)
+{
+        if (mode == ORIGINAL_ASCENDING)
+                return g_list_sort (selection, compare_files_by_name_ascending);
+
+        if (mode == ORIGINAL_DESCENDING) {
+                return g_list_sort (selection, compare_files_by_name_descending);
+        }
+
+        if (mode == FIRST_MODIFIED) {
+            return g_list_sort (selection, compare_files_by_first_modified);
+        }
+
+        if (mode == LAST_MODIFIED) {
+            return g_list_sort (selection, compare_files_by_last_modified);
+        }
+
+        return NULL;
 }
\ No newline at end of file
diff --git a/src/nautilus-batch-rename-utilities.h b/src/nautilus-batch-rename-utilities.h
index 33e916f..28df907 100644
--- a/src/nautilus-batch-rename-utilities.h
+++ b/src/nautilus-batch-rename-utilities.h
@@ -26,4 +26,7 @@ GList* list_has_duplicates      (NautilusFilesView           *view,
 gchar* concat                   (gchar                       *s1,
                                  gchar                       *s2);
 
+GList* nautilus_batch_rename_sort (GList       *selection,
+                                   SortingMode mode);
+
 #endif /* NAUTILUS_BATCH_RENAME_UTILITIES_H */
\ No newline at end of file
diff --git a/src/nautilus-batch-rename.c b/src/nautilus-batch-rename.c
index 264f7cf..9479b7d 100644
--- a/src/nautilus-batch-rename.c
+++ b/src/nautilus-batch-rename.c
@@ -49,8 +49,8 @@ struct _NautilusBatchRename
         GtkWidget               *replace_mode_button;
         GtkWidget               *add_button;
         GtkWidget               *add_popover;
-        GtkWidget               *numbering_order_popover;
-        GtkWidget               *numbering_order_button;
+        GtkWidget               *add_button_label;
+        GtkWidget               *numbering_order_label;
 
         GList                   *listbox_rows;
 
@@ -59,13 +59,95 @@ struct _NautilusBatchRename
         NautilusFilesView       *view;
 
         GtkWidget               *expander_label;
+
+        GActionGroup            *action_group;
 };
 
 static void     batch_rename_dialog_on_closed           (GtkDialog              *dialog);
 static void     file_names_widget_entry_on_changed      (NautilusBatchRename    *dialog);
 
+
 G_DEFINE_TYPE (NautilusBatchRename, nautilus_batch_rename, GTK_TYPE_DIALOG);
 
+static void
+numbering_order_changed_descending (GSimpleAction       *action,
+                                    GVariant            *state,
+                                    gpointer            user_data)
+{
+        NautilusBatchRename *dialog;
+
+        dialog = NAUTILUS_BATCH_RENAME (user_data);
+
+        dialog->selection = nautilus_batch_rename_sort (dialog->selection, ORIGINAL_DESCENDING);
+
+        gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
+                             "Original name (Descending)");
+
+        /* update display text */
+        file_names_widget_entry_on_changed (dialog);
+}
+
+static void
+numbering_order_changed_ascending (GSimpleAction       *action,
+                                   GVariant            *state,
+                                   gpointer            user_data)
+{
+        NautilusBatchRename *dialog;
+
+        dialog = NAUTILUS_BATCH_RENAME (user_data);
+
+        dialog->selection = nautilus_batch_rename_sort (dialog->selection, ORIGINAL_ASCENDING);
+
+        gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
+                             "Original name (Ascending)  ");
+
+        /* update display text */
+        file_names_widget_entry_on_changed (dialog);
+}
+
+static void
+numbering_order_changed_first_modified (GSimpleAction       *action,
+                                        GVariant            *state,
+                                        gpointer            user_data)
+{
+        NautilusBatchRename *dialog;
+
+        dialog = NAUTILUS_BATCH_RENAME (user_data);
+
+        dialog->selection = nautilus_batch_rename_sort (dialog->selection, FIRST_MODIFIED);
+
+        gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
+                             "First Modified                       ");
+
+        /* update display text */
+        file_names_widget_entry_on_changed (dialog);
+}
+
+static void
+numbering_order_changed_last_modified (GSimpleAction       *action,
+                                       GVariant            *state,
+                                       gpointer            user_data)
+{
+        NautilusBatchRename *dialog;
+
+        dialog = NAUTILUS_BATCH_RENAME (user_data);
+
+        dialog->selection = nautilus_batch_rename_sort (dialog->selection, LAST_MODIFIED);
+
+        gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
+                             "Last Modified                       ");
+
+        /* update display text */
+        file_names_widget_entry_on_changed (dialog);
+}
+
+const GActionEntry dialog_entries[] = {
+        { "numbering-order-changed-descending",  numbering_order_changed_descending },
+        { "numbering-order-changed-ascending",  numbering_order_changed_ascending },
+        { "numbering-order-changed-first-modified",  numbering_order_changed_first_modified },
+        { "numbering-order-changed-last-modified",  numbering_order_changed_last_modified },
+};
+
 static GList*
 batch_rename_get_new_names (NautilusBatchRename *dialog)
 {
@@ -177,10 +259,16 @@ create_row_for_label (const gchar *new_text,
                                   "xalign", 0.0,
                                   "margin-start", 6,
                                   NULL);
-        gtk_box_pack_end (GTK_BOX (box), label_new, FALSE, FALSE, 0);
-        gtk_box_pack_end (GTK_BOX (box), arrow, FALSE, FALSE, 0);
-        gtk_box_pack_end (GTK_BOX (box), label_old, FALSE, FALSE, 0);
-        gtk_list_box_row_set_selectable (GTK_LIST_BOX_ROW (row), FALSE);
+
+        gtk_label_set_ellipsize (GTK_LABEL (label_new), PANGO_ELLIPSIZE_END);
+        gtk_label_set_max_width_chars (GTK_LABEL (label_new), MAX_DISPLAY_LEN);
+
+        gtk_label_set_max_width_chars (GTK_LABEL (label_old), MAX_DISPLAY_LEN);
+
+        gtk_box_pack_end (GTK_BOX (box), label_new, TRUE, FALSE, 0);
+        gtk_box_pack_end (GTK_BOX (box), arrow, TRUE, FALSE, 0);
+        gtk_box_pack_end (GTK_BOX (box), label_old, TRUE, FALSE, 0);
+        gtk_list_box_row_set_selectable (GTK_LIST_BOX_ROW (row), TRUE);
 
         gtk_container_add (GTK_CONTAINER (row), box);
         gtk_widget_show_all (row);
@@ -196,13 +284,11 @@ fill_display_listbox (NautilusBatchRename *dialog,
         GList *l1;
         GList *l2;
         GList *l;
-        gchar *tmp_name;
         NautilusFile *file;
 
         /* clear rows from listbox (if there are any) */
         if (dialog->listbox_rows != NULL)
                 for (l = dialog->listbox_rows; l != NULL; l = l->next) {
-                        /*Fix me: figure why this shows warning on run */
                         gtk_container_remove (GTK_CONTAINER (dialog->conflict_listbox),
                                               GTK_WIDGET (l->data));
                 }
@@ -210,12 +296,9 @@ fill_display_listbox (NautilusBatchRename *dialog,
         g_list_free (dialog->listbox_rows);
         dialog->listbox_rows = NULL;
 
-        /* add rows to a list so that they can be removed when there appears
-         * a new conflict */
-        dialog->listbox_rows = g_list_prepend (dialog->listbox_rows,
-                                                 (gpointer) row);
-
-        for (l1 = new_names, l2 = dialog->selection; l1 != NULL, l2 != NULL; l1 = l1->next, l2 = l2->next) {
+        /* add rows to a list so that they can be removed when the renaming
+         * result changes */
+        for (l1 = new_names, l2 = dialog->selection; l1 != NULL || l2 != NULL; l1 = l1->next, l2 = l2->next) 
{
                 file = NAUTILUS_FILE (l2->data);
 
                 row = create_row_for_label (l1->data, nautilus_file_get_name (file), TRUE);
@@ -231,12 +314,11 @@ static void
 file_names_widget_entry_on_changed (NautilusBatchRename *dialog)
 {
         gchar *entry_text;
-        gchar *replace_text;
+        //gchar *replace_text;
         gchar *file_name;
         GList *new_names;
         GList *duplicates;
         gchar *display_text = NULL;
-        NautilusFile *file;
         gboolean singe_conflict;
 
         if(dialog->selection == NULL)
@@ -305,7 +387,7 @@ file_names_widget_entry_on_changed (NautilusBatchRename *dialog)
 
         g_free (entry_text);
         g_free (file_name);
-        g_free (replace_text);
+        //g_free (replace_text);
         g_free (display_text);
 }
 
@@ -334,20 +416,20 @@ static void
 batch_rename_mode_changed (NautilusBatchRename *dialog)
 {
         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->format_mode_button))) {
-                gtk_stack_set_visible_child_name (dialog->mode_stack, "format");
+                gtk_stack_set_visible_child_name (GTK_STACK (dialog->mode_stack), "format");
 
                 dialog->mode = NAUTILUS_BATCH_RENAME_PREPEND;
 
                 gtk_entry_set_text (GTK_ENTRY (dialog->name_entry),
-                                    gtk_entry_get_text (dialog->find_entry));
+                                    gtk_entry_get_text (GTK_ENTRY (dialog->find_entry)));
 
         } else {
-                gtk_stack_set_visible_child_name (dialog->mode_stack, "replace");
+                gtk_stack_set_visible_child_name (GTK_STACK (dialog->mode_stack), "replace");
 
                 dialog->mode = NAUTILUS_BATCH_RENAME_REPLACE;
 
                 gtk_entry_set_text (GTK_ENTRY (dialog->find_entry),
-                                    gtk_entry_get_text (dialog->name_entry));
+                                    gtk_entry_get_text ( GTK_ENTRY (dialog->name_entry)));
         }
 
         /* update display text */
@@ -365,24 +447,33 @@ add_button_clicked (NautilusBatchRename *dialog)
 }
 
 static void
-numbering_order_button_clicked (NautilusBatchRename *dialog)
-{
-        if (gtk_widget_is_visible (dialog->numbering_order_popover))
-                gtk_widget_set_visible (dialog->numbering_order_popover, FALSE);
-        else
-                gtk_widget_set_visible (dialog->numbering_order_popover, TRUE);
-}
-
-static void
 add_popover_closed (NautilusBatchRename *dialog)
 {
         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->add_button), FALSE);
 }
 
 static void
-numbering_order_popover_closed (NautilusBatchRename *dialog)
+nautilus_batch_rename_initialize_actions (NautilusBatchRename *dialog)
 {
-        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->numbering_order_button), FALSE);
+        GAction *action;
+
+        dialog->action_group = G_ACTION_GROUP (g_simple_action_group_new ());
+
+        g_action_map_add_action_entries (G_ACTION_MAP (dialog->action_group),
+                                        dialog_entries,
+                                        G_N_ELEMENTS (dialog_entries),
+                                        dialog);
+        gtk_widget_insert_action_group (GTK_WIDGET (dialog),
+                                        "dialog",
+                                        G_ACTION_GROUP (dialog->action_group));
+
+        action = g_action_map_lookup_action (G_ACTION_MAP (dialog->action_group),
+                                             "numbering-order-changed-ascending");
+        g_simple_action_set_enabled (G_SIMPLE_ACTION (action),TRUE);
+
+        action = g_action_map_lookup_action (G_ACTION_MAP (dialog->action_group),
+                                             "numbering-order-changed-descending");
+        g_simple_action_set_enabled (G_SIMPLE_ACTION (action),TRUE);
 }
 
 static void
@@ -409,8 +500,8 @@ nautilus_batch_rename_class_init (NautilusBatchRenameClass *klass)
         gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, format_mode_button);
         gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, add_button);
         gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, add_popover);
-        gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, numbering_order_popover);
-        gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, numbering_order_button);
+        gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, add_button_label);
+        gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, numbering_order_label);
 
         gtk_widget_class_bind_template_callback (widget_class, file_names_widget_entry_on_changed);
         gtk_widget_class_bind_template_callback (widget_class, batch_rename_dialog_on_closed);
@@ -418,8 +509,6 @@ nautilus_batch_rename_class_init (NautilusBatchRenameClass *klass)
         gtk_widget_class_bind_template_callback (widget_class, batch_rename_mode_changed);
         gtk_widget_class_bind_template_callback (widget_class, add_button_clicked);
         gtk_widget_class_bind_template_callback (widget_class, add_popover_closed);
-        gtk_widget_class_bind_template_callback (widget_class, numbering_order_popover_closed);
-        gtk_widget_class_bind_template_callback (widget_class, numbering_order_button_clicked);
 }
 
 GtkWidget*
@@ -446,6 +535,9 @@ nautilus_batch_rename_new (NautilusFilesView *view)
         //gtk_label_set_ellipsize (GTK_LABEL (dialog->expander_label), PANGO_ELLIPSIZE_END);
         //gtk_label_set_max_width_chars (GTK_LABEL (dialog->expander_label), MAX_DISPLAY_LEN - 1);
 
+        gtk_label_set_markup_with_mnemonic (GTK_LABEL (dialog->add_button_label),
+                                            "<b>+</b> _Add");
+
         gtk_widget_set_vexpand (dialog->rename_button, FALSE);
 
         files_nr = 0;
@@ -457,6 +549,8 @@ nautilus_batch_rename_new (NautilusFilesView *view)
         sprintf (dialog_title, "Renaming %d files", files_nr);
         gtk_window_set_title (GTK_WINDOW (dialog), dialog_title);
 
+        nautilus_batch_rename_initialize_actions (dialog);
+
         /* update display text */
         file_names_widget_entry_on_changed (dialog);
 
diff --git a/src/nautilus-batch-rename.h b/src/nautilus-batch-rename.h
index 63fab69..b48423f 100644
--- a/src/nautilus-batch-rename.h
+++ b/src/nautilus-batch-rename.h
@@ -15,6 +15,15 @@ typedef enum {
         NAUTILUS_BATCH_RENAME_FORMAT = 3,
 } NautilusBatchRenameModes;
 
+typedef enum {
+        ORIGINAL_ASCENDING = 0,
+        ORIGINAL_DESCENDING = 1,
+        FIRST_CREATED = 2,
+        LAST_CREATED = 3,
+        FIRST_MODIFIED = 4,
+        LAST_MODIFIED = 5,
+} SortingMode;
+
 #define NAUTILUS_TYPE_BATCH_RENAME (nautilus_batch_rename_get_type())
 
 G_DECLARE_FINAL_TYPE (NautilusBatchRename, nautilus_batch_rename, NAUTILUS, BATCH_RENAME, GtkDialog);
diff --git a/src/resources/ui/nautilus-batch-rename-dialog.ui 
b/src/resources/ui/nautilus-batch-rename-dialog.ui
index 8fd3c92..1a38109 100644
--- a/src/resources/ui/nautilus-batch-rename-dialog.ui
+++ b/src/resources/ui/nautilus-batch-rename-dialog.ui
@@ -118,8 +118,13 @@
                         <child>
                           <object class="GtkToggleButton" id="add_button">
                             <property name="visible">True</property>
-                            <property name="label" translatable="yes">+ Add</property>
                             <signal name="toggled" handler="add_button_clicked" swapped="yes" />
+                            <child>
+                              <object class="GtkLabel" id="add_button_label">
+                                <property name="visible">True</property>
+                                <property name="can_focus">False</property>
+                              </object>
+                            </child>
                           </object>
                         </child>
                       </object>
@@ -130,7 +135,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkLabel" id="numbering_order_label">
+                      <object class="GtkLabel" id="numbering_order">
                         <property name="visible">True</property>
                         <property name="label" translatable="yes">Automatic Numbering Order</property>
                         <property name="can_focus">False</property>
@@ -143,14 +148,30 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkToggleButton" id="numbering_order_button">
+                      <object class="GtkMenuButton" id="numbering_order_button">
                         <property name="visible">True</property>
-                        <signal name="toggled" handler="numbering_order_button_clicked" swapped="yes" />
+                        <property name="sensitive">True</property>
+                        <property name="use-popover">True</property>
+                        <property name="menu_model">numbering_order_menu</property>
                         <child>
-                          <object class="GtkLabel" id="numering_order">
+                          <object class="GtkBox">
                             <property name="visible">True</property>
-                            <property name="label" translatable="yes">Original name (Ascending)</property>
-                            <property name="can_focus">False</property>
+                            <property name="orientation">horizontal</property>
+                            <property name="spacing">15</property>
+                            <child>
+                              <object class="GtkLabel" id="numbering_order_label">
+                                <property name="visible">True</property>
+                                <property name="label" translatable="yes">Original name (Ascending)  
</property>
+                                <property name="can_focus">False</property>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="GtkImage" id="action_icon">
+                                <property name="visible">True</property>
+                                <property name="icon-name">pan-down-symbolic</property>
+                                <property name="icon-size">1</property>
+                              </object>
+                            </child>
                           </object>
                         </child>
                       </object>
@@ -282,13 +303,7 @@
       </object>
     </child>
   </template>
-  <object class="GtkAdjustment" id="adjustment">
-    <property name="upper">100000000</property>
-    <property name="lower">0</property>
-    <property name="value">1</property>
-    <property name="step_increment">1</property>
-    <property name="page_increment">10</property>
-  </object>
+
   <object class="GtkPopover" id="add_popover">
     <property name="relative-to">add_button</property>
     <property name="position">bottom</property>
@@ -302,17 +317,32 @@
       </object>
     </child>
   </object>
-  <object class="GtkPopover" id="numbering_order_popover">
-    <property name="relative-to">numbering_order_button</property>
-    <property name="position">bottom</property>
-    <signal name="closed" handler="numbering_order_popover_closed" swapped="yes" />
-    <child>
-      <object class="GtkLabel">
-        <property name="visible">True</property>
-        <property name="label" translatable="yes">some other ordering option</property>
-        <property name="can_focus">False</property>
-        <property name="sensitive">True</property>
-      </object>
-    </child>
+
+  <object class="GtkImage" id="done_image">
+    <property name="visible">True</property>
+    <property name="icon_name">object-select-symbolic</property>
   </object>
+
+  <menu id="numbering_order_menu">
+    <section>
+      <item>
+        <attribute name="id">ascending</attribute>
+        <attribute name="label" translatable="yes">Original name (Ascending) </attribute>
+        <attribute name="action">dialog.numbering-order-changed-ascending</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Original name (Descending)</attribute>
+        <attribute name="action">dialog.numbering-order-changed-descending</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">First Modified</attribute>
+        <attribute name="action">dialog.numbering-order-changed-first-modified</attribute>
+        <attribute name="icon">object-select-symbolic</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Last Modified</attribute>
+        <attribute name="action">dialog.numbering-order-changed-last-modified</attribute>
+      </item>
+    </section>
+  </menu>
 </interface>
\ No newline at end of file


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