[gnome-calendar/calendar-management] source-manager: implement sources.default::state-changed signal



commit 50cd93128181ac10ee4000438366967b29912b12
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Feb 4 01:32:10 2015 -0200

    source-manager: implement sources.default::state-changed signal

 src/gcal-source-manager-dialog.c |   62 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 61 insertions(+), 1 deletions(-)
---
diff --git a/src/gcal-source-manager-dialog.c b/src/gcal-source-manager-dialog.c
index 97d8292..ec6299f 100644
--- a/src/gcal-source-manager-dialog.c
+++ b/src/gcal-source-manager-dialog.c
@@ -43,6 +43,7 @@ struct _GcalSourceManagerDialogPrivate
 
   /* selected source */
   ESource                 *selected_source;
+  gboolean                 updating;
 
   /* properties */
   GcalManager             *manager; /* weak ref */
@@ -64,6 +65,10 @@ static void     color_action_activated                     (GSimpleAction
                                                             GVariant            *value,
                                                             gpointer             user_data);
 
+static void     default_action_state_changed               (GSimpleAction       *action,
+                                                            GVariant            *value,
+                                                            gpointer             user_data);
+
 static void     file_action_activated                      (GSimpleAction       *menu_item,
                                                             GVariant            *value,
                                                             gpointer             user_data);
@@ -133,7 +138,7 @@ static GActionEntry action_entries[] =
   { "local", local_action_activated, NULL, NULL, NULL },
   { "url", url_action_activated, NULL, NULL, NULL },
   { "show", NULL, NULL, "false", show_action_state_changed },
-  { "default", NULL, NULL, "false", NULL },
+  { "default", NULL, NULL, "false", default_action_state_changed },
   { "rename", NULL, NULL, NULL, NULL },
   { "color", color_action_activated, NULL, NULL, NULL },
   { "remove", NULL, NULL, NULL, NULL },
@@ -183,6 +188,54 @@ color_action_activated (GSimpleAction *menu_item,
 }
 
 /**
+ * default_action_state_changed:
+ *
+ * Change the default calendar.
+ *
+ * Returns:
+ */
+static void
+default_action_state_changed (GSimpleAction *action,
+                              GVariant      *value,
+                              gpointer       user_data)
+{
+  GcalSourceManagerDialogPrivate *priv;
+  GtkWidget *old_default_row, *new_default_row;
+  GtkWidget *old_default_grid, *new_default_grid;
+  gboolean should_change;
+
+  priv = gcal_source_manager_dialog_get_instance_private (GCAL_SOURCE_MANAGER_DIALOG (user_data));
+  should_change = g_variant_get_boolean (value);
+
+  g_assert_nonnull (priv->selected_source);
+
+  if (priv->updating)
+      goto out;
+
+  /* Never unset the default calendar */
+  if (!should_change)
+    return;
+
+  /* Hide icon from old source */
+  old_default_row = g_hash_table_lookup (priv->source_to_row, gcal_manager_get_default_source 
(priv->manager));
+  old_default_grid = gtk_bin_get_child (GTK_BIN (old_default_row));
+
+  gtk_widget_hide (gtk_grid_get_child_at (GTK_GRID (old_default_grid), 2, 0));
+
+  /* Show icon of the new default source */
+  new_default_row = g_hash_table_lookup (priv->source_to_row, priv->selected_source);
+  new_default_grid = gtk_bin_get_child (GTK_BIN (new_default_row));
+
+  gtk_widget_show (gtk_grid_get_child_at (GTK_GRID (new_default_grid), 2, 0));
+
+  /* Set the new source */
+  gcal_manager_set_default_source (priv->manager, priv->selected_source);
+
+out:
+  g_simple_action_set_state (action, value);
+}
+
+/**
  * file_action_activated:
  *
  * Spawns a file chooser dialog with proper
@@ -357,12 +410,16 @@ show_action_state_changed (GSimpleAction *menu_item,
 
   g_assert_nonnull (priv->selected_source);
 
+  if (priv->updating)
+      goto out;
+
   /* Show/hide the source from the views */
   if (should_show)
     gcal_manager_enable_source (priv->manager, priv->selected_source);
   else
     gcal_manager_disable_source (priv->manager, priv->selected_source);
 
+out:
   g_simple_action_set_state (menu_item, value);
 }
 
@@ -636,6 +693,7 @@ update_action_states (GcalSourceManagerDialog *dialog,
   priv = gcal_source_manager_dialog_get_instance_private (dialog);
 
   priv->selected_source = source;
+  priv->updating = TRUE;
 
   if (source == NULL)
     return;
@@ -649,6 +707,8 @@ update_action_states (GcalSourceManagerDialog *dialog,
   action = g_action_map_lookup_action (G_ACTION_MAP (priv->action_group), "default");
   g_action_change_state (action, g_variant_new_boolean (is_default));
   g_simple_action_set_enabled (G_SIMPLE_ACTION (action), !gcal_manager_is_client_writable (priv->manager, 
source));
+
+  priv->updating = FALSE;
 }
 
 GtkWidget *


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