summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandru Pandelea <alexandru.pandelea@gmail.com>2016-07-11 22:00:34 +0300
committerAlexandru Pandelea <alexandru.pandelea@gmail.com>2016-07-11 22:00:34 +0300
commit062c3355b460f9c427466a75d8a3dd67fb73a283 (patch)
treec88bc9272ee6c36301670505bddb4ce06d7be7fb
parentf41b0a8474007d65a1de6ff5e6e96a6125c5a996 (diff)
downloadnautilus-062c3355b460f9c427466a75d8a3dd67fb73a283.tar.gz
Improve code
-rw-r--r--src/nautilus-batch-rename-utilities.c119
-rw-r--r--src/nautilus-batch-rename-utilities.h10
-rw-r--r--src/nautilus-batch-rename.c37
-rw-r--r--src/nautilus-batch-rename.h3
-rw-r--r--src/nautilus-file.c15
-rw-r--r--src/nautilus-file.h1
-rw-r--r--src/nautilus-files-view.c5
-rw-r--r--src/resources/ui/nautilus-batch-rename-dialog.ui4
8 files changed, 90 insertions, 104 deletions
diff --git a/src/nautilus-batch-rename-utilities.c b/src/nautilus-batch-rename-utilities.c
index db7e6a8e7..1c80671fd 100644
--- a/src/nautilus-batch-rename-utilities.c
+++ b/src/nautilus-batch-rename-utilities.c
@@ -1,6 +1,6 @@
+
#include "nautilus-batch-rename.h"
#include "nautilus-batch-rename-utilities.h"
-#include "nautilus-files-view.h"
#include "nautilus-file.h"
#include <glib.h>
@@ -9,6 +9,7 @@
#include <stdarg.h>
#define MAX_DISPLAY_LEN 40
+#define MAX_FILTER_LEN 500
typedef struct {
NautilusFile *file;
@@ -20,12 +21,15 @@ batch_rename_append (gchar *file_name,
gchar *entry_text)
{
gchar *result;
+ gint len;
+
+ len = strlen (entry_text) + strlen (file_name) + 1;
+ result = g_malloc (len);
- result = malloc (strlen (entry_text) + strlen (file_name) + 1);
if (result == NULL) {
return strdup (file_name);
}
- sprintf (result, "%s%s", file_name, entry_text);
+ g_snprintf (result, len, "%s%s", file_name, entry_text);
return result;
}
@@ -35,13 +39,16 @@ batch_rename_prepend (gchar *file_name,
gchar *entry_text)
{
gchar *result;
+ gint len;
+
+ len = strlen (entry_text) + strlen (file_name) + 1;
+ result = g_malloc (len);
- result = malloc (strlen (entry_text) + strlen (file_name) + 1);
if (result == NULL) {
return strdup (file_name);
}
- sprintf (result, "%s%s", entry_text, file_name);
+ g_snprintf (result, len, "%s%s", entry_text, file_name);
return result;
}
@@ -51,10 +58,9 @@ batch_rename_replace (gchar *string,
gchar *substr,
gchar *replacement)
{
- gchar *tok = NULL;
- gchar *newstr = NULL;
- gchar *oldstr = NULL;
- gint skip_chars;
+ GString *new_string;
+ gchar **splitted_string;
+ gint i, n_splits;
if (substr == NULL || replacement == NULL) {
return strdup (string);
@@ -64,34 +70,29 @@ batch_rename_replace (gchar *string,
return strdup (string);
}
- newstr = strdup (string);
+ splitted_string = g_strsplit (string, substr, -1);
+ if (splitted_string == NULL)
+ return string;
- skip_chars = 0;
+ n_splits = g_strv_length (splitted_string);
- while ((tok = strstr (newstr + skip_chars, substr))) {
- oldstr = newstr;
- newstr = malloc (strlen (oldstr) - strlen (substr) + strlen (replacement) + 1);
+ new_string = g_string_new ("");
- if (newstr == NULL) {
- g_free (oldstr);
- return strdup (string);
- }
+ i = 0;
- memcpy (newstr, oldstr, tok - oldstr);
- memcpy (newstr + (tok - oldstr), replacement, strlen (replacement));
- memcpy (newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ),
- strlen (oldstr) - strlen (substr) - (tok - oldstr));
- memset (newstr + strlen (oldstr) - strlen (substr) + strlen (replacement) , '\0', 1 );
+ while (i < n_splits) {
+ g_string_append (new_string, splitted_string[i]);
- skip_chars = strlen (oldstr) - strlen (tok) + strlen (replacement);
- g_free (oldstr);
+ if (i != n_splits - 1)
+ g_string_append (new_string, replacement);
+ i++;
}
- return newstr;
+ return new_string->str;
}
gchar*
-get_new_name (NautilusBatchRenameModes mode,
+get_new_name (NautilusBatchRenameMode mode,
gchar *file_name,
gchar *entry_text,
...)
@@ -120,7 +121,7 @@ get_new_name (NautilusBatchRenameModes mode,
}
GList*
-get_new_names_list (NautilusBatchRenameModes mode,
+get_new_names_list (NautilusBatchRenameMode mode,
GList *selection,
gchar *entry_text,
gchar *replace_text)
@@ -140,15 +141,15 @@ get_new_names_list (NautilusBatchRenameModes mode,
/* get the new name here and add it to the list*/
if (mode == NAUTILUS_BATCH_RENAME_PREPEND)
result = g_list_prepend (result,
- (gpointer) batch_rename_prepend (file_name, entry_text));
+ batch_rename_prepend (file_name, entry_text));
if (mode == NAUTILUS_BATCH_RENAME_APPEND)
result = g_list_prepend (result,
- (gpointer) batch_rename_append (file_name, entry_text));
+ batch_rename_append (file_name, entry_text));
if (mode == NAUTILUS_BATCH_RENAME_REPLACE)
result = g_list_prepend (result,
- (gpointer) batch_rename_replace (file_name, entry_text, replace_text));
+ batch_rename_replace (file_name, entry_text, replace_text));
g_free (file_name);
}
@@ -157,7 +158,7 @@ get_new_names_list (NautilusBatchRenameModes mode,
}
gchar*
-get_new_display_name (NautilusBatchRenameModes mode,
+get_new_display_name (NautilusBatchRenameMode mode,
gchar *file_name,
gchar *entry_text,
gchar *replace_text)
@@ -187,7 +188,7 @@ list_has_duplicates (NautilusFilesView *view,
if (strcmp (l1->data, file_name) != 0 && file_with_name_exists (view, l1->data) == TRUE) {
result = g_list_prepend (result,
- (gpointer) (l1->data));
+ l1->data);
}
g_free (file_name);
@@ -195,19 +196,6 @@ list_has_duplicates (NautilusFilesView *view,
return result;
}
-gchar*
-concat(gchar *s1, gchar *s2)
-{
- gchar *result;
-
- result = malloc (strlen(s1) + strlen(s2) + 1);
-
- memcpy(result, s1, strlen(s1));
- memcpy(result + strlen(s1), s2, strlen(s2) + 1);
-
- return result;
-}
-
gint
compare_files_by_name_ascending (gconstpointer a,
gconstpointer b)
@@ -317,14 +305,14 @@ nautilus_batch_rename_sort (GList *selection,
for (l = selection; l != NULL; l = l->next) {
CreateDateElem *elem;
- elem = malloc (sizeof (CreateDateElem*));
+ elem = g_malloc (sizeof (CreateDateElem*));
file = NAUTILUS_FILE (l->data);
elem->file = file;
elem->position = (gint*) g_hash_table_lookup (hash_table, nautilus_file_get_name (file));
- createDate_list = g_list_prepend (createDate_list, (gpointer) elem);
+ createDate_list = g_list_prepend (createDate_list, elem);
}
if (mode == FIRST_CREATED)
@@ -359,31 +347,31 @@ check_creation_date_for_selection (GList *selection)
NautilusFile *file;
gchar *query = "SELECT nfo:fileName(?file) nie:contentCreated(?file) WHERE { ?file a nfo:FileDataObject. ";
- filter1 = malloc (150);
- sprintf (filter1, "FILTER(tracker:uri-is-parent('%s', nie:url(?file)))",
+ filter1 = g_malloc (MAX_FILTER_LEN);
+ g_snprintf (filter1, MAX_FILTER_LEN, "FILTER(tracker:uri-is-parent('%s', nie:url(?file)))",
nautilus_file_get_parent_uri (NAUTILUS_FILE (selection->data)));
- sparql = concat (query, filter1);
+ sparql = g_strconcat (query, filter1, NULL);
for (l = selection; l != NULL; l = l->next) {
- filter2 = malloc (150);
+ filter2 = g_malloc (MAX_FILTER_LEN);
file = NAUTILUS_FILE (l->data);
if (l == selection)
- sprintf (filter2, "FILTER (nfo:fileName(?file) = '%s' ", nautilus_file_get_name (file));
+ g_snprintf (filter2, MAX_FILTER_LEN, "FILTER (nfo:fileName(?file) = '%s' ", nautilus_file_get_name (file));
else
- sprintf (filter2, "|| nfo:fileName(?file) = '%s'", nautilus_file_get_name (file));
+ g_snprintf (filter2, MAX_FILTER_LEN, "|| nfo:fileName(?file) = '%s'", nautilus_file_get_name (file));
tmp = sparql;
- sparql = concat (sparql, filter2);
+ sparql = g_strconcat (sparql, filter2, NULL);
g_free (tmp);
g_free (filter2);
}
tmp = sparql;
- sparql = concat (sparql, ")} ORDER BY ASC(nie:contentCreated(?file))");
+ sparql = g_strconcat (sparql, ")} ORDER BY ASC(nie:contentCreated(?file))", NULL);
connection = tracker_sparql_connection_get (NULL, &error);
if (!connection)
@@ -410,7 +398,7 @@ check_creation_date_for_selection (GList *selection)
/* Iterate, synchronously, the results */
while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
- value = malloc (sizeof(int));
+ value = g_malloc (sizeof(int));
*value = i++;
g_hash_table_insert (hash_table,
@@ -431,6 +419,23 @@ check_creation_date_for_selection (GList *selection)
g_object_unref (connection);
g_free (filter1);
+ g_free (sparql);
return hash_table;
}
+
+gboolean
+nautilus_file_can_rename_files (GList *selection)
+{
+ GList *l;
+ NautilusFile *file;
+
+ for (l = selection; l != NULL; l = l->next) {
+ file = NAUTILUS_FILE (l->data);
+
+ if (!nautilus_file_can_rename (file))
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/src/nautilus-batch-rename-utilities.h b/src/nautilus-batch-rename-utilities.h
index 9aaa1cf3a..875cde61d 100644
--- a/src/nautilus-batch-rename-utilities.h
+++ b/src/nautilus-batch-rename-utilities.h
@@ -5,17 +5,17 @@
#include <gtk/gtk.h>
#include <tracker-sparql.h>
-gchar* get_new_name (NautilusBatchRenameModes mode,
+gchar* get_new_name (NautilusBatchRenameMode mode,
gchar *file_name,
gchar *entry_text,
...);
-GList* get_new_names_list (NautilusBatchRenameModes mode,
+GList* get_new_names_list (NautilusBatchRenameMode mode,
GList *selection,
gchar *entry_text,
gchar *replace_text);
-gchar* get_new_display_name (NautilusBatchRenameModes mode,
+gchar* get_new_display_name (NautilusBatchRenameMode mode,
gchar *file_name,
gchar *entry_text,
gchar *replace_text);
@@ -24,9 +24,6 @@ GList* list_has_duplicates (NautilusFilesView *view,
GList *names,
GList *old_names);
-gchar* concat (gchar *s1,
- gchar *s2);
-
GList* nautilus_batch_rename_sort (GList *selection,
SortingMode mode,
...);
@@ -50,5 +47,6 @@ gint compare_files_by_last_created (gconstpointer a,
gconstpointer b);
GHashTable* check_creation_date_for_selection (GList *selection);
+gboolean nautilus_file_can_rename_files (GList *selection);
#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 40d54cd9d..be192ecbf 100644
--- a/src/nautilus-batch-rename.c
+++ b/src/nautilus-batch-rename.c
@@ -4,7 +4,7 @@
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
+ * the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
@@ -28,6 +28,7 @@
#define ADD_TEXT_ENTRY_SIZE 550
#define REPLACE_ENTRY_SIZE 275
#define MAX_DISPLAY_LEN 65
+#define DIALOG_TITLE_LEN 25
struct _NautilusBatchRename
{
@@ -56,11 +57,9 @@ struct _NautilusBatchRename
GList *listbox_rows;
GList *selection;
- NautilusBatchRenameModes mode;
+ NautilusBatchRenameMode mode;
NautilusFilesView *view;
- GtkWidget *expander_label;
-
GActionGroup *action_group;
GMenu *numbering_order_menu;
@@ -88,7 +87,7 @@ numbering_order_changed (GSimpleAction *action,
if (g_strcmp0 (target_name, "name-ascending") == 0) {
gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
- "Original name (Ascending) ");
+ "Original name (Ascending)");
dialog->selection = nautilus_batch_rename_sort (dialog->selection,
ORIGINAL_ASCENDING);
}
@@ -102,21 +101,21 @@ numbering_order_changed (GSimpleAction *action,
if (g_strcmp0 (target_name, "first-modified") == 0) {
gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
- "First Modified ");
+ "First Modified");
dialog->selection = nautilus_batch_rename_sort (dialog->selection,
FIRST_MODIFIED);
}
if (g_strcmp0 (target_name, "last-modified") == 0) {
gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
- "Last Modified ");
+ "Last Modified");
dialog->selection = nautilus_batch_rename_sort (dialog->selection,
LAST_MODIFIED);
}
if (g_strcmp0 (target_name, "first-created") == 0) {
gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
- "First Created ");
+ "First Created");
dialog->selection = nautilus_batch_rename_sort (dialog->selection,
FIRST_CREATED,
dialog->create_date);
@@ -124,7 +123,7 @@ numbering_order_changed (GSimpleAction *action,
if (g_strcmp0 (target_name, "last-created") == 0) {
gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
- "Last Created ");
+ "Last Created");
dialog->selection = nautilus_batch_rename_sort (dialog->selection,
LAST_CREATED,
dialog->create_date);
@@ -147,8 +146,8 @@ batch_rename_get_new_names (NautilusBatchRename *dialog)
{
GList *result = NULL;
GList *selection;
- gchar *entry_text;
- gchar *replace_text;
+ g_autofree gchar *entry_text;
+ g_autofree gchar *replace_text;
selection = dialog->selection;
@@ -161,8 +160,6 @@ batch_rename_get_new_names (NautilusBatchRename *dialog)
result = get_new_names_list (dialog->mode, selection, entry_text, replace_text);
- g_free (entry_text);
-
result = g_list_reverse (result);
return result;
@@ -186,7 +183,7 @@ rename_files_on_names_accepted (NautilusBatchRename *dialog,
nautilus_rename_file (file, l2->data, NULL, NULL);
}
- batch_rename_dialog_on_closed (GTK_DIALOG (dialog));
+ gtk_window_close (GTK_WINDOW (dialog));
}
static void
@@ -291,7 +288,7 @@ fill_display_listbox (NautilusBatchRename *dialog,
gtk_container_add (GTK_CONTAINER (dialog->conflict_listbox), row);
dialog->listbox_rows = g_list_prepend (dialog->listbox_rows,
- (gpointer) row);
+ row);
}
}
@@ -322,11 +319,11 @@ file_names_widget_entry_on_changed (NautilusBatchRename *dialog)
gtk_widget_set_sensitive (dialog->rename_button, FALSE);
return;
- }
- else
+ } else {
/* re-enable the rename button if there are no more name conflicts */
if (duplicates == NULL && !gtk_widget_is_sensitive (dialog->rename_button))
gtk_widget_set_sensitive (dialog->rename_button, TRUE);
+ }
/* Update listbox that shows the result of the renaming for each file */
fill_display_listbox (dialog, new_names);
@@ -457,8 +454,6 @@ nautilus_batch_rename_class_init (NautilusBatchRenameClass *klass)
GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
- dialog_class->close = batch_rename_dialog_on_closed;
-
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/nautilus/ui/nautilus-batch-rename-dialog.ui");
gtk_widget_class_bind_template_child (widget_class, NautilusBatchRename, grid);
@@ -522,8 +517,8 @@ nautilus_batch_rename_new (NautilusFilesView *view)
for (l = dialog->selection; l != NULL; l = l->next)
files_nr++;
- dialog_title = malloc (25);
- sprintf (dialog_title, "Renaming %d files", files_nr);
+ dialog_title = g_malloc (DIALOG_TITLE_LEN);
+ g_snprintf (dialog_title, DIALOG_TITLE_LEN, "Renaming %d files", files_nr);
gtk_window_set_title (GTK_WINDOW (dialog), dialog_title);
gtk_popover_bind_model (GTK_POPOVER (dialog->numbering_order_popover),
diff --git a/src/nautilus-batch-rename.h b/src/nautilus-batch-rename.h
index 3f34e075c..95d042a59 100644
--- a/src/nautilus-batch-rename.h
+++ b/src/nautilus-batch-rename.h
@@ -3,6 +3,7 @@
#define NAUTILUS_BATCH_RENAME_H
#include <glib.h>
+#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include "nautilus-files-view.h"
@@ -13,7 +14,7 @@ typedef enum {
NAUTILUS_BATCH_RENAME_PREPEND = 1,
NAUTILUS_BATCH_RENAME_REPLACE = 2,
NAUTILUS_BATCH_RENAME_FORMAT = 3,
-} NautilusBatchRenameModes;
+} NautilusBatchRenameMode;
typedef enum {
ORIGINAL_ASCENDING = 0,
diff --git a/src/nautilus-file.c b/src/nautilus-file.c
index 349255cca..e3cbdfe20 100644
--- a/src/nautilus-file.c
+++ b/src/nautilus-file.c
@@ -1525,21 +1525,6 @@ real_can_rename (NautilusFile *file)
}
gboolean
-nautilus_file_can_rename_files (GList *selection)
-{
- GList *l;
- NautilusFile *file;
-
- for(l = selection; l != NULL; l = l->next) {
- file = NAUTILUS_FILE (l->data);
-
- if (!nautilus_file_can_rename (file))
- return FALSE;
- }
- return TRUE;
-}
-
-gboolean
nautilus_file_can_delete (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
diff --git a/src/nautilus-file.h b/src/nautilus-file.h
index 5723423d1..10505753f 100644
--- a/src/nautilus-file.h
+++ b/src/nautilus-file.h
@@ -266,7 +266,6 @@ gboolean nautilus_file_can_read (Nautilu
gboolean nautilus_file_can_write (NautilusFile *file);
gboolean nautilus_file_can_execute (NautilusFile *file);
gboolean nautilus_file_can_rename (NautilusFile *file);
-gboolean nautilus_file_can_rename_files (GList *selection);
gboolean nautilus_file_can_delete (NautilusFile *file);
gboolean nautilus_file_can_trash (NautilusFile *file);
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index cda409fef..e1118e1aa 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -29,6 +29,7 @@
#include "nautilus-application.h"
#include "nautilus-batch-rename.h"
+ #include "nautilus-batch-rename-utilities.h"
#include "nautilus-error-reporting.h"
#include "nautilus-floating-bar.h"
#include "nautilus-list-view.h"
@@ -5515,7 +5516,7 @@ real_action_rename (NautilusFilesView *view,
{
NautilusFile *file;
GList *selection;
- NautilusBatchRename *dialog;
+ GtkWidget *dialog;
g_assert (NAUTILUS_IS_FILES_VIEW (view));
@@ -6320,7 +6321,7 @@ real_update_actions_state (NautilusFilesView *view)
have_bulk_rename_tool ());
else
g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
- nautilus_file_can_rename_files (selection));//use nautilus_file_can_rename_files
+ nautilus_file_can_rename_files (selection));
} else {
g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
selection_count == 1 &&
diff --git a/src/resources/ui/nautilus-batch-rename-dialog.ui b/src/resources/ui/nautilus-batch-rename-dialog.ui
index 8715c02ea..69fd9d57f 100644
--- a/src/resources/ui/nautilus-batch-rename-dialog.ui
+++ b/src/resources/ui/nautilus-batch-rename-dialog.ui
@@ -159,7 +159,9 @@
<child>
<object class="GtkLabel" id="numbering_order_label">
<property name="visible">True</property>
- <property name="label" translatable="yes">Original name (Ascending) </property>
+ <property name="width-request">180</property>
+ <property name="xalign">0</property>
+ <property name="label" translatable="yes">Original name (Ascending)</property>
<property name="can_focus">False</property>
</object>
</child>