[nautilus-actions] Define utilitary functions for selecting a file or a directory



commit c4d81dfda00e98db732544e457a0cc559c509a85
Author: Pierre Wieser <pwieser trychlos org>
Date:   Thu Jun 17 01:44:18 2010 +0200

    Define utilitary functions for selecting a file or a directory

 ChangeLog                        |    6 ++
 src/nact/nact-gtk-utils.c        |  152 ++++++++++++++++++++++++++++++++++++++
 src/nact/nact-gtk-utils.h        |   14 ++++
 src/nact/nact-icommand-tab.c     |  126 +++++--------------------------
 src/nact/nact-ienvironment-tab.c |   41 ++++++++++
 5 files changed, 232 insertions(+), 107 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 7fe953b..cf0a0be 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2010-06-16 Pierre Wieser <pwieser trychlos org>
 
+	* src/nact/nact-gtk-utils.c:
+	* src/nact/nact-gtk-utils.h
+	(nact_gtk_utils_select_file, nact_gtk_utils_select_dir): New functions.
+
+	* src/nact/nact-icommand-tab.c: Updated accordingly.
+
 	* src/api/na-core-utils.h:
 	* src/core/na-core-utils.c (na_core_utils_slist_setup_element): New function.
 
diff --git a/src/nact/nact-gtk-utils.c b/src/nact/nact-gtk-utils.c
index 36e9e55..de25929 100644
--- a/src/nact/nact-gtk-utils.c
+++ b/src/nact/nact-gtk-utils.c
@@ -35,7 +35,13 @@
 #include <glib.h>
 #include <string.h>
 
+#include <core/na-iprefs.h>
+#include <core/na-updater.h>
+
+#include "base-iprefs.h"
+#include "nact-iprefs.h"
 #include "nact-gtk-utils.h"
