[nautilus-actions] nact_tree_ieditable_set_items(): new function



commit 197dc8d78a9806caf50eff94bebc70885e6401b8
Author: Pierre Wieser <pwieser trychlos org>
Date:   Thu Jan 5 23:14:06 2012 +0100

    nact_tree_ieditable_set_items(): new function
    
    This let us override an item with an newly imported one,
    as it appears that this feature was never been implemented.

 ChangeLog                        |    6 +++
 src/nact/nact-assistant-import.c |   18 +++++++--
 src/nact/nact-tree-ieditable.c   |   80 ++++++++++++++++++++++++++++++++++++++
 src/nact/nact-tree-ieditable.h   |    2 +
 src/nact/nact-tree-model-dnd.c   |   10 +++++
 5 files changed, 112 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 11a8b1b..9788491 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2012-01-05 Pierre Wieser <pwieser trychlos org>
 
+	* src/nact/nact-tree-ieditable.c:
+	* src/nact/nact-tree-ieditable.h (nact_tree_ieditable_set_items): New function.
+
+	* src/nact/nact-assistant-import.c (assistant_apply):
+	* src/nact/nact-tree-model-dnd.c (drop_uri_list): Updated accordingly.
+
 	* src/core/na-importer.c (manage_import_mode):
 	Release the duplicated object which has been cancelled by the user.
 
diff --git a/src/nact/nact-assistant-import.c b/src/nact/nact-assistant-import.c
index b9ecb7f..81279ee 100644
--- a/src/nact/nact-assistant-import.c
+++ b/src/nact/nact-assistant-import.c
@@ -93,6 +93,7 @@ struct _NactAssistantImportPrivate {
 	GtkTreeView *duplicates_listview;
 	NAIOption   *mode;
 	GList       *results;
+	GList       *overriden;
 };
 
 static const gchar        *st_xmlui_filename = PKGDATADIR "/nact-assistant-import.ui";
@@ -664,12 +665,10 @@ assistant_apply( BaseAssistant *wnd, GtkAssistant *assistant )
 
 	g_debug( "%s: window=%p, assistant=%p", thisfn, ( void * ) wnd, ( void * ) assistant );
 	window = NACT_ASSISTANT_IMPORT( wnd );
-
+	g_object_get( G_OBJECT( window ), BASE_PROP_PARENT, &main_window, NULL );
 	application = NACT_APPLICATION( base_window_get_application( main_window ));
 	updater = nact_application_get_updater( application );
 
-	g_object_get( G_OBJECT( wnd ), BASE_PROP_PARENT, &main_window, NULL );
-
 	memset( &importer_parms, '\0', sizeof( NAImporterParms ));
 	importer_parms.uris = gtk_file_chooser_get_uris( GTK_FILE_CHOOSER( window->private->file_chooser ));
 	importer_parms.check_fn = ( NAImporterCheckFn ) check_for_existence;
@@ -712,8 +711,13 @@ assistant_apply( BaseAssistant *wnd, GtkAssistant *assistant )
 		na_object_free_items( insertable_items );
 	}
 
+	/* contrarily, the tree store may or not take a new reference on overriding
+	 * items, so do not release it here
+	 */
 	if( overriden_items ){
-		na_object_free_items( overriden_items );
+		items_view = nact_main_window_get_items_view( NACT_MAIN_WINDOW( main_window ));
+		nact_tree_ieditable_set_items( NACT_TREE_IEDITABLE( items_view ), overriden_items );
+		window->private->overriden = overriden_items;
 	}
 }
 
@@ -840,6 +844,12 @@ prepare_importdone( NactAssistantImport *window, GtkAssistant *assistant, GtkWid
 	na_settings_set_string( NA_IPREFS_IMPORT_PREFERRED_MODE, mode_id );
 	g_free( mode_id );
 
+	/* release here our reference on overriding items
+	 */
+	if( window->private->overriden ){
+		na_object_free_items( window->private->overriden );
+	}
+
 	g_object_set( G_OBJECT( window ), BASE_PROP_WARN_ON_ESCAPE, FALSE, NULL );
 	gtk_assistant_set_page_complete( assistant, page, TRUE );
 	gtk_widget_show_all( page );
diff --git a/src/nact/nact-tree-ieditable.c b/src/nact/nact-tree-ieditable.c
index 4ebdadc..46e0c06 100644
--- a/src/nact/nact-tree-ieditable.c
+++ b/src/nact/nact-tree-ieditable.c
@@ -35,6 +35,7 @@
 #include <api/na-core-utils.h>
 #include <api/na-object-api.h>
 
+#include <core/na-factory-object.h>
 #include <core/na-updater.h>
 
 #include "base-keysyms.h"
@@ -42,6 +43,7 @@
 #include "nact-main-window.h"
 #include "nact-tree-ieditable.h"
 #include "nact-tree-model.h"
+#include "nact-tree-view.h"
 
 /* private interface data
  */
@@ -126,6 +128,8 @@ register_type( void )
 
 	type = g_type_register_static( G_TYPE_INTERFACE, "NactTreeIEditable", &info, 0 );
 
+	g_type_interface_add_prerequisite( type, NACT_TREE_VIEW_TYPE );
+
 	return( type );
 }
 
@@ -932,6 +936,82 @@ increment_counters( NactTreeIEditable *view, IEditableData *ied, GList *items )
 	g_signal_emit_by_name( G_OBJECT( ied->window ), TREE_SIGNAL_COUNT_CHANGED, FALSE, menus, actions, profiles );
 }
 
