[balsa/gtk3] Streamline handling of LibBalsaMailboxView



commit 32a07c816ac344bcc36ba3e675738dfd77d1caa1
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Thu Mar 14 15:33:11 2013 -0400

    Streamline handling of LibBalsaMailboxView
    
        * libbalsa/mailbox.c (libbalsa_mailbox_finalize): free the view;
        (libbalsa_mailbox_view_free), (lbm_get_view): check for NULL
        view;
        (libbalsa_mailbox_set_identity_name),
        (libbalsa_mailbox_set_threading_type),
        (libbalsa_mailbox_set_sort_type),
        (libbalsa_mailbox_set_sort_field), (libbalsa_mailbox_set_show),
        (libbalsa_mailbox_set_subscribe), (libbalsa_mailbox_set_exposed),
        (libbalsa_mailbox_set_open), (libbalsa_mailbox_set_filter),
        (libbalsa_mailbox_set_unread), (libbalsa_mailbox_set_total),
        (libbalsa_mailbox_set_mtime), (libbalsa_mailbox_get_filter):
        drop LibBalsaMailboxView::frozen and the hash table.
        * libbalsa/mailbox.h: ditto.
        * src/balsa-app.c (balsa_app_destroy), (append_url_if_open),
        (open_mailbox_by_url), (open_mailboxes_idle_cb),
        (balsa_add_open_mailbox_urls): traverse views in the config
        instead of in the hash table.
        * src/mailbox-conf.c (mailbox_conf_add),
        (mailbox_conf_view_check): ditto.
        * src/mailbox-node.c (balsa_mailbox_node_dispose),
        (check_url_func), (check_local_path), (load_mailbox_view),
        (check_imap_path): ditto.
        * src/pref-manager.c (update_view_defaults), (apply_prefs):
        ditto.
        * src/main.c (real_main), (balsa_cleanup): no hash table.
        * src/save-restore.c (config_view_remove),
        (config_defclient_save): no hash table.
        (config_mailbox_had_property), (config_mailbox_was_open),
        (config_mailbox_was_exposed), (config_load_mailbox_view),
        (config_save_mailbox_view): new methods.
        * src/save-restore.h: ditto.

 ChangeLog          |   36 +++++++++
 libbalsa/mailbox.c |   73 +++++--------------
 libbalsa/mailbox.h |    5 --
 src/balsa-app.c    |   27 ++++---
 src/mailbox-conf.c |   34 ++-------
 src/mailbox-node.c |   51 ++++++--------
 src/main.c         |    8 --
 src/pref-manager.c |   29 ++++++--
 src/save-restore.c |  201 +++++++++++++++++++++++++---------------------------
 src/save-restore.h |    9 ++-
 10 files changed, 228 insertions(+), 245 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9fa09c9..f8f3072 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,41 @@
 2013-03-14  Peter Bloomfield
 
+       Streamline handling of LibBalsaMailboxView
+
+       * libbalsa/mailbox.c (libbalsa_mailbox_finalize): free the view;
+       (libbalsa_mailbox_view_free), (lbm_get_view): check for NULL
+       view;
+       (libbalsa_mailbox_set_identity_name),
+       (libbalsa_mailbox_set_threading_type),
+       (libbalsa_mailbox_set_sort_type),
+       (libbalsa_mailbox_set_sort_field), (libbalsa_mailbox_set_show),
+       (libbalsa_mailbox_set_subscribe), (libbalsa_mailbox_set_exposed),
+       (libbalsa_mailbox_set_open), (libbalsa_mailbox_set_filter),
+       (libbalsa_mailbox_set_unread), (libbalsa_mailbox_set_total),
+       (libbalsa_mailbox_set_mtime), (libbalsa_mailbox_get_filter):
+       drop LibBalsaMailboxView::frozen and the hash table.
+       * libbalsa/mailbox.h: ditto.
+       * src/balsa-app.c (balsa_app_destroy), (append_url_if_open),
+       (open_mailbox_by_url), (open_mailboxes_idle_cb),
+       (balsa_add_open_mailbox_urls): traverse views in the config
+       instead of in the hash table.
+       * src/mailbox-conf.c (mailbox_conf_add),
+       (mailbox_conf_view_check): ditto.
+       * src/mailbox-node.c (balsa_mailbox_node_dispose),
+       (check_url_func), (check_local_path), (load_mailbox_view),
+       (check_imap_path): ditto.
+       * src/pref-manager.c (update_view_defaults), (apply_prefs):
+       ditto.
+       * src/main.c (real_main), (balsa_cleanup): no hash table.
+       * src/save-restore.c (config_view_remove),
+       (config_defclient_save): no hash table.
+       (config_mailbox_had_property), (config_mailbox_was_open),
+       (config_mailbox_was_exposed), (config_load_mailbox_view),
+       (config_save_mailbox_view): new methods.
+       * src/save-restore.h: ditto.
+
+2013-03-14  Peter Bloomfield
+
        * libbalsa/libbalsa-conf.c (libbalsa_conf_has_key): new
        function.
        * libbalsa/libbalsa-conf.h: ditto.
