[nautilus] Add possibility to register an external bulk rename tool



commit 27f14a83ca6b656c20f6f7851b11dee12bfd4540
Author: Holger Berndt <hb gnome org>
Date:   Tue Oct 5 22:01:02 2010 +0200

    Add possibility to register an external bulk rename tool
    
    If the new setting "bulk-rename-tool" in org.gnome.nautilus.preferences
    is set to a non-empty string, it is treated as a bulk renamer and
    gets invoked on a Rename action if multiple files are selected.
    Nautilus appends a space separated list of URIs of all selected
    files to the command string.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=306489

 libnautilus-private/nautilus-global-preferences.h  |    3 +
 .../org.gnome.nautilus.gschema.xml.in              |    5 +
 src/file-manager/fm-directory-view.c               |   85 +++++++++++++++++--
 3 files changed, 84 insertions(+), 9 deletions(-)
---
diff --git a/libnautilus-private/nautilus-global-preferences.h b/libnautilus-private/nautilus-global-preferences.h
index acdbb71..3292c29 100644
--- a/libnautilus-private/nautilus-global-preferences.h
+++ b/libnautilus-private/nautilus-global-preferences.h
@@ -194,6 +194,9 @@ typedef enum
 #define NAUTILUS_PREFERENCES_DESKTOP_NETWORK_NAME          "network-icon-name"
 #define NAUTILUS_PREFERENCES_DESKTOP_BACKGROUND_FADE       "background-fade"
 
+/* bulk rename utility */
+#define NAUTILUS_PREFERENCES_BULK_RENAME_TOOL              "bulk-rename-tool"
+
 /* Lockdown */
 #define NAUTILUS_PREFERENCES_LOCKDOWN_COMMAND_LINE         "disable-command-line"
 
diff --git a/libnautilus-private/org.gnome.nautilus.gschema.xml.in b/libnautilus-private/org.gnome.nautilus.gschema.xml.in
index 07282ec..c764a22 100644
--- a/libnautilus-private/org.gnome.nautilus.gschema.xml.in
+++ b/libnautilus-private/org.gnome.nautilus.gschema.xml.in
@@ -205,6 +205,11 @@
       <_summary>Whether to show hidden files</_summary>
       <_description>If set to true, then hidden files are shown by default in the file manager.  Hidden files are either dotfiles, listed in the folder's .hidden file or backup files ending with a tilde (~).</_description>
     </key>
+    <key name="bulk-rename-tool" type="ay">
+      <default>[]</default>
+      <_summary>Bulk rename utility</_summary>
+      <_description>If set, Nautilus will append URIs of selected files and treat the result as a command line for bulk renaming. Bulk rename applications can register themselves in this key by setting the key to a space-separated string of their executable name and any command line options. If the executable name is not set to a full path, it will be searched for in the search path.</_description>
+    </key>
   </schema>
 
   <schema id="org.gnome.nautilus.icon-view" path="/apps/nautilus/icon-view/" gettext-domain="nautilus">
diff --git a/src/file-manager/fm-directory-view.c b/src/file-manager/fm-directory-view.c
index da3207f..f09f03b 100644
--- a/src/file-manager/fm-directory-view.c
+++ b/src/file-manager/fm-directory-view.c
@@ -1873,6 +1873,25 @@ fm_directory_view_set_selection_locations (NautilusView *nautilus_view,
 	}
 }
 
+static char *
+get_bulk_rename_tool ()
+{
+	char *bulk_rename_tool;
+	g_settings_get (nautilus_preferences, NAUTILUS_PREFERENCES_BULK_RENAME_TOOL, "^ay", &bulk_rename_tool);
+	return g_strstrip (bulk_rename_tool);
+}
+
+static gboolean
+have_bulk_rename_tool ()
+{
+	char *bulk_rename_tool;
+	gboolean have_tool;
+
+	bulk_rename_tool = get_bulk_rename_tool ();
+	have_tool = ((bulk_rename_tool != NULL) && (*bulk_rename_tool != '\0'));
+	g_free (bulk_rename_tool);
+	return have_tool;
+}
 
 void
 fm_directory_view_init_view_iface (NautilusViewIface *iface)
