[balsa] Stop using gtk_main_iteration_do()



commit e556a72068b2f9eb4acf0d56cf33f3eaedc0a522
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sun Jan 12 18:09:44 2020 -0500

    Stop using gtk_main_iteration_do()
    
    Stop using gtk_main_iteration_do() to make the progress bar
    update. It caused all sorts of side effects by processing other events
    while the one needed the progress indicator was held up. Several
    hacky guards against bad consequences can be dropped.
    
    The progress bar is now not updated in the UI if it is used to
    indicate progress in a task that is executed in the main thread.
    A task that takes long enough to need it most likely needs to be
    carried out in a thread, anyway, and should be re-implemented as
    such.
    
    Scroll-on-open apparently depended on some of the less bad
    consequences, and to make it work again the order of execution
    of idle handlers for:
    • message loading;
    • message threading;
    • optional thread expansion;
    has to be managed.
    
    * libbalsa/mailbox.c (lbm_changed_idle_cb),
      (lbm_run_filters_on_reception_idle_cb): drop the guards;
    * libbalsa/mailbox_local.c (lbml_set_threading),
      (lbml_set_threading_idle_cb),
      (libbalsa_mailbox_local_set_threading),
      (lbm_local_update_view_filter),
      (libbalsa_mailbox_local_prepare_threading): drop guards, and
      manage order of execution;
    * src/balsa-index.c (balsa_index_init), (bndx_scroll_on_open_idle),
      (balsa_index_scroll_on_open): manage order of execution;
    * src/main-window.c (balsa_window_progress_bar_set_fraction):
      this was misleadingly named;
    * src/main-window.h: rename it;
    * src/main.c (balsa_progress_set_fraction): use the new name.

 ChangeLog                | 38 ++++++++++++++++++++++++++++++++++++++
 libbalsa/mailbox.c       | 30 ++++++++----------------------
 libbalsa/mailbox_local.c | 34 +++++++++++++---------------------
 src/balsa-index.c        | 18 ++++++++++++------
 src/main-window.c        | 18 +++---------------
 src/main-window.h        |  4 ++--
 src/main.c               |  3 +--
 7 files changed, 77 insertions(+), 68 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b73205500..cb7401bd2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,41 @@
+2020-01-12  Peter Bloomfield  <pbloomfield bellsouth net>
+
+       Stop using gtk_main_iteration_do()
+
+       Stop using gtk_main_iteration_do() to make the progress bar
+       update. It caused all sorts of side effects by processing other events
+       while the one needed the progress indicator was held up. Several
+       hacky guards against bad consequences can be dropped.
+
+       The progress bar is now not updated in the UI if it is used to
+       indicate progress in a task that is executed in the main thread.
+       A task that takes long enough to need it most likely needs to be
+       carried out in a thread, anyway, and should be re-implemented as
+       such.
+
+       Scroll-on-open apparently depended on some of the less bad
+       consequences, and to make it work again the order of execution
+       of idle handlers for:
+       • message loading;
+       • message threading;
+       • optional thread expansion;
+       has to be managed.
+
+       * libbalsa/mailbox.c (lbm_changed_idle_cb),
+       (lbm_run_filters_on_reception_idle_cb): drop the guards;
+       * libbalsa/mailbox_local.c (lbml_set_threading),
+       (lbml_set_threading_idle_cb),
+       (libbalsa_mailbox_local_set_threading),
+       (lbm_local_update_view_filter),
+       (libbalsa_mailbox_local_prepare_threading): drop guards, and
+       manage order of execution;
+       * src/balsa-index.c (balsa_index_init), (bndx_scroll_on_open_idle),
+       (balsa_index_scroll_on_open): manage order of execution;
+       * src/main-window.c (balsa_window_progress_bar_set_fraction):
+       this was misleadingly named;
+       * src/main-window.h: rename it;
+       * src/main.c (balsa_progress_set_fraction): use the new name.
+
 2020-01-10  Peter Bloomfield  <pbloomfield bellsouth net>
 
        mailbox: Check for finalized mailbox
diff --git a/libbalsa/mailbox.c b/libbalsa/mailbox.c
index 7efaba595..be43be74c 100644
--- a/libbalsa/mailbox.c
+++ b/libbalsa/mailbox.c
@@ -734,8 +734,7 @@ lbm_changed_idle_cb(LibBalsaMailbox * mailbox)
     libbalsa_lock_mailbox(mailbox);
     g_signal_emit(mailbox, libbalsa_mailbox_signals[CHANGED], 0);
     priv->changed_idle_id = 0;