+/*
+ * nact_tree_ieditable_set_items:
+ * @instance: this #NactTreeIEditable *instance.
+ * @items: a #GList of items to be set in the tree view.
+ *
+ * The provided list of @items will override the items already in the list
+ * which have the same identifier. But:
+ * - menus keep their children
+ * - entering actions fully override existing ones.
+ *
+ * Items which would not been found are just ignored.
+ * Items which are not of the same type are ignored.
+ *
+ * This feature is typically used when importing an item whose identifier
+ * already exists, and the user have chosen to override existing item.
+ * So we should be almost sure that each item actually exists in the view.
+ */
+void
+nact_tree_ieditable_set_items( NactTreeIEditable *instance, GList *items )
+{
+	static const gchar *thisfn = "nact_tree_ieditable_set_items";
+	IEditableData *ied;
+	GList *it;
+	NAObjectItem *new_item, *old_item;
+	gchar *id;
+	GtkTreePath *path, *insert_path;
+
+	g_return_if_fail( NACT_IS_TREE_IEDITABLE( instance ));
+
+	g_debug( "%s: instance=%p, items=%p (count=%d)",
+		thisfn, ( void * ) instance, ( void * ) items, g_list_length( items ));
+
+	ied = get_instance_data( instance );
+
+	for( it = items ; it ; it = it->next ){
+		new_item = NA_OBJECT_ITEM( it->data );
+
+		id = na_object_get_id( new_item );
+		old_item = nact_tree_view_get_item_by_id( NACT_TREE_VIEW( instance ), id );
+
+		if( !old_item ){
+			g_warning( "%s: id=%s: item not found - ignored", thisfn, id );
+
+		} else if( G_OBJECT_TYPE( old_item ) != G_OBJECT_TYPE( new_item )){
+			g_warning( "%s: id=%s: old is a %s while new is a %s - ignored", thisfn, id,
+					G_OBJECT_TYPE_NAME( old_item ), G_OBJECT_TYPE_NAME( new_item ));
+
+		} else if( NA_IS_OBJECT_MENU( old_item )){
+			/* hopefully, na_factory_object_copy only copy valuable properties
+			 * keeping dynamic variables as parent pointer, provider and provider
+			 * data, read-only status - notably children are not impacted by this
+			 * copy
+			 */
+			na_factory_object_copy( NA_IFACTORY_OBJECT( old_item ), NA_IFACTORY_OBJECT( new_item ));
+
+		} else if( NA_IS_OBJECT_ACTION( old_item )){
+			/* na_factory_object is not a deep copy, which is fine for the menu
+			 * but not for the action - it appears more easier to just substitute
+			 * the old item with the new one
+			 *
+			 * only children of the old item are its own profiles, which are to be
+			 * replaced with the profiles provided by the new item
+			 */
+			path = nact_tree_model_delete( ied->model, NA_OBJECT( old_item ));
+			insert_path = nact_tree_model_insert_before( ied->model, NA_OBJECT( new_item ), path );
+			gtk_tree_path_free( path );
+			gtk_tree_path_free( insert_path );
+
+		} else {
+			g_warning( "%s: should not come here!", thisfn );
+		}
+
+		g_free( id );
+	}
+}
+
 /**
  * nact_tree_ieditable_dump_modified:
  * @instance: this #NactTreeIEditable *instance.
diff --git a/src/nact/nact-tree-ieditable.h b/src/nact/nact-tree-ieditable.h
index d80ddae..a92201e 100644
--- a/src/nact/nact-tree-ieditable.h
+++ b/src/nact/nact-tree-ieditable.h
@@ -90,6 +90,8 @@ void     nact_tree_ieditable_insert_items  ( NactTreeIEditable *instance, GList
 void     nact_tree_ieditable_insert_at_path( NactTreeIEditable *instance, GList *items, GtkTreePath *path );
 void     nact_tree_ieditable_insert_into   ( NactTreeIEditable *instance, GList *items );
 
+void     nact_tree_ieditable_set_items     ( NactTreeIEditable *instance, GList *items );
+
 void     nact_tree_ieditable_dump_modified         ( const NactTreeIEditable *instance );
 gboolean nact_tree_ieditable_is_level_zero_modified( const NactTreeIEditable *instance );
 
diff --git a/src/nact/nact-tree-model-dnd.c b/src/nact/nact-tree-model-dnd.c
index 9e1f907..adc18de 100644
--- a/src/nact/nact-tree-model-dnd.c
+++ b/src/nact/nact-tree-model-dnd.c
@@ -920,6 +920,16 @@ drop_uri_list( NactTreeModel *model, GtkTreePath *dest, GtkSelectionData  *selec
 		nact_tree_ieditable_insert_at_path( NACT_TREE_IEDITABLE( view ), imported, dest );
 	}
 
+	/* override items if needed
+	 * they may safely be released after having updated the store
+	 */
+	if( overriden ){
+		na_object_dump_tree( overriden );
+		view = nact_main_window_get_items_view( main_window );
+		nact_tree_ieditable_set_items( NACT_TREE_IEDITABLE( view ), overriden );
+		na_object_free_items( overriden );
+	}
+
 	drop_done = TRUE;
 	na_object_free_items( imported );
 	na_object_free_items( overriden );



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