changeset 184:65eb40fb4f03

2003-6-16 Brian Masney <masneyb@gftp.org> * lib/gftp.h lib/misc.c lib/options.h lib/protocols.c src/gtk/misc-gtk.c - added remote_charsets option. Whenever a file is read in that is not in UTF-8, it will first attempt to convert it from the local charset, and if that fails, it will try each of the locales in this list. I have no idea if this even works, so if someone can test this, please let me know.
author masneyb
date Tue, 17 Jun 2003 02:02:42 +0000
parents 9d42809f9fb4
children cc94bd62d290
files ChangeLog lib/gftp.h lib/misc.c lib/options.h lib/protocols.c src/gtk/misc-gtk.c
diffstat 6 files changed, 111 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jun 16 16:32:24 2003 +0000
+++ b/ChangeLog	Tue Jun 17 02:02:42 2003 +0000
@@ -1,3 +1,11 @@
+2003-6-16 Brian Masney <masneyb@gftp.org>
+	* lib/gftp.h lib/misc.c lib/options.h lib/protocols.c
+	src/gtk/misc-gtk.c - added remote_charsets option. Whenever a file is
+	read in that is not in UTF-8, it will first attempt to convert it from
+	the local charset, and if that fails, it will try each of the locales
+	in this list. I have no idea if this even works, so if someone can test
+	this, please let me know.
+
 2003-6-15 Brian Masney <masneyb@gftp.org>
 	* lib/cache.c lib/gftp.h - added gftp_generate_cache_description(). 
 
@@ -995,7 +1003,7 @@
 
 	* cvsclean - added this script
 
-	* *.[ch] - added $Id: ChangeLog,v 1.89 2003/06/15 21:28:00 masneyb Exp $ tags
+	* *.[ch] - added $Id: ChangeLog,v 1.90 2003/06/17 02:02:40 masneyb Exp $ tags
 
 	* debian/* - updated files from Debian maintainer
 
--- a/lib/gftp.h	Mon Jun 16 16:32:24 2003 +0000
+++ b/lib/gftp.h	Tue Jun 17 02:02:42 2003 +0000
@@ -178,6 +178,9 @@
 struct gftp_file_tag 
 {
   char *file,			/* Our filename */
+       *utf8_file,		/* UTF-8 encoded filename for display purposes
+				   only. This is only set if file is not in 
+				   UTF-8 */
        *user,			/* User that owns it */
        *group,			/* Group that owns it */
        *attribs,		/* Attribs (-rwxr-x-rx) */
@@ -417,6 +420,11 @@
 #ifdef USE_SSL
   SSL * ssl;
 #endif
+
+#if GLIB_MAJOR_VERSION > 1
+  GIConv iconv; 
+  unsigned int iconv_initialized : 1;
+#endif
 };
 
 
--- a/lib/misc.c	Mon Jun 16 16:32:24 2003 +0000
+++ b/lib/misc.c	Tue Jun 17 02:02:42 2003 +0000
@@ -436,6 +436,8 @@
 {
   if (fle->file)
     g_free (fle->file);
+  if (fle->utf8_file)
+    g_free (fle->utf8_file);
   if (fle->user)
     g_free (fle->user);
   if (fle->group)
@@ -926,7 +928,9 @@
     file_suffixstr = "";
 
   ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, 
-                         file_prefixstr, fle->file, file_suffixstr);
+                         file_prefixstr, 
+                         fle->utf8_file != NULL ? fle->utf8_file : fle->file,
+                         file_suffixstr);
 
   g_free (tempstr1);
   g_free (tempstr2);
--- a/lib/options.h	Mon Jun 16 16:32:24 2003 +0000
+++ b/lib/options.h	Tue Jun 17 02:02:42 2003 +0000
@@ -45,6 +45,10 @@
    gftp_option_type_int, 0, NULL, 0, 
    N_("The maximum size of the log window in bytes for the GTK+ port"), 
    GFTP_PORT_GTK, NULL},
+  {"remote_charsets", N_("Remote Character Sets:"), 
+   gftp_option_type_text, "", NULL, 0,
+   N_("The character sets to try to convert the remote filenames to the current locale"), 
+   GFTP_PORT_ALL, NULL},
 
   {"append_transfers", N_("Append file transfers"), 
   gftp_option_type_checkbox, GINT_TO_POINTER(1), NULL, 0,
--- a/lib/protocols.c	Mon Jun 16 16:32:24 2003 +0000
+++ b/lib/protocols.c	Tue Jun 17 02:02:42 2003 +0000
@@ -282,6 +282,12 @@
       request->last_dir_entry_len = 0;
     }
 
