[PATCH] : filters (against CVS)



	Hi all,
here is the new patch against CVS (this one replaces others). It corrects 
2 or 3 annoying bugs :
	misbehaviour when creating new filters/deleting others that lead 
to duplicate filters or undeleted filters
	forgotten return at the end of a function that was presumed to 
return something useful (crash guaranteed ;-)
	another one I have yet forgotten ;)

Plus a lot of UI tweaks (grey/ungrey buttons when they are 
useless/useful), this needs a bit more work but I wanted to release the 
patch because of the bugs. I have also renamed "Find/Find again" to 
"Search/Search again" to avoid confusion with the usual Find/Find again 
that looks for a string in a text (thanks to the person who pointed that 
one, excuse-me I don't remeber who now).
Have fun
Bye
Manu
--- /home/manu/prog/balsa-cvs/balsa/libbalsa/filter-file.h	Sun Nov 18 19:20:23 2001
+++ balsa/libbalsa/filter-file.h	Thu Nov 22 12:26:18 2001
@@ -60,8 +60,4 @@
 
 void libbalsa_mailbox_filters_save_config(LibBalsaMailbox * mbox);
 
-/* Cleans all condition sections for a given filter section */
-
-void libbalsa_filter_clean_conditions(gchar *prefix,gchar* filter_section_name);
-
 #endif  /* __FILTER_FILE_H__ */
--- /home/manu/prog/balsa-cvs/balsa/libbalsa/filter-file.c	Sun Nov 18 19:20:23 2001
+++ balsa/libbalsa/filter-file.c	Thu Nov 22 13:23:13 2001
@@ -122,6 +122,27 @@
  * Position filter_errno
  */
 
+/* Temporary struct used to ensure that we keep the same order for conditions
+   as specified by the user */
+
+typedef struct {
+    LibBalsaCondition * cnd;
+    gint order;
+} LibBalsaTempCondition;
+
+/* You'll note that this comparison function is inversed
+   That's for a purpose ;-)
+   I use it to avoid a g_list_reverse call (because the order
+   will already be reversed because of the choice of the reversed
+   comparison func) */
+
+static gint compare_conditions_order(gconstpointer  a,gconstpointer  b)
+{
+    const LibBalsaTempCondition * t1=a;
+    const LibBalsaTempCondition * t2=b;
+    return t2->order-t1->order;
+}
+
 void
 libbalsa_conditions_new_from_config(gchar * prefix,
                                     gchar * filter_section_name,
@@ -132,6 +153,7 @@
     gchar *tmp,* condprefix,*key;
     gint pref_len=strlen(CONDITION_SECTION_PREFIX)+strlen(filter_section_name);
     gint err=FILTER_NOERR;
+    GList * tmp_list=NULL;
 
     FILTER_SETFLAG(fil,FILTER_VALID);
     FILTER_SETFLAG(fil,FILTER_COMPILED);
@@ -158,15 +180,13 @@
 		    filter_errno=FILTER_NOERR;
 		    libbalsa_condition_free(cond);
 		}
-		/* FIXME: Ugly hack to preserve condition order in a
-		   filter. It seems that gnome section iterator always
-		   retrieve section in reverse order so I prepend the
-		   conditions to have good order at the end for now
-		   this hack works, but we'll have to fix that in the
-		   future (gnome_config could change and break our
-		   assumption) and use order of condition section key
-		   to insure order of condition */
-		else libbalsa_filter_prepend_condition(fil,cond);
+		else {
+		    LibBalsaTempCondition * tmp=g_new(LibBalsaTempCondition,1);
+		    
+		    tmp->cnd=cond;
+		    tmp->order=atoi(strrchr(key,':')+1);
+		    tmp_list=g_list_prepend(tmp_list,tmp);
+		}
 	    }
 	    else FILTER_CLRFLAG(fil,FILTER_VALID);
 	    gnome_config_pop_prefix();
@@ -175,8 +195,23 @@
     }
     g_free(tmp);
     /* We position filter_errno to the last non-critical error */
