[PATCH] Remove children of a folder whenever it's deleted



---
 ChangeLog                                     |    5 ++
 libtinymailui-gtk/tny-gtk-folder-list-store.c |   70 +++++++++++++++++++------
 2 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9a0b5d5..2b41fd7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-01-23  Sergio Villar Senin  <svillar igalia com>
+
+	* libtinymailui-gtk/tny-gtk-folder-list-store.c (deleter): Remove
+	the children of a folder whenever it is deleted
+
 2009-01-20  Jose Dapena Paz  <jdapena igalia com>
 
 	* libtinymailui-gtk/tny-gtk-folder-list-store.c:
diff --git a/libtinymailui-gtk/tny-gtk-folder-list-store.c b/libtinymailui-gtk/tny-gtk-folder-list-store.c
index 4545612..104347f 100644
--- a/libtinymailui-gtk/tny-gtk-folder-list-store.c
+++ b/libtinymailui-gtk/tny-gtk-folder-list-store.c
@@ -1201,6 +1201,26 @@ updater (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer use
 	return FALSE;
 }
 
+static gboolean
+is_folder_ancestor (TnyFolder *parent, TnyFolder* item)
+{
+	gboolean retval = FALSE;
+	TnyFolderStore *parent_store = tny_folder_get_folder_store (TNY_FOLDER (item));
+
+	while (TNY_IS_FOLDER (parent_store) && !retval) {
+		if (parent_store == (TnyFolderStore *) parent) {
+			retval = TRUE;
+		} else {
+			GObject *old = parent_store;
+			parent_store = tny_folder_get_folder_store (TNY_FOLDER (parent_store));
+			g_object_unref (old);
+		}
+	}
+	g_object_unref (parent_store);
+
+	return retval;
+}
+
 static gboolean 
 deleter (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data1)
 {
@@ -1208,34 +1228,52 @@ deleter (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer use
 	TnyFolderType type = TNY_FOLDER_TYPE_UNKNOWN;
 	GObject *folder = user_data1;
 	TnyGtkFolderListStore *me = (TnyGtkFolderListStore*) model;
+	GtkTreeIter tmp_iter;
 
 	/* The deleter will compare all folders in the model with the deleted 
 	 * folder @folder, and if there's a match it will delete the folder's
 	 * row from the model. */
 
-	gtk_tree_model_get (model, iter, 
-		TNY_GTK_FOLDER_LIST_STORE_TYPE_COLUMN, 
+	gtk_tree_model_get (model, iter,
+		TNY_GTK_FOLDER_LIST_STORE_TYPE_COLUMN,
 		&type, -1);
 
-	if (type != TNY_FOLDER_TYPE_ROOT) 
-	{
-		GObject *fol = NULL;
+	if (type == TNY_FOLDER_TYPE_ROOT)
+		return FALSE;
 
-		gtk_tree_model_get (model, iter, 
-			TNY_GTK_FOLDER_LIST_STORE_INSTANCE_COLUMN, 
-			&fol, -1);
+	if (gtk_tree_model_get_iter_first (model, &tmp_iter)) {
+		gboolean more_items = TRUE;
 
-		if (fol == folder) {
+		do {
+			GObject *citem = NULL;
+			gboolean deleted;
+
+			deleted = FALSE;
+			gtk_tree_model_get (model, &tmp_iter,
+					    TNY_GTK_FOLDER_LIST_STORE_INSTANCE_COLUMN,
+					    &citem, -1);
 
-			remove_folder_observer_weak (me, TNY_FOLDER (folder), FALSE);
-			remove_folder_store_observer_weak (me, TNY_FOLDER_STORE (folder), FALSE);
+			if (TNY_IS_FOLDER (citem)) {
+				/* We need to remove both the folder and its children */
+				if ((citem == folder) ||
+				    is_folder_ancestor (TNY_FOLDER (folder), TNY_FOLDER (citem))) {
 
-			gtk_list_store_remove (GTK_LIST_STORE (model), iter);
-			retval = TRUE;
-		}
+					remove_folder_observer_weak (me, TNY_FOLDER (citem), FALSE);
+					remove_folder_store_observer_weak (me, TNY_FOLDER_STORE (citem), FALSE);
 
-		if (fol)
-			g_object_unref (fol);
+					gtk_list_store_remove (GTK_LIST_STORE (model), &tmp_iter);
+					deleted = TRUE;
+				}
+			}
+
+			if (citem)
+				g_object_unref (citem);
+
+			/* If the item was deleted then the iter was
+			   moved to the next row */
+			if (!deleted)
+				more_items = gtk_tree_model_iter_next (model, &tmp_iter);
+		} while (more_items);
 	}
 
 	return retval;
-- 
1.5.6.5


--------------040700080302080709000102--


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