Index: libbalsa/mailbox.c
===================================================================
RCS file: /cvs/gnome/balsa/libbalsa/mailbox.c,v
retrieving revision 1.222
diff -u -r1.222 mailbox.c
--- libbalsa/mailbox.c	18 Jun 2003 13:09:45 -0000	1.222
+++ libbalsa/mailbox.c	25 Aug 2003 12:44:19 -0000
@@ -1216,6 +1216,7 @@
     view->show = LB_MAILBOX_SHOW_UNSET;
     view->exposed = FALSE;
     view->open = FALSE;
+    view->in_sync = TRUE;
 
     return view;
 }
Index: libbalsa/mailbox.h
===================================================================
RCS file: /cvs/gnome/balsa/libbalsa/mailbox.h,v
retrieving revision 1.100
diff -u -r1.100 mailbox.h
--- libbalsa/mailbox.h	27 May 2003 13:01:43 -0000	1.100
+++ libbalsa/mailbox.h	25 Aug 2003 12:44:19 -0000
@@ -120,6 +120,7 @@
     LibBalsaMailboxShow          show;
     gboolean exposed;
     gboolean open;
+    gboolean in_sync;		/* view is in sync with config */
 };
 
 struct _LibBalsaMailbox {
Index: libbalsa/misc.c
===================================================================
RCS file: /cvs/gnome/balsa/libbalsa/misc.c,v
retrieving revision 1.114
diff -u -r1.114 misc.c
--- libbalsa/misc.c	28 Jul 2003 18:47:22 -0000	1.114
+++ libbalsa/misc.c	25 Aug 2003 12:44:20 -0000
@@ -129,6 +129,31 @@
     return retval;
 }
 
+gchar *
+libbalsa_urldecode(const gchar * str)
+{
+    gchar *retval;
+    gchar *x;
+
+    retval = g_new(char, strlen(str));
+
+    for (x = retval; *str != '\0'; str++, x++) {
+	*x = *str;
+	if (*x == '+')
+	    *x = ' ';
+	else if (*x == '%') {
+	    if (!*++str || !g_ascii_isxdigit(*str))
+		break;
+	    *x = g_ascii_xdigit_value(*str);
+	    if (!*++str || !g_ascii_isxdigit(*str))
+		break;
+	    *x = *x << 4 | g_ascii_xdigit_value(*str);
+	}
+    }
+
+    *x = '\0';
+    return retval;
+}
 
 /* FIXME: Move to address.c and change name to
  *   libbalsa_address_list_to_string or something */
Index: libbalsa/misc.h
===================================================================
RCS file: /cvs/gnome/balsa/libbalsa/misc.h,v
retrieving revision 1.70
diff -u -r1.70 misc.h
--- libbalsa/misc.h	28 Jul 2003 18:47:22 -0000	1.70
+++ libbalsa/misc.h	25 Aug 2003 12:44:20 -0000
@@ -75,6 +75,7 @@
 gchar *libbalsa_get_hostname(void);
 gchar *libbalsa_get_domainname(void);
 gchar *libbalsa_urlencode(const gchar* str);
+gchar *libbalsa_urldecode(const gchar * str);
 
 gboolean libbalsa_find_word(const gchar * word, const gchar * str);
 void libbalsa_wrap_string(gchar * str, int width);
Index: src/balsa-app.c
===================================================================
RCS file: /cvs/gnome/balsa/src/balsa-app.c,v
retrieving revision 1.238
diff -u -r1.238 balsa-app.c
--- src/balsa-app.c	15 Jul 2003 08:00:57 -0000	1.238
+++ src/balsa-app.c	25 Aug 2003 12:44:20 -0000
@@ -1078,3 +1078,173 @@
 void
 balsa_mailbox_nodes_unlock(gboolean exclusive) {}
 #endif
