[nautilus-actions: 9/45] Use GtkTreeModelFilter to display actions and profiles



commit 27fd859e4c984a1651e7f6572f5b3145d5f110e7
Author: Pierre Wieser <pwieser trychlos org>
Date:   Sat Jul 18 22:13:20 2009 +0200

    Use GtkTreeModelFilter to display actions and profiles

 ChangeLog                     |    1 +
 src/nact/nact-iactions-list.c |   82 +++++++++++++++++++++++++++++++---------
 src/nact/nact-iactions-list.h |    2 +-
 src/nact/nact-icommand-tab.c  |    6 +-
 src/nact/nact-main-window.c   |   80 +++++++++++++++++++++++----------------
 5 files changed, 115 insertions(+), 56 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d28be1b..813b649 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,7 @@
 	* src/nact/nact-iactions-list.h (set_sorted_actions):
 	New virtual function: advertise the implementer of the interface
 	of the new address of the sorted list.
+	Use GtkTreeModelFilter to display actions and profiles.
 
 	* src/nact/nact-icommand-tab.c:
 	* src/nact/nact-icommand-tab.h:
diff --git a/src/nact/nact-iactions-list.c b/src/nact/nact-iactions-list.c
index de7ffd0..cdfa9ed 100644
--- a/src/nact/nact-iactions-list.c
+++ b/src/nact/nact-iactions-list.c
@@ -49,7 +49,7 @@ struct NactIActionsListInterfacePrivate {
 enum {
 	IACTIONS_LIST_ICON_COLUMN = 0,
 	IACTIONS_LIST_LABEL_COLUMN,
-	IACTIONS_LIST_ACTION_COLUMN,
+	IACTIONS_LIST_NAOBJECT_COLUMN,
 	IACTIONS_LIST_N_COLUMN
 };
 
@@ -69,6 +69,7 @@ static void       v_on_selection_changed( GtkTreeSelection *selection, gpointer
 static gboolean   v_on_button_press_event( GtkWidget *widget, GdkEventButton *event, gpointer data );
 static gboolean   v_on_key_pressed_event( GtkWidget *widget, GdkEventKey *event, gpointer data );
 
+static gboolean   filter_visible( GtkTreeModel *model, GtkTreeIter *iter, gpointer data );
 static GtkWidget *get_actions_list_widget( NactWindow *window );
 static gint       sort_actions_by_label( gconstpointer a1, gconstpointer a2 );
 
@@ -154,18 +155,24 @@ nact_iactions_list_initial_load( NactWindow *window )
 	nact_iactions_list_set_send_selection_changed_on_fill_list( window, FALSE );
 	nact_iactions_list_set_is_filling_list( window, FALSE );
 
-	GtkListStore *model;
-	GtkTreeViewColumn *column;
 	GtkWidget *widget = get_actions_list_widget( window );
 
 	/* create the model */
-	model = gtk_list_store_new(
-			IACTIONS_LIST_N_COLUMN, GDK_TYPE_PIXBUF, G_TYPE_STRING, NA_ACTION_TYPE );
-	gtk_tree_view_set_model( GTK_TREE_VIEW( widget ), GTK_TREE_MODEL( model ));
-	g_object_unref( model );
+	GtkTreeStore *ts_model = gtk_tree_store_new(
+			IACTIONS_LIST_N_COLUMN, GDK_TYPE_PIXBUF, G_TYPE_STRING, NA_OBJECT_TYPE );
+
+	GtkTreeModel *tmf_model = gtk_tree_model_filter_new( GTK_TREE_MODEL( ts_model ), NULL );
+
+	gtk_tree_model_filter_set_visible_func(
+			GTK_TREE_MODEL_FILTER( tmf_model ), ( GtkTreeModelFilterVisibleFunc ) filter_visible, window, NULL );
+
+	gtk_tree_view_set_model( GTK_TREE_VIEW( widget ), tmf_model );
+
+	/*g_object_unref( tmf_model );*/
+	g_object_unref( ts_model );
 
 	/* create visible columns on the tree view */
-	column = gtk_tree_view_column_new_with_attributes(
+	GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
 			"icon", gtk_cell_renderer_pixbuf_new(), "pixbuf", IACTIONS_LIST_ICON_COLUMN, NULL );
 	gtk_tree_view_append_column( GTK_TREE_VIEW( widget ), column );
 
@@ -231,8 +238,9 @@ nact_iactions_list_fill( NactWindow *window )
 	nact_iactions_list_set_is_filling_list( window, TRUE );
 
 	GtkWidget *widget = get_actions_list_widget( window );
-	GtkListStore *model = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( widget )));
-	gtk_list_store_clear( model );
+	GtkTreeModelFilter *tmf_model = GTK_TREE_MODEL_FILTER( gtk_tree_view_get_model( GTK_TREE_VIEW( widget )));
+	GtkTreeStore *ts_model = GTK_TREE_STORE( gtk_tree_model_filter_get_model( tmf_model ));
+	gtk_tree_store_clear( ts_model );
 
 	GSList *actions = v_get_actions( window );
 	actions = g_slist_sort( actions, ( GCompareFunc ) sort_actions_by_label );