-    if (LIBBALSA_IS_MAILBOX(mailbox))
-        libbalsa_unlock_mailbox(mailbox);
+    libbalsa_unlock_mailbox(mailbox);
 
     return G_SOURCE_REMOVE;
 }
@@ -843,8 +842,6 @@ lbm_run_filters_on_reception_idle_cb(LibBalsaMailbox * mailbox)
     guint total;
     guint progress_total;
     LibBalsaProgress progress;
-    gboolean is_mailbox = TRUE;
-    gboolean is_open = TRUE;
 
     libbalsa_lock_mailbox(mailbox);
 
@@ -917,6 +914,7 @@ lbm_run_filters_on_reception_idle_cb(LibBalsaMailbox * mailbox)
         libbalsa_condition_unref(cond);
 
         msgnos = g_array_new(FALSE, FALSE, sizeof(guint));
+
         for (msgno = 1; msgno <= total; msgno++) {
             if (libbalsa_mailbox_message_match(mailbox, msgno, search_iter))
                 g_array_append_val(msgnos, msgno);
@@ -925,35 +923,23 @@ lbm_run_filters_on_reception_idle_cb(LibBalsaMailbox * mailbox)
                                                ((gdouble) ++progress_count)
                                                /
                                                ((gdouble) progress_total));
-                /* mailbox could have been closed during set-fraction */
-                if (!LIBBALSA_IS_MAILBOX(mailbox))
-                    is_open = is_mailbox = FALSE;
-                else if (!MAILBOX_OPEN(mailbox))
-                    is_open = FALSE;
-
-                if (!is_open)
-                    break;
             }
         }
-        libbalsa_mailbox_search_iter_unref(search_iter);
 
-        if (is_open) {
-            libbalsa_mailbox_register_msgnos(mailbox, msgnos);
-            libbalsa_filter_mailbox_messages(filter, mailbox, msgnos);
-            libbalsa_mailbox_unregister_msgnos(mailbox, msgnos);
-        }
+        libbalsa_mailbox_register_msgnos(mailbox, msgnos);
+        libbalsa_filter_mailbox_messages(filter, mailbox, msgnos);
+        libbalsa_mailbox_unregister_msgnos(mailbox, msgnos);
 
         g_array_free(msgnos, TRUE);
 
-        if (!is_open)
-            break;
+        libbalsa_mailbox_search_iter_unref(search_iter);
     }
+
     libbalsa_progress_set_text(&progress, NULL, 0);
 
     g_slist_free(filters);
 
-    if (is_mailbox)
-        libbalsa_unlock_mailbox(mailbox);
+    libbalsa_unlock_mailbox(mailbox);
 
     return FALSE;
 }