+
+/* Mailbox view methods */
+
+/* helper */
+static LibBalsaMailboxView *
+balsa_mailbox_get_view(LibBalsaMailbox * mailbox)
+{
+    LibBalsaMailboxView *view;
+
+    view = mailbox->view;
+    if (view)
+	return view;
+
+    view = g_hash_table_lookup(balsa_app.mailbox_views, mailbox->url);
+    if (view)
+	return view;
+
+    view = libbalsa_mailbox_view_new();
+    mailbox->view = view;
+    g_hash_table_insert(balsa_app.mailbox_views, g_strdup(mailbox->url),
+			view);
+
+    return view;
+}
+
+/* set methods */
+
+gboolean
+balsa_mailbox_set_identity_name(LibBalsaMailbox * mailbox,
+				const gchar * identity_name)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (!view->identity_name || strcmp(view->identity_name, identity_name)) {
+	g_free(view->identity_name);
+	view->identity_name = g_strdup(identity_name);
+	view->in_sync = FALSE;
+	return TRUE;
+    } else
+	return FALSE;
+}
+
+void
+balsa_mailbox_set_threading_type(LibBalsaMailbox * mailbox,
+				 LibBalsaMailboxThreadingType
+				 threading_type)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (view->threading_type != threading_type) {
+	view->threading_type = threading_type;
+	view->in_sync = FALSE;
+    }
+}
+
+void
+balsa_mailbox_set_sort_type(LibBalsaMailbox * mailbox,
+			    LibBalsaMailboxSortType sort_type)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (view->sort_type != sort_type) {
+	view->sort_type = sort_type;
+	view->in_sync = FALSE;
+    }
+}
+
+void
+balsa_mailbox_set_sort_field(LibBalsaMailbox * mailbox,
+			     LibBalsaMailboxSortFields sort_field)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (view->sort_field != sort_field) {
+	view->sort_field = sort_field;
+	view->in_sync = FALSE;
+    }
+}
+
+gboolean
+balsa_mailbox_set_show(LibBalsaMailbox * mailbox, LibBalsaMailboxShow show)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (view->show != show) {
+	/* Don't set not in sync if we're just replacing UNSET with the
+	 * default. */
+	if (view->show != LB_MAILBOX_SHOW_UNSET)
+	    view->in_sync = FALSE;
+	view->show = show;
+	return TRUE;
+    } else
+	return FALSE;
+}
+
+void
+balsa_mailbox_set_exposed(LibBalsaMailbox * mailbox, gboolean exposed)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (view->exposed != exposed) {
+	view->exposed = exposed;
+	view->in_sync = FALSE;
+    }
+}
+
+void
+balsa_mailbox_set_open(LibBalsaMailbox * mailbox, gboolean open)
+{
+    LibBalsaMailboxView *view = balsa_mailbox_get_view(mailbox);
+
+    if (view->open != open) {
+	view->open = open;
+	view->in_sync = FALSE;
+    }
+}
+
+/* get methods */
+
+LibBalsaAddress *
+balsa_mailbox_get_mailing_list_address(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->mailing_list_address : NULL;
+}
+
+const gchar *
+balsa_mailbox_get_identity_name(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->identity_name : NULL;
+}
+
+
+LibBalsaMailboxThreadingType
+balsa_mailbox_get_threading_type(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->threading_type
+	   : balsa_app.threading_type;
+}
+
+LibBalsaMailboxSortType
+balsa_mailbox_get_sort_type(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->sort_type
+	   : LB_MAILBOX_SORT_TYPE_ASC;
+}
+
+LibBalsaMailboxSortFields
+balsa_mailbox_get_sort_field(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->sort_field
+	   : LB_MAILBOX_SORT_DATE;
+}
+
+LibBalsaMailboxShow
+balsa_mailbox_get_show(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->show : LB_MAILBOX_SHOW_UNSET;
+}
+
+gboolean
+balsa_mailbox_get_exposed(LibBalsaMailbox * mailbox, gboolean exposed)
+{
+    return mailbox->view ? mailbox->view->exposed : FALSE;
+}
+
+gboolean
+balsa_mailbox_get_open(LibBalsaMailbox * mailbox)
+{
+    return mailbox->view ? mailbox->view->open : FALSE;
+}
Index: src/balsa-app.h
===================================================================
RCS file: /cvs/gnome/balsa/src/balsa-app.h,v
retrieving revision 1.192
diff -u -r1.192 balsa-app.h
--- src/balsa-app.h	23 Jul 2003 22:35:54 -0000	1.192
+++ src/balsa-app.h	25 Aug 2003 12:44:20 -0000
@@ -423,4 +423,33 @@
 void balsa_mailbox_nodes_lock(gboolean exclusive);
 void balsa_mailbox_nodes_unlock(gboolean exclusive);
 
