[gnome-calendar/mcatanzaro/one-at-a-time: 2/2] new-calendar-page: don't add local calendar if remote address is given



commit c32424b4ac87c1d2a1799eeb0469cab311585aba
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Thu Apr 2 19:06:57 2020 -0500

    new-calendar-page: don't add local calendar if remote address is given
    
    If the user has entered a remote address, then adding a new empty local
    calendar is definitely not desired. So stop doing it. Create the
    local_source only if the user has clicked the Add button and no remote
    sources are available. Rename the relevant widgets accordingly, and
    update the add button's sensitivity check to account for the fact that
    self->local_source no longer exists.
    
    See #566, but note this does not solve #566 as there is an
    additional bug somewhere else causing multiple remote calendars to be
    added at the same time when only a single ical file is used as the data
    source. This dialog does support adding multiple calendars (ESources)
    from a single URL, but that shouldn't be happening when the URL points
    to a single ical file. That bug appears to be in a different level of
    the code, not GcalNewCalendarPage.
    
    Also note that we require further changes to the strings on this dialog
    to conform to this new behavior, in a future commit. This commit is
    designed to not break string freeze.

 .../calendar-management/gcal-new-calendar-page.c   | 135 ++++++++++++---------
 .../calendar-management/gcal-new-calendar-page.ui  |   5 +-
 2 files changed, 78 insertions(+), 62 deletions(-)
---
diff --git a/src/gui/calendar-management/gcal-new-calendar-page.c 
b/src/gui/calendar-management/gcal-new-calendar-page.c
index ee784fff..848de828 100644
--- a/src/gui/calendar-management/gcal-new-calendar-page.c
+++ b/src/gui/calendar-management/gcal-new-calendar-page.c
@@ -54,8 +54,8 @@ struct _GcalNewCalendarPage
   GtkEntry           *credentials_password_entry;
   GtkPopover         *credentials_popover;
   GtkEntry           *credentials_user_entry;
-  GtkColorChooser    *local_calendar_color_button;
-  GtkEntry           *local_calendar_name_entry;
+  GtkColorChooser    *calendar_color_button;
+  GtkEntry           *calendar_name_entry;
   GtkWidget          *web_sources_listbox;
   GtkWidget          *web_sources_revealer;
 
@@ -65,8 +65,6 @@ struct _GcalNewCalendarPage
 
   GCancellable       *cancellable;
 
-  ESource            *local_source;
-
   GcalContext        *context;
 };
 
@@ -134,53 +132,46 @@ calendar_path_to_name_suggestion (GFile *file)
   return g_steal_pointer (&basename);
 }
 
-static void
-update_add_button (GcalNewCalendarPage *self)
+static char *
+get_calendar_name (GcalNewCalendarPage *self)
 {
-  gboolean valid;
+  g_autofree char *name = NULL;
 
-  valid = (self->local_source != NULL || self->remote_sources != NULL) &&
-      self->calendar_address_entry_state != ENTRY_STATE_VALIDATING &&
-      self->calendar_address_entry_state != ENTRY_STATE_INVALID;
+  name = g_strdup (gtk_entry_get_text (self->calendar_name_entry));
+  if (!name)
+    return NULL;
 
-  gtk_widget_set_sensitive (self->add_button, valid);
+  name = g_strstrip (name);
+  if (g_utf8_strlen (name, -1) == 0)
+    return NULL;
+
+  return g_steal_pointer (&name);
 }
 
