[nautilus-actions] Insert the new origin at the same position that the previous one



commit a02d818d48cd66436b63386292f6902eabe6e179
Author: Pierre Wieser <pwieser trychlos org>
Date:   Wed Oct 28 22:22:42 2009 +0100

    Insert the new origin at the same position that the previous one

 ChangeLog                      |   11 +++++++
 src/common/na-object-api.h     |    2 +
 src/common/na-object-item-fn.h |    2 +
 src/common/na-object-item.c    |   64 ++++++++++++++++++++++++++++++++++++++++
 src/nact/nact-main-menubar.c   |   25 +++++++++------
 5 files changed, 94 insertions(+), 10 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index e4bea0b..af41f1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2009-10-28 Pierre Wieser <pwieser trychlos org>
 
+	* src/common/na-object-api.h
+	(na_object_get_position, na_object_insert_at): New functions.
+
+	* src/common/na-object-item-fn.h:
+	* src/common/na-object-item.c
+	(na_object_item_get_position, na_object_item_insert_at):
+	New functions.
+
+	* src/nact/nact-main-menubar.c (save_item):
+	Insert the new origin at the same position that the previous one.
+
 	* src/nact/nautilus-actions-config-tool.ui:
 	Add a label in front of the internal item id.
 
diff --git a/src/common/na-object-api.h b/src/common/na-object-api.h
index 1b989da..422a2d2 100644
--- a/src/common/na-object-api.h
+++ b/src/common/na-object-api.h
@@ -70,6 +70,8 @@ G_BEGIN_DECLS
 
 /* NAObjectItem
  */
+#define na_object_get_position( object, child )		na_object_item_get_position( NA_OBJECT_ITEM( object ), NA_OBJECT( child ))
+#define na_object_insert_at( object, child, pos )	na_object_item_insert_at( NA_OBJECT_ITEM( object ), NA_OBJECT( child ), pos )
 #define na_object_insert_item( object, item, before ) \
 													na_object_item_insert_item( NA_OBJECT_ITEM( object ), NA_OBJECT( item ), ( NAObject * ) before )
 
diff --git a/src/common/na-object-item-fn.h b/src/common/na-object-item-fn.h
index 2439cb1..8eb0a02 100644
--- a/src/common/na-object-item-fn.h
+++ b/src/common/na-object-item-fn.h
@@ -50,7 +50,9 @@
 G_BEGIN_DECLS
 
 GdkPixbuf     *na_object_item_get_pixbuf( const NAObjectItem *object, GtkWidget *widget );
+gint           na_object_item_get_position( const NAObjectItem *object, const NAObject *child );
 
+void           na_object_item_insert_at( NAObjectItem *object, const NAObject *item, gint pos );
 void           na_object_item_insert_item( NAObjectItem *object, const NAObject *item, const NAObject *before );
 
 void           na_object_item_count_items( GList *items, gint *menus, gint *actions, gint *profiles, gboolean recurse );
diff --git a/src/common/na-object-item.c b/src/common/na-object-item.c
index a0f4e08..84ddeca 100644
--- a/src/common/na-object-item.c
+++ b/src/common/na-object-item.c
@@ -91,6 +91,70 @@ GdkPixbuf *na_object_item_get_pixbuf( const NAObjectItem *item, GtkWidget *widge
 }
 
 /**
+ * na_object_item_get_position:
+ * @object: this #NAObjectItem object.
+ * @child: a #NAObject child.
+ *
+ * Returns: the position of @child in the subitems list of @object,
+ * starting from zero, or -1 if not found.
+ */
+gint
+na_object_item_get_position( const NAObjectItem *object, const NAObject *child )
+{
+	gint pos = -1;
+
+	g_return_val_if_fail( NA_IS_OBJECT_ITEM( object ), pos );
+	g_return_val_if_fail( !child || NA_IS_OBJECT( child ), pos );
+
+	if( !child ){
+		return( pos );
+	}
+
+	if( !object->private->dispose_has_run ){
+
+		pos = g_list_index( object->private->items, ( gconstpointer ) child );
+	}
+
+	return( pos );
+}
+
+/**
+ * na_object_item_insert_at:
+ * @item: the #NAObjectItem in which add the subitem.
+ * @object: a #NAObject to be inserted in the list of subitems.
+ * @pos: the position at which the @object child should be inserted.
+ *
+ * Inserts a new @object in the list of subitems of @item.
+ *
+ * Doesn't modify the reference count on @object.
+ */
+void
+na_object_item_insert_at( NAObjectItem *item, const NAObject *object, gint pos )
+{
+	GList *it;
+	gint i;
+
+	g_return_if_fail( NA_IS_OBJECT_ITEM( item ));
+	g_return_if_fail( NA_IS_OBJECT( object ));
+
+	if( !item->private->dispose_has_run ){
+
+		if( pos == -1 || pos >= g_list_length( item->private->items )){
+			na_object_append_item( item, object );
+
+		} else {
+			i = 0;
+			for( it = item->private->items ; it && i <= pos ; it = it->next ){
+				if( i == pos ){
+					item->private->items = g_list_insert_before( item->private->items, it, ( gpointer ) object );
+				}
+				i += 1;
+			}
+		}
+	}
+}
+
+/**
  * na_object_item_insert_item:
  * @item: the #NAObjectItem to which add the subitem.
  * @object: a #NAObject to be inserted in the list of subitems.
diff --git a/src/nact/nact-main-menubar.c b/src/nact/nact-main-menubar.c
index adddf4b..2bf9cf9 100644
--- a/src/nact/nact-main-menubar.c
+++ b/src/nact/nact-main-menubar.c
@@ -751,19 +751,22 @@ on_save_activated( GtkAction *gtk_action, NactMainWindow *window )
 	g_return_if_fail( GTK_IS_ACTION( gtk_action ));
 	g_return_if_fail( NACT_IS_MAIN_WINDOW( window ));
 
-	/* delete the removed actions
+	/* delete removed and modified items
 	 * so that new actions with same id do not risk to be deleted later
 	 */
 	nact_main_window_remove_deleted( window );
