[nautilus-actions] Implement working directory



commit 6637dc16d85b148487e4ef19439ae83da4e69c7e
Author: Pierre Wieser <pwieser trychlos org>
Date:   Fri Jun 4 18:43:16 2010 +0200

    Implement working directory

 ChangeLog                            |   10 +++
 src/api/na-ifactory-object-data.h    |    1 +
 src/api/na-object-api.h              |    2 +
 src/core/na-factory-object.c         |   32 +++++++++
 src/core/na-factory-object.h         |    1 +
 src/core/na-object-profile-factory.c |   23 +++++++
 src/nact/nact-icommand-tab.c         |  120 ++++++++++++++++++++++++++++++++--
 7 files changed, 184 insertions(+), 5 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3ab16a7..e34175b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,16 @@
 
 2010-06-04 Pierre Wieser <pwieser trychlos org>
 
+	* src/api/na-object-api.h:
+	* src/api/na-ifactory-object-data.h:
+	* src/core/na-object-profile-factory.c: Add NAFO_DATA_WORKING_DIR data.
+
+	* src/core/na-factory-object.c:
+	* src/core/na-factory-object.h (na_factory_object_get_default):
+	New	function.
+
+	* src/nact/nact-icommand-tab.c: Implement working directory.
+
 	* src/nact/nact-imimetypes-tab.c: 
 	Take advantage of new nact_match_list_xxx functions.
 
diff --git a/src/api/na-ifactory-object-data.h b/src/api/na-ifactory-object-data.h
index 3dc6875..0ee6e53 100644
--- a/src/api/na-ifactory-object-data.h
+++ b/src/api/na-ifactory-object-data.h
@@ -78,6 +78,7 @@ G_BEGIN_DECLS
 #define NAFO_DATA_DESCNAME					"na-factory-data-descname"
 #define NAFO_DATA_PATH						"na-factory-data-path"
 #define NAFO_DATA_PARAMETERS				"na-factory-data-parameters"
+#define NAFO_DATA_WORKING_DIR				"na-factory-data-working-dir"
 #define NAFO_DATA_EXECUTION_MODE			"na-factory-data-execution-mode"
 #define NAFO_DATA_STARTUP_NOTIFY			"na-factory-data-startup-notify"
 #define NAFO_DATA_STARTUP_WMCLASS			"na-factory-data-startup-wm-class"
diff --git a/src/api/na-object-api.h b/src/api/na-object-api.h
index 7d95961..3747dfb 100644
--- a/src/api/na-object-api.h
+++ b/src/api/na-object-api.h
@@ -156,12 +156,14 @@ G_BEGIN_DECLS
  */
 #define na_object_get_path( obj )						(( gchar * ) na_ifactory_object_get_as_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_PATH ))
 #define na_object_get_parameters( obj )					(( gchar * ) na_ifactory_object_get_as_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_PARAMETERS ))
+#define na_object_get_working_dir( obj )				(( gchar * ) na_ifactory_object_get_as_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_WORKING_DIR ))
 #define na_object_is_file( obj )						(( gboolean ) GPOINTER_TO_UINT( na_ifactory_object_get_as_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_ISFILE )))
 #define na_object_is_dir( obj )							(( gboolean ) GPOINTER_TO_UINT( na_ifactory_object_get_as_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_ISDIR )))
 #define na_object_is_multiple( obj )					(( gboolean ) GPOINTER_TO_UINT( na_ifactory_object_get_as_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_MULTIPLE )))
 
 #define na_object_set_path( obj, path )					na_ifactory_object_set_from_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_PATH, ( const void * )( path ))
 #define na_object_set_parameters( obj, parms )			na_ifactory_object_set_from_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_PARAMETERS, ( const void * )( parms ))
+#define na_object_set_working_dir( obj, uri )			na_ifactory_object_set_from_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_WORKING_DIR, ( const void * )( uri ))
 #define na_object_set_isfile( obj, isfile )				na_ifactory_object_set_from_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_ISFILE, ( const void * ) GUINT_TO_POINTER( isfile ))
 #define na_object_set_isdir( obj, isdir )				na_ifactory_object_set_from_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_ISDIR, ( const void * ) GUINT_TO_POINTER( isdir ))
 #define na_object_set_multiple( obj, multiple )			na_ifactory_object_set_from_void( NA_IFACTORY_OBJECT( obj ), NAFO_DATA_MULTIPLE, ( const void * ) GUINT_TO_POINTER( multiple ))