-static void
-update_local_source (GcalNewCalendarPage *self)
+static gboolean
+has_calendar_name (GcalNewCalendarPage *self)
 {
-  g_autofree gchar *calendar_name = NULL;
-
-  g_clear_object (&self->local_source);
-
-  calendar_name = g_strdup (gtk_entry_get_text (self->local_calendar_name_entry));
-  calendar_name = g_strstrip (calendar_name);
-
-  if (calendar_name && g_utf8_strlen (calendar_name, -1) > 0)
-    {
-      g_autofree gchar *color_string = NULL;
-      ESourceExtension *ext;
-      ESource *source;
-      GdkRGBA color;
-
-      gtk_color_chooser_get_rgba (self->local_calendar_color_button, &color);
-      color_string = gdk_rgba_to_string (&color);
+  g_autofree char *name = get_calendar_name (self);
 
-      /* Create the new source and add the needed extensions */
-      source = e_source_new (NULL, NULL, NULL);
-      e_source_set_parent (source, "local-stub");
-      e_source_set_display_name (source, calendar_name);
-
-      ext = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
-      e_source_backend_set_backend_name (E_SOURCE_BACKEND (ext), "local");
-      e_source_selectable_set_color (E_SOURCE_SELECTABLE (ext), color_string);
+  return !!name;
+}
 
-      e_source_backend_set_backend_name (E_SOURCE_BACKEND (ext), "local");
+static void
+update_add_button (GcalNewCalendarPage *self)
+{
+  gboolean valid;
 
-      self->local_source = source;
-    }
+  /* Allow clicking the add button if:
+   *
+   * (a) URL entry is empty and calendar name entry is not empty (create a local calendar)
+   * (b) URL entry has reached valid state (create a remote calendar)
+   *
+   * The add button is always disabled if URL entry state is VALIDATING or INVALID.
+   */
+  valid = (self->calendar_address_entry_state == ENTRY_STATE_EMPTY && has_calendar_name (self)) ||
+          self->calendar_address_entry_state == ENTRY_STATE_VALID;
 
-  update_add_button (self);
+  gtk_widget_set_sensitive (self->add_button, valid);
 }
 
 static void
@@ -336,6 +327,7 @@ sources_discovered_cb (GObject      *source_object,
 
   g_debug ("Found %u sources", sources->len);
 
+  g_clear_pointer (&self->remote_sources, g_ptr_array_unref);
   self->remote_sources = g_steal_pointer (&sources);
   update_url_entry_state (self, ENTRY_STATE_VALID);
 
@@ -372,23 +364,57 @@ on_add_button_clicked_cb (GtkWidget           *button,
                           GcalNewCalendarPage *self)
 {
   GcalManager *manager;
+  g_autofree char *calendar_name = NULL;
+  g_autofree gchar *color_string = NULL;
+  ESourceExtension *ext;
+  GdkRGBA color;
+  ESource *source;
 
   manager = gcal_context_get_manager (self->context);
 
-  /* Commit the new source */
-  if (self->local_source)
-    gcal_manager_save_source (manager, self->local_source);
+  calendar_name = get_calendar_name (self);
+
+  gtk_color_chooser_get_rgba (self->calendar_color_button, &color);
+  color_string = gdk_rgba_to_string (&color);
 
   /* Commit each new remote source */
   if (self->remote_sources)
     {
-      guint i;
+      /* Apply the selected name and color to the first calendar source.
+       *
+       * FIXME: This might not be a reasonable thing to do when there are multiple sources.
+       */
+      source = g_ptr_array_index (self->remote_sources, 0);
+      if (calendar_name)
+        e_source_set_display_name (source, calendar_name);
+      ext = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
+      e_source_selectable_set_color (E_SOURCE_SELECTABLE (ext), color_string);
 
-      for (i = 0; i < self->remote_sources->len; i++)
+      for (guint i = 0; i < self->remote_sources->len; i++)
         gcal_manager_save_source (manager, g_ptr_array_index (self->remote_sources, i));
 
       g_clear_pointer (&self->remote_sources, g_ptr_array_unref);
     }
+  else
+    {
+      /* If the add button is sensitive, we then should either have
+       * remote_sources or calendar_name.
+       */
+      g_assert (calendar_name);
+
+      /* Create the new source and add the needed extensions */
+      source = e_source_new (NULL, NULL, NULL);
+      e_source_set_parent (source, "local-stub");
+      e_source_set_display_name (source, calendar_name);
+
+      ext = e_source_get_extension (source, E_SOURCE_EXTENSION_CALENDAR);
+      e_source_backend_set_backend_name (E_SOURCE_BACKEND (ext), "local");
+      e_source_selectable_set_color (E_SOURCE_SELECTABLE (ext), color_string);
+
+      e_source_backend_set_backend_name (E_SOURCE_BACKEND (ext), "local");
+
+      gcal_manager_save_source (manager, source);
+    }
 
   gcal_calendar_management_page_switch_page (GCAL_CALENDAR_MANAGEMENT_PAGE (self),
                                              "calendars",
@@ -470,14 +496,6 @@ on_cancel_button_clicked_cb (GtkWidget                  *button,
   gcal_calendar_management_page_switch_page (page, "calendars", NULL);
 }
 
-static void
-on_local_calendar_name_entry_text_changed_cb (GtkEntry            *entry,
-                                              GParamSpec          *pspec,
-                                              GcalNewCalendarPage *self)
-{
-  update_local_source (self);
-}
-
 static void
 on_web_description_label_link_activated_cb (GtkLabel            *label,
                                             gchar               *uri,
@@ -523,6 +541,7 @@ gcal_new_calendar_page_activate (GcalCalendarManagementPage *page,
 
   GCAL_EXIT;
 }
+
 static void
 gcal_new_calendar_page_deactivate (GcalCalendarManagementPage *page)
 {
@@ -538,11 +557,10 @@ gcal_new_calendar_page_deactivate (GcalCalendarManagementPage *page)
   gtk_container_remove (GTK_CONTAINER (headerbar), self->add_button);
   gtk_header_bar_set_show_close_button (headerbar, TRUE);
 
-  g_clear_object (&self->local_source);
   g_clear_pointer (&self->remote_sources, g_ptr_array_unref);
   update_add_button (self);
 
-  gtk_entry_set_text (self->local_calendar_name_entry, "");
+  gtk_entry_set_text (self->calendar_name_entry, "");
   gtk_entry_set_text (self->calendar_address_entry, "");
 
   toggle_url_entry_pulsing (self, FALSE);
@@ -641,8 +659,8 @@ gcal_new_calendar_page_class_init (GcalNewCalendarPageClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, credentials_password_entry);
   gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, credentials_popover);
   gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, credentials_user_entry);
-  gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, local_calendar_color_button);
-  gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, local_calendar_name_entry);
+  gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, calendar_color_button);
+  gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, calendar_name_entry);
   gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, web_sources_listbox);
   gtk_widget_class_bind_template_child (widget_class, GcalNewCalendarPage, web_sources_revealer);
 