+gboolean balsa_mailbox_set_identity_name(LibBalsaMailbox * mailbox,
+					 const gchar * identity_name);
+void balsa_mailbox_set_threading_type(LibBalsaMailbox * mailbox,
+				      LibBalsaMailboxThreadingType
+				      threading_type);
+void balsa_mailbox_set_sort_type(LibBalsaMailbox * mailbox,
+				 LibBalsaMailboxSortType sort_type);
+void balsa_mailbox_set_sort_field(LibBalsaMailbox * mailbox,
+				  LibBalsaMailboxSortFields sort_field);
+gboolean balsa_mailbox_set_show(LibBalsaMailbox * mailbox,
+				LibBalsaMailboxShow show);
+void balsa_mailbox_set_exposed(LibBalsaMailbox * mailbox,
+			       gboolean exposed);
+void balsa_mailbox_set_open(LibBalsaMailbox * mailbox, gboolean open);
+
+LibBalsaAddress *balsa_mailbox_get_mailing_list_address(LibBalsaMailbox *
+							mailbox);
+const gchar *balsa_mailbox_get_identity_name(LibBalsaMailbox * mailbox);
+LibBalsaMailboxThreadingType
+balsa_mailbox_get_threading_type(LibBalsaMailbox * mailbox);
+LibBalsaMailboxSortType balsa_mailbox_get_sort_type(LibBalsaMailbox *
+						    mailbox);
+LibBalsaMailboxSortFields balsa_mailbox_get_sort_field(LibBalsaMailbox *
+						       mailbox);
+LibBalsaMailboxShow balsa_mailbox_get_show(LibBalsaMailbox * mailbox);
+gboolean balsa_mailbox_get_exposed(LibBalsaMailbox * mailbox,
+				   gboolean exposed);
+gboolean balsa_mailbox_get_open(LibBalsaMailbox * mailbox);
+
 #endif				/* __BALSA_APP_H__ */
Index: src/balsa-index.c
===================================================================
RCS file: /cvs/gnome/balsa/src/balsa-index.c,v
retrieving revision 1.284
diff -u -r1.284 balsa-index.c
--- src/balsa-index.c	30 Jul 2003 12:13:31 -0000	1.284
+++ src/balsa-index.c	25 Aug 2003 12:44:21 -0000
@@ -480,40 +480,40 @@
 static void
 bndx_column_click(GtkTreeViewColumn * column, gpointer data)
 {
-    LibBalsaMailboxView *view;
+    LibBalsaMailbox *mailbox;
     GtkTreeView *tree_view = GTK_TREE_VIEW(data);
     GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
     gint col_id;
-    GtkSortType gtk_sort;
+    GtkSortType sort_type;
 
-    view = BALSA_INDEX(tree_view)->mailbox_node->mailbox->view;
+    mailbox = BALSA_INDEX(tree_view)->mailbox_node->mailbox;
     gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(model),
-                                         &col_id, &gtk_sort);
+					 &col_id, &sort_type);
 
     switch ((BndxTreeColumnId)col_id) {
     case BNDX_TREE_COLUMN_NO:
-        view->sort_field = LB_MAILBOX_SORT_NO;
+	balsa_mailbox_set_sort_field(mailbox, LB_MAILBOX_SORT_NO);
         break;
     case BNDX_TREE_COLUMN_SENDER:
-        view->sort_field = LB_MAILBOX_SORT_SENDER;
+	balsa_mailbox_set_sort_field(mailbox, LB_MAILBOX_SORT_SENDER);
         break;
     case BNDX_TREE_COLUMN_SUBJECT:
-        view->sort_field = LB_MAILBOX_SORT_SUBJECT;
+	balsa_mailbox_set_sort_field(mailbox, LB_MAILBOX_SORT_SUBJECT);
         break;
     case BNDX_TREE_COLUMN_DATE:
-        view->sort_field = LB_MAILBOX_SORT_DATE;
+	balsa_mailbox_set_sort_field(mailbox, LB_MAILBOX_SORT_DATE);
         break;
     case BNDX_TREE_COLUMN_SIZE:
-        view->sort_field = LB_MAILBOX_SORT_SIZE;
+	balsa_mailbox_set_sort_field(mailbox, LB_MAILBOX_SORT_SIZE);
         break;
     default:
-        view->sort_field = LB_MAILBOX_SORT_NATURAL;
+	balsa_mailbox_set_sort_field(mailbox, LB_MAILBOX_SORT_NATURAL);
         break;
     }
 
-    view->sort_type =
-        (gtk_sort == GTK_SORT_DESCENDING) ? LB_MAILBOX_SORT_TYPE_DESC
-                                          : LB_MAILBOX_SORT_TYPE_ASC;
+    balsa_mailbox_set_sort_type(mailbox, (sort_type == GTK_SORT_DESCENDING)
+				? LB_MAILBOX_SORT_TYPE_DESC
+				: LB_MAILBOX_SORT_TYPE_ASC);
 
     bndx_changed_find_row(BALSA_INDEX(tree_view));
     bndx_check_visibility(BALSA_INDEX(tree_view));
@@ -839,7 +839,7 @@
     /*
      * rename "from" column to "to" for outgoing mail
      */