-    if (filter_errno==FILTER_NOERR)
+    if (filter_errno==FILTER_NOERR) {
+	LibBalsaTempCondition * tmp;
+
 	filter_errno=err;
+	/* We sort the list of temp conditions, then
+	   we populate the conditions list of the filter */
+	tmp_list=g_list_sort(tmp_list,compare_conditions_order);
+	for (;tmp_list;) {
+	    tmp=(LibBalsaTempCondition *)(tmp_list->data);
+	    libbalsa_filter_prepend_condition(fil,tmp->cnd);
+	    g_free(tmp);
+	    tmp_list=g_list_remove_link(tmp_list,tmp_list);
+	}
+	/* We don't do a g_list_reverse because the comparison func
+	   which dictate the order of the list after the sort is already
+	   reversed */
+    }
 }
 
 /* End of helper functions */
@@ -228,11 +263,13 @@
     gnome_config_set_int("Match-fields",cond->match_fields);
 
     /* We clean all other keys, to have a clean config file */
-    gnome_config_clean_key("Match-string");
-    gnome_config_clean_key("Reg-exps");
-    gnome_config_clean_key("Low-date");
-    gnome_config_clean_key("High-date");
-    gnome_config_clean_key("Flags");
+    if (cond->type!=CONDITION_SIMPLE) gnome_config_clean_key("Match-string");
+    if (cond->type!=CONDITION_REGEX) gnome_config_clean_key("Reg-exps");
+    if (cond->type!=CONDITION_DATE) {
+	gnome_config_clean_key("Low-date");
+	gnome_config_clean_key("High-date");
+    }
+    if (cond->type!=CONDITION_FLAG) gnome_config_clean_key("Flags");
 
     switch(cond->type) {
     case CONDITION_SIMPLE:
@@ -273,41 +310,36 @@
  * to create the name of the section
  * We clean all preceding saved conditions to keep the config file clean and coherent
  */
+
+#define CONDITION_SECTION_MAX "999"
+
 void libbalsa_conditions_save_config(GSList * conds,gchar * prefix,gchar * filter_section_name)
 {
-    gint nb=0;
-    gchar * tmp;
+    gint nb=0,tmp_len=strlen(CONDITION_SECTION_MAX)+2;
+    gchar * buffer,* tmp;
+
+    /* We allocate once for all a buffer to store conditions sections names */
+    buffer=g_strdup_printf("%s" CONDITION_SECTION_PREFIX "%s" ":%s/",prefix,filter_section_name,CONDITION_SECTION_MAX);
+    tmp=strrchr(buffer,':')+1;
     for (;conds;conds=g_slist_next(conds),nb++) {
-	tmp=g_strdup_printf("%s" CONDITION_SECTION_PREFIX "%s" ":%d/",prefix,filter_section_name,nb);
-	gnome_config_push_prefix(tmp);
-	g_free(tmp);
+	snprintf(tmp,tmp_len,"%d/",nb);
+	gnome_config_push_prefix(buffer);
 	libbalsa_condition_save_config((LibBalsaCondition*) conds->data);
 	gnome_config_pop_prefix();
     }
-}
-
-/* Clean the old conditions section for a given filter section */
 
-void
-libbalsa_filter_clean_conditions(gchar *prefix,gchar * filter_section_name)
-{
-    void * iterator;
-    gchar *tmp,* condprefix,*key;
-    gint pref_len=strlen(CONDITION_SECTION_PREFIX)+strlen(filter_section_name);
-    
-    tmp=g_strconcat(CONDITION_SECTION_PREFIX,filter_section_name,NULL);
-    iterator = gnome_config_init_iterator_sections(prefix);
-
-    while ((iterator = gnome_config_iterator_next(iterator, &key, NULL))) {
-	
-	if (strncmp(key,tmp,pref_len)==0) {
-	    condprefix=g_strconcat(prefix,key,"/",NULL);
-	    gnome_config_clean_section(condprefix);
-	    g_free(condprefix);
+    /* This loop takes care of cleaning up old sections */
+    while (TRUE) {
+	snprintf(tmp,tmp_len,"%d/",nb++);
+	if (gnome_config_has_section(buffer)) {
+	    //g_print("Cleaning section %s\n",buffer);
+	    gnome_config_clean_section(buffer);
 	}
-	g_free(key);
+	else break;
     }
-}                           /* End of libbalsa_filter_clean_conditions() */
+    g_free(buffer);
+}
+
 /*
  * void libbalsa_filter_save_config(filter * f)
  *
@@ -344,12 +376,11 @@
     GSList * lst;
 
     /* We load the associated filters */
-    g_print("Loading filters to mailbox %s\n",mbox->name);
+    //g_print("Loading filters to mailbox %s\n",mbox->name);
     gnome_config_get_vector_with_default(MAILBOX_FILTERS_KEY,&nb_filters,&filters_names,&def);
     if (!def) {
 	for(i=0;i<nb_filters;i++) {
-	    g_print("Loading filter %s to mailbox %s\n",
-                    filters_names[i], mbox->name);
+	    //g_print("Loading filter %s to mailbox %s\n",filters_names[i], mbox->name);
 	    fil = libbalsa_filter_get_by_name(filters_names[i]);
 	    if (fil) {
 		LibBalsaMailboxFilter* mf = g_new(LibBalsaMailboxFilter,1);
@@ -410,7 +441,7 @@
     lst=names;
     for(i=0;i<nb_filters;i++) {
 	filters_names[i]=(gchar*)lst->data;
-	g_print("Saving filter %s to mailbox %s\n",filters_names[i],mbox->name);
+	//g_print("Saving filter %s to mailbox %s\n",filters_names[i],mbox->name);
 	lst=g_slist_next(lst);
     }
     g_slist_free(names);
--- /home/manu/prog/balsa-cvs/balsa/libbalsa/filter.c	Sun Nov 18 19:20:23 2001
+++ balsa/libbalsa/filter.c	Thu Nov 22 17:06:36 2001
@@ -250,7 +250,7 @@
     LibBalsaMailbox * source_mbox;
     gboolean result=FALSE;
 
-    g_return_val_if_fail(filter_list && messages, FALSE);
+    if (!filter_list || ! messages) return FALSE;
 
     source_mbox=LIBBALSA_MESSAGE(messages->data)->mailbox;
     for (;messages;messages=g_list_next(messages)) {
--- /home/manu/prog/balsa-cvs/balsa/libbalsa/mailbox-filter.c	Sun Nov 18 19:20:23 2001
+++ balsa/libbalsa/mailbox-filter.c	Thu Nov 22 16:59:22 2001
@@ -44,4 +44,5 @@
 	    lst=g_slist_prepend(lst,((LibBalsaMailboxFilter*)filters->data)->actual_filter);
 	}
     lst=g_slist_reverse(lst);
+    return lst;
 }
--- /home/manu/prog/balsa-cvs/balsa/src/filter-edit-callbacks.c	Sun Nov 18 19:20:28 2001
+++ balsa/src/filter-edit-callbacks.c	Thu Nov 22 13:48:08 2001
@@ -100,6 +100,14 @@
 extern GtkWidget *fe_action_option_menu;
 extern GtkWidget *fe_action_entry;
 
+/* Different buttons that need to be greyed or ungreyed */
+extern GtkWidget * fe_delete_button;
+extern GtkWidget * fe_apply_button;
+extern GtkWidget * fe_revert_button;
+extern GtkWidget * fe_condition_delete_button;
+extern GtkWidget * fe_condition_edit_button;
+GtkWidget * fe_regex_remove_button;
+
 #define ELEMENTS(x) (sizeof (x) / sizeof (x[0]))
 
 /* condition_has_changed allows us to be smart enough not to make the whole process
@@ -174,7 +182,6 @@
 
 /**************** Conditions *************************/
 
-
 /* Callback function to fill the regex entry with the selected regex */
 
 void fe_regexs_select_row(GtkWidget * widget, gint row, gint column,
@@ -182,8 +189,11 @@
 {
     gchar *str;
     
-    gtk_clist_get_text(fe_type_regex_list,row,0,&str);
-    gtk_entry_set_text(GTK_ENTRY(fe_type_regex_entry),str);
+    if (row<fe_type_regex_list->rows) {
+	gtk_clist_get_text(fe_type_regex_list,row,0,&str);
+	gtk_entry_set_text(GTK_ENTRY(fe_type_regex_entry),str);
+    }
+    else gtk_entry_set_text(GTK_ENTRY(fe_type_regex_entry),"");
 }
 
 /*
@@ -537,6 +547,7 @@
     case CONDITION_REGEX:
 	for (regex=cnd->match.regexs;regex;regex=g_slist_next(regex))
 	    gtk_clist_append(fe_type_regex_list,&(((LibBalsaConditionRegex *)regex->data)->string));
+	gtk_widget_set_sensitive(fe_regex_remove_button,cnd->match.regexs!=NULL);
 	fe_update_type_regex_label();
 	break;
     case CONDITION_DATE:
@@ -579,27 +590,32 @@
 	if (condition_has_changed) {
 	    new_cnd = libbalsa_condition_new();
             if (!condition_validate(new_cnd))
-              return;
+		return;
             /* No error occured, condition is valid, so change/add it based on is_new_condition
              * and only if something has changed of course
              */
             if (condition_has_changed) {
-              if (!is_new_condition) {
-                /* We free the old condition*/
-                row=GPOINTER_TO_INT(fe_conditions_list->selection->data);
-                libbalsa_condition_free((LibBalsaCondition*)
-                                        gtk_clist_get_row_data(fe_conditions_list,row));
-              }
-              else {
-                gchar * str[]={""};
-                /* It was a new condition, so add it to the list */
-                row=gtk_clist_append(fe_conditions_list,str);
-                gtk_clist_select_row(fe_conditions_list,row,-1);
-              }
-              /* Associate the new condition to the row in the clist*/
-              gtk_clist_set_row_data(fe_conditions_list,row,new_cnd);
-              /* And refresh the conditions list */
-              update_condition_list_label();
+		if (!is_new_condition) {
+		    /* We free the old condition*/
+		    row=GPOINTER_TO_INT(fe_conditions_list->selection->data);
+		    libbalsa_condition_free((LibBalsaCondition*)
+					    gtk_clist_get_row_data(fe_conditions_list,row));
+		}
+		else {
+		    gchar * str[]={""};
+		    /* It was a new condition, so add it to the list */
+		    row=gtk_clist_append(fe_conditions_list,str);
+		    gtk_clist_select_row(fe_conditions_list,row,-1);
+		    /* We make the buttons sensitive if they were unsensitive */
+		    if (fe_conditions_list->rows==1) {
+			gtk_widget_set_sensitive(fe_condition_delete_button,TRUE);
+			gtk_widget_set_sensitive(fe_condition_edit_button,TRUE);
+		    }
+		}
+		/* Associate the new condition to the row in the clist*/
+		gtk_clist_set_row_data(fe_conditions_list,row,new_cnd);
+		/* And refresh the conditions list */
+		update_condition_list_label();
             }
 	}
     case 1:  /* Cancel button */
@@ -677,7 +693,7 @@
 
     fe_type_regex_list = GTK_CLIST(gtk_clist_new(1));
 
-    gtk_clist_set_selection_mode(fe_type_regex_list, GTK_SELECTION_SINGLE);
+    gtk_clist_set_selection_mode(fe_type_regex_list, GTK_SELECTION_BROWSE);
     gtk_clist_set_row_height(fe_type_regex_list, 0);
     gtk_clist_set_reorderable(fe_type_regex_list, FALSE);
     gtk_clist_set_use_drag_icons(fe_type_regex_list, FALSE);
@@ -696,9 +712,9 @@
     gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
     gtk_signal_connect(GTK_OBJECT(button),
 		       "clicked", GTK_SIGNAL_FUNC(fe_add_pressed), NULL);
-    button = gtk_button_new_with_label(_("Remove"));
-    gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
-    gtk_signal_connect(GTK_OBJECT(button),
+    fe_regex_remove_button = gtk_button_new_with_label(_("Remove"));
+    gtk_box_pack_start(GTK_BOX(box), fe_regex_remove_button, TRUE, TRUE, 0);
+    gtk_signal_connect(GTK_OBJECT(fe_regex_remove_button),
 		       "clicked",
 		       GTK_SIGNAL_FUNC(fe_remove_pressed), NULL);
     button = gtk_button_new_with_label(_("One matches/None matches"));
@@ -938,7 +954,6 @@
 	build_condition_dialog();
 	/* For now this box is modal */
 	gtk_window_set_modal(GTK_WINDOW(condition_dialog),TRUE);
-
     }
     title = g_strconcat(_("Edit condition for filter: "),
 			((LibBalsaFilter*)
@@ -958,7 +973,7 @@
 fe_conditions_select_row(GtkWidget * widget, gint row, gint column,
                          GdkEventButton * bevent, gpointer data)
 {
-    if (bevent == NULL)
+    if (bevent == NULL || row<0)
 	return;
 
     if (bevent->type == GDK_2BUTTON_PRESS)
@@ -974,11 +989,16 @@
     selected = fe_conditions_list->selection;
     
     if (!selected)
-      return;
+	return;
     row=GPOINTER_TO_INT(selected->data);
     libbalsa_condition_free((LibBalsaCondition*) 
                             gtk_clist_get_row_data(fe_conditions_list,row));
     gtk_clist_remove(fe_conditions_list,row);
+
+    if (!fe_conditions_list->rows) {
+	gtk_widget_set_sensitive(fe_condition_delete_button,FALSE);
+	gtk_widget_set_sensitive(fe_condition_edit_button,FALSE);
+    }
 }
 
 /**************** Filters ****************************/
@@ -1037,6 +1057,8 @@
 				//g_print("Removed\n");
 				for (j=i;j<nb_filters-1;j++)
 				    filters_names[j]=filters_names[j+1];
+				/* We put NULL to be sure that g_strfreev does not
+				   free already freed memory */
 				filters_names[--nb_filters]=NULL;
 			    }
 			}
@@ -1134,13 +1156,6 @@
 			G_LEVEL_ORDER,
 			G_TRAVERSE_ALL, 10, update_filters_mailbox, NULL);
 
-	/* Update filters config (remove sections of removed filters) */
-	for (names_lst=filters_names_changes;names_lst;names_lst=g_list_next(names_lst))
-	    if (!((filters_names_rec *)names_lst->data)->new_name) {
-		// g_print("Cleaning section for %s\n",((filters_names_rec *)names_lst->data)->old_name);
-		clean_filter_config_section(((filters_names_rec *)names_lst->data)->old_name);
-	    }
-	
 	gnome_dialog_close(GNOME_DIALOG(dialog));
 	config_filters_save();
 	break;
@@ -1186,6 +1201,7 @@
     
     gtk_clist_append(fe_type_regex_list, &text);
     condition_has_changed=TRUE;
+    gtk_widget_set_sensitive(fe_regex_remove_button,TRUE);
 }			/* end fe_add_pressed() */
 
 /*
@@ -1205,6 +1221,8 @@
     
     gtk_clist_remove(fe_type_regex_list, GPOINTER_TO_INT(selected->data));
     condition_has_changed=TRUE;
+    if (!fe_type_regex_list->rows)
+	gtk_widget_set_sensitive(fe_regex_remove_button,FALSE);
 }			/* end fe_remove_pressed() */
 
 /************************************************************/
@@ -1352,6 +1370,23 @@
     libbalsa_filter_free(fil, NULL);
     
     gtk_clist_remove(fe_filters_list, row);
+    if (row>=fe_filters_list->rows) row=fe_filters_list->rows-1;
+    if (row<0) {
+	/* We make the filters delete,revert,apply buttons unsensitive */
+	gtk_widget_set_sensitive(fe_delete_button,FALSE);
+	gtk_widget_set_sensitive(fe_apply_button,FALSE);
+	gtk_widget_set_sensitive(fe_revert_button,FALSE);
+	/* We clear all widgets */
+	gtk_entry_set_text(GTK_ENTRY(fe_name_entry),"");
+	gtk_entry_set_text(GTK_ENTRY(fe_popup_entry),"");
+	gtk_entry_set_text(GTK_ENTRY(fe_action_entry),"");
+	gtk_entry_set_text(GTK_ENTRY(gnome_file_entry_gtk_entry(GNOME_FILE_ENTRY(fe_sound_entry))),"");
+	gtk_clist_clear(fe_conditions_list);
+	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fe_sound_button),FALSE);
+	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fe_popup_button),FALSE);
+    }
+    else
+	gtk_clist_select_row(fe_filters_list,row,-1);
 }			/* end fe_delete_pressed() */
 
 /*
@@ -1533,8 +1568,11 @@
 
     fil=(LibBalsaFilter*)gtk_clist_get_row_data(fe_filters_list,row);
     
-    /* FIXME : Should be impossible */
-    g_assert(fil!=NULL);
+    /* FIXME : this seems necessary : this callback is called
+       really early, so early that the clist has not been populated
+       in between! (this is related to the selection mode
+       I have chosen : GTK_CLIST_SELECTION_BROWSE)*/
+    if (!fil) return;
     
     /* Populate all fields with filter data */
     gtk_entry_set_text(GTK_ENTRY(fe_name_entry),fil->name);