diff --git a/src/core/na-factory-object.c b/src/core/na-factory-object.c
index adfd517..2540b6d 100644
--- a/src/core/na-factory-object.c
+++ b/src/core/na-factory-object.c
@@ -251,6 +251,38 @@ na_factory_object_iter_on_boxed( const NAIFactoryObject *object, NAFactoryObject
 }
 
 /**
+ * na_factory_object_get_default:
+ * @object: this #NAIFactoryObject object.
+ * @name: the searched name.
+ *
+ * Returns: the default value for this @object, as a newly allocated
+ * string which should be g_free() by the caller.
+ */
+gchar *
+na_factory_object_get_default( NAIFactoryObject *object, const gchar *name )
+{
+	static const gchar *thisfn = "na_factory_object_set_defaults";
+	gchar *value;
+	NADataDef *def;
+
+	g_return_val_if_fail( NA_IS_IFACTORY_OBJECT( object ), NULL );
+
+	value = NULL;
+
+	if( ifactory_object_initialized && !ifactory_object_finalized ){
+
+		g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
+
+		def = na_factory_object_get_data_def( object, name );
+		if( def ){
+			value = g_strdup( def->default_value );
+		}
+	}
+
+	return( value );
+}
+
+/**
  * na_factory_object_set_defaults:
  * @object: this #NAIFactoryObject object.
  *
diff --git a/src/core/na-factory-object.h b/src/core/na-factory-object.h
index 119cd59..1fc804f 100644
--- a/src/core/na-factory-object.h
+++ b/src/core/na-factory-object.h
@@ -53,6 +53,7 @@ NADataDef   *na_factory_object_get_data_def     ( const NAIFactoryObject *object
 NADataGroup *na_factory_object_get_data_groups  ( const NAIFactoryObject *object );
 void         na_factory_object_iter_on_boxed    ( const NAIFactoryObject *object, NAFactoryObjectIterBoxedFn pfn, void *data );
 
+gchar       *na_factory_object_get_default      ( NAIFactoryObject *object, const gchar *name );
 void         na_factory_object_set_defaults     ( NAIFactoryObject *object );
 
 void         na_factory_object_move_boxed       ( NAIFactoryObject *target, const NAIFactoryObject *source, NADataBoxed *boxed );
diff --git a/src/core/na-object-profile-factory.c b/src/core/na-object-profile-factory.c
index 2e9de1e..0d5e30b 100644
--- a/src/core/na-object-profile-factory.c
+++ b/src/core/na-object-profile-factory.c
@@ -154,6 +154,29 @@ static NADataDef data_def_profile [] = {
 				NULL,
 				N_( "<STRING>" ) },
 
+	{ NAFO_DATA_WORKING_DIR,
+				TRUE,
+				TRUE,
+				TRUE,
+				N_( "Working directory" ),
+				N_( "The working directory the command will be started in.\n" \
+					"Defaults to \"%d\"." ),
+				NAFD_TYPE_STRING,
+				"%d",
+				FALSE,
+				TRUE,
+				TRUE,
+				FALSE,
+				FALSE,
+				"working-dir",
+				"Path",
+				0,
+				NULL,
+				0,
+				0,
+				NULL,
+				NULL },
+
 	{ NAFO_DATA_EXECUTION_MODE,
 				TRUE,
 				TRUE,
diff --git a/src/nact/nact-icommand-tab.c b/src/nact/nact-icommand-tab.c
index ab39ed4..fb5a60d 100644
--- a/src/nact/nact-icommand-tab.c
+++ b/src/nact/nact-icommand-tab.c
@@ -39,6 +39,7 @@
 #include <api/na-object-api.h>
 
 #include <core/na-iprefs.h>
+#include <core/na-factory-object.h>
 
 #include "base-window.h"
 #include "base-iprefs.h"
@@ -62,6 +63,8 @@ struct NactICommandTabInterfacePrivate {
 #define IPREFS_LEGEND_DIALOG				"icommand-legend-dialog"
 #define IPREFS_COMMAND_CHOOSER				"icommand-command-chooser"
 #define IPREFS_FOLDER_URI					"icommand-folder-uri"
+#define IPREFS_WORKING_DIR_DIALOG			"icommand-working-dir-dialog"
+#define IPREFS_WORKING_DIR_URI				"icommand-working-dir-uri"
 
 /* a data set in the LegendDialog GObject
  */