+#include "nact-application.h"
 
 /**
  * nact_gtk_utils_set_editable:
@@ -156,3 +162,149 @@ nact_gtk_utils_render( const gchar *name, GtkImage *widget, gint size )
 		g_object_unref( pixbuf );
 	}
 }
+
+/**
+ * nact_gtk_utils_select_file:
+ * @window: the #BaseWindow which will be the parent of the dialog box.
+ * @title: the title of the dialog box.
+ * @dialog_name: the name of the dialog box in Preferences to read/write
+ *  its size and position.
+ * @entry: the #GtkEntry which is associated with the selected file.
+ * @entry_name: the name of the entry in Preferences to be readen/written.
+ * @default_dir_uri: the URI of the directory which should be set in there is
+ *  not yet any preference (see @entry_name)
+ *
+ * Opens a #GtkFileChooserDialog and let the user choose an existing file
+ * -> choose and display an existing file name
+ * -> record the dirname URI.
+ *
+ * If the user validates its selection, the choosen file pathname will be
+ * written in the @entry #GtkEntry, while the corresponding dirname
+ * URI will be written as @entry_name in Preferences.
+ */
+void
+nact_gtk_utils_select_file( BaseWindow *window,
+				const gchar *title, const gchar *dialog_name,
+				GtkWidget *entry, const gchar *entry_name,
+				const gchar *default_dir_uri )
+{
+	NactApplication *application;
+	NAUpdater *updater;
+	GtkWindow *toplevel;
+	GtkWidget *dialog;
+	const gchar *text;
+	gchar *filename, *uri;
+
+	application = NACT_APPLICATION( base_window_get_application( window ));
+	updater = nact_application_get_updater( application );
+	toplevel = base_window_get_toplevel( window );
+
+	dialog = gtk_file_chooser_dialog_new(
+			title,
+			toplevel,
+			GTK_FILE_CHOOSER_ACTION_OPEN,
+			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+			GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+			NULL
+			);
+
+	base_iprefs_position_named_window( window, GTK_WINDOW( dialog ), dialog_name );
+
+	text = gtk_entry_get_text( GTK_ENTRY( entry ));
+
+	if( text && strlen( text )){
+		gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( dialog ), text );
+
+	} else {
+		uri = na_iprefs_read_string( NA_IPREFS( updater ), entry_name, default_dir_uri );
+		gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( dialog ), uri );
+		g_free( uri );
+	}
+
+	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( entry ), filename );
+	    g_free( filename );
+	  }
+
+	uri = gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( dialog ));
+	nact_iprefs_write_string( window, entry_name, uri );
+	g_free( uri );
+
+	base_iprefs_save_named_window_position( window, GTK_WINDOW( dialog ), dialog_name );
+
+	gtk_widget_destroy( dialog );
+}
+
+/**
+ * nact_gtk_utils_select_dir:
+ * @window: the #BaseWindow which will be the parent of the dialog box.
+ * @title: the title of the dialog box.
+ * @dialog_name: the name of the dialog box in Preferences to read/write
+ *  its size and position.
+ * @entry: the #GtkEntry which is associated with the field.
+ * @entry_name: the name of the entry in Preferences to be readen/written.
+ * @default_dir_uri: the URI of the directory which should be set in there is
+ *  not yet any preference (see @entry_name)
+ *
+ * Opens a #GtkFileChooserDialog and let the user choose an existing directory
+ * -> choose and display an existing dir name
+ * -> record the dirname URI of this dir name.
+ *
+ * If the user validates its selection, the choosen file pathname will be
+ * written in the @entry #GtkEntry, while the corresponding dirname
+ * URI will be written as @entry_name in Preferences.
+ */
+void
+nact_gtk_utils_select_dir( BaseWindow *window,
+				const gchar *title, const gchar *dialog_name,
+				GtkWidget *entry, const gchar *entry_name,
+				const gchar *default_dir_uri )
+{
+	NactApplication *application;
+	NAUpdater *updater;
+	GtkWindow *toplevel;
+	GtkWidget *dialog;
+	const gchar *text;
+	gchar *dir, *uri;
+
+	application = NACT_APPLICATION( base_window_get_application( window ));
+	updater = nact_application_get_updater( application );
+	toplevel = base_window_get_toplevel( window );
+
+	dialog = gtk_file_chooser_dialog_new(
+			title,
+			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( window, GTK_WINDOW( dialog ), dialog_name );
+
+	text = gtk_entry_get_text( GTK_ENTRY( entry ));
+
+	if( text && strlen( text )){
+		gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( dialog ), text );
+
+	} else {
+		uri = na_iprefs_read_string( NA_IPREFS( updater ), entry_name, default_dir_uri );
+		gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( dialog ), uri );
+		g_free( uri );
+	}
+
+	if( gtk_dialog_run( GTK_DIALOG( dialog )) == GTK_RESPONSE_ACCEPT ){
+		dir = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ));
+		gtk_entry_set_text( GTK_ENTRY( entry ), dir );
+	    g_free( dir );
+	  }
+
+	uri = gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( dialog ));
+	nact_iprefs_write_string( window, entry_name, uri );
+	g_free( uri );
+
+	base_iprefs_save_named_window_position( window, GTK_WINDOW( dialog ), dialog_name );
+
+	gtk_widget_destroy( dialog );
+}
diff --git a/src/nact/nact-gtk-utils.h b/src/nact/nact-gtk-utils.h
index 4e5fc77..2ba5c80 100644
--- a/src/nact/nact-gtk-utils.h
+++ b/src/nact/nact-gtk-utils.h
@@ -39,6 +39,8 @@
 
 #include <gtk/gtk.h>
 
+#include "base-window.h"
+
 G_BEGIN_DECLS
 
 void       nact_gtk_utils_set_editable( GtkObject *widget, gboolean editable );
@@ -48,6 +50,18 @@ void       nact_gtk_utils_set_editable( GtkObject *widget, gboolean editable );
 GdkPixbuf *nact_gtk_utils_get_pixbuf( const gchar *name, GtkWidget *widget, gint size );
 void       nact_gtk_utils_render( const gchar *name, GtkImage *widget, gint size );
 
+/* standard dialog boxes
+ */
+void       nact_gtk_utils_select_file( BaseWindow *window,
+				const gchar *title, const gchar *dialog_name,
+				GtkWidget *entry, const gchar *entry_name,
+				const gchar *default_dir_uri );
+
+void       nact_gtk_utils_select_dir( BaseWindow *window,
+				const gchar *title, const gchar *dialog_name,
+				GtkWidget *entry, const gchar *entry_name,
+				const gchar *default_dir_uri );
+
 G_END_DECLS
 
 #endif /* __NACT_GTK_UTILS_H__ */
