[gnome-calendar: 1/2] utils: reworked uri splitting with help of libsoup



commit 0f6efd8668d35151892aaee0059e577992192428
Author: Guenther Wutz <info gunibert de>
Date:   Mon Dec 11 20:08:09 2017 +0100

    utils: reworked uri splitting with help of libsoup
    
    Currently we split a URI string with a Regex which is error prone. As
    libsoup already provides a facility to parse a URI into its parts
    we should use this instead. Therefore our utility method is not necessary anymore.

 src/gcal-source-dialog.c | 44 +++++++++++++-------------
 src/gcal-utils.c         | 80 ------------------------------------------------
 src/gcal-utils.h         |  6 ----
 3 files changed, 22 insertions(+), 108 deletions(-)
---
diff --git a/src/gcal-source-dialog.c b/src/gcal-source-dialog.c
index 68206ee9..c55fca4a 100644
--- a/src/gcal-source-dialog.c
+++ b/src/gcal-source-dialog.c
@@ -925,11 +925,11 @@ stack_visible_child_name_changed (GObject    *object,
           auth = e_source_get_extension (self->source, E_SOURCE_EXTENSION_AUTHENTICATION);
           webdav = e_source_get_extension (self->source, E_SOURCE_EXTENSION_WEBDAV_BACKEND);
           soup = e_source_webdav_dup_soup_uri (webdav);
-          uri = g_strdup_printf ("%s://%s%s:%d",
+          uri = g_strdup_printf ("%s://%s:%d%s",
                                  soup_uri_get_scheme (soup),
                                  e_source_authentication_get_host (auth),
-                                 e_source_webdav_get_resource_path (webdav),
-                                 e_source_authentication_get_port (auth));
+                                 e_source_authentication_get_port (auth),
+                                 e_source_webdav_get_resource_path (webdav));
 
           gtk_link_button_set_uri (GTK_LINK_BUTTON (self->calendar_url_button), uri);
           gtk_button_set_label (GTK_BUTTON (self->calendar_url_button), uri);
@@ -1234,14 +1234,15 @@ validate_url_cb (GcalSourceDialog *dialog)
   ESourceExtension *ext;
   ESourceWebdav *webdav;
   ESource *source;
-  SoupURI *soup_uri;
+  g_autoptr (SoupURI) soup_uri;
   const gchar *uri;
-  gchar *host, *path;
-  gboolean uri_valid, is_file;
+  const gchar *host, *path;
+  gboolean is_file;
 
   dialog->validate_url_resource_id = 0;
   soup_uri = NULL;
   host = path = NULL;
+  is_file = FALSE;
 
   /**
    * Remove any reminescent ESources
@@ -1263,10 +1264,15 @@ validate_url_cb (GcalSourceDialog *dialog)
 
   /* 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);
+  soup_uri = soup_uri_new (uri);
+  if (!soup_uri)
+    return FALSE;
 
-  if (!host || !uri_valid)
-    goto out;
+  host = soup_uri_get_host (soup_uri);
+  path = soup_uri_get_path (soup_uri);
+
+  if (soup_uri_get_scheme (soup_uri) == SOUP_URI_SCHEME_FILE)
+    is_file = TRUE;
 
   g_debug ("Detected host: '%s', path: '%s'", host, path);
 
@@ -1285,7 +1291,6 @@ 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_soup_uri (webdav, soup_uri);
 
@@ -1302,7 +1307,7 @@ validate_url_cb (GcalSourceDialog *dialog)
       /* Update buttons */
       gtk_widget_set_sensitive (dialog->add_button, source != NULL);
 
-      goto out;
+      return FALSE;
     }
 
   /* Pulse the entry while it performs the check */
@@ -1366,11 +1371,6 @@ validate_url_cb (GcalSourceDialog *dialog)
 
   e_named_parameters_free (credentials);
 