-    if (mailbox->view->show == LB_MAILBOX_SHOW_TO) {
+    if (balsa_mailbox_get_show(mailbox) == LB_MAILBOX_SHOW_TO) {
         GtkTreeViewColumn *column = gtk_tree_view_get_column(tree_view, 3);
 
         gtk_tree_view_column_set_title(column, _("To"));
@@ -857,7 +857,7 @@
 
     /* Set the tree store, load messages, and do threading. The ref
      * table will be populated during this threading. */
-    bndx_load_and_thread(index, mailbox->view->threading_type);
+    bndx_load_and_thread(index, balsa_mailbox_get_threading_type(mailbox));
 
     bndx_moveto(index);
 
@@ -1492,8 +1492,10 @@
 	if (msg->mailbox)
 	    bndx_add_message(bindex, msg);
     }
-    balsa_index_threading(bindex, 
-			  bindex->mailbox_node->mailbox->view->threading_type);
+    balsa_index_threading(bindex,
+			  balsa_mailbox_get_threading_type(bindex->
+							   mailbox_node->
+							   mailbox));
     for (list = messages; list; list = g_list_next(list)) {
         LibBalsaMessage *msg = (LibBalsaMessage *) list->data;
         GtkTreeIter iter;
@@ -2088,7 +2090,8 @@
     g_return_if_fail(mailbox != NULL);
 
     if (thtype == LB_MAILBOX_THREADING_FLAT
-        && mailbox->view->threading_type != LB_MAILBOX_THREADING_FLAT)
+        && balsa_mailbox_get_threading_type(mailbox)
+	!= LB_MAILBOX_THREADING_FLAT)
         /* Changing to flat: it's faster to reload from scratch. */
         bndx_load_and_thread(index, thtype);
     else
@@ -2461,7 +2464,7 @@
     num = g_strdup_printf("%ld", LIBBALSA_MESSAGE_GET_NO(message) + 1);
 
     append_dots = FALSE;
-    if (mailbox->view->show == LB_MAILBOX_SHOW_TO) {
+    if (balsa_mailbox_get_show(mailbox) == LB_MAILBOX_SHOW_TO) {
 	if (message->headers && message->headers->to_list) {
 	    list = g_list_first(message->headers->to_list);
 	    addy = list->data;
@@ -2601,7 +2604,9 @@
 
     /* rethread and select the next message */
     balsa_index_threading(index,
-                          index->mailbox_node->mailbox->view->threading_type);
+			  balsa_mailbox_get_threading_type(index->
+							   mailbox_node->
+							   mailbox));
     if (next_message)
         bndx_select_message(index, next_message);
     g_get_current_time (&index->last_use);
@@ -2702,8 +2707,8 @@
     for (list = mailbox->message_list; list; list = list->next)
         bndx_add_message(index, list->data);
     bndx_set_threading_type(index, thtype);
-    bndx_set_sort_order(index, mailbox->view->sort_field,
-                        mailbox->view->sort_type);
+    bndx_set_sort_order(index, balsa_mailbox_get_sort_field(mailbox),
+                        balsa_mailbox_get_sort_type(mailbox));
 }
 
 /* Set a tree store for the tree view, replacing the current one if
@@ -2827,8 +2832,8 @@
 		     order == LB_MAILBOX_SORT_TYPE_ASC);
 
     
-    index->mailbox_node->mailbox->view->sort_field = field;
-    index->mailbox_node->mailbox->view->sort_type  = order;
+    balsa_mailbox_set_sort_field(index->mailbox_node->mailbox, field);
+    balsa_mailbox_set_sort_type(index->mailbox_node->mailbox, order);
     
     switch(field) {
     case LB_MAILBOX_SORT_NO:      col_id = BNDX_TREE_COLUMN_NO;      break;
@@ -2847,7 +2852,7 @@
 static void
 bndx_set_threading_type(BalsaIndex * index, int thtype)
 {
-    index->mailbox_node->mailbox->view->threading_type = thtype;
+    balsa_mailbox_set_threading_type(index->mailbox_node->mailbox, thtype);
     balsa_index_threading(index, thtype);
 
     /* expand tree if specified in config */
Index: src/balsa-mblist.c
===================================================================
RCS file: /cvs/gnome/balsa/src/balsa-mblist.c,v
retrieving revision 1.154
diff -u -r1.154 balsa-mblist.c
--- src/balsa-mblist.c	27 May 2003 13:08:20 -0000	1.154
+++ src/balsa-mblist.c	25 Aug 2003 12:44:22 -0000
@@ -534,7 +534,7 @@
             gtk_tree_model_get(model, &child_iter,
                                MBNODE_COLUMN, &mbnode, -1);
             if (mbnode && mbnode->mailbox)
-                mbnode->mailbox->view->exposed = TRUE;
+		balsa_mailbox_set_exposed(mbnode->mailbox, TRUE);
         } while (gtk_tree_model_iter_next(model, &child_iter));
     }
 }
