changeset 4089:9e24c8746d99

- introduce new API functions uri_to_display_basename() and uri_to_display_dirname(). each function derives utf8 encoded basename or dirname from given uri respectively. - input plugins can use these functions as aud_uri_to_display_basename() / aud_uri_to_display_dirname() on associating FIELD_FILE_NAME / FIELD_FILE_PATH.
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Mon, 17 Dec 2007 21:56:17 +0900
parents 2b1bba93f726
children 4d4691fdebe1
files src/audacious/input.c src/audacious/plugin.h src/audacious/pluginenum.c src/audacious/strings.c src/audacious/strings.h src/audacious/tuple.c src/audacious/tuple.h
diffstat 7 files changed, 70 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/audacious/input.c	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/input.c	Mon Dec 17 21:56:17 2007 +0900
@@ -570,14 +570,16 @@
     ProbeResult *pr;
 
     if (filename == NULL)
-    return NULL;
+        return NULL;
 
     filename_proxy = g_strdup(filename);
 
     pr = input_check_file(filename_proxy, FALSE);
 
-    if (!pr)
+    if (!pr) {
+        g_free(filename_proxy);
         return NULL;
+    }
 
     ip = pr->ip;
 
@@ -587,23 +589,24 @@
         input = ip->get_song_tuple(filename_proxy);
     else
     {
-        gchar *tmp;
+        gchar *scratch;
 
-        input = tuple_new();
+        scratch = uri_to_display_basename(filename);
+        tuple_associate_string(input, FIELD_FILE_NAME, NULL, scratch);
+        g_free(scratch);
 
-        tmp = g_strdup(filename);
-        if ((ext = strrchr(tmp, '.')))
-            *ext = '\0';
+        scratch = uri_to_display_dirname(filename);
+        tuple_associate_string(input, FIELD_FILE_PATH, NULL, scratch);
+        g_free(scratch);
 
-        tuple_associate_string(input, FIELD_FILE_NAME, NULL, g_path_get_basename(tmp));
+        ext = strrchr(filename, '.');
+        if (ext != NULL) {
+            ++ext;
+            tuple_associate_string(input, FIELD_FILE_EXT, NULL, ext);
+        }
 
-        if (ext)
-            tuple_associate_string(input, FIELD_FILE_EXT, NULL, ext + 1);
-
-        tuple_associate_string(input, FIELD_FILE_PATH, NULL, g_path_get_dirname(tmp));
         tuple_associate_int(input, FIELD_LENGTH, NULL, -1);
 
-        g_free(tmp);
     }
 
     g_free(filename_proxy);
--- a/src/audacious/plugin.h	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/plugin.h	Mon Dec 17 21:56:17 2007 +0900
@@ -592,6 +592,9 @@
     void (*input_get_volume)(gint * l, gint * r);
 
     gchar *(*construct_uri)(gchar *string, const gchar *playlist_name);
+    gchar *(*uri_to_display_basename)(const gchar * uri);
+    gchar *(*uri_to_display_dirname)(const gchar * uri);
+
 };
 
 /* Convenience macros for accessing the public API. */
@@ -934,7 +937,8 @@
 #define aud_input_get_volume			_audvt->input_get_volume
 
 #define aud_construct_uri        _audvt->construct_uri
-
+#define aud_uri_to_display_basename _audvt->uri_to_display_basename
+#define aud_uri_to_display_dirname _audvt->uri_to_display_dirname
 
 #include "audacious/auddrct.h"
 
--- a/src/audacious/pluginenum.c	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/pluginenum.c	Mon Dec 17 21:56:17 2007 +0900
@@ -389,6 +389,9 @@
 
     .input_get_volume = input_get_volume,
     .construct_uri = construct_uri,
+    .uri_to_display_basename = uri_to_display_basename,
+    .uri_to_display_dirname = uri_to_display_dirname,
+
 };
 
 /*****************************************************************/
--- a/src/audacious/strings.c	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/strings.c	Mon Dec 17 21:56:17 2007 +0900
@@ -194,6 +194,7 @@
     return out_str;
 }
 
+/* convert name of absolute path in local file system encoding into utf8 string */
 gchar *
 filename_to_utf8(const gchar * filename)
 {
@@ -209,6 +210,40 @@
     return str_to_utf8_fallback(filename);
 }
 
