[gnome-calendar] source-dialog: improve URI handler



commit fc074bcde3b83af2c473586332057fe07f1bf888
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Thu Apr 13 09:17:52 2017 -0300

    source-dialog: improve URI handler
    
    Now we have a more reliable way to detect whether
    the passed URI is a file or a resource.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780455

 configure.ac             |    1 +
 src/gcal-source-dialog.c |  122 +++++++++++++++++++++++++--------------------
 src/gcal-utils.c         |   18 ++++++-
 src/gcal-utils.h         |    3 +-
 4 files changed, 87 insertions(+), 57 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 92dc0c8..3006a54 100644
--- a/configure.ac
+++ b/configure.ac
@@ -159,6 +159,7 @@ PKG_CHECK_MODULES([CALENDAR], [
                   libedataserver-1.2 >= $EDATASERVER_REQUIRED
                   libedataserverui-1.2 >= $EDATASERVERUI_REQUIRED
                   libical >= $LIBICAL_REQUIRED
+                  libsoup-2.4
                   gsettings-desktop-schemas >= $GSETTINGS_DESKTOP_SCHEMAS_REQUIRED])
 
 AC_CHECK_LIB([ical], [icaltime_days_in_year], [],
diff --git a/src/gcal-source-dialog.c b/src/gcal-source-dialog.c
index dfd30a4..2977f41 100644
--- a/src/gcal-source-dialog.c
+++ b/src/gcal-source-dialog.c
@@ -25,6 +25,7 @@
 #include <glib/gi18n.h>
 #include <goa/goa.h>
 #include <libedataserverui/libedataserverui.h>
+#include <libsoup/soup.h>
 
 /**
  * SECTION:gcal-source-dialog
@@ -1116,13 +1117,17 @@ static gboolean
 validate_url_cb (GcalSourceDialog *dialog)
 {
   ESourceAuthentication *auth;
+  ENamedParameters *credentials;
   ESourceExtension *ext;
   ESourceWebdav *webdav;
   ESource *source;
+  SoupURI *soup_uri;
+  const gchar *uri;
   gchar *host, *path;
-  gboolean uri_valid;
+  gboolean uri_valid, is_file;
 
   dialog->validate_url_resource_id = 0;
+  soup_uri = NULL;
   host = path = NULL;
 
   /**
@@ -1144,13 +1149,14 @@ validate_url_cb (GcalSourceDialog *dialog)
   gtk_entry_set_icon_from_icon_name (GTK_ENTRY (dialog->calendar_address_entry), GTK_ENTRY_ICON_SECONDARY, 
NULL);
 
   /* Get the hostname and file path from the server */
-  uri_valid = uri_get_fields (gtk_entry_get_text (GTK_ENTRY (dialog->calendar_address_entry)), NULL, &host, 
&path);
+  uri = gtk_entry_get_text (GTK_ENTRY (dialog->calendar_address_entry));
+  uri_valid = uri_get_fields (uri, NULL, &host, &path, &is_file);
 
-  g_debug ("[source-dialog] host: '%s', path: '%s'", host, path);
-
-  if (host == NULL || !uri_valid)
+  if (!host || !uri_valid)
     goto out;
 
+  g_debug ("Detected host: '%s', path: '%s'", host, path);
+
   /**
    * Create the new source and add the needed
    * extensions.
@@ -1166,81 +1172,89 @@ validate_url_cb (GcalSourceDialog *dialog)
   e_source_authentication_set_host (auth, host);
 
   /* Webdav */
+  soup_uri = soup_uri_new (uri);
   webdav = e_source_get_extension (source, E_SOURCE_EXTENSION_WEBDAV_BACKEND);
-  e_source_webdav_set_resource_path (webdav, path);
+  e_source_webdav_set_soup_uri (webdav, soup_uri);
 
   /*
    * If we're dealing with an absolute file path,
    * there is no need to check the server for more
    * sources.
    */
-  if (g_str_has_suffix (path, ".ics"))
+  if (is_file)
     {
       /* Set the private source so it saves at closing */
       dialog->remote_sources = g_list_append (dialog->remote_sources, source);
 
       /* Update buttons */
       gtk_widget_set_sensitive (dialog->add_button, source != NULL);
+
+      goto out;
+    }
+
+  /* Pulse the entry while it performs the check */
+  dialog->calendar_address_id = g_timeout_add (ENTRY_PROGRESS_TIMEOUT, (GSourceFunc) pulse_web_entry, 
dialog);
+
+  /*
+   * Try to retrieve the sources without prompting
+   * username and password. If we get any error,
+   * then it prompts and retry.
+   */
+  credentials = e_named_parameters_new ();
+
+  if (!dialog->prompt_password)
+    {
+      g_debug ("Trying to connect without credentials...");
+
+      /* NULL credentials */
+      e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_USERNAME, NULL);
+      e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_PASSWORD, NULL);
+
+      e_webdav_discover_sources (source,
+                                 uri,
+                                 E_WEBDAV_DISCOVER_SUPPORTS_EVENTS,
+                                 credentials,
+                                 NULL,
+                                 discover_sources_cb,
+                                 dialog);
     }
   else
     {
-      ENamedParameters *credentials;
+      gint response;
+      gchar *user, *password;
+
+      g_debug ("No credentials failed, retrying with user credentials...");
 
-      /* Pulse the entry while it performs the check */
-      dialog->calendar_address_id = g_timeout_add (ENTRY_PROGRESS_TIMEOUT, (GSourceFunc) pulse_web_entry, 
dialog);
+      user = password = NULL;
+      response = prompt_credentials (dialog, &user, &password);
 
       /*
-       * Try to retrieve the sources without prompting
-       * username and password. If we get any error,
-       * then it prompts and retry.
+       * User entered username and password, let's try
+       * with it.
        */
-      credentials = e_named_parameters_new ();
-
-      if (!dialog->prompt_password)
+      if (response == GTK_RESPONSE_OK)
         {
-          g_debug ("[source-dialog] Trying to connect without credentials...");
-
-          /* NULL credentials */
-          e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_USERNAME, NULL);
-          e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_PASSWORD, NULL);
-
-          e_webdav_discover_sources (source, gtk_entry_get_text (GTK_ENTRY (dialog->calendar_address_entry)),
-                                     E_WEBDAV_DISCOVER_SUPPORTS_EVENTS, credentials, NULL, 
discover_sources_cb,
+          /* User inputted credentials */
+          e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_USERNAME, user);
+          e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_PASSWORD, password);
+
+          e_webdav_discover_sources (source,
+                                     uri,
+                                     E_WEBDAV_DISCOVER_SUPPORTS_EVENTS,
+                                     credentials,
+                                     NULL,
+                                     discover_sources_cb,
                                      dialog);
         }