diff --git a/libbalsa/mailbox.c b/libbalsa/mailbox.c
index eae27c8..d4956ea 100644
--- a/libbalsa/mailbox.c
+++ b/libbalsa/mailbox.c
@@ -491,7 +491,7 @@ libbalsa_mailbox_finalize(GObject * object)
     }
 #endif                          /*BALSA_USE_THREADS */
 
-    /* The LibBalsaMailboxView is owned by balsa_app.mailbox_views. */
+    libbalsa_mailbox_view_free(mailbox->view);
     mailbox->view = NULL;
 
     if (mailbox->changed_idle_id) {
@@ -2355,7 +2355,6 @@ libbalsa_mailbox_set_threading(LibBalsaMailbox *mailbox,
  * Mailbox view methods                                                *
  * =================================================================== */
 
-GHashTable *libbalsa_mailbox_view_table;
 static LibBalsaMailboxView libbalsa_mailbox_view_default = {
     NULL,                      /* mailing_list_address */
     NULL,                      /* identity_name        */
@@ -2369,7 +2368,6 @@ static LibBalsaMailboxView libbalsa_mailbox_view_default = {
     0,                         /* exposed              */
     0,                         /* open                 */
     1,                         /* in_sync              */
-    0,                         /* frozen               */
     0,                         /* used                 */
 #ifdef HAVE_GPGME
     LB_MAILBOX_CHK_CRYPT_MAYBE, /* gpg_chk_mode         */
@@ -2393,6 +2391,9 @@ libbalsa_mailbox_view_new(void)
 void
 libbalsa_mailbox_view_free(LibBalsaMailboxView * view)
 {
+    if (!view)
+        return;
+
     if (view->mailing_list_address)
         g_object_unref(view->mailing_list_address);
     g_free(view->identity_name);
@@ -2403,24 +2404,13 @@ libbalsa_mailbox_view_free(LibBalsaMailboxView * view)
 static LibBalsaMailboxView *
 lbm_get_view(LibBalsaMailbox * mailbox)
 {
-    LibBalsaMailboxView *view;
-
     if (!mailbox)
        return &libbalsa_mailbox_view_default;
 
-    view = mailbox->view;
-    if (!view) {
-        view =
-            g_hash_table_lookup(libbalsa_mailbox_view_table, mailbox->url);
-        if (!view) {
-            view = libbalsa_mailbox_view_new();
-            g_hash_table_insert(libbalsa_mailbox_view_table,
-                                g_strdup(mailbox->url), view);
-        }
-        mailbox->view = view;
-    }
+    if (!mailbox->view)
+        mailbox->view = libbalsa_mailbox_view_new();
 
-    return view;
+    return mailbox->view;
 }
 
 /* Set methods; NULL mailbox is valid, and changes the default value. */
@@ -2431,9 +2421,7 @@ libbalsa_mailbox_set_identity_name(LibBalsaMailbox * mailbox,
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen
-       && (!view->identity_name
-           || strcmp(view->identity_name, identity_name))) {
+    if (!view->identity_name || strcmp(view->identity_name, identity_name)) {
        g_free(view->identity_name);
        view->identity_name = g_strdup(identity_name);
        if (mailbox)
@@ -2450,7 +2438,7 @@ libbalsa_mailbox_set_threading_type(LibBalsaMailbox * mailbox,
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->threading_type != threading_type) {
+    if (view->threading_type != threading_type) {
        view->threading_type = threading_type;
        if (mailbox)
            view->in_sync = 0;
@@ -2463,7 +2451,7 @@ libbalsa_mailbox_set_sort_type(LibBalsaMailbox * mailbox,
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->sort_type != sort_type) {
+    if (view->sort_type != sort_type) {
        view->sort_type = sort_type;
        if (mailbox)
            view->in_sync = 0;
@@ -2476,7 +2464,7 @@ libbalsa_mailbox_set_sort_field(LibBalsaMailbox * mailbox,
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->sort_field != sort_field) {
+    if (view->sort_field != sort_field) {
        view->sort_field_prev = view->sort_field;
        view->sort_field = sort_field;
        if (mailbox)
@@ -2489,7 +2477,7 @@ libbalsa_mailbox_set_show(LibBalsaMailbox * mailbox, LibBalsaMailboxShow show)
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->show != show) {
+    if (view->show != show) {
        /* Don't set not in sync if we're just replacing UNSET with the
         * default. */
        if (mailbox && view->show != LB_MAILBOX_SHOW_UNSET)
@@ -2506,7 +2494,7 @@ libbalsa_mailbox_set_subscribe(LibBalsaMailbox * mailbox,
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->subscribe != subscribe) {
+    if (view->subscribe != subscribe) {
        /* Don't set not in sync if we're just replacing UNSET with the
         * default. */
        if (mailbox && view->subscribe != LB_MAILBOX_SUBSCRIBE_UNSET)
@@ -2522,7 +2510,7 @@ libbalsa_mailbox_set_exposed(LibBalsaMailbox * mailbox, gboolean exposed)
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->exposed != exposed) {
+    if (view->exposed != exposed) {
        view->exposed = exposed ? 1 : 0;
        if (mailbox)
            view->in_sync = 0;
@@ -2534,7 +2522,7 @@ libbalsa_mailbox_set_open(LibBalsaMailbox * mailbox, gboolean open)
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->open != open) {
+    if (view->open != open) {
        view->open = open ? 1 : 0;
        if (mailbox)
            view->in_sync = 0;
@@ -2546,29 +2534,13 @@ libbalsa_mailbox_set_filter(LibBalsaMailbox * mailbox, gint filter)
 {
     LibBalsaMailboxView *view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->filter != filter) {
+    if (view->filter != filter) {
        view->filter = filter;
        if (mailbox)
            view->in_sync = 0;
     }
 }
 
-/* Freeze or unfreeze the view: no changes are made while the view is
- * frozen;
- * - changing the default is not allowed;
- * - no action needed if the view is NULL. */
-void
-libbalsa_mailbox_set_frozen(LibBalsaMailbox * mailbox, gboolean frozen)
-{
-    LibBalsaMailboxView *view;
-
-    g_return_if_fail(mailbox != NULL);
-
-    view = mailbox->view;
-    if (view)
-       view->frozen = frozen ? 1 : 0;
-}
-
 #ifdef HAVE_GPGME
 gboolean 
 libbalsa_mailbox_set_crypto_mode(LibBalsaMailbox * mailbox,
@@ -2598,7 +2570,7 @@ libbalsa_mailbox_set_unread(LibBalsaMailbox * mailbox, gint unread)
     view = lbm_get_view(mailbox);
     view->used = 1;
 
-    if (!view->frozen && view->unread != unread) {
+    if (view->unread != unread) {
        view->unread = unread;
         view->in_sync = 0;
     }
@@ -2614,7 +2586,7 @@ libbalsa_mailbox_set_total(LibBalsaMailbox * mailbox, gint total)
 
     view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->total != total) {
+    if (view->total != total) {
        view->total = total;
         view->in_sync = 0;
     }
@@ -2630,7 +2602,7 @@ libbalsa_mailbox_set_mtime(LibBalsaMailbox * mailbox, time_t mtime)
 
     view = lbm_get_view(mailbox);
 
-    if (!view->frozen && view->mtime != mtime) {
+    if (view->mtime != mtime) {
        view->mtime = mtime;
         view->in_sync = 0;
     }
@@ -2715,13 +2687,6 @@ libbalsa_mailbox_get_filter(LibBalsaMailbox * mailbox)
        mailbox->view->filter : libbalsa_mailbox_view_default.filter;
 }
 
-gboolean
-libbalsa_mailbox_get_frozen(LibBalsaMailbox * mailbox)
-{
-    return (mailbox && mailbox->view) ?
-       mailbox->view->frozen : FALSE;
-}
-
 #ifdef HAVE_GPGME
 LibBalsaChkCryptoMode
 libbalsa_mailbox_get_crypto_mode(LibBalsaMailbox * mailbox)
diff --git a/libbalsa/mailbox.h b/libbalsa/mailbox.h
index 04ab65b..d53b15b 100644
--- a/libbalsa/mailbox.h
+++ b/libbalsa/mailbox.h
@@ -180,7 +180,6 @@ struct _LibBalsaMailboxView {
     gboolean exposed;
     gboolean open;
     gboolean in_sync;          /* view is in sync with config */
-    gboolean frozen;           /* don't update view if set    */
     gboolean used;             /* keep track of usage         */
 
 #ifdef HAVE_GPGME
@@ -538,8 +537,6 @@ void libbalsa_mailbox_unlink_and_prepend(LibBalsaMailbox * mailbox,
                                         GNode * node, GNode * parent);
 
 /* Mailbox views. */
-extern GHashTable *libbalsa_mailbox_view_table;
-
 LibBalsaMailboxView *libbalsa_mailbox_view_new(void);
 void libbalsa_mailbox_view_free(LibBalsaMailboxView * view);
 gboolean libbalsa_mailbox_set_identity_name(LibBalsaMailbox * mailbox,
@@ -560,7 +557,6 @@ void libbalsa_mailbox_set_exposed(LibBalsaMailbox * mailbox,
                                  gboolean exposed);
 void libbalsa_mailbox_set_open(LibBalsaMailbox * mailbox, gboolean open);
 void libbalsa_mailbox_set_filter(LibBalsaMailbox * mailbox, gint filter);
-void libbalsa_mailbox_set_frozen(LibBalsaMailbox * mailbox, gboolean frozen);
 #ifdef HAVE_GPGME
 gboolean libbalsa_mailbox_set_crypto_mode(LibBalsaMailbox * mailbox,
                                          LibBalsaChkCryptoMode gpg_chk_mode);
@@ -584,7 +580,6 @@ LibBalsaMailboxSubscribe libbalsa_mailbox_get_subscribe(LibBalsaMailbox *
 gboolean libbalsa_mailbox_get_exposed(LibBalsaMailbox * mailbox);
 gboolean libbalsa_mailbox_get_open(LibBalsaMailbox * mailbox);
 gint libbalsa_mailbox_get_filter(LibBalsaMailbox * mailbox);
-gboolean libbalsa_mailbox_get_frozen(LibBalsaMailbox * mailbox);
 #ifdef HAVE_GPGME
 LibBalsaChkCryptoMode libbalsa_mailbox_get_crypto_mode(LibBalsaMailbox * mailbox);
 #endif
diff --git a/src/balsa-app.c b/src/balsa-app.c
index cc0fd19..406d2ce 100644
--- a/src/balsa-app.c
+++ b/src/balsa-app.c
@@ -36,6 +36,7 @@
 #include <fcntl.h>
 
 #include "filter-funcs.h"
+#include "libbalsa-conf.h"
 #include "misc.h"
 #include "server.h"
 #include "smtp-server.h"
@@ -432,7 +433,6 @@ balsa_app_init(void)
 void
 balsa_app_destroy(void)
 {
-    config_views_save();
     config_save();
 
     g_list_foreach(balsa_app.address_book_list, (GFunc)g_object_unref, NULL);
@@ -495,12 +495,14 @@ update_timer(gboolean update, guint minutes)
    Release the passed argument when done.
  */
 
-static void
-append_url_if_open(const gchar * url, LibBalsaMailboxView * view,
+static gboolean
+append_url_if_open(const gchar * group, const gchar * url,
                    GPtrArray * array)
 {
-    if (view->open)
+    if (config_mailbox_was_open(url))
         g_ptr_array_add(array, g_strdup(url));
+
+    return FALSE;
 }
 
 static void
@@ -522,8 +524,7 @@ open_mailbox_by_url(const gchar * url, gboolean hidden)
             balsa_mblist_open_mailbox(mailbox);
     } else {
         /* Do not try to open it next time. */
-        LibBalsaMailboxView *view =
-            g_hash_table_lookup(libbalsa_mailbox_view_table, url);
+        LibBalsaMailboxView *view = config_load_mailbox_view(url);
         /* The mailbox may have been requested to be open because its
          * stored view might say so or the user requested it from the
          * command line - in which case, view may or may not be present.
@@ -531,6 +532,8 @@ open_mailbox_by_url(const gchar * url, gboolean hidden)
         if (view) {
             view->open = FALSE;
             view->in_sync = FALSE;
+            config_save_mailbox_view(url, view);
+            libbalsa_mailbox_view_free(view);
         }
         balsa_information(LIBBALSA_INFORMATION_WARNING,
                           _("Couldn't open mailbox \"%s\""), url);
@@ -544,12 +547,13 @@ open_mailboxes_idle_cb(gchar ** urls)
 
     gdk_threads_enter();
 
-    if (!urls && libbalsa_mailbox_view_table) {
+    if (!urls) {
         GPtrArray *array;
 
         array = g_ptr_array_new();
-        g_hash_table_foreach(libbalsa_mailbox_view_table,
-                             (GHFunc) append_url_if_open, array);
+        libbalsa_conf_foreach_group(VIEW_BY_URL_SECTION_PREFIX,
+                                    (LibBalsaConfForeachFunc)
+                                    append_url_if_open, array);
         g_ptr_array_add(array, NULL);
         urls = (gchar **) g_ptr_array_free(array, FALSE);
     }
@@ -576,8 +580,9 @@ open_mailboxes_idle_cb(gchar ** urls)
 void
 balsa_add_open_mailbox_urls(GPtrArray * url_array)
 {
-    g_hash_table_foreach(libbalsa_mailbox_view_table,
-                         (GHFunc) append_url_if_open, url_array);
+    libbalsa_conf_foreach_group(VIEW_BY_URL_SECTION_PREFIX,
+                                (LibBalsaConfForeachFunc)
+                                append_url_if_open, url_array);
 }
 
 GtkWidget *
diff --git a/src/mailbox-conf.c b/src/mailbox-conf.c
index dcefc46..972ab8c 100644
--- a/src/mailbox-conf.c
+++ b/src/mailbox-conf.c
@@ -1035,37 +1035,12 @@ mailbox_conf_add(MailboxConfWindow * mcw)
        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 if it's not already in the table, insert it. */
-       if (mcw->mailbox->view !=
-           g_hash_table_lookup(libbalsa_mailbox_view_table,
-                               mcw->mailbox->url))
-           g_hash_table_insert(libbalsa_mailbox_view_table,
-                               g_strdup(mcw->mailbox->url),
-                               mcw->mailbox->view);
-    }
-
     if(save_to_config)
        config_mailbox_add(mcw->mailbox, NULL);
 
     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(libbalsa_mailbox_view_table,
-                                mcw->mailbox->url))
-           g_hash_table_insert(libbalsa_mailbox_view_table,
-                               g_strdup(mcw->mailbox->url),
-                               mcw->mailbox->view);
-    }
 }
 
 /* Create a page for the type of mailbox... */
@@ -1595,10 +1570,13 @@ mailbox_conf_view_check(BalsaMailboxConfView * view_info,
 
     changed = FALSE;
 
-    if (!mailbox->view)
-       /* The mailbox may not have its URL yet, so we can't insert it
-        * into libbalsa_mailbox_view_table yet. */
+    g_warn_if_fail(mailbox->view == NULL);
+    g_print("%s set view on %s\n", __func__, mailbox->name);
+    mailbox->view = config_load_mailbox_view(mailbox->url);
+    if (!mailbox->view) {
+       /* The mailbox may not have its URL yet */
        mailbox->view = libbalsa_mailbox_view_new();
+    }
 
     active =
         gtk_combo_box_get_active(GTK_COMBO_BOX
diff --git a/src/mailbox-node.c b/src/mailbox-node.c
index f1df95c..78c0106 100644
--- a/src/mailbox-node.c
+++ b/src/mailbox-node.c
@@ -192,12 +192,13 @@ static void
 balsa_mailbox_node_dispose(GObject * object)
 {
     BalsaMailboxNode *mn = BALSA_MAILBOX_NODE(object);
+    LibBalsaMailbox *mailbox = mn->mailbox;
 
-    if(mn->mailbox) {
-       libbalsa_mailbox_set_frozen(mn->mailbox, TRUE);
+    if (mailbox) {
+        config_save_mailbox_view(mailbox->url, mailbox->view);
        if (balsa_app.main_window)
            balsa_window_close_mbnode(balsa_app.main_window, mn);
-       g_object_unref(mn->mailbox);
+       g_object_unref(mailbox);
        mn->mailbox = NULL;
     }
 
@@ -324,13 +325,17 @@ struct _CheckPathInfo {
     gboolean must_scan;
 };
 
-static void
-check_url_func(const gchar * url, LibBalsaMailboxView * view,
-              CheckPathInfo * cpi)
+static gboolean
+check_url_func(const gchar * group, const gchar * url, CheckPathInfo * cpi)
 {
-    if ((view->exposed || view->open) && !cpi->must_scan
-       && strncmp(url, cpi->url, strlen(cpi->url)) == 0)
-       cpi->must_scan = TRUE;
+    if (!cpi->must_scan && g_str_has_prefix(url, cpi->url)
+        && (config_mailbox_was_exposed(url)
+            || (balsa_app.remember_open_mboxes
+                && config_mailbox_was_open(url)))
+        )
+        cpi->must_scan = TRUE;
+
+    return FALSE;
 }
 
 static gboolean
@@ -348,13 +353,11 @@ check_local_path(const gchar * path, guint depth)
        /* Top level folder. */
        return TRUE;
 
-    if (!libbalsa_mailbox_view_table)
-        return FALSE;
-
     cpi.url = g_strconcat("file://", path, NULL);
     cpi.must_scan = FALSE;
-    g_hash_table_foreach(libbalsa_mailbox_view_table,
-                         (GHFunc) check_url_func, &cpi);
+    libbalsa_conf_foreach_group(VIEW_BY_URL_SECTION_PREFIX,
+                                (LibBalsaConfForeachFunc) check_url_func,
+                                &cpi);
     if(balsa_app.debug) 
        printf("check_local_path: path \"%s\" must_scan %d.\n",
                cpi.url, cpi.must_scan);
@@ -392,16 +395,8 @@ load_mailbox_view(BalsaMailboxNode * mbnode)
 {
     LibBalsaMailbox *mailbox = mbnode->mailbox;
 
-    if (!mailbox->url)
-       return;
-
-    mailbox->view =
-       g_hash_table_lookup(libbalsa_mailbox_view_table, mailbox->url);
-    if (libbalsa_mailbox_get_frozen(mailbox)
-       && libbalsa_mailbox_get_open(mailbox))
-       /* We are rescanning. */
-       balsa_window_open_mbnode(balsa_app.main_window, mbnode, TRUE);
-    libbalsa_mailbox_set_frozen(mailbox, FALSE);
+    if (!mailbox->view)
+        mailbox->view = config_load_mailbox_view(mailbox->url);
 }
 
 static gboolean
@@ -1406,13 +1401,11 @@ check_imap_path(const gchar *fn, LibBalsaServer * server, guint depth)
     if (depth < balsa_app.imap_scan_depth)
         return TRUE;
 
-    if (!libbalsa_mailbox_view_table)
-        return FALSE;
-
     cpi.url = libbalsa_imap_url(server, fn);
     cpi.must_scan = FALSE;
-    g_hash_table_foreach(libbalsa_mailbox_view_table,
-                         (GHFunc) check_url_func, &cpi);
+    libbalsa_conf_foreach_group(VIEW_BY_URL_SECTION_PREFIX,
+                                (LibBalsaConfForeachFunc) check_url_func,
+                                &cpi);
     if(balsa_app.debug) 
        printf("check_imap_path: path \"%s\" must_scan %d.\n",
                cpi.url, cpi.must_scan);
diff --git a/src/main.c b/src/main.c
index 74bc3a7..3d082ed 100644
--- a/src/main.c
+++ b/src/main.c
@@ -660,12 +660,6 @@ real_main(int argc, char *argv[])
     /* checking for valid config files */
     config_init(cmd_get_stats);
 
-    libbalsa_mailbox_view_table =
-       g_hash_table_new_full(g_str_hash, g_str_equal,
-                             (GDestroyNotify) g_free,
-                             (GDestroyNotify) libbalsa_mailbox_view_free);
-    config_views_load();
-
     default_icon = balsa_pixmap_finder("balsa_icon.png");
     if(default_icon) { /* may be NULL for developer installations */
         gtk_window_set_default_icon_from_file(default_icon, NULL);
@@ -746,8 +740,6 @@ balsa_cleanup(void)
     pthread_mutex_unlock(&checking_mail_lock);
 #endif
     balsa_app_destroy();
-    g_hash_table_destroy(libbalsa_mailbox_view_table);
-    libbalsa_mailbox_view_table = NULL;
 
     libbalsa_conf_drop_all();
 }
diff --git a/src/pref-manager.c b/src/pref-manager.c
index 907aa68..c0b7dca 100644
--- a/src/pref-manager.c
+++ b/src/pref-manager.c
@@ -785,18 +785,35 @@ destroy_pref_window_cb(void)
     already_open = FALSE;
 }
 
-    /* GHFunc callback; update any view that is using the current default
+    /* LibBalsaConfForeachFunc callback;
+     * update any view that is using the current default
      * value to the new default value. */
-static void
-update_view_defaults(const gchar * url, LibBalsaMailboxView * view,
+static gboolean
+update_view_defaults(const gchar * group, const gchar * url,
                      gpointer data)
 {
+    LibBalsaMailbox *mailbox;
+    LibBalsaMailboxView *view;
+
+    mailbox = balsa_find_mailbox_by_url(url);
+    view = mailbox ? mailbox->view : config_load_mailbox_view(url);
+
+    if (!view)
+        return FALSE;
+
     if (view->filter == libbalsa_mailbox_get_filter(NULL))
         view->filter = pui->filter;
     if (view->sort_field == libbalsa_mailbox_get_sort_field(NULL))
         view->sort_field = pui->sort_field_index;
     if (view->threading_type == libbalsa_mailbox_get_threading_type(NULL))
         view->threading_type = pui->threading_type_index;
+
+    if (!mailbox) {
+        config_save_mailbox_view(url, view);
+        libbalsa_mailbox_view_free(view);
+    }
+
+    return FALSE;
 }
 
 static void
@@ -836,8 +853,10 @@ apply_prefs(GtkDialog * pbox)
      * Before changing the default mailbox view, update any current
      * views that have default values.
      */
-    g_hash_table_foreach(libbalsa_mailbox_view_table,
-                         (GHFunc) update_view_defaults, NULL);
+    libbalsa_conf_foreach_group(VIEW_BY_URL_SECTION_PREFIX,
+                                (LibBalsaConfForeachFunc)
+                                update_view_defaults, NULL);
+
 
     g_free(balsa_app.local_mail_directory);
     balsa_app.local_mail_directory =
diff --git a/src/save-restore.c b/src/save-restore.c
index 312c75b..a868985 100644
--- a/src/save-restore.c
+++ b/src/save-restore.c
@@ -53,7 +53,6 @@
 #define ADDRESS_BOOK_SECTION_PREFIX "address-book-"
 #define IDENTITY_SECTION_PREFIX "identity-"
 #define VIEW_SECTION_PREFIX "view-"
-#define VIEW_BY_URL_SECTION_PREFIX "viewByUrl-"
 #define SMTP_SERVER_SECTION_PREFIX "smtp-server-"
 
 static gint config_global_load(void);
@@ -1738,126 +1737,97 @@ config_identities_save(void)
     }
 }
 
-static gboolean
-config_view_load(const gchar * key, const gchar * value, gpointer data)
+/* Get viewByUrl prefix */
+static gchar *
+view_by_url_prefix(const gchar * url)
 {
-    gboolean compat = GPOINTER_TO_INT(data);
-    gchar *url;
-    gint def;
+    gchar *url_enc;
+    gchar *prefix;
 
-    libbalsa_conf_push_group(key);
+    url_enc = libbalsa_urlencode(url);
+    prefix = g_strconcat(VIEW_BY_URL_SECTION_PREFIX, url_enc, NULL);
+    g_free(url_enc);
 
-    url = compat ? libbalsa_conf_get_string_with_default("URL", &def) :
-        libbalsa_urldecode(value);
+    return prefix;
+}
 
-    if (!compat || !def) {
-        LibBalsaMailboxView *view;
-        gint tmp;
-        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(libbalsa_mailbox_view_table, g_strdup(url),
-                            view);
+LibBalsaMailboxView *
+config_load_mailbox_view(const gchar * url)
+{
+    gchar *prefix;
+    LibBalsaMailboxView *view;
+
+    if (!url)
+        return NULL;
+
+    prefix = view_by_url_prefix(url);
+    if (!libbalsa_conf_has_group(prefix)) {
+        g_free(prefix);
+        return NULL;
+    }
+
+    libbalsa_conf_push_group(prefix);
+
+    view = libbalsa_mailbox_view_new();
+
+    if (libbalsa_conf_has_key("MailingListAddress")) {
+        gchar *address;
 
-        address =
-            libbalsa_conf_get_string_with_default("MailingListAddress",
-                                                  &def);
+        address = libbalsa_conf_get_string("MailingListAddress");
         view->mailing_list_address =
-            def ? NULL : internet_address_list_parse_string(address);
+            internet_address_list_parse_string(address);
         g_free(address);
+    }
+
+    view->identity_name = libbalsa_conf_get_string("Identity");
 
-        view->identity_name = libbalsa_conf_get_string("Identity");
+    if (libbalsa_conf_has_key("Threading"))
+        view->threading_type = libbalsa_conf_get_int("Threading");
 
-        tmp = libbalsa_conf_get_int_with_default("Threading", &def);
-        if (!def)
-            view->threading_type = tmp;
+    if (libbalsa_conf_has_key("GUIFilter"))
+        view->filter = libbalsa_conf_get_int("GUIFilter");
 
-        tmp = libbalsa_conf_get_int_with_default("GUIFilter", &def);
-        if (!def)
-            view->filter = tmp;
+    if (libbalsa_conf_has_key("SortType"))
+        view->sort_type = libbalsa_conf_get_int("SortType");
 
-        tmp = libbalsa_conf_get_int_with_default("SortType", &def);
-        if (!def)
-            view->sort_type = tmp;
+    if (libbalsa_conf_has_key("SortField"))
+        view->sort_field = libbalsa_conf_get_int("SortField");
 
-        tmp = libbalsa_conf_get_int_with_default("SortField", &def);
-        if (!def)
-            view->sort_field = tmp;
+    if (libbalsa_conf_has_key("Show"))
+        view->show = libbalsa_conf_get_int("Show");
 
-        tmp = libbalsa_conf_get_int_with_default("Show", &def);
-        if (!def)
-            view->show = tmp;
+    if (libbalsa_conf_has_key("Subscribe"))
+        view->subscribe = libbalsa_conf_get_int("Subscribe");
 
-        tmp = libbalsa_conf_get_int_with_default("Subscribe", &def);
-        if (!def)
-            view->subscribe = tmp;
+    if (libbalsa_conf_has_key("Exposed"))
+        view->exposed = libbalsa_conf_get_bool("Exposed");
 
-        tmp = libbalsa_conf_get_bool_with_default("Exposed", &def);
-        if (!def)
-            view->exposed = tmp;
+    if (libbalsa_conf_has_key("Open"))
+        view->open = libbalsa_conf_get_bool("Open");
 
-        tmp = libbalsa_conf_get_bool_with_default("Open", &def);
-        if (!def)
-            view->open = tmp;
 #ifdef HAVE_GPGME
-        tmp = libbalsa_conf_get_int_with_default("CryptoMode", &def);
-        if (!def)
-            view->gpg_chk_mode = tmp;
+    if (libbalsa_conf_has_key("CryptoMode"))
+        view->gpg_chk_mode = libbalsa_conf_get_int("CryptoMode");
 #endif
 
-        tmp = libbalsa_conf_get_int_with_default("Total", &def);
-        if (!def)
-            view->total = tmp;
+    if (libbalsa_conf_has_key("Total"))
+        view->total = libbalsa_conf_get_int("Total");
 
-        tmp = libbalsa_conf_get_int_with_default("Unread", &def);
-        if (!def)
-            view->unread = tmp;
+    if (libbalsa_conf_has_key("Unread"))
+        view->unread = libbalsa_conf_get_int("Unread");
 
-        tmp = libbalsa_conf_get_int_with_default("ModTime", &def);
-        if (!def)
-            view->mtime = tmp;
-    }
+    if (libbalsa_conf_has_key("ModTime"))
+        view->mtime = libbalsa_conf_get_int("ModTime");
 
     libbalsa_conf_pop_group();
-    g_free(url);
+    g_free(prefix);
 
-    return FALSE;
+    return view;
 }
 
 void
-config_views_load(void)
-{
-    /* Load old-style config sections in compatibility mode. */
-    libbalsa_conf_foreach_group(VIEW_SECTION_PREFIX,
-                                config_view_load,
-                                GINT_TO_POINTER(TRUE));
-    libbalsa_conf_foreach_group(VIEW_BY_URL_SECTION_PREFIX,
-                                config_view_load,
-                                GINT_TO_POINTER(FALSE));
-}
-
-/* Get viewByUrl prefix */
-static gchar *
-view_by_url_prefix(const gchar * url)
-{
-    gchar *url_enc;
-    gchar *prefix;
-
-    url_enc = libbalsa_urlencode(url);
-    prefix = g_strconcat(VIEW_BY_URL_SECTION_PREFIX, url_enc, NULL);
-    g_free(url_enc);
-
-    return prefix;
-}
-
-/* config_views_save:
-   iterates over all mailbox views.
-*/
-static void
-save_view(const gchar * url, LibBalsaMailboxView * view)
+config_save_mailbox_view(const gchar * url, LibBalsaMailboxView * view)
 {
     gchar *prefix;
 
@@ -1924,21 +1894,11 @@ save_view(const gchar * url, LibBalsaMailboxView * view)
 }
 
 void
-config_views_save(void)
-{
-    config_remove_groups(VIEW_SECTION_PREFIX);
-    /* save current */
-    g_hash_table_foreach(libbalsa_mailbox_view_table, (GHFunc) save_view,
-                        NULL);
-}
-
-void
 config_view_remove(const gchar * url)
 {
     gchar *prefix = view_by_url_prefix(url);
     libbalsa_conf_remove_group(prefix);
     g_free(prefix);
-    g_hash_table_remove(libbalsa_mailbox_view_table, url);
 }
 
 static void
@@ -2147,3 +2107,38 @@ config_defclient_save(void)
     }
     g_object_unref(info);
 }
+
+static gboolean
+config_mailbox_had_property(const gchar * url, const gchar * key)
+{
+    gchar *prefix;
+    gboolean retval = FALSE;
+
+    prefix = view_by_url_prefix(url);
+    if (!libbalsa_conf_has_group(prefix)) {
+        g_free(prefix);
+        return retval;
+    }
+
+    libbalsa_conf_push_group(prefix);
+
+    if (libbalsa_conf_has_key(key))
+        retval = libbalsa_conf_get_bool(key);
+
+    libbalsa_conf_pop_group();
+    g_free(prefix);
+
+    return retval;
+}
+
+gboolean
+config_mailbox_was_open(const gchar * url)
+{
+    return config_mailbox_had_property(url, "Open");
+}
+
+gboolean
+config_mailbox_was_exposed(const gchar * url)
+{
+    return config_mailbox_had_property(url, "Exposed");
+}
diff --git a/src/save-restore.h b/src/save-restore.h
index 3481078..4878856 100644
--- a/src/save-restore.h
+++ b/src/save-restore.h
@@ -29,6 +29,8 @@
 #include "libbalsa.h"
 #include "mailbox-node.h"
 
+#define VIEW_BY_URL_SECTION_PREFIX "viewByUrl-"
+
 typedef enum {
     SPECIAL_INBOX = 0,
     SPECIAL_SENT,
@@ -59,9 +61,12 @@ void config_address_book_delete(LibBalsaAddressBook * ab);
 void config_address_books_load(void);
 
 void config_identities_save(void);
-void config_views_load(void);
-void config_views_save(void);
 void config_view_remove(const gchar * url);
+LibBalsaMailboxView *config_load_mailbox_view(const gchar * url);
+void config_save_mailbox_view(const gchar * url, LibBalsaMailboxView * view);
+
+gboolean config_mailbox_was_open(const gchar * url);
+gboolean config_mailbox_was_exposed(const gchar * url);
 
 void config_filters_save(void);
 void config_mailbox_filters_save(LibBalsaMailbox * mbox);


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