@@ -274,14 +282,28 @@ nact_iactions_list_fill( NactWindow *window )
 				}
 			}
 		}
-		gtk_list_store_append( model, &iter );
-		gtk_list_store_set( model, &iter,
+		gtk_tree_store_append( ts_model, &iter, NULL );
+		gtk_tree_store_set( ts_model, &iter,
 				    IACTIONS_LIST_ICON_COLUMN, icon,
 				    IACTIONS_LIST_LABEL_COLUMN, label,
-				    IACTIONS_LIST_ACTION_COLUMN, action,
+				    IACTIONS_LIST_NAOBJECT_COLUMN, action,
 				    -1);
 		g_debug( "%s: action=%p", thisfn, action );
 
+		GSList *profiles = na_action_get_profiles( action );
+		GSList *ip;
+		GtkTreeIter profile_iter;
+		for( ip = profiles ; ip ; ip = ip->next ){
+			NAActionProfile *profile = NA_ACTION_PROFILE( ip->data );
+			gchar *profile_label = na_action_profile_get_label( profile );
+			gtk_tree_store_append( ts_model, &profile_iter, &iter );
+			gtk_tree_store_set( ts_model, &profile_iter,
+					    IACTIONS_LIST_LABEL_COLUMN, profile_label,
+					    IACTIONS_LIST_NAOBJECT_COLUMN, profile,
+					    -1);
+			g_free( profile_label );
+		}
+
 		g_free( iconname );
 		g_free( label );
 	}
@@ -318,7 +340,7 @@ nact_iactions_list_set_selection( NactWindow *window, const gchar *uuid, const g
 		gtk_tree_model_get(
 				model,
 				&iter,
-				IACTIONS_LIST_ACTION_COLUMN, &action, IACTIONS_LIST_LABEL_COLUMN, &iter_label,
+				IACTIONS_LIST_NAOBJECT_COLUMN, &action, IACTIONS_LIST_LABEL_COLUMN, &iter_label,
 				-1 );
 
 		iter_uuid = na_action_get_uuid( action );
@@ -353,18 +375,21 @@ nact_iactions_list_set_focus( NactWindow *window )
 }
 
 /**
- * Returns the currently selected action.
+ * Returns the currently selected action or profile.
  */
