[nautilus-actions] Use a command pattern rather than a prefix for execution in a terminal



commit a6c52a495f121248171017559852014fa02f614a
Author: Pierre Wieser <pwieser trychlos org>
Date:   Fri Feb 25 14:15:02 2011 +0100

    Use a command pattern rather than a prefix for execution in a terminal

 ChangeLog                                    |   15 +++++++++
 docs/reference/nautilus-actions-sections.txt |    1 +
 src/api/na-core-utils.h                      |    1 +
 src/core/na-core-utils.c                     |   32 ++++++++++++++++++++
 src/core/na-settings.c                       |    2 +-
 src/core/na-settings.h                       |    2 +-
 src/core/na-tokens.c                         |   25 +++++++++------
 src/core/na-tokens.h                         |   10 +++---
 src/nact/nact-preferences-editor.c           |   41 +++++++++++++------------
 src/nact/nact-preferences.ui                 |    8 ++--
 10 files changed, 96 insertions(+), 41 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f09ec81..2f2baed 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
 2011-02-25 Pierre Wieser <pwieser trychlos org>
 
+	* src/api/na-core-utils.h:
+	* src/core/na-core-utils.c (na_core_utils_str_subst): New function.
+
+	* src/core/na-settings.c:
+	* src/core/na-settings.h:
+	Rename "terminal-prefix" user preference as "terminal-pattern".
+
+	* src/core/na-tokens.c:
+	* src/core/na-tokens.h (na_tokens_command_from_terminal_prefix):
+	Renamed as na_tokens_command_for_terminal().
+
+	* docs/reference/nautilus-actions-sections.txt:
+	* src/nact/nact-preferences-editor.c:
+	* src/nact/nact-preferences.ui: Updated accordingly.
+
 	* src/nact/nact-iexecution-tab.c (on_main_selection_changed):
 	Disable not implemented frames.
 
diff --git a/docs/reference/nautilus-actions-sections.txt b/docs/reference/nautilus-actions-sections.txt
index d2459e6..0bda1e2 100644
--- a/docs/reference/nautilus-actions-sections.txt
+++ b/docs/reference/nautilus-actions-sections.txt
@@ -728,6 +728,7 @@ na_core_utils_str_collate
 na_core_utils_str_remove_char
 na_core_utils_str_remove_suffix
 na_core_utils_str_split_first_word
+na_core_utils_str_subst
 na_core_utils_slist_add_message
 na_core_utils_slist_duplicate
 na_core_utils_slist_dump
diff --git a/src/api/na-core-utils.h b/src/api/na-core-utils.h
index c01d070..0167751 100644
--- a/src/api/na-core-utils.h
+++ b/src/api/na-core-utils.h
@@ -53,6 +53,7 @@ int      na_core_utils_str_collate( const gchar *str1, const gchar *str2 );
 gchar   *na_core_utils_str_remove_char( const gchar *string, const gchar *to_remove );
 gchar   *na_core_utils_str_remove_suffix( const gchar *string, const gchar *suffix );
 void     na_core_utils_str_split_first_word( const gchar *string, gchar **first, gchar **other );
+gchar   *na_core_utils_str_subst( const gchar *pattern, const gchar *key, const gchar *subst );
 
 /* some functions to get or set GSList list of strings
  */
diff --git a/src/core/na-core-utils.c b/src/core/na-core-utils.c
index d08ef7b..6277a1f 100644
--- a/src/core/na-core-utils.c
+++ b/src/core/na-core-utils.c
@@ -252,6 +252,38 @@ na_core_utils_str_split_first_word( const gchar *string, gchar **first, gchar **
 	}
 }
 