@@ -652,7 +670,6 @@ gcal_new_calendar_page_class_init (GcalNewCalendarPageClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, on_credential_button_clicked_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_credential_entry_activate_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_file_chooser_button_file_set_cb);
-  gtk_widget_class_bind_template_callback (widget_class, on_local_calendar_name_entry_text_changed_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_url_entry_text_changed_cb);
   gtk_widget_class_bind_template_callback (widget_class, on_web_description_label_link_activated_cb);
 }
diff --git a/src/gui/calendar-management/gcal-new-calendar-page.ui 
b/src/gui/calendar-management/gcal-new-calendar-page.ui
index fae3a5d5..e8366b5a 100644
--- a/src/gui/calendar-management/gcal-new-calendar-page.ui
+++ b/src/gui/calendar-management/gcal-new-calendar-page.ui
@@ -36,10 +36,9 @@
             </child>
 
             <child>
-              <object class="GtkEntry" id="local_calendar_name_entry">
+              <object class="GtkEntry" id="calendar_name_entry">
                 <property name="visible">True</property>
                 <property name="placeholder-text" translatable="yes">Calendar Name</property>
-                <signal name="notify::text" handler="on_local_calendar_name_entry_text_changed_cb" 
object="GcalNewCalendarPage" swapped="no" />
               </object>
             </child>
 
@@ -68,7 +67,7 @@
             </child>
 
             <child>
-              <object class="GtkColorButton" id="local_calendar_color_button">
+              <object class="GtkColorButton" id="calendar_color_button">
                 <property name="visible">True</property>
               </object>
             </child>


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