diff --git a/src/nact/nact-icommand-tab.c b/src/nact/nact-icommand-tab.c
index c792756..1a66155 100644
--- a/src/nact/nact-icommand-tab.c
+++ b/src/nact/nact-icommand-tab.c
@@ -570,56 +570,10 @@ on_parameters_changed( GtkEntry *entry, NactICommandTab *instance )
 static void
 on_path_browse( GtkButton *button, NactICommandTab *instance )
 {
-	gboolean set_current_location = FALSE;
-	gchar *uri = NULL;
-	NactApplication *application;
-	NAUpdater *updater;
-	GtkWindow *toplevel;
-	GtkWidget *dialog;
-	GtkWidget *path_entry;
-	const gchar *path;
-	gchar *filename;
-
-	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 command" ),
-			toplevel,
-			GTK_FILE_CHOOSER_ACTION_OPEN,
-			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_COMMAND_CHOOSER );
-
-	path_entry = get_path_entry( instance );
-	path = gtk_entry_get_text( GTK_ENTRY( path_entry ));
-
-	if( path && strlen( path )){
-		set_current_location = gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( dialog ), path );
-
-	} else {
-		uri = na_iprefs_read_string( NA_IPREFS( updater ), IPREFS_FOLDER_URI, "file:///bin" );
-		gtk_file_chooser_set_current_folder_uri( GTK_FILE_CHOOSER( dialog ), uri );
-		g_free( uri );
-	}
-
-	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 );
-	  }
-
-	uri = gtk_file_chooser_get_current_folder_uri( GTK_FILE_CHOOSER( dialog ));
-	nact_iprefs_write_string( BASE_WINDOW( instance ), IPREFS_FOLDER_URI, uri );
-	g_free( uri );
-
-	base_iprefs_save_named_window_position( BASE_WINDOW( instance ), GTK_WINDOW( dialog ), IPREFS_COMMAND_CHOOSER );
-
-	gtk_widget_destroy( dialog );
+	nact_gtk_utils_select_file(
+			BASE_WINDOW( instance ),
+			_( "Choosing a command" ), IPREFS_COMMAND_CHOOSER,
+			get_path_entry( instance ), IPREFS_FOLDER_URI, "file:///bin" );
 }
 
 static void
@@ -633,78 +587,36 @@ on_path_changed( GtkEntry *entry, NactICommandTab *instance )
 				G_OBJECT( instance ),
 				TAB_UPDATABLE_PROP_SELECTED_PROFILE, &profile,
 				NULL );
+		g_return_if_fail( profile );
 
-		if( profile ){
-
-			na_object_set_path( profile, gtk_entry_get_text( entry ));
-			g_signal_emit_by_name( G_OBJECT( instance ), TAB_UPDATABLE_SIGNAL_ITEM_UPDATED, profile, FALSE );
-			update_example_label( instance, profile );
-		}
+		na_object_set_path( profile, gtk_entry_get_text( entry ));
+		g_signal_emit_by_name( G_OBJECT( instance ), TAB_UPDATABLE_SIGNAL_ITEM_UPDATED, profile, FALSE );
+		update_example_label( instance, profile );
 	}
 }
 
 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;
+	NAObjectProfile *profile;
 	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_SELECTED_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 );
-	  }
+	g_object_get(
+			G_OBJECT( instance ),
+			TAB_UPDATABLE_PROP_SELECTED_PROFILE, &profile,
+			NULL );
 
-	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 );
+	default_value = na_factory_object_get_default( NA_IFACTORY_OBJECT( profile ), NAFO_DATA_WORKING_DIR );
 
-	base_iprefs_save_named_window_position( BASE_WINDOW( instance ), GTK_WINDOW( dialog ), IPREFS_WORKING_DIR_DIALOG );
+	nact_gtk_utils_select_dir(
+			BASE_WINDOW( instance ),
+			_( "Choosing a working directory" ), IPREFS_WORKING_DIR_DIALOG,
+			wdir_entry, IPREFS_WORKING_DIR_URI, default_value );
 
