[gnome-calendar/gnome-3-24] source-dialog: improve URI handler



commit 272ce7a836d61c758670c837f9252abe8f787cd8
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 |  129 +++++++++++++++++++++++++--------------------
 src/gcal-utils.c         |   18 ++++++-
 src/gcal-utils.h         |    3 +-
 4 files changed, 91 insertions(+), 60 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 8693ab2..8a8db4f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -147,6 +147,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 f89b25b..18737e5 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>
 
 struct _GcalSourceDialog
 {
@@ -1241,13 +1242,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;
 
   /**
@@ -1268,14 +1273,15 @@ validate_url_cb (GcalSourceDialog *dialog)
   // Clear the entry icon
   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);
+  /* Get the hostname and file path from the server */
+  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.
@@ -1290,82 +1296,91 @@ validate_url_cb (GcalSourceDialog *dialog)
   auth = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
   e_source_authentication_set_host (auth, host);
 
-  // Webdav
+  /* 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);
 
@@ -1563,8 +1578,8 @@ 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);
+      /* Get the new resource path from the uri */
+      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 5a9dfb3..acd5688 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -758,7 +758,8 @@ gboolean
 uri_get_fields (const gchar  *uri,
                 gchar       **schema,
                 gchar       **host,
-                gchar       **path)
+                gchar       **path,
+                gboolean     *is_file)
 {
   GRegex *regex;
   GMatchInfo *match;
@@ -791,6 +792,8 @@ uri_get_fields (const gchar  *uri,
 
       if (path != NULL)
         *path = g_match_info_fetch (match, 3);
+
+      g_match_info_free (match);
     }
   else
     {
@@ -804,7 +807,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 2f65a8b..edab635 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -123,7 +123,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]