[balsa] Remember and restore the order of open mailboxes



commit 0927a4503bf66f3f09964ad08b58ad6bd0e2a238
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue Feb 5 22:20:46 2019 -0500

    Remember and restore the order of open mailboxes
    
    Remember and restore the order of open mailboxes between sessions.
    
    * libbalsa/mailbox.c (libbalsa_mailbox_set_open): set the
      position to the unset value, -1, when a mailbox is closed;
      (libbalsa_mailbox_set_position): new method;
      (libbalsa_mailbox_get_position): ditto;
    * libbalsa/mailbox.h: new methods.
    * src/balsa-app.c (append_url_if_open): use the saved position
      of a mailbox when added it to the window;
    * src/main-window.c (balsa_window_new),
      (bw_real_open_mbnode_idle_cb), (bw_notebook_page_notify_cb):
    * src/save-restore.c (config_load_mailbox_view),
      (config_save_mailbox_view), (config_mailbox_was_exposed),
      (config_mailbox_get_int_property), (config_mailbox_get_position):
      save and restore it.

 ChangeLog          | 19 +++++++++++++++++++
 libbalsa/mailbox.c | 32 ++++++++++++++++++++++++++++++--
 libbalsa/mailbox.h |  3 +++
 src/balsa-app.c    | 10 +++++++---
 src/main-window.c  | 45 ++++++++++++++++++++++++++++++++++-----------
 src/save-restore.c | 41 ++++++++++++++++++++++++++++++++++++++---
 src/save-restore.h |  1 +
 7 files changed, 132 insertions(+), 19 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6e8e525cd..2e545ec65 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2019-02-05  Peter Bloomfield  <pbloomfield bellsouth net>
+
+       Remember and restore the order of open mailboxes between
+       sessions.
+
+       * libbalsa/mailbox.c (libbalsa_mailbox_set_open): set the
+       position to the unset value, -1, when a mailbox is closed;
+       (libbalsa_mailbox_set_position): new method;
+       (libbalsa_mailbox_get_position): ditto;
+       * libbalsa/mailbox.h: new methods.
+       * src/balsa-app.c (append_url_if_open): use the saved position
+       of a mailbox when added it to the window;
+       * src/main-window.c (balsa_window_new),
+       (bw_real_open_mbnode_idle_cb), (bw_notebook_page_notify_cb):
+       * src/save-restore.c (config_load_mailbox_view),
+       (config_save_mailbox_view), (config_mailbox_was_exposed),
+       (config_mailbox_get_int_property), (config_mailbox_get_position):
+       save and restore it.
+
 2019-02-04  Peter Bloomfield  <pbloomfield bellsouth net>
 
        * meson_options.txt: make 'autocrypt' a boolean option, default
diff --git a/libbalsa/mailbox.c b/libbalsa/mailbox.c
index 564347a46..1a48c8060 100644
--- a/libbalsa/mailbox.c
+++ b/libbalsa/mailbox.c
@@ -2316,7 +2316,8 @@ static LibBalsaMailboxView libbalsa_mailbox_view_default = {
 #endif
     -1,                         /* total messages      */
     -1,                         /* unread messages     */
-    0                           /* mod time             */
+    0,                          /* mod time             */
+    -1                          /* position             */
 };
 
 LibBalsaMailboxView *
@@ -2477,8 +2478,11 @@ libbalsa_mailbox_set_open(LibBalsaMailbox * mailbox, gboolean open)
 
     if (view->open != open) {
        view->open = open ? 1 : 0;
-       if (mailbox)
+       if (mailbox != NULL) {
+           if (!open)
+                view->position = -1;
            view->in_sync = 0;
+        }
     }
 }
 
@@ -2561,6 +2565,23 @@ libbalsa_mailbox_set_mtime(LibBalsaMailbox * mailbox, time_t mtime)
     }
 }
 
+void
+libbalsa_mailbox_set_position(LibBalsaMailbox * mailbox, gint position)
+{
+    LibBalsaMailboxView *view;
+
+    /* Changing the default is not allowed. */
+    g_return_if_fail(mailbox != NULL);
+
+    view = lbm_get_view(mailbox);
+    view->used = 1;
+
+    if (view->position != position) {
+       view->position = position;
+        view->in_sync = 0;
+    }
+}
+
 /* End of set methods. */
 
 /* Get methods; NULL mailbox is valid, and returns the default value. */
@@ -2675,6 +2696,13 @@ libbalsa_mailbox_get_mtime(LibBalsaMailbox * mailbox)
        mailbox->view->mtime : libbalsa_mailbox_view_default.mtime;
 }
 