@@ -551,7 +551,7 @@
             gtk_tree_model_get(model, &child_iter,
                                MBNODE_COLUMN, &mbnode, -1);
             if (mbnode->mailbox)
-                mbnode->mailbox->view->exposed = FALSE;
+		balsa_mailbox_set_exposed(mbnode->mailbox, FALSE);
             bmbl_tree_collapse_helper(model, &child_iter);
         } while (gtk_tree_model_iter_next(model, &child_iter));
     }
@@ -1219,12 +1219,14 @@
 
             /* Make sure the show column is set before showing the
              * mailbox in the list. */
-            if (mailbox->view->show == LB_MAILBOX_SHOW_UNSET)
-                mailbox->view->show = ((   mailbox == balsa_app.sentbox
-                                        || mailbox == balsa_app.draftbox
-                                        || mailbox == balsa_app.outbox)
-                                       ? LB_MAILBOX_SHOW_TO
-                                       : LB_MAILBOX_SHOW_FROM);
+            if (balsa_mailbox_get_show(mailbox) == LB_MAILBOX_SHOW_UNSET)
+		balsa_mailbox_set_show(mailbox,
+				       mailbox == balsa_app.sentbox
+				       || mailbox == balsa_app.draftbox
+				       || mailbox == balsa_app.outbox
+				       ? LB_MAILBOX_SHOW_TO
+				       : LB_MAILBOX_SHOW_FROM);
+
 	}
 	g_signal_connect(G_OBJECT(mailbox),
 			 "set-unread-messages-flag",
Index: src/mailbox-conf.c
===================================================================
RCS file: /cvs/gnome/balsa/src/mailbox-conf.c,v
retrieving revision 1.142
diff -u -r1.142 mailbox-conf.c
--- src/mailbox-conf.c	14 Aug 2003 23:33:08 -0000	1.142
+++ src/mailbox-conf.c	25 Aug 2003 12:44:22 -0000
@@ -782,9 +782,6 @@
 
     mcw->mailbox = g_object_new(mcw->mailbox_type, NULL);
     mbnode = balsa_mailbox_node_new_from_mailbox(mcw->mailbox);
-    /* Now the mailbox has a view, so we can populate it, but it's not
-     * yet in balsa_app.mailbox_views, because the mailbox url isn't set.
-     */
     mailbox_conf_view_check(mcw->view_info, mcw->mailbox);
 
     if ( LIBBALSA_IS_MAILBOX_LOCAL(mcw->mailbox) ) {
@@ -830,6 +827,20 @@
 	g_assert_not_reached();
     }
 
+    if (!LIBBALSA_IS_MAILBOX_POP3(mcw->mailbox)) {
+	/* Mailbox must have an URL by now... */
+	g_assert(mcw->mailbox->url != NULL);
+	/* ...and a view... */
+	g_assert(mcw->mailbox->view != NULL);
+	/* ...and the view must be different from any that we picked up
+	 * from the config file. */
+	g_assert(mcw->mailbox->view !=
+		 g_hash_table_lookup(balsa_app.mailbox_views,
+				     mcw->mailbox->url));
+	g_hash_table_insert(balsa_app.mailbox_views,
+			    g_strdup(mcw->mailbox->url),
+			    mcw->mailbox->view);
+    }
 
     if(save_to_config)
 	config_mailbox_add(mcw->mailbox, NULL);
@@ -839,18 +850,9 @@
     if (LIBBALSA_IS_MAILBOX_POP3(mcw->mailbox))
 	/* redraw the pop3 server list */
 	update_mail_servers();
-    else {/* redraw the main mailbox list */
-	/* If the new mailbox is in the local mail tree, its view will
-	 * already be in the mailbox-views, in which case inserting it
-	 * again would cause the view to be freed, so we'd better
-	 * check... */
-	if (!g_hash_table_lookup(balsa_app.mailbox_views,
-				 mcw->mailbox->url))
-	    g_hash_table_insert(balsa_app.mailbox_views,
-				g_strdup(mcw->mailbox->url),
-				mcw->mailbox->view);
+    else
+	/* redraw the main mailbox list */
 	balsa_mblist_repopulate(balsa_app.mblist_tree_store);
-    }
 }
 
 /* Create a page for the type of mailbox... */
