[gnome-software/1310-updates-page-blanks-while-updating-flatpaks: 9/9] flatpak: Delay installation changes notification when running update




commit d87308e3e8813f4fc879817474d315c644049aca
Author: Milan Crha <mcrha redhat com>
Date:   Thu Jul 15 12:58:31 2021 +0200

    flatpak: Delay installation changes notification when running update
    
    When running update of several applications, each of them causes a notification
    about the Flatpak installation change. No need to reload the GUI for every
    single application, better to delay the notification until the update is done.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1310

 plugins/flatpak/gs-flatpak.c        | 51 ++++++++++++++++++++++++++++++++++---
 plugins/flatpak/gs-flatpak.h        |  3 +++
 plugins/flatpak/gs-plugin-flatpak.c |  6 ++++-
 3 files changed, 55 insertions(+), 5 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index f1f87f61a..7e9c571f8 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -44,6 +44,8 @@ struct _GsFlatpak {
        GHashTable              *remote_title; /* gchar *remote name ~> gchar *remote title */
        GMutex                   remote_title_mutex;
        gboolean                 requires_full_rescan;
+       gint                     busy; /* (atomic) */
+       gboolean                 changed_while_busy;
 };
 
 G_DEFINE_TYPE (GsFlatpak, gs_flatpak, G_TYPE_OBJECT)
@@ -390,6 +392,19 @@ gs_flatpak_create_source (GsFlatpak *self, FlatpakRemote *xremote)
        return g_steal_pointer (&app);
 }
 
+static gboolean
+gs_flatpak_claim_changed_idle_cb (gpointer user_data)
+{
+       GsFlatpak *self = user_data;
+
+       self->requires_full_rescan = TRUE;
+
+       gs_plugin_cache_invalidate (self->plugin);
+       gs_plugin_reload (self->plugin);
+
+       return G_SOURCE_REMOVE;
+}
+
 static void
 gs_plugin_flatpak_changed_cb (GFileMonitor *monitor,
                              GFile *child,
@@ -416,10 +431,11 @@ gs_plugin_flatpak_changed_cb (GFileMonitor *monitor,
                xb_silo_invalidate (self->silo);
        g_clear_pointer (&writer_locker, g_rw_lock_writer_locker_free);
 
-       self->requires_full_rescan = TRUE;
-
-       gs_plugin_cache_invalidate (self->plugin);
-       gs_plugin_reload (self->plugin);
+       if (gs_flatpak_get_busy (self)) {
+               self->changed_while_busy = TRUE;
+       } else {
+               gs_flatpak_claim_changed_idle_cb (self);
+       }
 }
 
 static gboolean
@@ -3789,3 +3805,30 @@ gs_flatpak_new (GsPlugin *plugin, FlatpakInstallation *installation, GsFlatpakFl
        self->flags = flags;
        return GS_FLATPAK (self);
 }
+
+void
+gs_flatpak_set_busy (GsFlatpak *self,
+                    gboolean busy)
+{
+       g_return_if_fail (GS_IS_FLATPAK (self));
+
+       if (busy) {
+               g_atomic_int_inc (&self->busy);
+       } else {
+               g_return_if_fail (g_atomic_int_get (&self->busy) > 0);
+               if (g_atomic_int_dec_and_test (&self->busy)) {
+                       if (self->changed_while_busy) {
+                               self->changed_while_busy = FALSE;
+                               g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, gs_flatpak_claim_changed_idle_cb,
+                                       g_object_ref (self), g_object_unref);
+                       }
+               }
+       }
+}
+
+gboolean
+gs_flatpak_get_busy (GsFlatpak *self)
+{
+       g_return_val_if_fail (GS_IS_FLATPAK (self), FALSE);
+       return g_atomic_int_get (&self->busy) > 0;
+}
diff --git a/plugins/flatpak/gs-flatpak.h b/plugins/flatpak/gs-flatpak.h
index 5eb7bb452..26a16e6f2 100644
--- a/plugins/flatpak/gs-flatpak.h
+++ b/plugins/flatpak/gs-flatpak.h
@@ -128,5 +128,8 @@ gboolean    gs_flatpak_add_recent           (GsFlatpak              *self,
                                                 guint64                 age,
                                                 GCancellable           *cancellable,
                                                 GError                 **error);
+void           gs_flatpak_set_busy             (GsFlatpak              *self,
+                                                gboolean                busy);
+gboolean       gs_flatpak_get_busy             (GsFlatpak              *self);
 
 G_END_DECLS
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 808e1a56e..1cd2b9d6a 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -1240,12 +1240,16 @@ gs_plugin_update (GsPlugin *plugin,
        while (g_hash_table_iter_next (&iter, &key, &value)) {
                GsFlatpak *flatpak = GS_FLATPAK (key);
                GsAppList *list_tmp = GS_APP_LIST (value);
+               gboolean success;
 
                g_assert (GS_IS_FLATPAK (flatpak));
                g_assert (list_tmp != NULL);
                g_assert (gs_app_list_length (list_tmp) > 0);
 
-               if (!gs_plugin_flatpak_update (plugin, flatpak, list_tmp, cancellable, error))
+               gs_flatpak_set_busy (flatpak, TRUE);
+               success = gs_plugin_flatpak_update (plugin, flatpak, list_tmp, cancellable, error);
+               gs_flatpak_set_busy (flatpak, FALSE);
+               if (!success)
                        return FALSE;
        }
        return TRUE;


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