Browse Source

Added vfs_change_encoding() and vfs_path_element_need_cleanup_converter() functions.

Changes for handling directory encoding.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
Slava Zanko 13 years ago
parent
commit
a5dc2de78b
6 changed files with 70 additions and 43 deletions
  1. 3 0
      lib/vfs/interface.c
  2. 26 1
      lib/vfs/path.c
  3. 2 0
      lib/vfs/path.h
  4. 30 0
      lib/vfs/vfs.c
  5. 2 0
      lib/vfs/vfs.h
  6. 7 42
      src/filemanager/panel.c

+ 3 - 0
lib/vfs/interface.c

@@ -536,7 +536,10 @@ mc_closedir (DIR * dirp)
         vfs_path_element_t *vfs_path_element;
         vfs_path_element = vfs_class_data_find_by_handle (handle);
         if (vfs_path_element->dir.converter != str_cnv_from_term)
+        {
             str_close_conv (vfs_path_element->dir.converter);
+            vfs_path_element->dir.converter = INVALID_CONV;
+        }
 
         result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
         vfs_free_handle (handle);

+ 26 - 1
lib/vfs/path.c

@@ -310,8 +310,11 @@ vfs_path_from_str (const char *path_str)
     {
         element = g_new0 (vfs_path_element_t, 1);
         element->class = g_ptr_array_index (vfs__classes_list, 0);
-        element->path = g_strdup (path);
+        element->path = vfs_translate_path_n (path);
         element->raw_url_str = NULL;
+
+        element->encoding = vfs_get_encoding (path);
+        element->dir.converter = INVALID_CONV;
         vpath->path = g_list_prepend (vpath->path, element);
     }
     g_free (path);
@@ -391,6 +394,12 @@ vfs_path_element_free (vfs_path_element_t * element)
     vfs_url_free (element->url);
     g_free (element->path);
     g_free (element->encoding);
+
+    if (vfs_path_element_need_cleanup_converter (element))
+    {
+        str_close_conv (element->dir.converter);
+    }
+
     g_free (element);
 }
 
@@ -440,3 +449,19 @@ vfs_prefix_to_class (const char *prefix)
 }
 
 /* --------------------------------------------------------------------------------------------- */
+
+/**
+ * Check if need cleanup charset converter for vfs_path_element_t
+ *
+ * @param element part of path
+ *
+ * @return TRUE if need cleanup converter or FALSE otherwise
+ */
+
+gboolean
+vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
+{
+    return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
+}
+
+/* --------------------------------------------------------------------------------------------- */

+ 2 - 0
lib/vfs/path.h

@@ -51,6 +51,8 @@ void vfs_path_element_free (vfs_path_element_t * element);
 
 struct vfs_class *vfs_prefix_to_class (const char *prefix);
 
+gboolean vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element);
+
 /*** inline functions ****************************************************************************/
 
 #endif

+ 30 - 0
lib/vfs/vfs.c

@@ -683,3 +683,33 @@ _vfs_get_cwd (void)
 }
 
 /* --------------------------------------------------------------------------------------------- */
+
+/**
+ * Change encoding for last part (vfs_path_element_t) of vpath
+ *
+ * @param vpath pointer to path structure
+ * encoding name of charset
+ *
+ * @return pointer to path structure (for use function in anoter functions)
+ */
+vfs_path_t *
+vfs_change_encoding (vfs_path_t * vpath, const char *encoding)
+{
+    vfs_path_element_t *path_element = vfs_path_get_by_index (vpath, -1);
+
+    /* don't add current encoding */
+    if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
+        return vpath;
+
+    g_free (path_element->encoding);
+    path_element->encoding = g_strdup (encoding);
+
+    if (vfs_path_element_need_cleanup_converter (path_element))
+        str_close_conv (path_element->dir.converter);
+
+    path_element->dir.converter = str_crt_conv_from (path_element->encoding);
+
+    return vpath;
+}
+
+/* --------------------------------------------------------------------------------------------- */

+ 2 - 0
lib/vfs/vfs.h

@@ -272,6 +272,8 @@ void vfs_free_handle (int handle);
 
 char *_vfs_get_cwd (void);
 
+vfs_path_t *vfs_change_encoding (vfs_path_t * vpath, const char *encoding);
+
 /**
  * Interface functions described in interface.c
  */

+ 7 - 42
src/filemanager/panel.c

@@ -1147,39 +1147,6 @@ adjust_top_file (WPanel * panel)
  * retun new string
  */
 
-static char *
-add_encoding_to_path (const char *path, const char *encoding)
-{
-    char *result;
-    char *semi;
-    char *slash;
-
-    semi = g_strrstr (path, VFS_ENCODING_PREFIX);
-
-    if (semi != NULL)
-    {
-        slash = strchr (semi, PATH_SEP);
-        if (slash != NULL)
-        {
-            result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
-        }
-        else
-        {
-            *semi = '\0';
-            result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
-            *semi = '#';
-        }
-    }
-    else
-    {
-        result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
-    }
-
-    return result;
-}
-
-/* --------------------------------------------------------------------------------------------- */
-
 static char *
 panel_save_name (WPanel * panel)
 {
@@ -4051,16 +4018,14 @@ panel_change_encoding (WPanel * panel)
     if (encoding != NULL)
     {
         vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
-        vfs_path_element_t *path_element = vfs_path_get_by_index (vpath, -1);
 
-        /* don't add current encoding */
-        if ((path_element->encoding == NULL) || (strcmp (encoding, path_element->encoding) != 0))
-        {
-            cd_path = add_encoding_to_path (panel->cwd, encoding);
-            if (!do_panel_cd (panel, cd_path, cd_parse_command))
-                message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
-            g_free (cd_path);
-        }
+        vfs_change_encoding (vpath, encoding);
+
+        cd_path = vfs_path_to_str (vpath);
+        if (!do_panel_cd (panel, cd_path, cd_parse_command))
+            message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
+        g_free (cd_path);
+
         vfs_path_free (vpath);
     }
 }