@@ -1158,8 +1160,8 @@
     gtk_table_attach(GTK_TABLE(table), box, 1, 2, row, row + 1,
                      GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
 
-    button = (view && view->show == LB_MAILBOX_SHOW_FROM
-              ? view_info->show_from : view_info->show_to);
+    button = (view && view->show == LB_MAILBOX_SHOW_TO
+              ? view_info->show_to : view_info->show_from);
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
 
     return view_info;
@@ -1176,38 +1178,32 @@
                         LibBalsaMailbox * mailbox)
 {
     gboolean changed;
-    LibBalsaMailboxView *view;
 
     g_return_if_fail(LIBBALSA_IS_MAILBOX(mailbox));
     if(view_info == NULL) /* POP3 mailboxes do not have view_info */
         return;
 
     changed = FALSE;
-    view = mailbox->view;
+
+    if (!mailbox->view)
+	mailbox->view = libbalsa_mailbox_view_new();
 
     if (view_info->identity_name) {
-        changed = (!view->identity_name
-                   || strcmp(view->identity_name,
-                             view_info->identity_name));
-        g_free(view->identity_name);
-        view->identity_name = view_info->identity_name;
+	changed =
+	    balsa_mailbox_set_identity_name(mailbox,
+					    view_info->identity_name);
+	g_free(view_info->identity_name);
         view_info->identity_name = NULL;
     }
 
-    if (gtk_toggle_button_get_active
-        (GTK_TOGGLE_BUTTON(view_info->show_from))) {
-        if (view->show != LB_MAILBOX_SHOW_FROM) {
-            view->show = LB_MAILBOX_SHOW_FROM;
-            changed = TRUE;
-        }
-    } else {
-        if (view->show != LB_MAILBOX_SHOW_TO) {
-            view->show = LB_MAILBOX_SHOW_TO;
-            changed = TRUE;
-        }
-    }
+    if (balsa_mailbox_set_show
+	(mailbox,
+	 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(view_info->show_from))
+	 ? LB_MAILBOX_SHOW_FROM
+	 : LB_MAILBOX_SHOW_TO))
+	changed = TRUE;
 
-    if (!changed || !view->open)
+    if (!changed || !balsa_mailbox_get_open(mailbox))
         return;
 
     balsa_mblist_close_mailbox(mailbox);
Index: src/mailbox-node.c
===================================================================
RCS file: /cvs/gnome/balsa/src/mailbox-node.c,v
retrieving revision 1.59
diff -u -r1.59 mailbox-node.c
--- src/mailbox-node.c	27 May 2003 15:55:17 -0000	1.59
+++ src/mailbox-node.c	25 Aug 2003 12:44:23 -0000
@@ -262,25 +262,16 @@
 load_mailbox_view(BalsaMailboxNode * mbnode)
 {
     LibBalsaMailbox *mailbox = mbnode->mailbox;
-    LibBalsaMailboxView *view;
 
-    if (!mailbox->url) {
-        /* The mailbox is being created: attach a view now, it will be
-         * inserted in balsa_app.mailbox_views later. */
-        mailbox->view = libbalsa_mailbox_view_new();
+    if (!mailbox->url)
         return;
-    }
 
-    view = g_hash_table_lookup(balsa_app.mailbox_views, mailbox->url);
-    if (!view) {
-        view = libbalsa_mailbox_view_new();
-        g_hash_table_insert(balsa_app.mailbox_views,
-                            g_strdup(mailbox->url), view);
-    }
-    if (view->exposed)
+    mailbox->view =
+	g_hash_table_lookup(balsa_app.mailbox_views, mailbox->url);
+
+    if (mailbox->view && mailbox->view->exposed)
         while ((mbnode = mbnode->parent))
             mbnode->expanded = TRUE;
-    mailbox->view = view;
 }
 
 static void
Index: src/main-window.c
===================================================================
RCS file: /cvs/gnome/balsa/src/main-window.c,v
retrieving revision 1.507
diff -u -r1.507 main-window.c
--- src/main-window.c	7 Aug 2003 21:01:42 -0000	1.507
+++ src/main-window.c	25 Aug 2003 12:44:24 -0000
@@ -1093,7 +1093,8 @@
         gtk_widget_set_sensitive(view_menu[view_menu_entries[i]].widget, enable);
 
     if (mailbox)