diff --git a/libbalsa/mailbox_local.c b/libbalsa/mailbox_local.c
index 151691fe1..f5ea717e2 100644
--- a/libbalsa/mailbox_local.c
+++ b/libbalsa/mailbox_local.c
@@ -1133,9 +1133,6 @@ static void
 lbml_set_threading(LibBalsaMailbox * mailbox,
                    LibBalsaMailboxThreadingType thread_type)
 {
-    if (libbalsa_mailbox_get_msg_tree(mailbox) == NULL)
-        return;
-
     switch (thread_type) {
     case LB_MAILBOX_THREADING_JWZ:
         lbml_thread_messages(mailbox, TRUE);
@@ -1148,8 +1145,7 @@ lbml_set_threading(LibBalsaMailbox * mailbox,
         break;
     }
 
-    if (libbalsa_mailbox_get_messages_loaded(mailbox))
-        libbalsa_mailbox_set_messages_threaded(mailbox, TRUE);
+    libbalsa_mailbox_set_messages_threaded(mailbox, TRUE);
 }
 
 typedef struct {
@@ -1160,6 +1156,12 @@ typedef struct {
 static gboolean
 lbml_set_threading_idle_cb(LbmlSetThreadingInfo * info)
 {
+    if (libbalsa_mailbox_get_msg_tree(info->mailbox) == NULL)
+        return G_SOURCE_REMOVE;
+
+    if (!libbalsa_mailbox_get_messages_loaded(info->mailbox))
+        return G_SOURCE_CONTINUE;
+
     lbml_set_threading(info->mailbox, info->thread_type);
     g_object_unref(info->mailbox);
     g_free(info);
@@ -1215,16 +1217,15 @@ libbalsa_mailbox_local_set_threading(LibBalsaMailbox * mailbox,
         }
     }
 
-    if (libbalsa_am_i_subthread()) {
+    { /* Scope */
         LbmlSetThreadingInfo *info;
 
         info = g_new(LbmlSetThreadingInfo, 1);
         info->mailbox = g_object_ref(mailbox);
         info->thread_type = thread_type;
         g_idle_add((GSourceFunc) lbml_set_threading_idle_cb, info);
-    } else {
-        lbml_set_threading(mailbox, thread_type);
     }
+
 #if defined(DEBUG_LOADING_AND_THREADING)
     printf("after threading time=%lu\n", (unsigned long) time(NULL));
 #endif
@@ -1276,15 +1277,13 @@ lbm_local_update_view_filter(LibBalsaMailbox * mailbox,
                                           FALSE);
         libbalsa_progress_set_fraction(&progress, ((gdouble) msgno) /
                                        ((gdouble) total));
-        if (!LIBBALSA_IS_MAILBOX(mailbox) || !MAILBOX_OPEN(mailbox))
-            break;
     }
     libbalsa_progress_set_text(&progress, NULL, 0);
     libbalsa_mailbox_search_iter_unref(iter_view);
 
     /* If this is not a flags-only filter, the new mailbox tree is
      * temporary, so we don't want to save it. */
-    if (is_flag_only && LIBBALSA_IS_MAILBOX(mailbox))
+    if (is_flag_only)
         lbm_local_queue_save_tree(LIBBALSA_MAILBOX_LOCAL(mailbox));
 }
 
@@ -1352,7 +1351,6 @@ libbalsa_mailbox_local_prepare_threading(LibBalsaMailbox * mailbox,
     gchar *text;
     guint total;
     LibBalsaProgress progress = LIBBALSA_PROGRESS_INIT;
-    gboolean retval = TRUE;
 
     libbalsa_lock_mailbox(mailbox);
     libbalsa_mailbox_local_set_threading_info(local);
@@ -1368,17 +1366,12 @@ libbalsa_mailbox_local_prepare_threading(LibBalsaMailbox * mailbox,
             libbalsa_progress_set_fraction(&progress,
                                            ((gdouble) msgno) /
                                            ((gdouble) (total - start)));
-            if (!LIBBALSA_IS_MAILBOX(mailbox) || !MAILBOX_OPEN(mailbox)) {
-                /* Mailbox was closed during set-fraction. */
-                retval = FALSE;
-                break;
-            }
         }
     }
 
     libbalsa_progress_set_text(&progress, NULL, 0);
 
-    if (retval && need_thread && !priv->thread_id) {
+    if (need_thread && !priv->thread_id) {
         if (libbalsa_mailbox_get_threading_type(mailbox) !=
             LB_MAILBOX_THREADING_FLAT
             || libbalsa_mailbox_get_sort_field(mailbox) !=
@@ -1389,10 +1382,9 @@ libbalsa_mailbox_local_prepare_threading(LibBalsaMailbox * mailbox,
         }
     }
 
-    if (LIBBALSA_IS_MAILBOX(mailbox))
-        libbalsa_unlock_mailbox(mailbox);
+    libbalsa_unlock_mailbox(mailbox);
 
-    return retval;
+    return TRUE;
 }
 
 /* fetch message structure method: all local mailboxes have their own
diff --git a/src/balsa-index.c b/src/balsa-index.c
index 673b51be7..d7ca5cbd6 100644
--- a/src/balsa-index.c
+++ b/src/balsa-index.c
@@ -174,6 +174,8 @@ struct _BalsaIndex {
 
     LibBalsaMailboxSearchIter *search_iter;
     BalsaIndexWidthPreference width_preference;
+
+    gboolean expanded;
 };
 
 /* Class type. */
@@ -484,7 +486,6 @@ balsa_index_init(BalsaIndex * index)
                      G_CALLBACK(bndx_drag_cb), NULL);
 
     balsa_index_set_column_widths(index);
-    gtk_widget_show_all (GTK_WIDGET(index));
 }
 
 /* Callbacks used by bndx_instance_init. */
@@ -822,7 +823,11 @@ bndx_scroll_on_open_idle(BalsaIndex *bindex)
     if (!libbalsa_mailbox_get_messages_threaded(mailbox))
         return TRUE; /* G_SOURCE_CONTINUE */
 
-    balsa_index_update_tree(bindex, balsa_app.expand_tree);
+    if (balsa_app.expand_tree && !bindex->expanded) {
+        gtk_tree_view_expand_all(tree_view);
+        bindex->expanded = TRUE;
+        return TRUE; /* G_SOURCE_CONTINUE */
+    }
 
     /* From here on, we return only G_SOURCE_REMOVE, so we should clear
      * the id: */
@@ -855,9 +860,9 @@ bndx_scroll_on_open_idle(BalsaIndex *bindex)
 
     if (gtk_tree_view_get_model(tree_view)) {
         if ((view_on_open && GPOINTER_TO_INT(view_on_open))
-            || balsa_app.view_message_on_open)
+            || balsa_app.view_message_on_open) {
             bndx_select_row(bindex, path);
-        else {
+        } else {
             GtkTreeSelection *selection;
             gulong changed_id = bindex->selection_changed_id;
 
@@ -869,6 +874,7 @@ bndx_scroll_on_open_idle(BalsaIndex *bindex)
         }
     }
     gtk_tree_path_free(path);
+    gtk_widget_show(GTK_WIDGET(bindex));
 
     return FALSE;
 }
