]> git.sesse.net Git - vlc/blobdiff - modules/misc/playlist/xspf.c
* XSPF nested playlist
[vlc] / modules / misc / playlist / xspf.c
index 73e6675601c7f7e06db4e7c2486cf0c84b84fbb1..d2bbc8e594992488a230a93f68470fded0d5f285 100644 (file)
  * \file modules/misc/playlist/xspf.c
  * \brief XSPF playlist export functions
  */
-#include <stdio.h>
 #include <vlc/vlc.h>
-#include <vlc/intf.h>
-
-#include "vlc_meta.h"
-#include "vlc_strings.h"
-#include "charset.h"
+#include <vlc_interface.h>
+#include <vlc_playlist.h>
+#include <vlc_input.h>
+#include <vlc_meta.h>
+#include <vlc_strings.h>
+#include <vlc_charset.h>
 #include "xspf.h"
 
 /**
@@ -84,7 +84,7 @@ int E_(xspf_export_playlist)( vlc_object_t *p_this )
     fprintf( p_export->p_file, "\t</trackList>\n" );
 
     /* export the tree structure in <extension> */
-    fprintf( p_export->p_file, "\t<extension>\n" );
+    fprintf( p_export->p_file, "\t<extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" );
     i_count = 0;
     for( i = 0; i < p_node->i_children; i++ )
     {
@@ -110,6 +110,7 @@ static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
 {
     char *psz;
     char *psz_temp;
+    mtime_t i_duration;
 
     if( !p_item ) return;
 
@@ -138,23 +139,27 @@ static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
     ( *p_i_count )++;
 
     /* -> the location */
-    if( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
+
+    char *psz_uri = input_item_GetURI( p_item->p_input );
+
+    if( psz_uri && *psz_uri )
     {
-        psz = assertUTF8URI( p_item->p_input->psz_uri );
+        psz = assertUTF8URI( psz_uri );
         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
         free( psz );
     }
 
     /* -> the name/title (only if different from uri)*/
-    if( p_item->p_input->psz_name &&
-        p_item->p_input->psz_uri &&
-        strcmp( p_item->p_input->psz_uri, p_item->p_input->psz_name ) )
+    char *psz_name = input_item_GetTitle( p_item->p_input );
+    if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
     {
-        psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
+        psz_temp = convert_xml_special_chars( psz_name );
         if( *psz_temp )
             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
         free( psz_temp );
     }
+    free( psz_name );
+    free( psz_uri );
 
     if( p_item->p_input->p_meta == NULL )
     {
@@ -162,22 +167,10 @@ static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
     }
 
     /* -> the artist/creator */
-    psz = p_item->p_input->p_meta->psz_artist ?
-                        strdup( p_item->p_input->p_meta->psz_artist ):
-                        strdup( "" );
-    if( psz && !*psz )
-    {
-        free( psz );
-        psz = NULL;
-    }
-    if( !psz )
-    {
-        psz = p_item->p_input->p_meta->psz_artist ?
-                        strdup( p_item->p_input->p_meta->psz_artist ):
-                        strdup( "" );
-    }
+    psz = input_item_GetArtist( p_item->p_input );
+    if( psz == NULL ) psz = strdup( "" );
     psz_temp = convert_xml_special_chars( psz );
-    if( psz ) free( psz );
+    free( psz );
     if( *psz_temp )
     {
         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
@@ -185,11 +178,10 @@ static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
     free( psz_temp );
 
     /* -> the album */
-    psz = p_item->p_input->p_meta->psz_album ?
-                        strdup( p_item->p_input->p_meta->psz_album ):
-                        strdup( "" );
+    psz = input_item_GetAlbum( p_item->p_input );
+    if( psz == NULL ) psz = strdup( "" );
     psz_temp = convert_xml_special_chars( psz );
-    if( psz ) free( psz );
+    free( psz );
     if( *psz_temp )
     {
         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
@@ -197,24 +189,44 @@ static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
     free( psz_temp );
 
     /* -> the track number */
-    psz = p_item->p_input->p_meta->psz_tracknum ?
-                        strdup( p_item->p_input->p_meta->psz_tracknum ):
-                        strdup( "" );
-    if( psz )
+    psz = input_item_GetTrackNum( p_item->p_input );
+    if( psz == NULL ) psz = strdup( "" );
+    if( psz && *psz )
     {
-        if( *psz )
-        {
-            fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
-        }
-        free( psz );
+        int i_tracknum = atoi( psz );
+        if( i_tracknum > 0 )
+            fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
     }
+    free( psz );
+
+    /* -> the description */
+    psz = input_item_GetDescription( p_item->p_input );
+    if( psz == NULL ) psz = strdup( "" );
+    psz_temp = convert_xml_special_chars( psz );
+    free( psz );
+    if( *psz_temp )
+    {
+        fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
+    }
+    free( psz_temp );
+
+    psz = input_item_GetArtURL( p_item->p_input );
+    if( psz == NULL ) psz = strdup( "" );
+    if( !EMPTY_STR( psz ) )
+    {
+        psz_uri = assertUTF8URI( psz );
+        fprintf( p_file, "\t\t\t<image>%s</image>\n", psz_uri );
+        free( psz_uri );
+    }
+    free( psz );
 
 xspfexportitem_end:
     /* -> the duration */
-    if( p_item->p_input->i_duration > 0 )
+    i_duration = input_item_GetDuration( p_item->p_input );
+    if( i_duration > 0 )
     {
         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
-                 (long)(p_item->p_input->i_duration / 1000) );
+                 (long)(i_duration / 1000) );
     }
 
     fprintf( p_file, "\t\t</track>\n" );
@@ -240,7 +252,7 @@ static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
         char *psz_temp;
         psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
         fprintf( p_file, "\t\t<node title=\"%s\">\n",
-                 *psz_temp ? p_item->p_input->psz_name : "" );
+                 *psz_temp ? psz_temp : "" );
         free( psz_temp );
 
         for( i = 0; i < p_item->i_children; i++ )
@@ -271,7 +283,7 @@ static char *assertUTF8URI( char *psz_name )
 {
     char *psz_ret = NULL;              /**< the new result buffer to return */
     char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
-    vlc_bool_t b_name_is_uri = VLC_FALSE;
+    vlc_bool_t b_uri_is_file = VLC_FALSE; /**< we do additional %-encoding if the URI is a file:// one */
 
     if( !psz_name || !*psz_name )
         return NULL;
@@ -288,16 +300,23 @@ static char *assertUTF8URI( char *psz_name )
         return NULL;
 
     /** \todo check for a valid scheme part preceding the colon */
-    if( strchr( psz_s, ':' ) )
+    size_t i_delim = strcspn( psz_s, ":" );
+    if( i_delim != strlen( psz_s ) )
     {
-        psz_d = psz_ret;
-        b_name_is_uri = VLC_TRUE;
+        i_delim++; /* skip the ':' */
+        strncpy( psz_ret, psz_s, i_delim );
+        psz_d = psz_ret + i_delim;
+        psz_s += i_delim;
+
+        if( !strncmp( psz_s, "file://", 7 ) )
+            b_uri_is_file = VLC_TRUE;
     }
     /* assume "file" scheme if no scheme-part is included */
     else
     {
         strcpy( psz_ret, "file://" );
         psz_d = psz_ret + 7;
+        b_uri_is_file = VLC_TRUE;
     }
 
     while( *psz_s )
@@ -308,7 +327,17 @@ static char *assertUTF8URI( char *psz_name )
             *psz_s == '>' ||
             *psz_s == '&' ||
             *psz_s == ' ' ||
-            ( *psz_s == '%' && !b_name_is_uri ) )
+            ( b_uri_is_file && (
+            *psz_s == ':' ||
+            *psz_s == '"' ||
+            *psz_s == '?' ||
+            *psz_s == '#' ||
+            *psz_s == '[' ||
+            *psz_s == ']' ||
+            *psz_s == '@' ||
+            *psz_s == '%' )
+            )
+          )
         {
             *psz_d++ = '%';
             *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];