+/* derives basename from uri. basename is in utf8 */
+gchar *
+uri_to_display_basename(const gchar * uri)
+{
+    gchar *realfn, *utf8fn, *basename;
+
+    g_return_val_if_fail(uri, NULL);
+
+    realfn = g_filename_from_uri(uri, NULL, NULL);
+    utf8fn = g_filename_display_name(realfn ? realfn : uri); // guaranteed to be non-NULL
+    basename = g_path_get_basename(utf8fn);
+
+    g_free(realfn); g_free(utf8fn);
+
+    return basename;
+}
+
+/* derives dirname from uri. dirname is in utf8 */
+gchar *
+uri_to_display_dirname(const gchar * uri)
+{
+    gchar *realfn, *utf8fn, *dirname;
+
+    g_return_val_if_fail(uri, NULL);
+
+    realfn = g_filename_from_uri(uri, NULL, NULL);
+    utf8fn = g_filename_display_name(realfn ? realfn : uri);  // guaranteed to be non-NULL
+    dirname = g_path_get_dirname(utf8fn);
+
+    g_free(realfn); g_free(utf8fn);
+
+    return dirname;
+}
+
 gchar *
 str_to_utf8(const gchar * str)
 {
--- a/src/audacious/strings.h	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/strings.h	Mon Dec 17 21:56:17 2007 +0900
@@ -38,9 +38,12 @@
 gboolean str_has_suffix_nocase(const gchar * str, const gchar * suffix);
 gboolean str_has_suffixes_nocase(const gchar * str, gchar * const *suffixes);
 
+gchar *str_to_utf8(const gchar * str);
 gchar *str_to_utf8_fallback(const gchar * str);
+
 gchar *filename_to_utf8(const gchar * filename);
-gchar *str_to_utf8(const gchar * str);
+gchar *uri_to_display_basename(const gchar * uri);
+gchar *uri_to_display_dirname(const gchar * uri);
 
 const gchar *str_skip_chars(const gchar * str, const gchar * chars);
 
--- a/src/audacious/tuple.c	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/tuple.c	Mon Dec 17 21:56:17 2007 +0900
@@ -171,31 +171,26 @@
 Tuple *
 tuple_new_from_filename(const gchar *filename)
 {
-    gchar *scratch, *ext, *realfn;
+    gchar *scratch, *ext;
     Tuple *tuple;
 
     g_return_val_if_fail(filename != NULL, NULL);
 
     tuple = tuple_new();
-    
     g_return_val_if_fail(tuple != NULL, NULL);
 
-    realfn = g_filename_from_uri(filename, NULL, NULL);
-
-    scratch = g_path_get_basename(realfn ? realfn : filename);
+    scratch = uri_to_display_basename(filename);
     _tuple_associate_raw_string(tuple, FIELD_FILE_NAME, NULL, scratch);
     g_free(scratch);
 
-    scratch = g_path_get_dirname(realfn ? realfn : filename);
+    scratch = uri_to_display_dirname(filename);
     _tuple_associate_raw_string(tuple, FIELD_FILE_PATH, NULL, scratch);
     g_free(scratch);
-
-    g_free(realfn); realfn = NULL;
-
+    
     ext = strrchr(filename, '.');
     if (ext != NULL) {
         ++ext;
-        _tuple_associate_raw_string(tuple, FIELD_FILE_EXT, NULL, scratch);
+        _tuple_associate_raw_string(tuple, FIELD_FILE_EXT, NULL, ext);
     }
 
     return tuple;
--- a/src/audacious/tuple.h	Sun Dec 16 18:54:45 2007 +0100
+++ b/src/audacious/tuple.h	Mon Dec 17 21:56:17 2007 +0900
@@ -39,8 +39,8 @@
     FIELD_QUALITY,
 
     FIELD_CODEC,
-    FIELD_FILE_NAME,
-    FIELD_FILE_PATH,
+    FIELD_FILE_NAME, /* This is always in UTF-8 */
+    FIELD_FILE_PATH, /* This is always in UTF-8 */
     FIELD_FILE_EXT,
     FIELD_SONG_ARTIST,