[gnome-todo] window: Refresh providers when window is focused



commit 7867016aebab83bf03345804b5292202c8dcde84
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Mar 30 21:06:35 2019 -0300

    window: Refresh providers when window is focused
    
    So that whenever the user wants to check GNOME To Do, they
    will have a freshly updated list of tasks.
    
    Related to https://gitlab.gnome.org/GNOME/gnome-todo/issues/234

 src/gtd-window.c  | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gtd-window.ui |  1 +
 2 files changed, 74 insertions(+)
---
diff --git a/src/gtd-window.c b/src/gtd-window.c
index 673bb6d..772ed43 100644
--- a/src/gtd-window.c
+++ b/src/gtd-window.c
@@ -24,6 +24,7 @@
 #include "interfaces/gtd-provider.h"
 #include "interfaces/gtd-panel.h"
 #include "gtd-application.h"
+#include "gtd-debug.h"
 #include "gtd-enum-types.h"
 #include "gtd-task-list-view.h"
 #include "gtd-manager.h"
@@ -78,6 +79,8 @@ struct _GtdWindow
 
   /* mode */
   GtdWindowMode       mode;
+
+  gulong              block_refresh_timeout_id;
 };
 
 typedef struct
@@ -88,6 +91,9 @@ typedef struct
 } ErrorData;
 
 
+static gboolean      block_refresh_cb                            (gpointer           data);
+
+
 G_DEFINE_TYPE (GtdWindow, gtd_window, GTK_TYPE_APPLICATION_WINDOW)
 
 
@@ -179,6 +185,14 @@ remove_widgets (GtdWindow *self,
     }
 }
 
+static void
+block_refresh (GtdWindow *self)
+{
+  g_debug ("Blocking refreshs triggered by window focus");
+
+  self->block_refresh_timeout_id = g_timeout_add_seconds (20, block_refresh_cb, self);
+}
+
 static void
 on_plugin_loaded_cb (GtdWindow      *self,
                      gpointer        unused_field,
@@ -322,6 +336,47 @@ load_geometry (GtdWindow *self)
     gtk_window_maximize (window);
 }
 
+static gboolean
+block_refresh_cb (gpointer data)
+{
+  GtdWindow *self = data;
+
+  g_debug ("Unblocking refreshs triggered by window focus");
+
+  self->block_refresh_timeout_id = 0;
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+on_active_state_changed_cb (GObject    *object,
+                            GParamSpec *pspec,
+                            GtdWindow  *self)
+{
+  g_autoptr (GList) providers = NULL;
+  GList *l;
+
+  GTD_ENTRY;
+
+  if (!gtk_window_is_active (GTK_WINDOW (self)) ||
+      self->block_refresh_timeout_id > 0)
+    {
+      GTD_RETURN ();
+    }
+
+  g_debug ("Window focused, refreshing providers");
+
+  providers = gtd_manager_get_providers (gtd_manager_get_default ());
+
+  /* Refresh all providers */
+  for (l = providers; l; l = l->next)
+    gtd_provider_refresh (l->data);
+
+  block_refresh (self);
+
+  GTD_EXIT;
+}
+
 static void
 on_cancel_selection_button_clicked (GtkWidget *button,
                                     GtdWindow *self)
@@ -515,6 +570,20 @@ gtd_window_constructed (GObject *object)
   g_signal_connect (gtd_manager_get_default (), "show-notification", G_CALLBACK (on_show_notification_cb), 
self);
 }
 
+static void
+gtd_window_finalize (GObject *object)
+{
+  GtdWindow *self = GTD_WINDOW (object);
+
+  G_OBJECT_CLASS (gtd_window_parent_class)->finalize (object);
+
+  if (self->block_refresh_timeout_id)
+    {
+      g_source_remove (self->block_refresh_timeout_id);
+      self->block_refresh_timeout_id = 0;
+    }
+}
+
 static void
 gtd_window_get_property (GObject    *object,
                          guint       prop_id,
@@ -560,6 +629,7 @@ gtd_window_class_init (GtdWindowClass *klass)
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
   object_class->constructed = gtd_window_constructed;
+  object_class->finalize = gtd_window_finalize;
   object_class->get_property = gtd_window_get_property;
   object_class->set_property = gtd_window_set_property;
 
@@ -597,6 +667,7 @@ gtd_window_class_init (GtdWindowClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GtdWindow, panel_box_end);
   gtk_widget_class_bind_template_child (widget_class, GtdWindow, panel_box_start);
 
+  gtk_widget_class_bind_template_callback (widget_class, on_active_state_changed_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_cancel_selection_button_clicked);
   gtk_widget_class_bind_template_callback (widget_class, on_stack_visible_child_cb);
 }
@@ -616,6 +687,8 @@ gtd_window_init (GtdWindow *self)
   /* Development build */
   if (is_development_build ())
     setup_development_build (self);
+
+  block_refresh (self);
 }
 
 GtkWidget*
diff --git a/src/gtd-window.ui b/src/gtd-window.ui
index e5732ce..697f27f 100644
--- a/src/gtd-window.ui
+++ b/src/gtd-window.ui
@@ -6,6 +6,7 @@
     <property name="can_focus">False</property>
     <property name="default_width">800</property>
     <property name="default_height">600</property>
+    <signal name="notify::is-active" handler="on_active_state_changed_cb" object="GtdWindow" swapped="no" />
     <style>
       <class name="org-gnome-Todo"/>
     </style>


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