-out:
-  g_clear_pointer (&soup_uri, soup_uri_free);
-  g_free (host);
-  g_free (path);
-
   return FALSE;
 }
 
@@ -1562,16 +1562,18 @@ discover_sources_cb (GObject      *source,
   /* TODO: show a list of calendars */
   for (aux = discovered_sources; aux != NULL; aux = aux->next)
     {
+      g_autoptr (SoupURI) soup_uri = NULL;
       EWebDAVDiscoveredSource *discovered_source;
-      gchar *resource_path = NULL;
-      gboolean uri_valid;
+      const gchar *resource_path = NULL;
 
       discovered_source = aux->data;
 
+      soup_uri = soup_uri_new (discovered_source->href);
+
       /* Get the new resource path from the uri */
-      uri_valid = uri_get_fields (discovered_source->href, NULL, NULL, &resource_path, NULL);
+      resource_path = soup_uri_get_path (soup_uri);
 
-      if (uri_valid)
+      if (soup_uri)
         {
           ESourceSelectable *selectable;
           ESourceWebdav *webdav;
@@ -1609,8 +1611,6 @@ discover_sources_cb (GObject      *source,
 
           gtk_widget_show_all (row);
         }
-
-      g_free (resource_path);
     }
 
   /* Free things up */
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index 4558f540..61af5bf2 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -875,86 +875,6 @@ fix_popover_menu_icons (GtkPopover *popover)
 }
 
 /**
- * uri_get_fields:
- * @uri: the URI
- * @schema: (nullable): return location for the schema of the URI
- * @host: (nullable): return location for the host of the URI
- * @path: (nullable): return location for the resource path
- *
- * Split the given URI into the fields.
- *
- * Returns: #TRUE if @uri could be parsed, #FALSE otherwise
- */
-gboolean
-uri_get_fields (const gchar  *uri,
-                gchar       **schema,
-                gchar       **host,
-                gchar       **path,
-                gboolean     *is_file)
-{
-  GRegex *regex;
-  GMatchInfo *match;
-  gboolean valid;
-
-  g_return_val_if_fail (uri != NULL, FALSE);
-
-  match = NULL;
-  valid = FALSE;
-
-  regex = g_regex_new 
("([a-zA-Z0-9\\+\\.\\-]*:\\/\\/){0,1}([-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b)([-a-zA-Z0-9@:%_\\+.//=]*)",
-                       G_REGEX_CASELESS, 0, NULL);
-
-  /*
-   * Retrieved matching URI. The whole url is
-   * checked and the regex groups are:
-   * 1. schema
-   * 2. host
-   * 3. server path
-   */
-  if (g_regex_match (regex, uri, 0, &match))
-    {
-      valid = TRUE;
-
-      if (schema != NULL)
-        *schema = g_match_info_fetch (match, 1);
-
-      if (host != NULL)
-        *host = g_match_info_fetch (match, 2);
-
-      if (path != NULL)
-        *path = g_match_info_fetch (match, 3);
-
-      g_match_info_free (match);
-    }
-  else
-    {
-      if (schema != NULL)
-        *schema = NULL;
-
-      if (host != NULL)
-        *host = NULL;
-
-      if (path != NULL)
-        *path = NULL;
-    }
-
-  /* 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;
-}
-
-/**
  * get_source_parent_name_color:
  * @manager: a #GcalManager
  * @source: an #ESource
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index 3b5bd9ac..99e7bb09 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -134,12 +134,6 @@ gsize           e_utf8_strftime_fix_am_pm                       (gchar
 
 void            fix_popover_menu_icons                          (GtkPopover            *popover);
 
-gboolean        uri_get_fields                                  (const gchar           *uri,
-                                                                 gchar                **schema,
-                                                                 gchar                **host,
-                                                                 gchar                **path,
-                                                                 gboolean              *is_file);
-
 void            get_source_parent_name_color                    (GcalManager           *manager,
                                                                  ESource               *source,
                                                                  gchar                **name,


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