@@ -94,6 +97,8 @@ static void       on_legend_clicked( GtkButton *button, NactICommandTab *instanc
 static void       on_parameters_changed( GtkEntry *entry, NactICommandTab *instance );
 static void       on_path_browse( GtkButton *button, NactICommandTab *instance );
 static void       on_path_changed( GtkEntry *entry, NactICommandTab *instance );
+static void       on_wdir_browse( GtkButton *button, NactICommandTab *instance );
+static void       on_wdir_changed( GtkEntry *entry, NactICommandTab *instance );
 static gchar     *parse_parameters( NactICommandTab *instance );
 static void       set_label_label( NactICommandTab *instance, const gchar *color );
 static void       update_example_label( NactICommandTab *instance, NAObjectProfile *profile );
@@ -204,8 +209,8 @@ void
 nact_icommand_tab_runtime_init_toplevel( NactICommandTab *instance )
 {
 	static const gchar *thisfn = "nact_icommand_tab_runtime_init_toplevel";
-	GtkWidget *label_entry, *path_entry, *parameters_entry;
-	GtkButton *path_button, *legend_button;
+	GtkWidget *label_entry, *path_entry, *parameters_entry, *wdir_entry;
+	GtkButton *path_button, *legend_button, *wdir_button;
 
 	g_debug( "%s: instance=%p", thisfn, ( void * ) instance );
 	g_return_if_fail( NACT_IS_ICOMMAND_TAB( instance ));
@@ -247,6 +252,20 @@ nact_icommand_tab_runtime_init_toplevel( NactICommandTab *instance )
 				"clicked",
 				G_CALLBACK( on_legend_clicked ));
 
+		wdir_entry = base_window_get_widget( BASE_WINDOW( instance ), "WorkingDirectoryEntry" );
+		base_window_signal_connect(
+				BASE_WINDOW( instance ),
+				G_OBJECT( wdir_entry ),
+				"changed",
+				G_CALLBACK( on_wdir_changed ));
+
+		wdir_button = GTK_BUTTON( base_window_get_widget( BASE_WINDOW( instance ), "CommandWorkingDirectoryButton" ));
+		base_window_signal_connect(
+				BASE_WINDOW( instance ),
+				G_OBJECT( wdir_button ),
+				"clicked",
+				G_CALLBACK( on_wdir_browse ));
+
 		base_window_signal_connect(
 				BASE_WINDOW( instance ),
 				G_OBJECT( instance ),
@@ -315,8 +334,8 @@ on_tab_updatable_selection_changed( NactICommandTab *instance, gint count_select
 	NAObjectItem *item;
 	NAObjectProfile *profile;
 	gboolean enable_tab;
-	GtkWidget *label_entry, *path_entry, *parameters_entry;
-	gchar *label, *path, *parameters;
+	GtkWidget *label_entry, *path_entry, *parameters_entry, *wdir_entry;
+	gchar *label, *path, *parameters, *wdir;
 	gboolean editable;
 	GtkButton *path_button;
 	GtkButton *legend_button;
@@ -369,6 +388,14 @@ on_tab_updatable_selection_changed( NactICommandTab *instance, gint count_select
 		legend_button = get_legend_button( instance );
 		gtk_widget_set_sensitive( GTK_WIDGET( legend_button ), profile != NULL );
 
+		wdir_entry = base_window_get_widget( BASE_WINDOW( instance ), "WorkingDirectoryEntry" );
+		wdir = profile ? na_object_get_working_dir( profile ) : g_strdup( "" );
+		wdir = wdir ? wdir : g_strdup( "" );
+		gtk_entry_set_text( GTK_ENTRY( wdir_entry ), wdir );
+		g_free( wdir );
+		gtk_widget_set_sensitive( wdir_entry, profile != NULL );
+		nact_gtk_utils_set_editable( GTK_OBJECT( wdir_entry ), editable );
+
 		st_on_selection_change = FALSE;
 	}
 }
@@ -594,7 +621,7 @@ on_path_browse( GtkButton *button, NactICommandTab *instance )
 	if( gtk_dialog_run( GTK_DIALOG( dialog )) == GTK_RESPONSE_ACCEPT ){
 		filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ));
 		gtk_entry_set_text( GTK_ENTRY( path_entry ), filename );
-	    g_free (filename);
+	    g_free( filename );
 	  }
 
 	uri = gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( dialog ));