+/**
+ * na_core_utils_str_subst:
+ * @pattern: the pattern.
+ * @key: the key string to be substituted.
+ * @subst: the string which will replace @key.
+ *
+ * Returns: a copy of @pattern where the first occurrence of @key has been
+ * substituted with @subst, as a newly allocated string which should be
+ * g_free() by the caller.
+ *
+ * Returns: a copy of @pattern if @key is not found in @pattern.
+ */
+gchar *
+na_core_utils_str_subst( const gchar *pattern, const gchar *key, const gchar *subst )
+{
+	GString *result;
+	gchar *found;
+
+	result = g_string_new( "" );
+	found = g_strstr_len( pattern, -1, key );
+	if( found ){
+		result = g_string_append_len( result, pattern, ( gssize )( found - pattern ));
+		result = g_string_append( result, subst );
+		result = g_string_append( result, found + g_utf8_strlen( key, -1 ));
+
+	} else {
+		result = g_string_append( result, pattern );
+	}
+
+	return( g_string_free( result, FALSE ));
+}
+
 void
 na_core_utils_slist_add_message( GSList **messages, const gchar *format, ... )
 {
diff --git a/src/core/na-settings.c b/src/core/na-settings.c
index 6ed2767..889682f 100644
--- a/src/core/na-settings.c
+++ b/src/core/na-settings.c
@@ -183,7 +183,7 @@ static const KeyDef st_def_keys[] = {
 	{ NA_IPREFS_RELABEL_DUPLICATE_PROFILE,        GROUP_NACT,    NA_DATA_TYPE_BOOLEAN,     "false" },
 	{ NA_IPREFS_SCHEME_ADD_SCHEME_WSP,            GROUP_NACT,    NA_DATA_TYPE_UINT_LIST,   "" },
 	{ NA_IPREFS_SCHEME_DEFAULT_LIST,              GROUP_NACT,    NA_DATA_TYPE_STRING_LIST, "" },
-	{ NA_IPREFS_TERMINAL_PREFIX,                  GROUP_RUNTIME, NA_DATA_TYPE_STRING,      "" },
+	{ NA_IPREFS_TERMINAL_PATTERN,                 GROUP_RUNTIME, NA_DATA_TYPE_STRING,      "" },
 	{ NA_IPREFS_IO_PROVIDER_READABLE,             NA_IPREFS_IO_PROVIDER_GROUP, NA_DATA_TYPE_BOOLEAN, "true" },
 	{ NA_IPREFS_IO_PROVIDER_WRITABLE,             NA_IPREFS_IO_PROVIDER_GROUP, NA_DATA_TYPE_BOOLEAN, "true" },
 	{ 0 }
diff --git a/src/core/na-settings.h b/src/core/na-settings.h
index 70745b9..d5e2176 100644
--- a/src/core/na-settings.h
+++ b/src/core/na-settings.h
@@ -124,7 +124,7 @@ G_BEGIN_DECLS
 #define NA_IPREFS_RELABEL_DUPLICATE_PROFILE			"relabel-when-duplicate-profile"
 #define NA_IPREFS_SCHEME_ADD_SCHEME_WSP				"scheme-add-scheme-wsp"
 #define NA_IPREFS_SCHEME_DEFAULT_LIST				"scheme-default-list"
-#define NA_IPREFS_TERMINAL_PREFIX					"terminal-prefix"
+#define NA_IPREFS_TERMINAL_PATTERN					"terminal-pattern"
 
 #define NA_IPREFS_IO_PROVIDER_GROUP					"io-provider"
 #define NA_IPREFS_IO_PROVIDER_READABLE				"readable"
diff --git a/src/core/na-tokens.c b/src/core/na-tokens.c
index e1c4403..ac66f9e 100644
--- a/src/core/na-tokens.c
+++ b/src/core/na-tokens.c
@@ -542,8 +542,8 @@ execute_action_command( gchar *command, const NAObjectProfile *profile )
 static gchar *
 get_command_execution_display_output( const gchar *command )
 {
-	static const gchar *bin_sh = "/bin/sh -c ";
-	return( na_tokens_command_from_terminal_prefix( bin_sh, command ));
+	static const gchar *bin_sh = "/bin/sh -c \"%s\"";
+	return( na_tokens_command_for_terminal( bin_sh, command ));
 }
 
 static gchar *
@@ -562,27 +562,32 @@ static gchar *
 get_command_execution_terminal( const gchar *command )
 {
 	gchar *run_command;
-	gchar *prefix;
+	gchar *pattern;
 
-	prefix = na_settings_get_string( NA_IPREFS_TERMINAL_PREFIX, NULL, NULL );
-	run_command = na_tokens_command_from_terminal_prefix( prefix, command );
-	g_free( prefix );
+	pattern = na_settings_get_string( NA_IPREFS_TERMINAL_PATTERN, NULL, NULL );
+	run_command = na_tokens_command_for_terminal( pattern, command );
+	g_free( pattern );
 
 	return( run_command );
 }
 
 /**
- * na_tokens_command_from_terminal_prefix:
+ * na_tokens_command_for_terminal:
+ * @pattern: the command pattern; should include a 'COMMAND' keyword
+ * @command: the command to be actually run in the terminal
+ *
+ * Returns: the command to be run, as a newly allocated string which should
+ * be g_free() by the caller.
  */
 gchar *
-na_tokens_command_from_terminal_prefix( const gchar *prefix, const gchar *command )
+na_tokens_command_for_terminal( const gchar *pattern, const gchar *command )
 {
 	gchar *run_command;
 	gchar *quoted;
 
-	if( prefix && strlen( prefix ) && strncmp( command, prefix, strlen( prefix ))){
+	if( pattern && strlen( pattern )){
 		quoted = g_shell_quote( command );
-		run_command = g_strconcat( prefix, " ", quoted, NULL );
+		run_command = na_core_utils_str_subst( pattern, "COMMAND", quoted );
 		g_free( quoted );
 
 	} else {
diff --git a/src/core/na-tokens.h b/src/core/na-tokens.h
index 2a90a96..32cc7c1 100644
--- a/src/core/na-tokens.h
+++ b/src/core/na-tokens.h
@@ -111,13 +111,13 @@ typedef struct {
 
 GType     na_tokens_get_type( void );
 
-NATokens *na_tokens_new_for_example   ( void );
-NATokens *na_tokens_new_from_selection( GList *selection );
+NATokens *na_tokens_new_for_example     ( void );
+NATokens *na_tokens_new_from_selection  ( GList *selection );
 
-gchar    *na_tokens_parse_for_display ( const NATokens *tokens, const gchar *string, gboolean utf8 );
-void      na_tokens_execute_action    ( const NATokens *tokens, const NAObjectProfile *profile );
+gchar    *na_tokens_parse_for_display   ( const NATokens *tokens, const gchar *string, gboolean utf8 );
+void      na_tokens_execute_action      ( const NATokens *tokens, const NAObjectProfile *profile );
 
-gchar    *na_tokens_command_from_terminal_prefix( const gchar *prefix, const gchar *command );
+gchar    *na_tokens_command_for_terminal( const gchar *pattern, const gchar *command );
 
 G_END_DECLS
 
diff --git a/src/nact/nact-preferences-editor.c b/src/nact/nact-preferences-editor.c
index f32335c..c53917e 100644
--- a/src/nact/nact-preferences-editor.c
+++ b/src/nact/nact-preferences-editor.c
@@ -69,8 +69,8 @@ struct _NactPreferencesEditorPrivate {
 	gboolean about_item_mandatory;
 
 	/* second tab: runtime execution */
-	gchar   *terminal_prefix;
-	gboolean terminal_prefix_mandatory;
+	gchar   *terminal_pattern;
+	gboolean terminal_pattern_mandatory;
 	gchar   *desktop;
 	gboolean desktop_mandatory;
 
@@ -138,8 +138,8 @@ static void     root_menu_setup( NactPreferencesEditor *editor );
 static void     root_menu_on_toggled( GtkToggleButton *button, NactPreferencesEditor *editor );
 static void     about_item_setup( NactPreferencesEditor *editor );
 static void     about_item_on_toggled( GtkToggleButton *button, NactPreferencesEditor *editor );
-static void     terminal_prefix_setup( NactPreferencesEditor *editor );
-static void     terminal_prefix_on_changed( GtkEntry *entry, NactPreferencesEditor *editor );
+static void     terminal_pattern_setup( NactPreferencesEditor *editor );
+static void     terminal_pattern_on_changed( GtkEntry *entry, NactPreferencesEditor *editor );
 static void     desktop_create_model( NactPreferencesEditor *editor );
 static void     desktop_setup( NactPreferencesEditor *editor );
 static void     desktop_on_changed( GtkComboBox *combo, NactPreferencesEditor *editor );
@@ -388,7 +388,7 @@ on_base_initialize_base_window( NactPreferencesEditor *editor )
 
 		/* second tab: runtime execution
 		 */
-		terminal_prefix_setup( editor );
+		terminal_pattern_setup( editor );
 		desktop_setup( editor );
 
 		/* third tab: ui preferences
@@ -600,45 +600,46 @@ about_item_on_toggled( GtkToggleButton *button, NactPreferencesEditor *editor )
 }
 
 /*
- * terminal prefix
- * the prefix to add to the command when execution mode is 'Terminal'
+ * terminal command
+ * the command when execution mode is 'Terminal'
+ * should have a 'COMMAND' keyword inside
  */
 static void
-terminal_prefix_setup( NactPreferencesEditor *editor )
+terminal_pattern_setup( NactPreferencesEditor *editor )
 {
 	gboolean editable;
 	GtkWidget *entry;
 
-	editor->private->terminal_prefix = na_settings_get_string( NA_IPREFS_TERMINAL_PREFIX, NULL, &editor->private->terminal_prefix_mandatory );
-	editable = !editor->private->preferences_locked && !editor->private->terminal_prefix_mandatory;
+	editor->private->terminal_pattern = na_settings_get_string( NA_IPREFS_TERMINAL_PATTERN, NULL, &editor->private->terminal_pattern_mandatory );
+	editable = !editor->private->preferences_locked && !editor->private->terminal_pattern_mandatory;
 
 	entry = base_window_get_widget( BASE_WINDOW( editor ), "TerminalPrefixEntry" );
-	gtk_entry_set_text( GTK_ENTRY( entry ), editor->private->terminal_prefix );
+	gtk_entry_set_text( GTK_ENTRY( entry ), editor->private->terminal_pattern );
 	gtk_widget_set_sensitive( entry, !editor->private->preferences_locked );
 	base_gtk_utils_set_editable( G_OBJECT( entry ), editable );
 
-	terminal_prefix_on_changed( GTK_ENTRY( entry ), editor );
+	terminal_pattern_on_changed( GTK_ENTRY( entry ), editor );
 
 	base_window_signal_connect( BASE_WINDOW( editor ),
-			G_OBJECT( entry ), "changed", G_CALLBACK( terminal_prefix_on_changed ));
+			G_OBJECT( entry ), "changed", G_CALLBACK( terminal_pattern_on_changed ));
 }
 
 static void
-terminal_prefix_on_changed( GtkEntry *entry, NactPreferencesEditor *editor )
+terminal_pattern_on_changed( GtkEntry *entry, NactPreferencesEditor *editor )
 {
 	gboolean editable;
 	gchar *example_label;
 	gchar *example_markup;
 	GtkWidget *example_widget;
 
-	editable = !editor->private->preferences_locked && !editor->private->terminal_prefix_mandatory;
+	editable = !editor->private->preferences_locked && !editor->private->terminal_pattern_mandatory;
 
 	if( editable ){
-		g_free( editor->private->terminal_prefix );
-		editor->private->terminal_prefix = g_strdup( gtk_entry_get_text( entry ));
+		g_free( editor->private->terminal_pattern );
+		editor->private->terminal_pattern = g_strdup( gtk_entry_get_text( entry ));
 
 		example_widget = base_window_get_widget( BASE_WINDOW( editor ), "TerminalPrefixExample" );
-		example_label = na_tokens_command_from_terminal_prefix( editor->private->terminal_prefix, "ls -l" );
+		example_label = na_tokens_command_for_terminal( editor->private->terminal_pattern, "ls -l" );
 
 		/* i18n: command-line example: Ex.: gnome-terminal -c "ls -l" */
 		example_markup = g_markup_printf_escaped(
@@ -1109,8 +1110,8 @@ on_dialog_ok( BaseDialog *dialog )
 
 		/* second tab: runtime execution
 		 */
-		if( !editor->private->terminal_prefix_mandatory ){
-			na_settings_set_string( NA_IPREFS_TERMINAL_PREFIX, editor->private->terminal_prefix );
+		if( !editor->private->terminal_pattern_mandatory ){
+			na_settings_set_string( NA_IPREFS_TERMINAL_PATTERN, editor->private->terminal_pattern );
 		}
 		if( !editor->private->desktop_mandatory ){
 			na_settings_set_string( NA_IPREFS_DESKTOP_ENVIRONMENT, editor->private->desktop );
diff --git a/src/nact/nact-preferences.ui b/src/nact/nact-preferences.ui
index 1d004a2..b059d07 100644
--- a/src/nact/nact-preferences.ui
+++ b/src/nact/nact-preferences.ui
@@ -210,8 +210,8 @@ Note that this item will be displayed only if a unique menu is defined in the Na
                             <child>
                               <object class="GtkTable" id="table711">
                                 <property name="visible">True</property>
-                                <property name="tooltip_text" translatable="yes">Specify here the command to add as a prefix of the action path and parameters when the action is to be executed in a terminal.
-This prefix should be so that the terminal is kept opened after the execution.</property>
+                                <property name="tooltip_text" translatable="yes">Specify here the command to run and keep opened a terminal.
+This command should include a 'COMMAND' keyword, which will be substituted at runtime with the action path and parameters.</property>
                                 <property name="n_rows">2</property>
                                 <property name="n_columns">2</property>
                                 <property name="column_spacing">4</property>
@@ -220,7 +220,7 @@ This prefix should be so that the terminal is kept opened after the execution.</
                                   <object class="GtkLabel" id="label711">
                                     <property name="visible">True</property>
                                     <property name="xalign">1</property>
-                                    <property name="label" translatable="yes">_Command prefix :</property>
+                                    <property name="label" translatable="yes">_Command pattern :</property>
                                     <property name="use_underline">True</property>
                                   </object>
                                   <packing>
@@ -1154,8 +1154,8 @@ You can add a new scheme by clicking on the '+' button.</property>
   </object>
   <object class="GtkSizeGroup" id="RuntimeExecutionLabel">
     <widgets>
-      <widget name="label711"/>
       <widget name="label721"/>
+      <widget name="label711"/>
     </widgets>
   </object>
 </interface>



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