+	nact_iactions_list_removed_modified( NACT_IACTIONS_LIST( window ));
 
+	/* always write the level zero list of items
+	 * and reset the corresponding modification flag
+	 */
 	application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
 	pivot = nact_application_get_pivot( application );
 	items = nact_iactions_list_get_items( NACT_IACTIONS_LIST( window ));
 	na_pivot_write_level_zero( pivot, items );
 
-	/* remove modified items
-	 */
-	nact_iactions_list_removed_modified( NACT_IACTIONS_LIST( window ));
+	mis = ( MenubarIndicatorsStruct * ) g_object_get_data( G_OBJECT( window ), MENUBAR_PROP_INDICATORS );
+	mis->level_zero_order_changed = FALSE;
 
 	/* recursively save the modified items
 	 * check is useless here if item was not modified, but not very costly
@@ -774,11 +777,6 @@ on_save_activated( GtkAction *gtk_action, NactMainWindow *window )
 	}
 	g_list_free( items );
 
-	/* reset level zero indicator
-	 */
-	mis = ( MenubarIndicatorsStruct * ) g_object_get_data( G_OBJECT( window ), MENUBAR_PROP_INDICATORS );
-	mis->level_zero_order_changed = FALSE;
-
 	/* get ride of notification messages of IOProviders
 	 */
 	na_ipivot_consumer_delay_notify( NA_IPIVOT_CONSUMER( window ));
@@ -798,6 +796,7 @@ save_item( NactMainWindow *window, NAPivot *pivot, NAObjectItem *item )
 	NAObjectItem *dup_pivot;
 	GList *subitems, *it;
 	NAObjectItem *parent;
+	gint pos;
 
 	g_return_if_fail( NACT_IS_MAIN_WINDOW( window ));
 	g_return_if_fail( NA_IS_PIVOT( pivot ));
@@ -822,10 +821,12 @@ save_item( NactMainWindow *window, NAPivot *pivot, NAObjectItem *item )
 			 */
 			origin = ( NAObjectItem * ) na_object_get_origin( item );
 			parent = NULL;
+			pos = -1;
 
 			if( origin ){
 				parent = na_object_get_parent( origin );
 				if( parent ){
+					pos = na_object_get_position( parent, origin );
 					na_object_remove_item( parent, origin );
 				} else {
 					na_pivot_remove_item( pivot, NA_OBJECT( origin ));
@@ -836,7 +837,11 @@ save_item( NactMainWindow *window, NAPivot *pivot, NAObjectItem *item )
 			na_object_reset_origin( item, dup_pivot );
 			na_object_set_parent( dup_pivot, parent );
 			if( parent ){
-				na_object_append_item( parent, dup_pivot );
+				if( pos == -1 ){
+					na_object_append_item( parent, dup_pivot );
+				} else {
+					na_object_insert_at( parent, dup_pivot, pos );
+				}
 			} else {
 				na_pivot_add_item( pivot, NA_OBJECT( dup_pivot ));
 			}



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