-      else
-        {
-          gint response;
-          gchar *user, *password;
 
-          g_debug ("[source-dialog] No credentials failed, retrying with user credentials...");
-
-          user = password = NULL;
-          response = prompt_credentials (dialog, &user, &password);
-
-          /*
-           * User entered username and password, let's try
-           * with it.
-           */
-          if (response == GTK_RESPONSE_OK)
-            {
-              /* User inputted credentials */
-              e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_USERNAME, user);
-              e_named_parameters_set (credentials, E_SOURCE_CREDENTIAL_PASSWORD, password);
-
-              e_webdav_discover_sources (source, gtk_entry_get_text (GTK_ENTRY 
(dialog->calendar_address_entry)),
-                                         E_WEBDAV_DISCOVER_SUPPORTS_EVENTS, credentials, NULL, 
discover_sources_cb,
-                                         dialog);
-            }
-
-          g_free (user);
-          g_free (password);
-        }
-
-      e_named_parameters_free (credentials);
+      g_free (user);
+      g_free (password);
     }
 
+  e_named_parameters_free (credentials);
+
 out:
+  g_clear_pointer (&soup_uri, soup_uri_free);
   g_free (host);
   g_free (path);
 
@@ -1439,7 +1453,7 @@ discover_sources_cb (GObject      *source,
       src = aux->data;
 
       /* Get the new resource path from the uri */
-      uri_valid = uri_get_fields (src->href, NULL, NULL, &resource_path);
+      uri_valid = uri_get_fields (src->href, NULL, NULL, &resource_path, NULL);
 
       if (uri_valid)
         {
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index d0646b1..c3cb35d 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -906,7 +906,8 @@ gboolean
 uri_get_fields (const gchar  *uri,
                 gchar       **schema,
                 gchar       **host,
-                gchar       **path)
+                gchar       **path,
+                gboolean     *is_file)
 {
   GRegex *regex;
   GMatchInfo *match;
@@ -939,6 +940,8 @@ uri_get_fields (const gchar  *uri,
 
       if (path != NULL)
         *path = g_match_info_fetch (match, 3);
+
+      g_match_info_free (match);
     }
   else
     {
@@ -952,7 +955,18 @@ uri_get_fields (const gchar  *uri,
         *path = NULL;
     }
 
-  g_match_info_free (match);
+  /* File extension */
+  if (is_file)
+    {
+      GRegex *extension_regex;
+
+      extension_regex = g_regex_new ("(\\.[a-zA-Z0-9]+)$", G_REGEX_CASELESS, 0, NULL);
+
+      *is_file = g_regex_match (extension_regex, uri, 0, NULL);
+
+      g_regex_unref (extension_regex);
+    }
+
   g_regex_unref (regex);
   return valid;
 }
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index 90345ac..b4ab0ce 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -121,7 +121,8 @@ void            fix_popover_menu_icons                          (GtkPopover
 gboolean        uri_get_fields                                  (const gchar           *uri,
                                                                  gchar                **schema,
                                                                  gchar                **host,
-                                                                 gchar                **path);
+                                                                 gchar                **path,
+                                                                 gboolean              *is_file);
 
 void            get_source_parent_name_color                    (GcalManager           *manager,
                                                                  ESource               *source,


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