@@ -6053,6 +6072,41 @@ action_paste_files_into_callback (GtkAction *action,
 }
 
 static void
+invoke_external_bulk_rename_utility (FMDirectoryView *view,
+				     GList *selection)
+{
+	GString *cmd;
+	char *parameter;
+	char *quoted_parameter;
+	char *bulk_rename_tool;
+	GList *walk;
+	NautilusFile *file;
+	GError *error = NULL;
+
+	/* assemble command line */
+	bulk_rename_tool = get_bulk_rename_tool ();
+	cmd = g_string_new (bulk_rename_tool);
+	g_free (bulk_rename_tool);
+	for (walk = selection; walk; walk = walk->next) {
+		file = walk->data;
+		parameter = nautilus_file_get_uri (file);
+		quoted_parameter = g_shell_quote (parameter);
+		g_free (parameter);
+		cmd = g_string_append (cmd, " ");
+		cmd = g_string_append (cmd, quoted_parameter);
+		g_free (quoted_parameter);
+	}
+
+	/* spawning and error handling */
+	gdk_spawn_command_line_on_screen (gtk_widget_get_screen (GTK_WIDGET (view)), cmd->str, &error);
+	g_string_free (cmd, TRUE);
+	if (error != NULL) {
+		eel_show_error_dialog (_("Could not invoke bulk rename utility"), error->message, NULL);
+		g_error_free (error);
+	}
+}
+
+static void
 real_action_rename (FMDirectoryView *view,
 		    gboolean select_all)
 {
@@ -6064,13 +6118,20 @@ real_action_rename (FMDirectoryView *view,
 	selection = fm_directory_view_get_selection (view);
 
 	if (selection_not_empty_in_menu_callback (view, selection)) {
-		file = NAUTILUS_FILE (selection->data);
-		if (!select_all) {
-			/* directories don't have a file extension, so
-			 * they are always pre-selected as a whole */
-			select_all = nautilus_file_is_directory (file);
+		/* If there is more than one file selected, invoke a batch renamer */
+		if (selection->next != NULL) {
+			if (have_bulk_rename_tool ()) {
+				invoke_external_bulk_rename_utility (view, selection);
+			}
+		} else {
+			file = NAUTILUS_FILE (selection->data);
+			if (!select_all) {
+				/* directories don't have a file extension, so
+				 * they are always pre-selected as a whole */
+				select_all = nautilus_file_is_directory (file);
+			}
+			EEL_CALL_METHOD (FM_DIRECTORY_VIEW_CLASS, view, start_renaming_file, (view, file, select_all));
 		}
-		EEL_CALL_METHOD (FM_DIRECTORY_VIEW_CLASS, view, start_renaming_file, (view, file, select_all));
 	}
 
 	nautilus_file_list_free (selection);
@@ -8441,9 +8502,15 @@ real_update_menus (FMDirectoryView *view)
 
 	action = gtk_action_group_get_action (view->details->dir_action_group,
 					      FM_ACTION_RENAME);
-	gtk_action_set_sensitive (action,
-				  selection_count == 1 &&
-				  fm_directory_view_can_rename_file (view, selection->data));
+	/* rename sensitivity depending on selection */
+	if (selection_count > 1) {
+		/* If multiple files are selected, sensitivity depends on whether a bulk renamer is registered. */
+		gtk_action_set_sensitive (action, have_bulk_rename_tool ());
+	} else {
+		gtk_action_set_sensitive (action,
+					  selection_count == 1 &&
+					  fm_directory_view_can_rename_file (view, selection->data));
+	}
 
 	action = gtk_action_group_get_action (view->details->dir_action_group,
 					      FM_ACTION_NEW_FOLDER);



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