@@ -878,11 +884,11 @@ balsa_index_scroll_on_open(BalsaIndex * bindex)
 {
     /* Scroll in an idle handler, because the mailbox is perhaps being
      * opened in its own idle handler. */
-    /* Actually use a 10 millisecond timeout, bacause the handler will
+    /* Actually use a 500 millisecond timeout, bacause the handler will
      * reschedule itself until messages are loaded and threaded. */
     if (bindex->scroll_on_open_idle_id == 0) {
         bindex->scroll_on_open_idle_id =
-            g_timeout_add(10, (GSourceFunc) bndx_scroll_on_open_idle, bindex);
+            g_timeout_add(500, (GSourceFunc) bndx_scroll_on_open_idle, bindex);
     }
 }
 
diff --git a/src/main-window.c b/src/main-window.c
index 026c1a587..24c89ebee 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -4582,13 +4582,6 @@ bw_update_progress_bar(BalsaWindow *window,
     g_idle_add((GSourceFunc) bw_update_progress_bar_idle_cb, info);
 }
 
-static void
-bw_progress_bar_set_fraction(BalsaWindow *window,
-                             gdouble      fraction)
-{
-    bw_update_progress_bar(window, FALSE, NULL, TRUE, fraction);
-}
-
 /* balsa_window_increase_activity
  *
  * Calling this causes this to the progress bar of the window to
@@ -4711,7 +4704,7 @@ balsa_window_setup_progress(BalsaWindow * window, const gchar * text)
     return TRUE;
 }
 
-/* balsa_window_increment_progress
+/* balsa_window_progress_bar_set_fraction
  *
  * If the progress bar has been initialized using
  * balsa_window_setup_progress, this function increments the
@@ -4723,8 +4716,7 @@ balsa_window_setup_progress(BalsaWindow * window, const gchar * text)
  * main thread from processing events.
  **/
 void
-balsa_window_increment_progress(BalsaWindow * window, gdouble fraction,
-                                gboolean flush)
+balsa_window_progress_bar_set_fraction(BalsaWindow * window, gdouble fraction)
 {
     BalsaWindowPrivate *priv = balsa_window_get_instance_private(window);
 
@@ -4735,11 +4727,7 @@ balsa_window_increment_progress(BalsaWindow * window, gdouble fraction,
     if (priv->progress_type != BALSA_PROGRESS_INCREMENT)
         return;
 
-    bw_progress_bar_set_fraction(window, fraction);
-
-    if (flush)
-        while (gtk_events_pending())
-            gtk_main_iteration_do(FALSE);
+    bw_update_progress_bar(window, FALSE, NULL, TRUE, fraction);
 }
 
 /*
diff --git a/src/main-window.h b/src/main-window.h
index ed616c658..a105fdf47 100644
--- a/src/main-window.h
+++ b/src/main-window.h
@@ -102,8 +102,8 @@ gboolean balsa_window_next_unread(BalsaWindow * window);
 gboolean balsa_window_setup_progress(BalsaWindow * window,
                                      const gchar * text);
 void balsa_window_clear_progress(BalsaWindow* window);
-void balsa_window_increment_progress(BalsaWindow * window,
-                                     gdouble fraction, gboolean flush);
+void balsa_window_progress_bar_set_fraction(BalsaWindow * window,
+                                            gdouble       fraction);
 void balsa_window_increase_activity(BalsaWindow * window,
                                     const gchar * message);
 void balsa_window_decrease_activity(BalsaWindow * window,
diff --git a/src/main.c b/src/main.c
index e8fd12fcc..c9dac278e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -416,8 +416,7 @@ balsa_progress_set_fraction(LibBalsaProgress * progress, gdouble fraction)
     min_fraction  += LIBBALSA_PROGRESS_MIN_UPDATE_STEP;
 
     if (balsa_app.main_window)
-        balsa_window_increment_progress(balsa_app.main_window, fraction,
-                                        !libbalsa_am_i_subthread());
+        balsa_window_progress_bar_set_fraction(balsa_app.main_window, fraction);
 }
 
 static void


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