-        balsa_window_set_threading_menu(mailbox->view->threading_type);
+	balsa_window_set_threading_menu(balsa_mailbox_get_threading_type
+					(mailbox));
 }
 
 /*
@@ -1637,8 +1638,7 @@
     balsa_app.open_mailbox_list =
         g_list_prepend(balsa_app.open_mailbox_list, m);
     UNLOCK_OPEN_LIST;
-    if (m->view)
-        m->view->open = TRUE;
+    balsa_mailbox_set_open(m, TRUE);
 }
 static void unregister_open_mailbox(LibBalsaMailbox *m)
 {
@@ -1646,8 +1646,7 @@
     balsa_app.open_mailbox_list =
         g_list_remove(balsa_app.open_mailbox_list, m);
     UNLOCK_OPEN_LIST;
-    if (m->view)
-        m->view->open = FALSE;
+    balsa_mailbox_set_open(m, FALSE);
 }
 static gboolean is_open_mailbox(LibBalsaMailbox *m)
 {
Index: src/save-restore.c
===================================================================
RCS file: /cvs/gnome/balsa/src/save-restore.c,v
retrieving revision 1.259
diff -u -r1.259 save-restore.c
--- src/save-restore.c	23 Jul 2003 22:35:55 -0000	1.259
+++ src/save-restore.c	25 Aug 2003 12:44:24 -0000
@@ -42,6 +42,7 @@
 #define ADDRESS_BOOK_SECTION_PREFIX "address-book-"
 #define IDENTITY_SECTION_PREFIX "identity-"
 #define VIEW_SECTION_PREFIX "view-"
+#define VIEW_BY_URL_SECTION_PREFIX "viewByUrl-"
 
 static gint config_section_init(const char* section_prefix, 
 				gint (*cb)(const char*));
@@ -1480,27 +1481,32 @@
     }
 }
 
-void
-config_views_load(void)
+static void
+config_views_load_with_prefix(const gchar * prefix, gboolean compat)
 {
     void *iterator;
     gchar *key, *val, *tmp;
-    int pref_len = strlen(VIEW_SECTION_PREFIX);
+    int pref_len = strlen(prefix);
     int def;
 
     iterator = gnome_config_init_iterator_sections(BALSA_CONFIG_PREFIX);
     while ((iterator = gnome_config_iterator_next(iterator, &key, &val))) {
-	if (strncmp(key, VIEW_SECTION_PREFIX, pref_len) == 0) {
+	if (strncmp(key, prefix, pref_len) == 0) {
 	    gchar *url;
 	    tmp = g_strconcat(BALSA_CONFIG_PREFIX, key, "/", NULL);
 	    gnome_config_push_prefix(tmp);
 	    g_free(tmp);
-	    url = gnome_config_get_string_with_default("URL", &def);
-	    if (!def) {
+	    url = compat
+		? gnome_config_get_string_with_default("URL", &def)
+		: libbalsa_urldecode(&key[pref_len]);
+	    if (!compat || !def) {
                 LibBalsaMailboxView *view;
                 gchar *address;
 
                 view = libbalsa_mailbox_view_new();
+		/* In compatibility mode, mark as not in sync, to force
+		 * the view to be saved in the config file. */
+		view->in_sync = !compat;
                 g_hash_table_insert(balsa_app.mailbox_views,
                                     g_strdup(url), view);
 
@@ -1516,7 +1522,7 @@
                 view->threading_type =
                     gnome_config_get_int_with_default("Threading", &def);
                 if (def)
-                    view->threading_type = LB_MAILBOX_THREADING_SIMPLE;
+                    view->threading_type = balsa_app.threading_type;
 
                 view->sort_type =
                     gnome_config_get_int_with_default("SortType", &def);
@@ -1551,12 +1557,21 @@
     }
 }
 
+void
+config_views_load(void)
+{
+    /* Load old-style config sections in compatibility mode. */
+    config_views_load_with_prefix(VIEW_SECTION_PREFIX, TRUE);
+    config_views_load_with_prefix(VIEW_BY_URL_SECTION_PREFIX, FALSE);
+}
+
 /* config_views_save:
    iterates over all mailboxes and save the views.
 */
 static gboolean