@@ -1544,9 +1582,9 @@
 		       FILTER_CHKFLAG(fil,FILTER_POPUP) ? fil->popup_text : "");
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fe_sound_button),
 				 FILTER_CHKFLAG(fil,FILTER_SOUND));
-    /*    gtk_entry_set_text(GTK_ENTRY(fe_sound_entry),
-	  FILTER_CHKFLAG(fil,FILTER_SOUND) ? fil->sound : "");*/
-
+    gtk_entry_set_text(GTK_ENTRY(gnome_file_entry_gtk_entry(GNOME_FILE_ENTRY(fe_sound_entry))),
+		       FILTER_CHKFLAG(fil,FILTER_SOUND) ? fil->sound : "");
+    
     gtk_option_menu_set_history(GTK_OPTION_MENU(fe_action_option_menu), fil->action-1);
     gtk_option_menu_set_history(GTK_OPTION_MENU(fe_op_codes_option_menu), fil->conditions_op-1);
 
@@ -1573,6 +1611,18 @@
     if (filter_errno!=FILTER_NOERR)
 	gnome_dialog_close(fe_window);
 
-    if (fe_conditions_list->rows)
+    if (fe_conditions_list->rows) {
 	gtk_clist_select_row(fe_conditions_list,0,-1);
+	gtk_widget_set_sensitive(fe_condition_delete_button,TRUE);
+	gtk_widget_set_sensitive(fe_condition_edit_button,TRUE);
+    }
+    else {
+	gtk_widget_set_sensitive(fe_condition_delete_button,FALSE);
+	gtk_widget_set_sensitive(fe_condition_edit_button,FALSE);
+    }
+
+    /* We make the filters delete,revert,apply buttons sensitive */
+    gtk_widget_set_sensitive(fe_delete_button,TRUE);
+    gtk_widget_set_sensitive(fe_apply_button,TRUE);
+    gtk_widget_set_sensitive(fe_revert_button,TRUE);
 }                      /* end fe_clist_select_row */
