diff options
author | Darin Adler <darin@src.gnome.org> | 2002-01-09 23:36:35 +0000 |
---|---|---|
committer | Darin Adler <darin@src.gnome.org> | 2002-01-09 23:36:35 +0000 |
commit | c2db1e57900049794d5e1a5c7c4562f7d76aac47 (patch) | |
tree | 0c4425dc6bacacae898600836b8b98eb6d24d508 | |
parent | 47c7d71426d427b0c38149a7a77446ef279c3cd1 (diff) | |
download | nautilus-c2db1e57900049794d5e1a5c7c4562f7d76aac47.tar.gz |
First cut at ensuring file names are always valid UTF-8. We probably need
* libnautilus-private/nautilus-file.c: (make_valid_utf8),
(nautilus_file_get_display_name), (nautilus_file_get_name): First
cut at ensuring file names are always valid UTF-8. We probably
need other checks like this for various other places we read in
external stuff. Also, we might later decide to cache the "made
valid" name. Finally, we might want to support the glib
G_BROKEN_FILENAMES feature.
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | libnautilus-private/nautilus-file.c | 68 |
2 files changed, 62 insertions, 16 deletions
@@ -1,5 +1,15 @@ 2002-01-09 Darin Adler <darin@bentspoon.com> + * libnautilus-private/nautilus-file.c: (make_valid_utf8), + (nautilus_file_get_display_name), (nautilus_file_get_name): First + cut at ensuring file names are always valid UTF-8. We probably + need other checks like this for various other places we read in + external stuff. Also, we might later decide to cache the "made + valid" name. Finally, we might want to support the glib + G_BROKEN_FILENAMES feature. + +2002-01-09 Darin Adler <darin@bentspoon.com> + * src/file-manager/fm-directory-view.c: (fm_directory_view_destroy), (fm_directory_view_finalize), (fm_directory_view_class_init): diff --git a/libnautilus-private/nautilus-file.c b/libnautilus-private/nautilus-file.c index 5eb5ff733..3a254f3bf 100644 --- a/libnautilus-private/nautilus-file.c +++ b/libnautilus-private/nautilus-file.c @@ -2259,9 +2259,49 @@ nautilus_file_set_integer_metadata (NautilusFile *file, metadata); } +static char * +make_valid_utf8 (char *name) +{ + GString *string; + const char *remainder, *invalid; + int remaining_bytes, valid_bytes; + + string = NULL; + remainder = name; + remaining_bytes = strlen (name); + + while (remaining_bytes != 0) { + if (g_utf8_validate (remainder, remaining_bytes, &invalid)) { + break; + } + valid_bytes = invalid - remainder; + + if (string == NULL) { + string = g_string_sized_new (remaining_bytes); + } + g_string_append_len (string, remainder, valid_bytes); + g_string_append_c (string, '?'); + + remaining_bytes -= valid_bytes + 1; + remainder = invalid + 1; + } + + if (string == NULL) { + return name; + } + + g_string_append (string, remainder); + g_string_append (string, _(" (invalid Unicode)")); + g_assert (g_utf8_validate (string->str, -1, NULL)); + g_free (name); + return g_string_free (string, FALSE); +} + char * nautilus_file_get_display_name (NautilusFile *file) { + char *name; + if (file == NULL) { return NULL; } @@ -2269,30 +2309,26 @@ nautilus_file_get_display_name (NautilusFile *file) g_return_val_if_fail (NAUTILUS_IS_FILE (file), NULL); if (file->details->got_link_info && file->details->display_name != NULL) { - return g_strdup (file->details->display_name); + name = g_strdup (file->details->display_name); } else { - return nautilus_file_get_name (file); + name = nautilus_file_get_name (file); + if (name == NULL) { + /* Fall back to the escaped form if the unescaped form is no + * good. This is dangerous for people who code with the name, + * but convenient for people who just want to display it. + */ + name = g_strdup (file->details->relative_uri); + } } -} + return make_valid_utf8 (name); +} char * nautilus_file_get_name (NautilusFile *file) { - char *name; - - name = gnome_vfs_unescape_string (file->details->relative_uri, "/"); - if (name != NULL) { - return name; - } - - /* Fall back to the escaped form if the unescaped form is no - * good. This is dangerous for people who code with the name, - * but convenient for people who just want to display it. - */ - return g_strdup (file->details->relative_uri); + return gnome_vfs_unescape_string (file->details->relative_uri, "/"); } - void nautilus_file_monitor_add (NautilusFile *file, |