-save_view(GNode * node, int *cnt)
+save_view(GNode * node)
 {
+    gchar *url_enc;
     gchar *prefix;
     LibBalsaMailboxView *view;
     BalsaMailboxNode* mn = BALSA_MAILBOX_NODE(node->data);
@@ -1565,28 +1580,38 @@
     if(!mn->mailbox) return FALSE;
 
     view = mn->mailbox->view;
-    prefix = g_strdup_printf("%s%d/",
-			     BALSA_CONFIG_PREFIX VIEW_SECTION_PREFIX, 
-			     ++(*cnt));
+    if (!view || view->in_sync)
+	return FALSE;
+    view->in_sync = TRUE;
+
+    url_enc = libbalsa_urlencode(mn->mailbox->url);
+    prefix = g_strconcat(BALSA_CONFIG_PREFIX VIEW_BY_URL_SECTION_PREFIX,
+			 url_enc, "/", NULL);
+    g_free(url_enc);
+
+    gnome_config_clean_section(prefix);
     gnome_config_push_prefix(prefix);
     g_free(prefix);
-    gnome_config_set_string("URL", mn->mailbox->url);
+
     if (view->mailing_list_address) {
        gchar* tmp = libbalsa_address_to_gchar(view->mailing_list_address, 0);
        gnome_config_set_string("MailingListAddress", tmp);
        g_free(tmp);
-    } else {
-       gnome_config_clean_key("MailingListAddress");
     }
-    if(view->identity_name)
+    if (view->identity_name)
        gnome_config_set_string("Identity", view->identity_name);
-    else gnome_config_clean_key("Identity");
-    gnome_config_set_int("Threading",   view->threading_type);
-    gnome_config_set_int("SortType",    view->sort_type);
-    gnome_config_set_int("SortField",   view->sort_field);
-    gnome_config_set_int("Show", view->show);
-    gnome_config_set_bool("Exposed", view->exposed);
-    gnome_config_set_bool("Open", view->open);
+    if (view->threading_type != balsa_app.threading_type)
+	gnome_config_set_int("Threading", view->threading_type);
+    if (view->sort_type != LB_MAILBOX_SORT_TYPE_ASC)
+	gnome_config_set_int("SortType", view->sort_type);
+    if (view->sort_field != LB_MAILBOX_SORT_DATE)
+	gnome_config_set_int("SortField", view->sort_field);
+    if (view->show != LB_MAILBOX_SHOW_FROM)
+	gnome_config_set_int("Show", view->show);
+    if (view->exposed)
+	gnome_config_set_bool("Exposed", TRUE);
+    if (view->open)
+	gnome_config_set_bool("Open", TRUE);
 
     gnome_config_pop_prefix();
     return FALSE;
@@ -1595,13 +1620,11 @@
 void
 config_views_save(void)
 {
-    int cnt = 0;
-
     config_clean_sections(VIEW_SECTION_PREFIX);
     /* save current */
     balsa_mailbox_nodes_lock(FALSE);
     g_node_traverse(balsa_app.mailbox_nodes, G_IN_ORDER, G_TRAVERSE_ALL, -1,
-		   (GNodeTraverseFunc) save_view, &cnt);
+		   (GNodeTraverseFunc) save_view, NULL);
     balsa_mailbox_nodes_unlock(FALSE);
 }
 
Index: src/sendmsg-window.c
===================================================================
RCS file: /cvs/gnome/balsa/src/sendmsg-window.c,v
retrieving revision 1.429
diff -u -r1.429 sendmsg-window.c
--- src/sendmsg-window.c	13 Aug 2003 22:16:04 -0000	1.429
+++ src/sendmsg-window.c	25 Aug 2003 12:44:26 -0000
@@ -2475,13 +2475,13 @@
 static gboolean
 set_identity_from_mailbox(BalsaSendmsg* bsmsg)
 {
-    gchar *identity;
+    const gchar *identity;
     LibBalsaMessage *message = bsmsg->orig_message;
     LibBalsaIdentity* ident;
     GList *ilist;
 
     if( message && message->mailbox && balsa_app.identities) {
-        identity = message->mailbox->view->identity_name;
+        identity = balsa_mailbox_get_identity_name(message->mailbox);
         if(!identity) return FALSE;
         for (ilist = balsa_app.identities;
              ilist != NULL;
@@ -4164,11 +4164,13 @@
 set_list_post_address(BalsaSendmsg * bsmsg)
 {
     LibBalsaMessage *message = bsmsg->orig_message;
+    LibBalsaAddress *mailing_list_address;
 
-    if (message->mailbox->view->mailing_list_address) {
+    mailing_list_address =
+	balsa_mailbox_get_mailing_list_address(message->mailbox);
+    if (mailing_list_address) {
         gchar *tmp =
-            libbalsa_address_to_gchar(message->mailbox->view->
-                                      mailing_list_address, 0);
+            libbalsa_address_to_gchar(mailing_list_address, 0);
  	libbalsa_utf8_sanitize(&tmp, balsa_app.convert_unknown_8bit,
  			       balsa_app.convert_unknown_8bit_codeset, NULL);
         gtk_entry_set_text(GTK_ENTRY(bsmsg->to[1]), tmp);
