gvfs r1425 - in trunk: . daemon



Author: otte
Date: Thu Feb 28 15:33:12 2008
New Revision: 1425
URL: http://svn.gnome.org/viewvc/gvfs?rev=1425&view=rev

Log:
2008-02-28  Benjamin Otte  <otte gnome org>

	* daemon/gvfsdaemonutils.c: (gvfs_file_info_populate_default),
	(gvfs_file_info_populate_names),
	(gvfs_file_info_populate_content_types):
	* daemon/gvfsdaemonutils.h:
	add functions that are supposed to set default values on a remote file
	info structure, so we don't duplicate the code across multiple
	backends.

	* daemon/gvfsbackendftp.c: (ftp_error_set_from_response),
	(ftp_connection_new), (file_info_get_flags), (file_info_query):
	use it.



Modified:
   trunk/ChangeLog
   trunk/daemon/gvfsbackendftp.c
   trunk/daemon/gvfsdaemonutils.c
   trunk/daemon/gvfsdaemonutils.h

Modified: trunk/daemon/gvfsbackendftp.c
==============================================================================
--- trunk/daemon/gvfsbackendftp.c	(original)
+++ trunk/daemon/gvfsbackendftp.c	Thu Feb 28 15:33:12 2008
@@ -43,6 +43,7 @@
 #include "gvfsjobqueryattributes.h"
 #include "gvfsjobenumerate.h"
 #include "gvfsdaemonprotocol.h"
+#include "gvfsdaemonutils.h"
 
 #if 1
 #define DEBUG g_print
@@ -118,7 +119,12 @@
    * but group responses with the same message. */
   switch (response)
     {
-
+      case 332: /* Need account for login. */
+      case 532: /* Need account for storing files. */
+	/* FIXME: implement a sane way to handle accounts. */
+	code = G_IO_ERROR_NOT_SUPPORTED;
+	msg = _("Accounts are unsupported");
+	break;
       case 421: /* Service not available, closing control connection. */
 	code = G_IO_ERROR_FAILED;
 	msg = _("Host closed connection");
@@ -151,8 +157,6 @@
       case 502: /* Command not implemented. */
       case 503: /* Bad sequence of commands. */
       case 504: /* Command not implemented for that parameter. */
-      case 532: /* Need account for storing files. */
-	/* FIXME: implement a sane way to handle accounts. */
 	code = G_IO_ERROR_NOT_SUPPORTED;
 	msg = _("Operation unsupported");
 	break;
@@ -491,30 +495,16 @@
   if (status == 0)
     goto fail;
 
-  status = ftp_connection_send (conn, RESPONSE_PASS_300, NULL,
+  status = ftp_connection_send (conn, RESPONSE_PASS_300, error,
                                 "USER %s", username);
   if (status == 0)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
-		   _("Invalid username"));
-      goto fail;
-    }
+    goto fail;
   else if (STATUS_GROUP (status) == 3)
     {
-      status = ftp_connection_send (conn, RESPONSE_PASS_300, NULL,
+      status = ftp_connection_send (conn, 0, error,
 				    "PASS %s", password);
       if (status == 0)
-	{
-	  g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
-		       _("Invalid password"));
-	  goto fail;
-	}
-      else if (STATUS_GROUP (status) == 3)
-	{
-	  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
-		       _("Accounts are not supported"));
-	  goto fail;
-	}
+	goto fail;
     }
 
   /* only binary transfers please */
@@ -1119,7 +1109,6 @@
 }
 
 typedef enum {
-  FILE_INFO_DISPLAY_NAME = (1 << 0),
   FILE_INFO_SIZE         = (1 << 1),
   FILE_INFO_MTIME	 = (1 << 2),
   FILE_INFO_TYPE         = (1 << 3)
@@ -1131,9 +1120,6 @@
 {
   FileInfoFlags flags = 0;
 
-  if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME))
-    flags |= FILE_INFO_DISPLAY_NAME;
-
   if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_STANDARD_SIZE) &&
       (conn->features& FTP_FEATURE_SIZE))
     flags |= FILE_INFO_SIZE;