@@ -627,6 +654,89 @@ on_path_changed( GtkEntry *entry, NactICommandTab *instance )
 	}
 }
 
+static void
+on_wdir_browse( GtkButton *button, NactICommandTab *instance )
+{
+	gboolean set_current_location = FALSE;
+	gchar *uri = NULL;
+	NactApplication *application;
+	NAObjectProfile *profile;
+	NAUpdater *updater;
+	GtkWindow *toplevel;
+	GtkWidget *dialog;
+	GtkWidget *wdir_entry;
+	const gchar *wdir;
+	gchar *wdir_uri;
+	gchar *default_value;
+
+	application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( instance )));
+	updater = nact_application_get_updater( application );
+	toplevel = base_window_get_toplevel( BASE_WINDOW( instance ));
+
+	dialog = gtk_file_chooser_dialog_new(
+			_( "Choosing a working directory" ),
+			toplevel,
+			GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
+			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+			GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+			NULL
+			);
+
+	base_iprefs_position_named_window( BASE_WINDOW( instance ), GTK_WINDOW( dialog ), IPREFS_WORKING_DIR_DIALOG );
+
+	wdir_entry = base_window_get_widget( BASE_WINDOW( instance ), "WorkingDirectoryEntry" );
+	wdir = gtk_entry_get_text( GTK_ENTRY( wdir_entry ));
+
+	if( wdir && strlen( wdir )){
+		set_current_location = gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( dialog ), wdir );
+
+	} else {
+		g_object_get(
+				G_OBJECT( instance ),
+				TAB_UPDATABLE_PROP_EDITED_PROFILE, &profile,
+				NULL );
+
+		default_value = na_factory_object_get_default( NA_IFACTORY_OBJECT( profile ), NAFO_DATA_WORKING_DIR );
+		uri = na_iprefs_read_string( NA_IPREFS( updater ), IPREFS_WORKING_DIR_URI, default_value );
+		gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( dialog ), uri );
+		g_free( uri );
+		g_free( default_value );
+	}
+
+	if( gtk_dialog_run( GTK_DIALOG( dialog )) == GTK_RESPONSE_ACCEPT ){
+		wdir_uri = gtk_file_chooser_get_uri( GTK_FILE_CHOOSER( dialog ));
+		gtk_entry_set_text( GTK_ENTRY( wdir_entry ), wdir_uri );
+	    g_free( wdir_uri );
+	  }
+
+	uri = gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( dialog ));
+	nact_iprefs_write_string( BASE_WINDOW( instance ), IPREFS_WORKING_DIR_URI, uri );
+	g_free( uri );
+
+	base_iprefs_save_named_window_position( BASE_WINDOW( instance ), GTK_WINDOW( dialog ), IPREFS_WORKING_DIR_DIALOG );
+
+	gtk_widget_destroy( dialog );
+}
+
+static void
+on_wdir_changed( GtkEntry *entry, NactICommandTab *instance )
+{
+	NAObjectProfile *edited;
+
+	if( !st_on_selection_change ){
+
+		g_object_get(
+				G_OBJECT( instance ),
+				TAB_UPDATABLE_PROP_EDITED_PROFILE, &edited,
+				NULL );
+
+		if( edited ){
+			na_object_set_working_dir( edited, gtk_entry_get_text( entry ));
+			g_signal_emit_by_name( G_OBJECT( instance ), TAB_UPDATABLE_SIGNAL_ITEM_UPDATED, edited, FALSE );
+		}
+	}
+}
+
 /*
  * Valid parameters :
  *



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