[gnome-todo] plugins: use the new GtdTimer API



commit a8b7846f776a3a26c666413202c0e56b58523363
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sun Apr 23 16:25:15 2017 -0300

    plugins: use the new GtdTimer API

 plugins/background/gtd-plugin-background.c    |   49 ++++++++++++++++---------
 plugins/scheduled-panel/gtd-panel-scheduled.c |    5 +++
 plugins/today-panel/gtd-panel-today.c         |   39 ++------------------
 3 files changed, 41 insertions(+), 52 deletions(-)
---
diff --git a/plugins/background/gtd-plugin-background.c b/plugins/background/gtd-plugin-background.c
index 1c2c40c..39c00ec 100644
--- a/plugins/background/gtd-plugin-background.c
+++ b/plugins/background/gtd-plugin-background.c
@@ -212,27 +212,16 @@ get_tasks_for_today (guint *n_events)
   return result;
 }
 
-/*
- * Callbacks
- */
 static void
-on_startup_changed (GSettings           *settings,
-                    const gchar         *key,
-                    GtdPluginBackground *self)
-{
-  set_autostart_enabled (self, g_settings_get_boolean (settings, key));
-}
-
-static gboolean
-on_startup_timeout_cb (GtdPluginBackground *self)
+send_notification (GtdPluginBackground *self)
 {
   GNotification *notification;
   GApplication *app;
   GtdWindow *window;
+  guint n_tasks;
   GList *tasks;
   gchar *title;
   gchar *body;
-  guint n_tasks;
 
   window = get_window ();
 
@@ -241,17 +230,18 @@ on_startup_timeout_cb (GtdPluginBackground *self)
    * notify about the number of tasks.
    */
   if (gtk_window_is_active (GTK_WINDOW (window)))
-    goto out;
+    return;
 
   /* The user don't want to be bothered with notifications */
   if (!g_settings_get_boolean (self->settings, "show-notifications"))
-    goto out;
+    return;
 
   app = g_application_get_default ();
   tasks = get_tasks_for_today (&n_tasks);
 
+  /* If n_tasks == 0, tasks == NULL, thus we don't need to free it */
   if (n_tasks == 0)
-    goto out;
+    return;
 
   title = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
                                         "You have %d task for today",
@@ -270,8 +260,24 @@ on_startup_timeout_cb (GtdPluginBackground *self)
 
   g_clear_pointer (&tasks, g_list_free);
   g_clear_object (&notification);
+}
+
+/*
+ * Callbacks
+ */
+static void
+on_startup_changed (GSettings           *settings,
+                    const gchar         *key,
+                    GtdPluginBackground *self)
+{
+  set_autostart_enabled (self, g_settings_get_boolean (settings, key));
+}
+
+static gboolean
+on_startup_timeout_cb (GtdPluginBackground *self)
+{
+  send_notification (self);
 
-out:
   self->startup_notification_timeout_id = 0;
 
   g_signal_handlers_disconnect_by_func (gtd_manager_get_default (),
@@ -315,6 +321,11 @@ watch_manager_for_new_lists (GtdPluginBackground *self)
                             "list-removed",
                             G_CALLBACK (on_tasklist_notified),
                             self);
+
+  g_signal_connect_swapped (gtd_manager_get_timer (manager),
+                            "update",
+                            G_CALLBACK (send_notification),
+                            self);
 }
 
 /*
@@ -367,6 +378,10 @@ gtd_plugin_background_deactivate (GtdActivatable *activatable)
                                         on_tasklist_notified,
                                         self);
 
+  g_signal_handlers_disconnect_by_func (gtd_manager_get_timer (manager),
+                                        send_notification,
+                                        self);
+
   /* Deactivate the timeout */
   if (self->startup_notification_timeout_id > 0)
     {
diff --git a/plugins/scheduled-panel/gtd-panel-scheduled.c b/plugins/scheduled-panel/gtd-panel-scheduled.c
index d138cc4..5415552 100644
--- a/plugins/scheduled-panel/gtd-panel-scheduled.c
+++ b/plugins/scheduled-panel/gtd-panel-scheduled.c
@@ -499,6 +499,11 @@ gtd_panel_scheduled_init (GtdPanelScheduled *self)
                             G_CALLBACK (gtd_panel_scheduled_count_tasks),
                             self);
 
+  g_signal_connect_swapped (gtd_manager_get_timer (manager),
+                            "update",
+                            G_CALLBACK (gtd_panel_scheduled_count_tasks),
+                            self);
+
   /* Setup a title */
   self->title = g_strdup (_("Scheduled"));
 
diff --git a/plugins/today-panel/gtd-panel-today.c b/plugins/today-panel/gtd-panel-today.c
index ac6db14..83c22ed 100644
--- a/plugins/today-panel/gtd-panel-today.c
+++ b/plugins/today-panel/gtd-panel-today.c
@@ -147,40 +147,6 @@ gtd_panel_today_count_tasks (GtdPanelToday *panel)
   g_list_free (tasklists);
 }
 
-static gboolean
-gtd_panel_today_update_today_timeout_cb (GtdPanelToday *panel)
-{
-  GDateTime *tomorrow;
-  GDateTime *today;
-  GDateTime *now;
-  gint seconds;
-
-  now = g_date_time_new_now_local ();
-  today = g_date_time_new_local (g_date_time_get_year (now),
-                                 g_date_time_get_month (now),
-                                 g_date_time_get_day_of_month (now),
-                                 0,
-                                 0,
-                                 0);
-  tomorrow = g_date_time_add_days (today, 1);
-  seconds = g_date_time_difference (now, tomorrow) / G_TIME_SPAN_SECOND;
-
-  /* Recount tasks */
-  gtd_panel_today_count_tasks (panel);
-
-  panel->day_change_callback_id = g_timeout_add_seconds (seconds,
-                                                         (GSourceFunc) 
gtd_panel_today_update_today_timeout_cb,
-                                                         panel);
-
-  gtd_task_list_view_set_default_date (GTD_TASK_LIST_VIEW (panel->view), now);
-
-  g_clear_pointer (&tomorrow, g_date_time_unref);
-  g_clear_pointer (&today, g_date_time_unref);
-  g_clear_pointer (&now, g_date_time_unref);
-
-  return G_SOURCE_REMOVE;
-}
-
 /**********************
  * GtdPanel iface init
  **********************/
@@ -327,7 +293,10 @@ gtd_panel_today_init (GtdPanelToday *self)
   gtk_widget_show_all (GTK_WIDGET (self));
 
   /* Start timer */
-  gtd_panel_today_update_today_timeout_cb (self);
+  g_signal_connect_swapped (gtd_manager_get_timer (manager),
+                            "update",
+                            G_CALLBACK (gtd_panel_today_count_tasks),
+                            self);
 
   g_clear_pointer (&now, g_date_time_unref);
 }


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