[nautilus/wip/alexpandelea/batchRename] Use Gstring* instead of gchar*



commit f8d32edc72ad9f253316ac0da69042a6db264ad7
Author: Alexandru Pandelea <alexandru pandelea gmail com>
Date:   Sat Jul 16 12:58:08 2016 +0300

    Use Gstring* instead of gchar*

 src/nautilus-batch-rename-utilities.c            |   82 +++++++++++++---------
 src/nautilus-batch-rename-utilities.h            |    5 +-
 src/nautilus-batch-rename.c                      |   29 ++++----
 src/resources/ui/nautilus-batch-rename-dialog.ui |   24 +++++-
 4 files changed, 89 insertions(+), 51 deletions(-)
---
diff --git a/src/nautilus-batch-rename-utilities.c b/src/nautilus-batch-rename-utilities.c
index 8bc4dd9..082a56e 100644
--- a/src/nautilus-batch-rename-utilities.c
+++ b/src/nautilus-batch-rename-utilities.c
@@ -25,44 +25,51 @@ static void cursor_callback (GObject      *object,
                              GAsyncResult *result,
                              gpointer      user_data);
 
-static gchar*
+void
+string_free (gpointer mem)
+{
+        g_string_free (mem, TRUE);
+}
+
+static GString*
 batch_rename_append (gchar *file_name,
                      gchar *entry_text)
 {
-        gchar *result;
-        gint len;
+        GString *result;
 
-        len = strlen (entry_text) + strlen (file_name);
-        result = g_malloc (len);
+        result = g_string_new ("");
 
         if (result == NULL) {
-            return strdup (file_name);
+                g_string_append (result, file_name);
+
+                return result;
         }
-        g_snprintf (result, len, "%s%s", file_name, entry_text);
+
+        g_string_append_printf (result, "%s%s", file_name, entry_text);
 
         return result;
 }
 
-static gchar*
+static GString*
 batch_rename_prepend (gchar *file_name,
                       gchar *entry_text)
 {
-        gchar *result;
-        gint len;
+        GString *result;
 
-        len = strlen (entry_text) + strlen (file_name);
-        result = g_malloc (len);
+        result = g_string_new ("");
 
         if (result == NULL) {
-            return strdup (file_name);
+                g_string_append (result, file_name);
+
+                return result;
         }
 
-        g_snprintf (result, len, "%s%s", entry_text, file_name);
+        g_string_append_printf (result, "%s%s", entry_text, file_name);
 
         return result;
 }
 
-static gchar*
+static GString*
 batch_rename_replace (gchar *string,
                       gchar *substr,
                       gchar *replacement)
@@ -71,21 +78,28 @@ batch_rename_replace (gchar *string,
         gchar **splitted_string;
         gint i, n_splits;
 
+        new_string = g_string_new ("");
+
         if (substr == NULL || replacement == NULL) {
-                return strdup (string);
+                g_string_append (new_string, string);
+
+                return new_string;
         }
 
         if (strcmp (substr, "") == 0) {
-                return strdup (string);
+                g_string_append (new_string, string);
+
+                return new_string;
         }
 
         splitted_string = g_strsplit (string, substr, -1);
-        if (splitted_string == NULL)
-            return string;
+        if (splitted_string == NULL) {
+                g_string_append (new_string, string);
 
-        n_splits = g_strv_length (splitted_string);
+                return new_string;
+        }
 
-        new_string = g_string_new ("");
+        n_splits = g_strv_length (splitted_string);
 
         for (i = 0; i < n_splits; i++) {
             g_string_append (new_string, splitted_string[i]);
@@ -94,7 +108,7 @@ batch_rename_replace (gchar *string,
                 g_string_append (new_string, replacement);
         }
 
-        return new_string->str;
+        return new_string;
 }
 
 GList*
@@ -105,32 +119,35 @@ get_new_names_list (NautilusBatchRenameMode      mode,
 {
         GList *l;
         GList *result;
-        gchar *file_name;
+        GString *file_name;
         NautilusFile *file;
 
         result = NULL;
+        file_name = g_string_new ("");
 
         for (l = selection; l != NULL; l = l->next) {
                 file = NAUTILUS_FILE (l->data);
 
-                file_name = strdup (nautilus_file_get_name (file));
+                g_string_append (file_name ,nautilus_file_get_name (file));
 
                 /* get the new name here and add it to the list*/
                 if (mode == NAUTILUS_BATCH_RENAME_PREPEND)
                         result = g_list_prepend (result,
-                                                 batch_rename_prepend (file_name, entry_text));
+                                                 batch_rename_prepend (file_name->str, entry_text));
 
                 if (mode == NAUTILUS_BATCH_RENAME_APPEND)
                         result = g_list_prepend (result,
-                                                 batch_rename_append (file_name, entry_text));
+                                                 batch_rename_append (file_name->str, entry_text));
 
                 if (mode == NAUTILUS_BATCH_RENAME_REPLACE)
                         result = g_list_prepend (result,
-                                                 batch_rename_replace (file_name, entry_text, replace_text));
+                                                 batch_rename_replace (file_name->str, entry_text, 
replace_text));
                 
-                g_free (file_name);
+                g_string_erase (file_name, 0, -1);
         }
 