@@ -1154,23 +1140,27 @@
 		 GFileInfo *     info,
 		 FileInfoFlags  flags)
 {
+  GFileType type;
   guint response;
 
   DEBUG ("query %s (flags %u)\n", filename, flags);
-  if (flags & FILE_INFO_DISPLAY_NAME)
-    {
-      char *display_name = g_filename_display_basename (filename);
-
-      if (strstr (display_name, "\357\277\275") != NULL)
-        {
-          char *p = display_name;
-          display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
-          g_free (p);
-        }
 
-      g_file_info_set_display_name (info, display_name);
-      g_free (display_name);
+  if (flags & FILE_INFO_TYPE)
+    {
+      /* kind of an evil trick here to determine the type.
+       * We cwd to the given filename.
+       * If it succeeds, it's a directroy, otherwise it's a file.
+       */
+      response = ftp_connection_send (conn, 0, NULL, "CWD %s", filename);
+      if (response == 0)
+	type = G_FILE_TYPE_REGULAR;
+      else
+	type = G_FILE_TYPE_DIRECTORY;
     }
+  else
+    type = G_FILE_TYPE_REGULAR;
+
+  gvfs_file_info_populate_default (info, filename, type);
 
   if (flags & FILE_INFO_SIZE)
     {
@@ -1204,18 +1194,6 @@
 	}
     }
 
-  if (flags & FILE_INFO_TYPE)
-    {
-      /* kind of an evil trick here to determine the type.
-       * We cwd to the given filename.
-       * If it succeeds, it's a directroy, otherwise it's a file.
-       */
-      response = ftp_connection_send (conn, 0, NULL, "CWD %s", filename);
-      if (response == 0)
-	g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR);
-      else
-	g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
-    }
 }
 
 static void

Modified: trunk/daemon/gvfsdaemonutils.c
==============================================================================
--- trunk/daemon/gvfsdaemonutils.c	(original)
+++ trunk/daemon/gvfsdaemonutils.c	Thu Feb 28 15:33:12 2008
@@ -146,3 +146,167 @@
   
   return buffer;
 }