--- /home/manu/prog/balsa-cvs/balsa/src/save-restore.c	Sun Nov 18 19:20:28 2001
+++ balsa/src/save-restore.c	Thu Nov 22 17:19:15 2001
@@ -1285,32 +1285,48 @@
 	filter_errno=non_critical_error;
 }
 
+#define FILTER_SECTION_MAX "9999"
+
 void
 config_filters_save(void)
 {
     GSList *list;
     LibBalsaFilter* fil;
-    gchar * tmp,* section_name;
-    gint i=0;
+    gchar * buffer,* tmp,* section_name;
+    gint i,nb=0,tmp_len=strlen(FILTER_SECTION_MAX)+2;
+
+    /* We allocate once for all a buffer to store conditions sections names */
+    buffer=g_strdup_printf(BALSA_CONFIG_PREFIX FILTER_SECTION_PREFIX "%s/",FILTER_SECTION_MAX);
+    /* section_name points to the beginning of the filter section name */
+    section_name=buffer+strlen(BALSA_CONFIG_PREFIX);
+    /* tmp points to the space where filter number is appended */
+    tmp=section_name+strlen(FILTER_SECTION_PREFIX);
 
     for(list = balsa_app.filters; list; list = g_slist_next(list)) {
 	fil = (LibBalsaFilter*)(list->data);
-	section_name=g_strdup_printf(FILTER_SECTION_PREFIX"%d",i);
-	tmp=g_strconcat(BALSA_CONFIG_PREFIX,section_name,"/",NULL);
-	gnome_config_push_prefix(tmp);
-	g_free(tmp);
+	i=snprintf(tmp,tmp_len,"%d/",nb++);
+	gnome_config_push_prefix(buffer);
 	libbalsa_filter_save_config(fil);
 	gnome_config_pop_prefix();
 
-	libbalsa_filter_clean_conditions(BALSA_CONFIG_PREFIX,section_name);
-
-	if (fil->conditions)
-	    libbalsa_conditions_save_config(fil->conditions,BALSA_CONFIG_PREFIX,section_name);
-
-	g_free(section_name);
-	gnome_config_sync();
-	i++;
+	/* We suppress the final "/", this is necessary in order that
+	 * libbalsa_conditions_save_config can construct the condition section name */
+	tmp[i]='\0';
+	//g_print("Section name = %s\n",section_name);
+	libbalsa_conditions_save_config(fil->conditions,BALSA_CONFIG_PREFIX,section_name);
+    }
+    gnome_config_sync();
+    /* This loop takes care of cleaning up old filter sections */
+    while (TRUE) {
+	snprintf(tmp,tmp_len,"%d/",nb++);
+	if (gnome_config_has_section(buffer)) {
+	    //g_print("Cleaning filter section %s\n",buffer);
+	    gnome_config_clean_section(buffer);
+	}
+	else break;
     }
+    gnome_config_sync();
+    g_free(buffer);
 }
 
 /* Looks for a mailbox filters section with MBOX_URL field equals to mbox->url
@@ -1318,8 +1334,7 @@
  * The returned string has to be freed by the caller
  */
 