+        g_string_free (file_name, TRUE);
+
         return result;
 }
 
@@ -146,7 +163,7 @@ list_has_duplicates (NautilusDirectory *model,
 
         GList *directory_files, *l1, *l2, *result;
         NautilusFile *file1, *file2;
-        GString *file_name1, *file_name2;
+        GString *file_name1, *file_name2, *new_name;
 
         directory_files = nautilus_directory_get_file_list (model);
 
@@ -157,20 +174,21 @@ list_has_duplicates (NautilusDirectory *model,
 
         for (l1 = new_names; l1 != NULL; l1 = l1->next) {
                 file1 = NAUTILUS_FILE (selection->data);
+                new_name = l1->data;
 
                 g_string_erase (file_name1, 0, -1);
                 g_string_append (file_name1, nautilus_file_get_name (file1));
 
                 /* check for duplicate only if the name has changed */
-                if (g_strcmp0 (l1->data, file_name1->str) != 0) {
+                if (!g_string_equal (new_name, file_name1)) {
                         for (l2 = directory_files; l2 != NULL; l2 = l2->next) {
                                file2 = NAUTILUS_FILE (l2->data);
 
                                 g_string_erase (file_name2, 0, -1);
                                 g_string_append (file_name2, nautilus_file_get_name (file2));
 
-                                if (g_strcmp0 (l1->data, file_name2->str) == 0) {
-                                        result = g_list_prepend (result, strdup (l1->data));
+                                if (g_string_equal (new_name, file_name2)) {
+                                        result = g_list_prepend (result, strdup (new_name->str));
                                         break;
                                 }
                         }
diff --git a/src/nautilus-batch-rename-utilities.h b/src/nautilus-batch-rename-utilities.h
index ced8797..967e347 100644
--- a/src/nautilus-batch-rename-utilities.h
+++ b/src/nautilus-batch-rename-utilities.h
@@ -37,8 +37,11 @@ gint compare_files_by_first_created             (gconstpointer a,
 gint compare_files_by_last_created              (gconstpointer a,
                                                  gconstpointer b);
 
-void check_creation_date_for_selection   (NautilusBatchRename *dialog,
+void check_creation_date_for_selection          (NautilusBatchRename *dialog,
                                                  GList               *selection);
+
 gboolean selection_has_single_parent            (GList *selection);
 
+void string_free                                (gpointer mem);
+
 #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 bb0a517..54025ab 100644
--- a/src/nautilus-batch-rename.c
+++ b/src/nautilus-batch-rename.c
@@ -45,7 +45,6 @@ struct _NautilusBatchRename
         GtkWidget               *replace_mode_button;
         GtkWidget               *add_button;
         GtkWidget               *add_popover;
-        GtkWidget               *add_button_label;
         GtkWidget               *numbering_order_label;
         GtkWidget               *scrolled_window;
         GtkWidget               *numbering_order_popover;
@@ -185,13 +184,15 @@ rename_files_on_names_accepted (NautilusBatchRename *dialog,
         GList *l2;
         GList *selection;
         NautilusFile *file;
+        GString *new_name;
 
         selection = dialog->selection;
 
         for (l1 = selection, l2 = new_names; l1 != NULL && l2 != NULL; l1 = l1->next, l2 = l2->next) {
                 file = NAUTILUS_FILE (l1->data);
+                new_name = l2->data;
 
-                nautilus_rename_file (file, l2->data, NULL, NULL);
+                nautilus_rename_file (file, new_name->str, NULL, NULL);
         }
 
         gtk_window_close (GTK_WINDOW (dialog));
@@ -254,6 +255,7 @@ create_row_for_label (const gchar *new_text,
                                   NULL);
 
         gtk_label_set_ellipsize (GTK_LABEL (label_new), PANGO_ELLIPSIZE_END);
+        gtk_label_set_ellipsize (GTK_LABEL (label_old), PANGO_ELLIPSIZE_END);
 
         gtk_box_pack_end (GTK_BOX (box), label_new, TRUE, FALSE, 0);
         gtk_box_pack_end (GTK_BOX (box), icon, TRUE, FALSE, 0);
@@ -275,6 +277,7 @@ fill_display_listbox (NautilusBatchRename *dialog,
         GList *l2;
         GList *l;
         NautilusFile *file;
+        GString *new_name;
 
         /* clear rows from listbox (if any) */
         if (dialog->listbox_rows != NULL)
@@ -290,8 +293,9 @@ fill_display_listbox (NautilusBatchRename *dialog,
          * result changes */
         for (l1 = new_names, l2 = dialog->selection; l1 != NULL || l2 != NULL; l1 = l1->next, l2 = l2->next) 
{
                 file = NAUTILUS_FILE (l2->data);
+                new_name = l1->data;
 
-                row = create_row_for_label (l1->data, nautilus_file_get_name (file), TRUE);
+                row = create_row_for_label (new_name->str, nautilus_file_get_name (file), TRUE);
 
                 gtk_container_add (GTK_CONTAINER (dialog->conflict_listbox), row);
 
@@ -304,7 +308,7 @@ static void
 select_nth_conflict (NautilusBatchRename *dialog)
 {
         GList *l, *new_names, *l2;
-        GString *file_name, *display_text;
+        GString *file_name, *display_text, *new_name;
         gint n;
         NautilusFile *file;
 
@@ -321,11 +325,12 @@ select_nth_conflict (NautilusBatchRename *dialog)
 
         for (l = new_names; l != NULL; l = l->next) {
                 file = NAUTILUS_FILE (l2->data);
+                new_name = l->data;
 
-                /* first g_strcmp0 is for not selecting a file that doesn't change
+                /* g_strcmp0 is used for not selecting a file that doesn't change
                  * it's name */
-                if (g_strcmp0 ((gchar*) l->data, nautilus_file_get_name (file)) &&
-                    g_strcmp0 (file_name->str, (gchar*) l->data) == 0)
+                if (g_strcmp0 (new_name->str, nautilus_file_get_name (file)) &&
+                    g_string_equal (file_name, new_name))
                         break;
                 n++;
                 l2 = l2->next;
@@ -342,7 +347,7 @@ select_nth_conflict (NautilusBatchRename *dialog)
         gtk_label_set_label (GTK_LABEL (dialog->conflict_label),
                              display_text->str);
 
-        g_list_free_full (new_names, g_free);
+        g_list_free_full (new_names, string_free);
 }
 
 static void
@@ -414,7 +419,7 @@ file_names_widget_entry_on_changed (NautilusBatchRename *dialog)
                         gtk_widget_set_sensitive (dialog->rename_button, TRUE);
         }
 
-        g_list_free_full (new_names, g_free);
+        g_list_free_full (new_names, string_free);
 }
 
 static void
@@ -429,7 +434,7 @@ file_names_widget_on_activate (NautilusBatchRename *dialog)
 
         /* if names are all right call rename_files_on_names_accepted*/
         rename_files_on_names_accepted (dialog, new_names);
-        g_list_free (new_names);
+        g_list_free_full (new_names, string_free);
 }
 
 static void
@@ -579,7 +584,6 @@ 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, add_button_label);
         gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, numbering_order_label);
         gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, scrolled_window);
         gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, numbering_order_popover);
@@ -650,9 +654,6 @@ nautilus_batch_rename_init (NautilusBatchRename *self)
 
         self->mode = NAUTILUS_BATCH_RENAME_PREPEND;
 
-        gtk_label_set_markup_with_mnemonic (GTK_LABEL (self->add_button_label),
-                                            "<b>+</b> _Add");
-
         gtk_popover_bind_model (GTK_POPOVER (self->numbering_order_popover),
                                 G_MENU_MODEL (self->numbering_order_menu),
                                 NULL);
diff --git a/src/resources/ui/nautilus-batch-rename-dialog.ui 
b/src/resources/ui/nautilus-batch-rename-dialog.ui
index b715b48..e38e42c 100644
--- a/src/resources/ui/nautilus-batch-rename-dialog.ui
+++ b/src/resources/ui/nautilus-batch-rename-dialog.ui
@@ -117,9 +117,25 @@
                             <property name="visible">True</property>
                             <signal name="toggled" handler="add_button_clicked" swapped="yes" />
                             <child>
-                              <object class="GtkLabel" id="add_button_label">
+                              <object class="GtkBox">
                                 <property name="visible">True</property>
-                                <property name="can_focus">False</property>
+                                <property name="orientation">horizontal</property>
+                                <property name="spacing">0</property>
+                                <child>
+                                  <object class="GtkImage">
+                                    <property name="visible">True</property>
+                                    <property name="icon-name">list-add-symbolic</property>
+                                    <property name="icon-size">1</property>
+                                  </object>
+                                </child>
+                                <child>
+                                  <object class="GtkLabel">
+                                    <property name="visible">True</property>
+                                    <property name="xalign">0</property>
+                                    <property name="label" translatable="yes">Add</property>
+                                    <property name="can_focus">False</property>
+                                  </object>
+                                </child>
                               </object>
                             </child>
                           </object>
@@ -210,7 +226,7 @@
                       <object class="GtkEntry" id="find_entry">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
-                        <property name="width_request">400</property>
+                        <property name="width_request">375</property>
                         <property name="hexpand">False</property>
                         <property name="activates-default">True</property>
                         <signal name="changed" handler="file_names_widget_entry_on_changed" swapped="yes" />
@@ -239,7 +255,7 @@
                       <object class="GtkEntry" id="replace_entry">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
-                        <property name="width_request">400</property>
+                        <property name="width_request">375</property>
                         <signal name="changed" handler="file_names_widget_entry_on_changed" swapped="yes" />
                         <signal name="activate" handler="file_names_widget_on_activate" swapped="yes" />
                       </object>


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