+
+/**
+ * gvfs_file_info_populate_default:
+ * @info: file info to populate
+ * @name_string: a bytes string of possibly the full path to the given file
+ * @type: type of this file
+ *
+ * Calls gvfs_file_info_populate_names() and 
+ * gvfs_file_info_populate_content_types() on the given @name_string.
+ **/
+void
+gvfs_file_info_populate_default (GFileInfo  *info,
+                                 const char *name_string,
+			         GFileType   type)
+{
+  char *edit_name;
+
+  g_return_if_fail (G_IS_FILE_INFO (info));
+  g_return_if_fail (name_string != NULL);
+
+  edit_name = gvfs_file_info_populate_names (info, name_string);
+  gvfs_file_info_populate_content_types (info, edit_name, type);
+  g_free (edit_name);
+}
+
+/**
+ * gvfs_file_info_populate_names:
+ * @info: the file info to fill
+ * @name_string: a bytes string of possibly the full path to the given file
+ *
+ * Sets the name of the file info to @name_string and determines display and 
+ * edit name for it.
+ *
+ * Returns: the utf-8 encoded edit name for the given file.
+ **/
+char *
+gvfs_file_info_populate_names (GFileInfo  *info,
+                               const char *name_string)
+{
+  //const char *slash;
+  char *edit_name;
+
+  g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
+  g_return_val_if_fail (name_string != NULL, NULL);
+
+#if 0
+  slash = strrchr (name_string, '/');
+  if (slash && slash[1])
+    name_string = slash + 1;
+#endif
+  edit_name = g_filename_display_basename (name_string);
+  g_file_info_set_edit_name (info, edit_name);
+
+  if (strstr (edit_name, "\357\277\275") != NULL)
+    {
+      char *display_name;
+      
+      display_name = g_strconcat (edit_name, _(" (invalid encoding)"), NULL);
+      g_file_info_set_display_name (info, display_name);
+      g_free (display_name);
+    }
+  else
+    g_file_info_set_display_name (info, edit_name);
+
+  return edit_name;
+}
+
+/**
+ * gvfs_file_info_populate_content_types:
+ * @info: the file info to fill
+ * @basename: utf-8 encoded base name of file
+ * @type: type of this file
+ *
+ * Takes the base name and guesses content type and icon with it. This function
+ * is intended for remote files. Do not use it for directories.
+ **/
+void
+gvfs_file_info_populate_content_types (GFileInfo  *info,
+				       const char *basename,
+				       GFileType   type)
+{
+  char *free_mimetype = NULL;
+  const char *mimetype;
+  GIcon *icon;
+
+  g_return_if_fail (G_IS_FILE_INFO (info));
+  g_return_if_fail (basename != NULL);
+
+  g_file_info_set_file_type (info, type);
+
+  switch (type)
+    {
+      case G_FILE_TYPE_DIRECTORY:
+	mimetype = "inode/directory";
+	break;
+      case G_FILE_TYPE_SYMBOLIC_LINK:
+	mimetype = "inode/symlink";
+	break;
+      case G_FILE_TYPE_SPECIAL:
+	mimetype = "inode/special";
+	break;
+      case G_FILE_TYPE_SHORTCUT:
+	mimetype = "inode/shortcut";
+	break;
+      case G_FILE_TYPE_MOUNTABLE:
+	mimetype = "inode/mountable";
+	break;
+      case G_FILE_TYPE_REGULAR:
+	free_mimetype = g_content_type_guess (basename, NULL, 0, NULL);
+	mimetype = free_mimetype;
+	break;
+      case G_FILE_TYPE_UNKNOWN:
+      default:
+        mimetype = "application/octet-stream";
+	break;
+    }
+
+  g_file_info_set_content_type (info, mimetype);
+  g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, mimetype);
+      
+  if (type == G_FILE_TYPE_DIRECTORY)
+    /* FIXME: or just folder? */
+    icon = g_themed_icon_new ("folder-remote");
+  else if (mimetype)
+    {
+      char *mimetype_icon, *generic_mimetype_icon, *type_icon;
+      const char *p;
+      char *icon_names[3];
+      int i;
+      
+      mimetype_icon = g_strdup (mimetype);
+      g_strdelimit (mimetype_icon, "/", '-');
+      
+      p = strchr (mimetype, '/');
+      if (p == NULL)
+	p = mimetype + strlen (mimetype);
+      
+      generic_mimetype_icon = g_malloc (p - mimetype + strlen ("-x-generic") + 1);
+      memcpy (generic_mimetype_icon, mimetype, p - mimetype);
+      memcpy (generic_mimetype_icon + (p - mimetype), "-x-generic", strlen ("-x-generic"));
+      generic_mimetype_icon[(p - mimetype) + strlen ("-x-generic")] = 0;
+      
+      type_icon = "text-x-generic";
+      
+      i = 0;
+      icon_names[i++] = mimetype_icon;
+      icon_names[i++] = generic_mimetype_icon;
+      if (strcmp (generic_mimetype_icon, type_icon) != 0 &&
+	  strcmp (mimetype_icon, type_icon) != 0) 
+	icon_names[i++] = type_icon;
+
+      icon = g_themed_icon_new_from_names (icon_names, i);
+      
+      g_free (mimetype_icon);
+      g_free (generic_mimetype_icon);
+      
+    }
+
+  g_file_info_set_icon (info, icon);
+  g_object_unref (icon);
+
+  g_free (free_mimetype);
+}
+

Modified: trunk/daemon/gvfsdaemonutils.h
==============================================================================
--- trunk/daemon/gvfsdaemonutils.h	(original)
+++ trunk/daemon/gvfsdaemonutils.h	Thu Feb 28 15:33:12 2008
@@ -38,6 +38,15 @@
 						   guint32           seq_nr,
 						   gsize            *len_out);
 
+void	     gvfs_file_info_populate_default	  (GFileInfo        *info,
+						   const char       *name_string,
+						   GFileType	     type);
+char *	     gvfs_file_info_populate_names	  (GFileInfo        *info,
+						   const char       *name_string);
+void	     gvfs_file_info_populate_content_types(GFileInfo        *info,
+						   const char       *basename,
+						   GFileType	     type);
+
 G_END_DECLS
 
 #endif /* __G_VFS_DAEMON_UTILS_H__ */



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