-NAAction *
+NAObject *
 nact_iactions_list_get_selected_action( NactWindow *window )
 {
 	GSList *list = nact_iactions_list_get_selected_actions( window );
 
-	NAAction *action = NA_ACTION( list->data );
+	NAObject *object = NA_OBJECT( list->data );
+
+	g_assert( object );
+	g_assert( NA_IS_OBJECT( object ));
 
 	g_slist_free( list );
 
-	return( action );
+	return( object );
 }
 
 /**
@@ -391,7 +416,7 @@ nact_iactions_list_get_selected_actions( NactWindow *window )
 		GtkTreePath *path = ( GtkTreePath * ) it->data;
 		gtk_tree_model_get_iter( model, &iter, path );
 
-		gtk_tree_model_get( model, &iter, IACTIONS_LIST_ACTION_COLUMN, &action, -1 );
+		gtk_tree_model_get( model, &iter, IACTIONS_LIST_NAOBJECT_COLUMN, &action, -1 );
 		actions = g_slist_prepend( actions, action );
 	}
 
@@ -538,6 +563,25 @@ v_on_key_pressed_event( GtkWidget *widget, GdkEventKey *event, gpointer user_dat
 	return( stop );
 }
 
+static gboolean
+filter_visible( GtkTreeModel *model, GtkTreeIter *iter, gpointer data )
+{
+	NAObject *object;
+	gtk_tree_model_get( model, iter, IACTIONS_LIST_NAOBJECT_COLUMN, &object, -1 );
+
+	if( object ){
+		if( NA_IS_ACTION( object )){
+			return( TRUE );
+		}
+
+		g_assert( NA_IS_ACTION_PROFILE( object ));
+		NAAction *action = NA_ACTION( na_action_profile_get_action( NA_ACTION_PROFILE( object )));
+		return( na_action_get_profiles_count( action ) > 1 );
+	}
+
+	return( FALSE );
+}
+
 static GtkWidget *
 get_actions_list_widget( NactWindow *window )
 {
diff --git a/src/nact/nact-iactions-list.h b/src/nact/nact-iactions-list.h
index 35925e6..2a988fe 100644
--- a/src/nact/nact-iactions-list.h
+++ b/src/nact/nact-iactions-list.h
@@ -74,7 +74,7 @@ GType     nact_iactions_list_get_type( void );
 void      nact_iactions_list_initial_load( NactWindow *window );
 void      nact_iactions_list_runtime_init( NactWindow *window );
 void      nact_iactions_list_fill( NactWindow *window );
-NAAction *nact_iactions_list_get_selected_action( NactWindow *window );
+NAObject *nact_iactions_list_get_selected_action( NactWindow *window );
 GSList  * nact_iactions_list_get_selected_actions( NactWindow *window );
 void      nact_iactions_list_set_selection( NactWindow *window, const gchar *uuid, const gchar *label );
 void      nact_iactions_list_set_focus( NactWindow *window );
diff --git a/src/nact/nact-icommand-tab.c b/src/nact/nact-icommand-tab.c
index 9845840..33ea5fb 100644
--- a/src/nact/nact-icommand-tab.c
+++ b/src/nact/nact-icommand-tab.c
@@ -224,17 +224,17 @@ nact_icommand_tab_set_profile( NactWindow *dialog, const NAActionProfile *profil
 	g_debug( "%s: dialog=%p, profile=%p", thisfn, dialog, profile );
 
 	GtkWidget *label_widget = base_window_get_widget( BASE_WINDOW( dialog ), "CommandProfileLabelEntry" );
-	gchar *label = na_action_profile_get_label( profile );
+	gchar *label = profile ? na_action_profile_get_label( profile ) : g_strdup( "" );
 	gtk_entry_set_text( GTK_ENTRY( label_widget ), label );
 	g_free( label );
 
 	GtkWidget *path_widget = get_path_widget( dialog );
-	gchar *path = na_action_profile_get_path( profile );
+	gchar *path = profile ? na_action_profile_get_path( profile ) : g_strdup( "" );
 	gtk_entry_set_text( GTK_ENTRY( path_widget ), path );
 	g_free( path );
 
 	GtkWidget *parameters_widget = get_parameters_widget( dialog );
-	gchar *parameters = na_action_profile_get_parameters( profile );
+	gchar *parameters = profile ? na_action_profile_get_parameters( profile ) : g_strdup( "" );
 	gtk_entry_set_text( GTK_ENTRY( parameters_widget ), parameters );
 	g_free( parameters );
 }
diff --git a/src/nact/nact-main-window.c b/src/nact/nact-main-window.c
index d138c3a..cb99885 100644
--- a/src/nact/nact-main-window.c
+++ b/src/nact/nact-main-window.c
@@ -60,14 +60,13 @@ struct NactMainWindowClassPrivate {
 /* private instance data
  */
 struct NactMainWindowPrivate {
-	gboolean      dispose_has_run;
-	GtkStatusbar *status_bar;
-	guint         status_context;
-	GtkWidget    *save_item;
-	GSList       *actions;
-	NAAction     *edited;
-	/*gchar        *current_uuid;
-	gchar        *current_label;*/
+	gboolean         dispose_has_run;
+	GtkStatusbar    *status_bar;
+	guint            status_context;
+	GtkWidget       *save_item;
+	GSList          *actions;
+	NAAction        *edited_action;
+	NAActionProfile *edited_profile;
 };
 
 /* the GConf key used to read/write size and position of auxiliary dialogs
@@ -103,13 +102,14 @@ static void             on_actions_list_selection_changed( GtkTreeSelection *sel
 static gboolean         on_actions_list_double_click( GtkWidget *widget, GdkEventButton *event, gpointer data );
 static gboolean         on_actions_list_enter_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data );
 static void             set_current_action( NactMainWindow *window );
+static void             set_current_profile( NactMainWindow *window );
 static NAAction        *get_edited_action( NactWindow *window );
+static NAActionProfile *get_edited_profile( NactWindow *window );
 static void             on_modified_field( NactWindow *window );
 static void             check_edited_status( NactWindow *window, const NAAction *action );
 static gboolean         is_action_modified( const NAAction *action );
 static gboolean         is_action_to_save( const NAAction *action );
 
-static NAActionProfile *get_edited_profile( NactWindow *window );
 static void             get_isfiledir( NactWindow *window, gboolean *isfile, gboolean *isdir );
 static gboolean         get_multiple( NactWindow *window );
 static GSList          *get_schemes( NactWindow *window );
@@ -539,8 +539,8 @@ setup_dialog_title( NactMainWindow *window )
 	BaseApplication *appli = BASE_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
 	gchar *title = base_application_get_name( appli );
 
-	if( window->private->edited ){
-		gchar *label = na_action_get_label( window->private->edited );
+	if( window->private->edited_action ){
+		gchar *label = na_action_get_label( window->private->edited_action );
 		gchar *tmp = g_strdup_printf( "%s - %s", title, label );
 		g_free( label );
 		g_free( title );
@@ -572,14 +572,26 @@ setup_dialog_menu( NactMainWindow *window )
 	gtk_widget_set_sensitive( NACT_MAIN_WINDOW( window )->private->save_item, to_save );
 }
 
+/*
+ * note that the IActionsList tree store may return an action or a profile
+ */
 static void
 on_actions_list_selection_changed( GtkTreeSelection *selection, gpointer user_data )
 {
 	g_assert( NACT_IS_MAIN_WINDOW( user_data ));
 	NactMainWindow *window = NACT_MAIN_WINDOW( user_data );
 
-	window->private->edited = NA_ACTION( nact_iactions_list_get_selected_action( NACT_WINDOW( window )));
-	set_current_action( window );
+	NAObject *object = nact_iactions_list_get_selected_action( NACT_WINDOW( window ));
+
+	if( NA_IS_ACTION( object )){
+		window->private->edited_action = NA_ACTION( object );
+		set_current_action( window );
+
+	} else {
+		g_assert( NA_IS_ACTION_PROFILE( object ));
+		window->private->edited_profile = NA_ACTION_PROFILE( object );
+		set_current_profile( window );
+	}
 }
 
 static gboolean