-	gtk_widget_destroy( dialog );
+	g_free( default_value );
 }
 
 static void
diff --git a/src/nact/nact-ienvironment-tab.c b/src/nact/nact-ienvironment-tab.c
index e945bc4..0e9ae68 100644
--- a/src/nact/nact-ienvironment-tab.c
+++ b/src/nact/nact-ienvironment-tab.c
@@ -405,6 +405,7 @@ on_tab_updatable_selection_changed( NactIEnvironmentTab *instance, gint count_se
 	GtkTreeSelection *selection;
 	GtkWidget *always_button, *show_button, *notshow_button;
 	GSList *desktops;
+	gchar *text;
 
 	g_return_if_fail( NACT_IS_IENVIRONMENT_TAB( instance ));
 
@@ -421,6 +422,8 @@ on_tab_updatable_selection_changed( NactIEnvironmentTab *instance, gint count_se
 
 			st_on_selection_change = TRUE;
 
+			/* selection count
+			 */
 			sel_count = na_object_get_selection_count( context );
 			na_core_utils_selcount_get_ope_int( sel_count, &selcount_ope, &selcount_int );
 			set_selection_count_selection( instance, selcount_ope, selcount_int );
@@ -434,6 +437,8 @@ on_tab_updatable_selection_changed( NactIEnvironmentTab *instance, gint count_se
 			entry = base_window_get_widget( BASE_WINDOW( instance ), "SelectionCountNumberEntry" );
 			nact_gtk_utils_set_editable( GTK_OBJECT( entry ), editable );
 
+			/* desktop environment
+			 */
 			raz_desktop_listview( instance );
 
 			always_button = base_window_get_widget( BASE_WINDOW( instance ), "ShowAlwaysButton" );
@@ -468,6 +473,36 @@ on_tab_updatable_selection_changed( NactIEnvironmentTab *instance, gint count_se
 
 			setup_desktop_listview( instance, desktops );
 
+			/* execution environment
+			 */
+			entry = base_window_get_widget( BASE_WINDOW( instance ), "TryExecEntry" );
+			text = na_object_get_try_exec( context );
+			text = text && strlen( text ) ? text : g_strdup( "" );
+			gtk_entry_set_text( GTK_ENTRY( entry ), text );
+			g_free( text );
+			nact_gtk_utils_set_editable( GTK_OBJECT( entry ), editable );
+
+			entry = base_window_get_widget( BASE_WINDOW( instance ), "ShowIfRegisteredEntry" );
+			text = na_object_get_show_if_registered( context );
+			text = text && strlen( text ) ? text : g_strdup( "" );
+			gtk_entry_set_text( GTK_ENTRY( entry ), text );
+			g_free( text );
+			nact_gtk_utils_set_editable( GTK_OBJECT( entry ), editable );
+
+			entry = base_window_get_widget( BASE_WINDOW( instance ), "ShowIfTrueEntry" );
+			text = na_object_get_show_if_true( context );
+			text = text && strlen( text ) ? text : g_strdup( "" );
+			gtk_entry_set_text( GTK_ENTRY( entry ), text );
+			g_free( text );
+			nact_gtk_utils_set_editable( GTK_OBJECT( entry ), editable );
+
+			entry = base_window_get_widget( BASE_WINDOW( instance ), "ShowIfRunningEntry" );
+			text = na_object_get_show_if_running( context );
+			text = text && strlen( text ) ? text : g_strdup( "" );
+			gtk_entry_set_text( GTK_ENTRY( entry ), text );
+			g_free( text );
+			nact_gtk_utils_set_editable( GTK_OBJECT( entry ), editable );
+
 			st_on_selection_change = FALSE;
 
 			path = gtk_tree_path_new_first();
@@ -682,6 +717,12 @@ on_desktop_toggled( GtkCellRendererToggle *renderer, gchar *path, BaseWindow *wi
 static void
 on_try_exec_changed( GtkEntry *entry, NactIEnvironmentTab *instance )
 {
+	NAIContext *context;
+	const gchar *text;
+
+	context = nact_main_tab_get_context( NACT_MAIN_WINDOW( instance ), NULL );
+	text = gtk_entry_get_text( entry );
+	na_object_set_try_exec( context, text );
 }
 
 static void



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