[easytag/wip/unstable: 30/33] Simplify filename_to_display()



commit 004a3404d808499b4908d732026e8ae8f03c3463
Author: David King <amigadave amigadave com>
Date:   Wed Jan 9 22:16:56 2013 +0000

    Simplify filename_to_display()
    
    Use g_filename_to_utf8() to convert filenames from the GLib filename
    encoding to UTF-8 for display to the user.

 src/charset.c |   55 +++++++++++++++++++------------------------------------
 1 files changed, 19 insertions(+), 36 deletions(-)
---
diff --git a/src/charset.c b/src/charset.c
index 081e44a..b84910c 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -473,10 +473,13 @@ gchar *convert_to_utf8 (const gchar *string)
 }
 
 /*
- * Convert a string from the filename system encoding to UTF-8.
- *  - conversion OK : returns the UTF-8 string (new allocated)
- *  - conversion KO : tries others encodings else returns an 'escaped' string
- */
+ * filename_to_display:
+ * @string: the string to convert
+ *
+ * Convert a string from the GLib filename encoding to UTF-8. If the conversion
+ * failed, an escaped string will be returned instead.
+ *
+ * Returns: a newly-allocated UTF-8 string. */
 gchar *
 filename_to_display (const gchar *string)
 {
@@ -485,39 +488,19 @@ filename_to_display (const gchar *string)
 
     g_return_val_if_fail (string != NULL, NULL);
 
-    if (g_utf8_validate(string, -1, NULL))
-    {
-        // String already in UTF-8
-        ret = g_strdup(string);
-    }else
-    {
-        const gchar *char_encoding;
-
-        // Get encoding associated to the locale without using UTF-8 (ex , if LANG=fr_FR.UTF-8 it will return ISO-8859-1)
-        char_encoding = get_encoding_from_locale(get_locale());
-        if (char_encoding)
-        {
-            //g_print("> char_encoding: %s\n",char_encoding);
-            error = NULL;
-            ret = g_convert(string, -1, "UTF-8", char_encoding, NULL, NULL, &error);
-        }
-
-        if (!ret)
-        {
-            // Failing that, try ISO-8859-1
-            error = NULL;
-            ret = g_convert(string, -1, "UTF-8", "ISO-8859-1", NULL, NULL, &error);
-        }
+    ret = g_filename_to_utf8 (string, -1, NULL, NULL, &error);
 
-        if (!ret)
-        {
-            gchar *escaped_str = g_strescape(string, NULL);
-            Log_Print(LOG_ERROR,_("The filename '%s' couldn't be converted into UTF-8 (%s)."),
-                        escaped_str, error && error->message ? error->message : _("Invalid UTF-8"));
-            g_clear_error(&error);
-
-            ret = escaped_str;
-        }
+    if (error)
+    {
+        gchar *escaped_str = g_strescape (string, NULL);
+        Log_Print (LOG_ERROR,
+                   _("The filename '%s' could not be converted into UTF-8 (%s)"),
+                   escaped_str,
+                   error && error->message ? error->message : _("Invalid UTF-8"));
+        g_clear_error (&error);
+
+        /* ret is NULL if error is set. */
+        ret = escaped_str;
     }
 
 #ifdef G_OS_WIN32


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