@@ -602,26 +614,27 @@ on_actions_list_enter_key_pressed( GtkWidget *widget, GdkEventKey *event, gpoint
 
 /*
  * update the notebook when selection changes in IActionsList
+ * if there is only one profile, we also setup the profile
  */
 static void
 set_current_action( NactMainWindow *window )
 {
-	/*NactMainWindow *window = NACT_MAIN_WINDOW( wnd );*/
+	g_debug( "set_current_action: current=%p", window->private->edited_action );
 
-	/*g_free( window->private->current_uuid );
-	window->private->current_uuid = NULL;
+	nact_iaction_tab_set_action( NACT_WINDOW( window ), window->private->edited_action );
 
-	g_free( window->private->current_label );
-	window->private->current_label = NULL;
+	window->private->edited_profile = NULL;
+	if( na_action_get_profiles_count( window->private->edited_action ) == 1 ){
+		window->private->edited_profile = NA_ACTION_PROFILE( na_action_get_profiles( window->private->edited_action )->data );
+	}
 
-	if( action ){
-		g_assert( NA_IS_ACTION( action ));
-		window->private->current_uuid = na_action_get_uuid( action );
-		window->private->current_label = na_action_get_label( action );
-	}*/
+	set_current_profile( window );
+}
 
-	g_debug( "set_current_action: current=%p", window->private->edited );
-	nact_iaction_tab_set_action( NACT_WINDOW( window ), window->private->edited );
+static void
+set_current_profile( NactMainWindow *window )
+{
+	nact_icommand_tab_set_profile( NACT_WINDOW( window ), window->private->edited_profile );
 }
 
 /*
@@ -632,7 +645,14 @@ static NAAction *
 get_edited_action( NactWindow *window )
 {
 	g_assert( NACT_IS_MAIN_WINDOW( window ));
-	return( NACT_MAIN_WINDOW( window )->private->edited );
+	return( NACT_MAIN_WINDOW( window )->private->edited_action );
+}
+
+static NAActionProfile *
+get_edited_profile( NactWindow *window )
+{
+	g_assert( NACT_IS_MAIN_WINDOW( window ));
+	return( NACT_MAIN_WINDOW( window )->private->edited_profile );
 }
 
 /*
@@ -645,7 +665,7 @@ on_modified_field( NactWindow *window )
 {
 	g_assert( NACT_IS_MAIN_WINDOW( window ));
 
-	check_edited_status( window, NACT_MAIN_WINDOW( window )->private->edited );
+	check_edited_status( window, NACT_MAIN_WINDOW( window )->private->edited_action );
 
 	setup_dialog_title( NACT_MAIN_WINDOW( window ));
 	setup_dialog_menu( NACT_MAIN_WINDOW( window ));
@@ -699,12 +719,6 @@ is_action_to_save( const NAAction *action )
 	return( GPOINTER_TO_INT( g_object_get_data( G_OBJECT( action ), "nact-main-window-action-can-save" )));
 }
 
-static NAActionProfile *
-get_edited_profile( NactWindow *window )
-{
-	return( NULL );
-}
-
 static void
 get_isfiledir( NactWindow *window, gboolean *isfile, gboolean *isdir )
 {



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