Re: Exposing get_filename_charset



On Thu, 2004-10-28 at 10:42 -0400, Owen Taylor wrote:
> On Thu, 2004-10-28 at 10:52 +0200, Alexander Larsson wrote:
> > I want to update nautilus to correctly handle the new
> > G_FILENAME_ENCODING in addition to G_BROKEN_FILENAMES. Is there any
> > chance that get_filename_charset() in gconvert.c could be exposed in the
> > public API?
> 
> Well, you wouldn't want exactly that, perhaps, since G_FILENAME_ENCODING
> is defined as a comma-separated list of encodings and
> get_filename_charset() just returns the first one.
> 
> The other question is whether we could push down what you are doing
> in Nautilus to GLib with a 
> 'g_filename_display_name (const char *filename)' or something like
> that.

Yeah. That one is likely useful in general.
The local files part of nautilus_file_get_display_name currently goes
like:

if (has_local_path (file)) {
	utf8_filenames = eel_get_filename_charset (&filename_charset);
	if (utf8_filenames) {
		/* If not valid utf8, and filenames are utf8, test if converting
		   from the locale works */
		if (!g_utf8_validate (name, -1, NULL)) {
			utf8_name = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
			if (utf8_name != NULL) {
				g_free (name);
				name = utf8_name;
				/* Guaranteed to be correct utf8 here */
				validated = TRUE;
			}
		} else {
			/* name was valid, no need to re-validate */
			validated = TRUE;
		}
	} else {
		/* Try to convert from filename charset to utf8 */
		utf8_name = g_convert (name, -1, "UTF-8", filename_charset, NULL, NULL, NULL);
		if (utf8_name != NULL) {
			g_free (name);
			name = utf8_name;
			/* Guaranteed to be correct utf8 here */
			validated = TRUE;
		}
	}
} else {
    other cases for some forms of URIs
}

and in the end if conversion to utf8 failed for the uri:

if (!validated && !g_utf8_validate (name, -1, NULL)) {
	utf8_name = eel_make_valid_utf8 (name);
	g_free (name);
	name = utf8_name;
}

char *
eel_make_valid_utf8 (const 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 g_strdup (name);
	}

	g_string_append (string, remainder);
	g_string_append (string, _(" (invalid Unicode)"));
	g_assert (g_utf8_validate (string->str, -1, NULL));

	return g_string_free (string, FALSE);
}



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Alexander Larsson                                            Red Hat, Inc 
                   alexl redhat com    alla lysator liu se 
He's an underprivileged arachnophobic paranormal investigator who must take 
medication to keep him sane. She's a vivacious Bolivian journalist on her way 
to prison for a murder she didn't commit. They fight crime! 




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