+  if (request->iconv_initialized)
+    {
+      g_iconv_close (request->iconv);
+      request->iconv_initialized = 0;
+    }
+
   return (ret);
 }
 
@@ -328,15 +334,84 @@
 }
 
 
+#if GLIB_MAJOR_VERSION > 1
+static char *
+_gftp_get_next_charset (char *remote_charsets, char **curpos)
+{
+  char *ret, *endpos;
+
+  if (**curpos == '\0')
+    return (NULL);
+
+  ret = *curpos;
+  if (*curpos != remote_charsets)
+    *(*curpos - 1) = '\0';
+
+  if ((endpos = strchr (*curpos, ' ')) == NULL)
+    *curpos += strlen (*curpos);
+  else
+    {
+      *endpos = '\0';
+      *curpos = endpos + 1;
+    }
+
+  return (ret);
+}
+
+
+static char *
+gftp_string_to_utf8 (gftp_request * request, char *str)
+{
+  char *ret, *remote_charsets, *stpos, *cur_charset;
+  gsize bread, bwrite;
+  GError * error;
+
+  if (request->iconv_initialized)
+    return (g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite, 
+                                  &error));
+
+  gftp_lookup_request_option (request, "remote_charsets", &remote_charsets);
+  if (*remote_charsets == '\0')
+    {
+      error = NULL;
+      if ((ret = g_locale_to_utf8 (str, -1, &bread, &bwrite, &error)) != NULL)
+        return (ret);
+
+      return (NULL);
+    }
+
+  ret = NULL;
+  stpos = remote_charsets;
+  while ((cur_charset = _gftp_get_next_charset (remote_charsets, 
+                                                &stpos)) != NULL)
+    {
+      if ((request->iconv = g_iconv_open ("UTF-8", cur_charset)) == (GIConv) -1)
+        continue;
+
+      error = NULL;
+      if ((ret = g_convert_with_iconv (str, -1, request->iconv, &bread, &bwrite,
+                                       &error)) == NULL)
+        {
+          g_iconv_close (request->iconv);
+          request->iconv = NULL;
+          continue;
+        }
+      else
+        {
+          request->iconv_initialized = 1;
+          break;
+        }
+    }
+
+  return (ret);
+}
+#endif
+
+
 int
 gftp_get_next_file (gftp_request * request, char *filespec, gftp_file * fle)
 {
   int fd, ret;
-#if GLIB_MAJOR_VERSION > 1
-  gsize bread, bwrite;
-  char *tempstr;
-  GError * error;
-#endif
 
   g_return_val_if_fail (request != NULL, GFTP_EFATAL);
 
@@ -356,18 +431,7 @@
 
 #if GLIB_MAJOR_VERSION > 1
       if (ret >= 0 && fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL))
-        {
-          error = NULL;
-          if ((tempstr = g_locale_to_utf8 (fle->file, -1, &bread, 
-                                           &bwrite, &error)) != NULL)
-            {
-              g_free (fle->file);
-              fle->file = tempstr;
-            }
-          else
-            g_warning ("Error when converting %s to UTF-8: %s\n", fle->file,
-                       error->message);
-        }
+        fle->utf8_file = gftp_string_to_utf8 (request, fle->file);
 #endif
 
       if (ret >= 0 && !request->cached && request->cachefd > 0 && 
--- a/src/gtk/misc-gtk.c	Mon Jun 16 16:32:24 2003 +0000
+++ b/src/gtk/misc-gtk.c	Tue Jun 17 02:02:42 2003 +0000
@@ -770,7 +770,10 @@
    
   gtk_clist_set_pixmap (GTK_CLIST (wdata->listbox), clist_num, 0, pix, bitmap);
 
-  if (fle->file)
+  if (fle->utf8_file)
+    gtk_clist_set_text (GTK_CLIST (wdata->listbox), clist_num, 1, 
+                        fle->utf8_file);
+  else if (fle->file)
     gtk_clist_set_text (GTK_CLIST (wdata->listbox), clist_num, 1, fle->file);
 
   if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c'))