+gint
+libbalsa_mailbox_get_position(LibBalsaMailbox * mailbox)
+{
+    return (mailbox && mailbox->view) ?
+       mailbox->view->position : libbalsa_mailbox_view_default.position;
+}
+
 /* End of get methods. */
 
 /* =================================================================== *
diff --git a/libbalsa/mailbox.h b/libbalsa/mailbox.h
index 890812b58..5dfd23d7a 100644
--- a/libbalsa/mailbox.h
+++ b/libbalsa/mailbox.h
@@ -184,6 +184,7 @@ struct _LibBalsaMailboxView {
     int unread;
     int total;
     time_t mtime;       /* Mailbox mtime when counts were cached. */
+    gint position;      /* Position in the notebook */
 };
 
 struct _LibBalsaMailbox {
@@ -556,6 +557,7 @@ gboolean libbalsa_mailbox_set_crypto_mode(LibBalsaMailbox * mailbox,
 void libbalsa_mailbox_set_unread(LibBalsaMailbox * mailbox, gint unread);
 void libbalsa_mailbox_set_total (LibBalsaMailbox * mailbox, gint total);
 void libbalsa_mailbox_set_mtime (LibBalsaMailbox * mailbox, time_t mtime);
+void libbalsa_mailbox_set_position(LibBalsaMailbox * mailbox, gint position);
 
 const gchar *libbalsa_mailbox_get_identity_name(LibBalsaMailbox * mailbox);
 LibBalsaMailboxThreadingType
@@ -577,6 +579,7 @@ LibBalsaChkCryptoMode libbalsa_mailbox_get_crypto_mode(LibBalsaMailbox * mailbox
 gint libbalsa_mailbox_get_unread(LibBalsaMailbox * mailbox);
 gint libbalsa_mailbox_get_total (LibBalsaMailbox * mailbox);
 time_t libbalsa_mailbox_get_mtime(LibBalsaMailbox * mailbox);
+gint libbalsa_mailbox_get_position(LibBalsaMailbox * mailbox);
 
 /** force update of given msgno */
 void libbalsa_mailbox_msgno_changed(LibBalsaMailbox  *mailbox, guint seqno);
diff --git a/src/balsa-app.c b/src/balsa-app.c
index 583e6093f..efcefc6af 100644
--- a/src/balsa-app.c
+++ b/src/balsa-app.c
@@ -489,10 +489,14 @@ append_url_if_open(const gchar * group, const gchar * encoded_url,
 
     url = libbalsa_urldecode(encoded_url);
 
-    if (config_mailbox_was_open(url))
-        g_ptr_array_add(array, url);
-    else
+    if (config_mailbox_was_open(url)) {
+        gint position;
+
+        position = config_mailbox_get_position(url);
+        g_ptr_array_insert(array, position, url);
+    } else {
         g_free(url);
+    }
 
     return FALSE;
 }
diff --git a/src/main-window.c b/src/main-window.c
index 47a34f68e..98d14bda9 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -171,6 +171,9 @@ static gboolean bw_notebook_drag_motion_cb(GtkWidget* widget,
                                            GdkDragContext* context,
                                            gint x, gint y, guint time,
                                            gpointer user_data);
+static void bw_notebook_page_notify_cb(GtkWidget  *child,
+                                       GParamSpec *child_property,
+                                       gpointer    user_data);
 
 
 static GtkWidget *bw_notebook_label_new (BalsaMailboxNode* mbnode);
@@ -2135,6 +2138,7 @@ balsa_window_new(GtkApplication *application)
 #endif
     GtkAdjustment *hadj, *vadj;
     GAction *action;
+    GtkWidget *notebook;
 
     /* Call to register custom balsa pixmaps with GNOME_STOCK_PIXMAPS
      * - allows for grey out */
@@ -2186,23 +2190,24 @@ balsa_window_new(GtkApplication *application)
 
     geometry_manager_attach(GTK_WINDOW(window), "MainWindow");
 
-    window->notebook = gtk_notebook_new();
-    gtk_notebook_set_show_tabs(GTK_NOTEBOOK(window->notebook),
+    window->notebook = notebook = gtk_notebook_new();
+    gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook),
                                balsa_app.show_notebook_tabs);
-    gtk_notebook_set_show_border (GTK_NOTEBOOK(window->notebook), FALSE);
-    gtk_notebook_set_scrollable (GTK_NOTEBOOK (window->notebook), TRUE);
-    g_signal_connect(G_OBJECT(window->notebook), "switch_page",
+    gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
+    gtk_notebook_set_scrollable(GTK_NOTEBOOK (notebook), TRUE);
+    g_signal_connect(notebook, "switch_page",
                      G_CALLBACK(bw_notebook_switch_page_cb), window);
-    gtk_drag_dest_set (GTK_WIDGET (window->notebook), GTK_DEST_DEFAULT_ALL,
+    gtk_drag_dest_set(notebook, GTK_DEST_DEFAULT_ALL,
                        notebook_drop_types, NUM_DROP_TYPES,
                        GDK_ACTION_DEFAULT | GDK_ACTION_COPY | GDK_ACTION_MOVE);
-    g_signal_connect(G_OBJECT (window->notebook), "drag-data-received",
+    g_signal_connect(notebook, "drag-data-received",
                      G_CALLBACK (bw_notebook_drag_received_cb), NULL);
-    g_signal_connect(G_OBJECT (window->notebook), "drag-motion",
+    g_signal_connect(notebook, "drag-motion",
                      G_CALLBACK (bw_notebook_drag_motion_cb), NULL);
-    balsa_app.notebook = window->notebook;
-    g_object_add_weak_pointer(G_OBJECT(window->notebook),
-                             (gpointer) &balsa_app.notebook);
+
+    balsa_app.notebook = notebook;
+    g_object_add_weak_pointer(G_OBJECT(notebook),
+                             (gpointer *) &balsa_app.notebook);
 
     window->preview = balsa_message_new();
     gtk_widget_hide(window->preview);
@@ -2820,6 +2825,8 @@ bw_real_open_mbnode_idle_cb(BalsaWindowRealOpenMbnodeInfo * info)
                                    GTK_POLICY_AUTOMATIC);
     gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(index));
     gtk_widget_show(scroll);
+    g_signal_connect(scroll, "child-notify::position",
+                     G_CALLBACK(bw_notebook_page_notify_cb), window->notebook);
     page_num = gtk_notebook_append_page(GTK_NOTEBOOK(window->notebook),
                                         scroll, label);
     gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(window->notebook),
@@ -4349,6 +4356,22 @@ static gboolean bw_notebook_drag_motion_cb(GtkWidget * widget,
     return FALSE;
 }
 
+static void
+bw_notebook_page_notify_cb(GtkWidget  *child,
+                           GParamSpec *child_property,
+                           gpointer    user_data)
+{
+    GtkNotebook *notebook = user_data;
+    BalsaIndex *bindex;
+    LibBalsaMailbox *mailbox;
+    gint page_num;
+
+    page_num = gtk_notebook_page_num(notebook, child);
+    bindex = BALSA_INDEX(gtk_bin_get_child(GTK_BIN(child)));
+    mailbox = bindex->mailbox_node->mailbox;
+    libbalsa_mailbox_set_position(mailbox, page_num);
+}
+
 /* bw_progress_timeout
  *
  * This function is called at a preset interval to cause the progress
diff --git a/src/save-restore.c b/src/save-restore.c
index 23313fb9c..f1a20dd53 100644
--- a/src/save-restore.c
+++ b/src/save-restore.c
@@ -1783,6 +1783,9 @@ config_load_mailbox_view(const gchar * url)
     if (libbalsa_conf_has_key("Open"))
         view->open = libbalsa_conf_get_bool("Open");
 
+    if (libbalsa_conf_has_key("Position"))
+        view->position = libbalsa_conf_get_bool("Position");
+
 #ifdef HAVE_GPGME
     if (libbalsa_conf_has_key("CryptoMode"))
         view->gpg_chk_mode = libbalsa_conf_get_int("CryptoMode");
@@ -1836,9 +1839,12 @@ config_save_mailbox_view(const gchar * url, LibBalsaMailboxView * view)
        libbalsa_conf_set_int("Subscribe",   view->subscribe);
     if (view->exposed        != libbalsa_mailbox_get_exposed(NULL))
        libbalsa_conf_set_bool("Exposed",    view->exposed);
-    if (balsa_app.remember_open_mboxes &&
-        view->open           != libbalsa_mailbox_get_open(NULL))
-       libbalsa_conf_set_bool("Open",       view->open);
+    if (balsa_app.remember_open_mboxes) {
+        if (view->open       != libbalsa_mailbox_get_open(NULL))
+            libbalsa_conf_set_bool("Open",   view->open);
+        if (view->position   != libbalsa_mailbox_get_position(NULL))
+            libbalsa_conf_set_int("Position", view->position);
+    }
 #ifdef HAVE_GPGME
     if (view->gpg_chk_mode   != libbalsa_mailbox_get_crypto_mode(NULL))
        libbalsa_conf_set_int("CryptoMode",  view->gpg_chk_mode);
@@ -2111,3 +2117,32 @@ config_mailbox_was_exposed(const gchar * url)
 {
     return config_mailbox_had_property(url, "Exposed");
 }
+
+static gint
+config_mailbox_get_int_property(const gchar * url, const gchar * key)
+{
+    gchar *prefix;
+    gint retval = -1;
+
+    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_int(key);
+
+    libbalsa_conf_pop_group();
+    g_free(prefix);
+
+    return retval;
+}
+
+gint
+config_mailbox_get_position(const gchar * url)
+{
+    return config_mailbox_get_int_property(url, "Position");
+}
diff --git a/src/save-restore.h b/src/save-restore.h
index adc2c9222..f6bb24587 100644
--- a/src/save-restore.h
+++ b/src/save-restore.h
@@ -65,6 +65,7 @@ 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);
+gint config_mailbox_get_position(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]