-gchar*
-mailbox_filters_section_lookup(const gchar * url)
+gchar * mailbox_filters_section_lookup(const gchar * url)
 {
     gint pref_len=strlen(MAILBOX_FILTERS_SECTION_PREFIX);
     guint url_len;
@@ -1347,41 +1362,7 @@
     return NULL;
 }
 
-
-/* Clean the section holding the content of filter and all conditions sections related to it
- */
-void
-clean_filter_config_section(const gchar* name)
-{
-    void *iterator;
-    gchar * key,* section,* fil_name;
-    gint pref_len = strlen(FILTER_SECTION_PREFIX);
-    gboolean found=FALSE;
-
-    iterator = gnome_config_init_iterator_sections(BALSA_CONFIG_PREFIX);
-    while (!found && 
-           (iterator = gnome_config_iterator_next(iterator, &key, NULL))) {
-
-	if (strncmp(key, FILTER_SECTION_PREFIX, pref_len) == 0) {
-	    section=g_strconcat(BALSA_CONFIG_PREFIX,key,"/",NULL);
-	    gnome_config_push_prefix(section);
-	    fil_name=gnome_config_get_string("Name");
-	    gnome_config_pop_prefix();
-	    if (strcmp(fil_name,name)==0) {
-		libbalsa_filter_clean_conditions(BALSA_CONFIG_PREFIX,key);
-		gnome_config_clean_section(section);
-		found=TRUE;
-	    }
-	    g_free(section);
-	    g_free(fil_name);
-	}
-	g_free(key);
-    }
-	
-}
-
-void
-config_mailbox_filters_save(LibBalsaMailbox * mbox)
+void config_mailbox_filters_save(LibBalsaMailbox * mbox)
 {
     gchar * tmp;
 
@@ -1410,8 +1391,7 @@
     gnome_config_sync();
 }
 
-void
-config_mailbox_filters_load(LibBalsaMailbox * mbox)
+void config_mailbox_filters_load(LibBalsaMailbox * mbox)
 {
     gchar * section;
 
@@ -1498,6 +1478,4 @@
             printf("Setting converted signature as executable.\n");
         }
     }
-} 
-                                                               
-
+}
--- /home/manu/prog/balsa-cvs/balsa/src/save-restore.h	Sun Nov 18 19:20:28 2001
+++ balsa/src/save-restore.h	Thu Nov 22 13:19:52 2001
@@ -58,7 +58,6 @@
 void config_mailbox_filters_load(LibBalsaMailbox * mbox);
 void config_mailbox_filters_save(LibBalsaMailbox * mbox);
 gchar * mailbox_filters_section_lookup(const gchar * url);
-void clean_filter_config_section(const gchar * name);
 #endif
 
 #endif				/* __SAVE_RESTORE_H__ */


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