[nautilus] Set a better file display name by default



commit 874f69f244ba397925fe71f9500ac21bed761bb0
Author: William Jon McCann <jmccann redhat com>
Date:   Mon Aug 27 22:56:35 2012 -0400

    Set a better file display name by default
    
    https://bugzilla.gnome.org/show_bug.cgi?id=524116

 libnautilus-private/nautilus-directory.c      |   25 +++-
 libnautilus-private/nautilus-file-utilities.c |  207 +++++++++++++++++++++++++
 libnautilus-private/nautilus-file-utilities.h |    5 +
 3 files changed, 231 insertions(+), 6 deletions(-)
---
diff --git a/libnautilus-private/nautilus-directory.c b/libnautilus-private/nautilus-directory.c
index 219cc33..5ac71c7 100644
--- a/libnautilus-private/nautilus-directory.c
+++ b/libnautilus-private/nautilus-directory.c
@@ -38,6 +38,7 @@
 #include "nautilus-vfs-directory.h"
 #include <eel/eel-glib-extensions.h>
 #include <eel/eel-string.h>
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
 enum {
@@ -453,19 +454,31 @@ nautilus_directory_get_existing_corresponding_file (NautilusDirectory *directory
 char *
 nautilus_directory_get_name_for_self_as_new_file (NautilusDirectory *directory)
 {
+	GFile *file;
 	char *directory_uri;
-	char *name, *colon;
-	
+	char *scheme;
+	char *name;
+	char *hostname = NULL;
+
 	directory_uri = nautilus_directory_get_uri (directory);
+	file = g_file_new_for_uri (directory_uri);
+	scheme = g_file_get_uri_scheme (file);
+	g_object_unref (file);
 
-	colon = strchr (directory_uri, ':');
-	if (colon == NULL || colon == directory_uri) {
+	nautilus_uri_parse (directory_uri, &hostname, NULL, NULL);
+	if (hostname == NULL) {
 		name = g_strdup (directory_uri);
+	} else if (scheme == NULL) {
+		name = g_strdup (hostname);
 	} else {
-		name = g_strndup (directory_uri, colon - directory_uri);
+		/* Translators: this is of the format "hostname (uri-scheme)" */
+		name = g_strdup_printf (_("%s (%s)"), hostname, scheme);
 	}
+
 	g_free (directory_uri);
-	
+	g_free (scheme);
+	g_free (hostname);
+
 	return name;
 }
 
diff --git a/libnautilus-private/nautilus-file-utilities.c b/libnautilus-private/nautilus-file-utilities.c
index 8cb3359..35a43d6 100644
--- a/libnautilus-private/nautilus-file-utilities.c
+++ b/libnautilus-private/nautilus-file-utilities.c
@@ -54,6 +54,213 @@ static void update_xdg_dir_cache (void);
 static void schedule_user_dirs_changed (void);
 static void desktop_dir_changed (void);
 
+
+/* Allowed characters outside alphanumeric for unreserved. */
+#define G_URI_OTHER_UNRESERVED "-._~"
+
+/* This or something equivalent will eventually go into glib/guri.h */
+gboolean
+nautilus_uri_parse (const char  *uri,
+		    char       **host,
+		    guint16     *port,
+		    char       **userinfo)
+{
+  char *tmp_str;
+  const char *start, *p;
+  char c;
+
+  g_return_val_if_fail (uri != NULL, FALSE);
+
+  if (host)
+    *host = NULL;
+
+  if (port)
+    *port = 0;
+
+  if (userinfo)
+    *userinfo = NULL;
+
+  /* From RFC 3986 Decodes:
+   * URI          = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
+   * hier-part    = "//" authority path-abempty
+   * path-abempty = *( "/" segment )
+   * authority    = [ userinfo "@" ] host [ ":" port ]
+   */
+
+  /* Check we have a valid scheme */
+  tmp_str = g_uri_parse_scheme (uri);
+
+  if (tmp_str == NULL)
+    return FALSE;
+
+  g_free (tmp_str);
+
+  /* Decode hier-part:
+   *  hier-part   = "//" authority path-abempty
+   */
+  p = uri;
+  start = strstr (p, "//");
+
+  if (start == NULL)
+    return FALSE;
+
+  start += 2;
+
+  if (strchr (start, '@') != NULL)
+    {
+      /* Decode userinfo:
+       * userinfo      = *( unreserved / pct-encoded / sub-delims / ":" )
+       * unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
+       * pct-encoded   = "%" HEXDIG HEXDIG
+       */
+      p = start;
+      while (1)
+	{
+	  c = *p++;
+
+	  if (c == '@')
+	    break;
+
+	  /* pct-encoded */
+	  if (c == '%')
+	    {
+	      if (!(g_ascii_isxdigit (p[0]) ||
+		    g_ascii_isxdigit (p[1])))
+		return FALSE;
+
+	      p++;
+
+	      continue;
+	    }
+
+	  /* unreserved /  sub-delims / : */
+	  if (!(g_ascii_isalnum (c) ||
+		strchr (G_URI_OTHER_UNRESERVED, c) ||
+		strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c) ||
+		c == ':'))
+	    return FALSE;
+	}
+
+      if (userinfo)
+	*userinfo = g_strndup (start, p - start - 1);
+
+      start = p;
+    }
+  else
+    {
+      p = start;
+    }
+
+
+  /* decode host:
+   * host          = IP-literal / IPv4address / reg-name
+   * reg-name      = *( unreserved / pct-encoded / sub-delims )
+   */
+
+  /* If IPv6 or IPvFuture */
+  if (*p == '[')
+    {
+      start++;
+      p++;
+      while (1)
+	{
+	  c = *p++;
+
+	  if (c == ']')
+	    break;
+
+	  /* unreserved /  sub-delims */
+	  if (!(g_ascii_isalnum (c) ||
+		strchr (G_URI_OTHER_UNRESERVED, c) ||
+		strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c) ||
+		c == ':' ||
+		c == '.'))
+	    goto error;
+	}
+    }
+  else
+    {
+      while (1)
+	{
+	  c = *p++;
+
+	  if (c == ':' ||
+	      c == '/' ||
+	      c == '?' ||
+	      c == '#' ||
+	      c == '\0')
+	    break;
+
+	  /* pct-encoded */
+	  if (c == '%')
+	    {
+	      if (!(g_ascii_isxdigit (p[0]) ||
+		    g_ascii_isxdigit (p[1])))
+		goto error;
+
+	      p++;
+
+	      continue;
+	    }
+
+	  /* unreserved /  sub-delims */
+	  if (!(g_ascii_isalnum (c) ||
+		strchr (G_URI_OTHER_UNRESERVED, c) ||
+		strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c)))
+	    goto error;
+	}
+    }
+
+  if (host)
+    *host = g_uri_unescape_segment (start, p - 1, NULL);
+
+  if (c == ':')
+    {
+      /* Decode pot:
+       *  port          = *DIGIT
+       */
+      guint tmp = 0;
+
+      while (1)
+	{
+	  c = *p++;
+
+	  if (c == '/' ||
+	      c == '?' ||
+	      c == '#' ||
+	      c == '\0')
+	    break;
+
+	  if (!g_ascii_isdigit (c))
+	    goto error;
+
+	  tmp = (tmp * 10) + (c - '0');
+
+	  if (tmp > 65535)
+	    goto error;
+	}
+      if (port)
+	*port = (guint16) tmp;
+    }
+
+  return TRUE;
+
+error:
+  if (host && *host)
+    {
+      g_free (*host);
+      *host = NULL;
+    }
+
+  if (userinfo && *userinfo)
+    {
+      g_free (*userinfo);
+      *userinfo = NULL;
+    }
+
+  return FALSE;
+}
+
 char *
 nautilus_compute_title_for_location (GFile *location)
 {
diff --git a/libnautilus-private/nautilus-file-utilities.h b/libnautilus-private/nautilus-file-utilities.h
index 3d1b41c..009fc3d 100644
--- a/libnautilus-private/nautilus-file-utilities.h
+++ b/libnautilus-private/nautilus-file-utilities.h
@@ -60,6 +60,11 @@ char *	 nautilus_compute_title_for_location	     (GFile *file);
 
 gboolean nautilus_is_file_roller_installed           (void);
 
+gboolean nautilus_uri_parse                          (const char  *uri,
+						      char       **host,
+						      guint16     *port,
+						      char       **userinfo);
+
 /* Return an allocated file name that is guranteed to be unique, but
  * tries to make the name readable to users.
  * This isn't race-free, so don't use for security-related things



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