]> git.sesse.net Git - vlc/blobdiff - modules/misc/playlist/xspf.c
Merge branch 'master' of git://git.videolan.org/vlc
[vlc] / modules / misc / playlist / xspf.c
index 9366518237c1ce5e951e99f5623d2512a64f444d..4f09caf96904c46e0fd347683ca4d1afa9befd16 100644 (file)
@@ -1,5 +1,11 @@
 /******************************************************************************
- * Copyright (C) 2006 Daniel Stränger <vlc at schmaller dot de>
+ * xspf.c : XSPF playlist export functions
+ ******************************************************************************
+ * Copyright (C) 2006-2009 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Daniel Stränger <vlc at schmaller dot de>
+ *          Yoann Peronneau <yoann@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * \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"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_playlist.h>
+#include <vlc_input.h>
+#include <vlc_strings.h>
+#include <vlc_url.h>
 #include "xspf.h"
 
+#include <assert.h>
+
+static void xspf_export_item( playlist_item_t *, FILE *, int * );
+static void xspf_extension_item( playlist_item_t *, FILE *, int * );
+
 /**
  * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
  * and closes the open xml elements
  * \param p_this the VLC playlist object
  * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
  */
-int E_(xspf_export_playlist)( vlc_object_t *p_this )
+int xspf_export_playlist( vlc_object_t *p_this )
 {
-    const playlist_t *p_playlist = (playlist_t *)p_this;
-    const playlist_export_t *p_export =
-        (playlist_export_t *)p_playlist->p_private;
-    int              i;
-    char             *psz;
+    const playlist_export_t *p_export = (playlist_export_t *)p_this;
+    int               i, i_count;
     char             *psz_temp;
-    playlist_item_t **pp_items = NULL;
-    int               i_size;
-    playlist_item_t  *p_node;
+    playlist_item_t  *p_node = p_export->p_root;
 
-    /* write XSPF XML header - since we don't use <extension>,
-     * we get by with version 0 */
+    /* write XSPF XML header */
     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
     fprintf( p_export->p_file,
-             "<playlist version=\"0\" xmlns=\"http://xspf.org/ns/0/\">\n" );
+             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" " \
+             "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n" );
+
+    if( !p_node ) return VLC_SUCCESS;
 
-    /* save tho whole playlist or only the current node */
-#define p_item p_playlist->status.p_item
-    if ( p_item )
+    /* save name of the playlist node */
+    psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
+    if( *psz_temp )
     {
-        if ( p_item->p_parent->p_input->i_type == ITEM_TYPE_PLAYLIST )
-        {
-            /* set the current node and its children */
-            p_node   = p_item->p_parent;
-            pp_items = p_node->pp_children;
-            i_size   = p_node->i_children;
-#undef p_item
-
-            /* save name of the playlist node */
-            psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
-            if ( *psz_temp )
-                fprintf(  p_export->p_file, "\t<title>%s</title>\n",
-                          psz_temp );
-            free( psz_temp );
-
-            /* save the creator of the playlist node */
-            psz = p_node->p_input->p_meta->psz_artist ? 
-                        strdup( p_node->p_input->p_meta->psz_artist ):
-                        strdup( "" );
-            if ( psz && !*psz )
-            {
-                free ( psz );
-                psz = NULL;
-            }
-
-            if ( !psz )
-               psz = p_node->p_input->p_meta->psz_author ? 
-                            strdup( p_node->p_input->p_meta->psz_author ):
-                            strdup( "" );
-
-            psz_temp = convert_xml_special_chars( psz );
-
-            if ( psz ) free( psz );
-            if ( *psz_temp )
-                fprintf(  p_export->p_file, "\t<creator>%s</creator>\n",
-                          psz_temp );
-            free( psz_temp );
-
-            /* save location of the playlist node */
-            psz = assertUTF8URI( p_export->psz_filename );
-            if ( psz && *psz )
-            {
-                fprintf( p_export->p_file, "\t<location>%s</location>\n",
-                         psz );
-                free( psz );
-            }
-        }
+        fprintf(  p_export->p_file, "\t<title>%s</title>\n", psz_temp );
     }
+    free( psz_temp );
 
-    /* prepare all the playlist children for export */
-    if ( !pp_items )
+    /* export all items in a flat format */
+    fprintf( p_export->p_file, "\t<trackList>\n" );
+    i_count = 0;
+    for( i = 0; i < p_node->i_children; i++ )
     {
-        pp_items = p_playlist->pp_items;
-        i_size   = p_playlist->i_size;
+        xspf_export_item( p_node->pp_children[i], p_export->p_file,
+                          &i_count );
     }
+    fprintf( p_export->p_file, "\t</trackList>\n" );
 
-    /* export all items */
-    fprintf( p_export->p_file, "\t<trackList>\n" );
-    for ( i = 0; i < i_size; i++ )
+    /* export the tree structure in <extension> */
+    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++ )
     {
-        xspf_export_item( pp_items[i], p_export->p_file );
+        xspf_extension_item( p_node->pp_children[i], p_export->p_file,
+                             &i_count );
     }
+    fprintf( p_export->p_file, "\t</extension>\n" );
 
     /* close the header elements */
-    fprintf( p_export->p_file, "\t</trackList>\n" );
     fprintf( p_export->p_file, "</playlist>\n" );
 
     return VLC_SUCCESS;
 }
 
+static char *input_xml( input_item_t *p_item, char *(*func)(input_item_t *) )
+{
+    char *tmp = func( p_item );
+    if( tmp == NULL )
+        return NULL;
+    char *ret = convert_xml_special_chars( tmp );
+    free( tmp );
+    return ret;
+}
+
 /**
  * \brief exports one item to file or traverse if item is a node
  * \param p_item playlist item to export
  * \param p_file file to write xml-converted item to
+ * \param p_i_count counter for track identifiers
  */
-static void xspf_export_item( playlist_item_t *p_item, FILE *p_file )
+static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
+                              int *p_i_count )
 {
-    int i;       /**< iterator for all children if the current item is a node */
-    char *psz;
-    char *psz_temp;
+    if( !p_item ) return;
 
-    if ( !p_item )
-        return;
-
-    /** \todo only "flat" playlists supported at this time.
-     *  extend to save the tree structure.
-     */
     /* if we get a node here, we must traverse it */
-    if ( p_item->i_children > 0 )
+    if( p_item->i_children > 0 )
     {
-        for ( i = 0; i < p_item->i_children; i++ )
-        {
-            xspf_export_item( p_item->pp_children[i], p_file );
-        }
+        for( int i = 0; i < p_item->i_children; i++ )
+            xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
         return;
     }
 
+    /* don't write empty nodes */
+    if( p_item->i_children == 0 )
+        return;
+
+    input_item_t *p_input = p_item->p_input;
+    char *psz;
+    mtime_t i_duration;
+
     /* leaves can be written directly */
-    fprintf( p_file, "\t\t<track>\n" );
+    fputs( "\t\t<track>\n", p_file );
 
     /* -> the location */
-    if ( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
-    {
-        psz = assertUTF8URI( p_item->p_input->psz_uri );
-        fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
-        free( psz );
-    }
+
+    char *psz_uri = input_xml( p_input, input_item_GetURI );
+    if( psz_uri && *psz_uri )
+        fprintf( p_file, "\t\t\t<location>%s</location>\n", psz_uri );
 
     /* -> 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 ) )
+    psz = input_xml( p_input, input_item_GetTitle );
+    if( psz && strcmp( psz_uri, psz ) )
+        fprintf( p_file, "\t\t\t<title>%s</title>\n", psz );
+    free( psz );
+    free( psz_uri );
+
+    if( p_item->p_input->p_meta == NULL )
     {
-        psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
-        if ( *psz_temp )
-            fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
-        free( psz_temp );
+        goto xspfexportitem_end;
     }
 
     /* -> 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_author ? 
-                        strdup( p_item->p_input->p_meta->psz_author ):
-                        strdup( "" );
-    psz_temp = convert_xml_special_chars( psz );
-    if ( psz ) free( psz );
-    if ( *psz_temp )
-        fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
-    free( psz_temp );
+    psz = input_xml( p_input, input_item_GetArtist );
+    if( psz && *psz )
+        fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz );
+    free( psz );
 
     /* -> the album */
-    psz = p_item->p_input->p_meta->psz_album ?
-                        strdup( p_item->p_input->p_meta->psz_album ):
-                        strdup( "" );
-    psz_temp = convert_xml_special_chars( psz );
-    if ( psz ) free( psz );
-    if ( *psz_temp )
-        fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
-    free( psz_temp );
+    psz = input_xml( p_input, input_item_GetAlbum );
+    if( psz && *psz )
+        fprintf( p_file, "\t\t\t<album>%s</album>\n", psz );
+    free( psz );
 
     /* -> 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_xml( p_input, input_item_GetTrackNum );
+    if( psz )
     {
-        if ( *psz )
-            fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
+        int i_tracknum = atoi( psz );
+
         free( psz );
+        if( i_tracknum > 0 )
+            fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
     }
 
+    /* -> the description */
+    psz = input_xml( p_input, input_item_GetDescription );
+    if( psz && *psz )
+        fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz );
+    free( psz );
+
+    psz = input_xml( p_input, input_item_GetArtURL );
+    if( psz && *psz )
+        fprintf( p_file, "\t\t\t<image>%s</image>\n", psz );
+    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>%"PRIu64"</duration>\n",
+                 i_duration / 1000 );
+
+    /* export the intenal id and the input's options (bookmarks, ...)
+     * in <extension> */
+    fputs( "\t\t\t<extension application=\""
+           "http://www.videolan.org/vlc/playlist/0\">\n", p_file );
+
+    /* print the id and increase the counter */
+    fprintf( p_file, "\t\t\t\t<vlc:id>%i</vlc:id>\n", *p_i_count );
+    ( *p_i_count )++;
+
+    for( int i = 0; i < p_item->p_input->i_options; i++ )
     {
-        fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
-                 (long)(p_item->p_input->i_duration / 1000) );
-    }
+        char* psz_src = p_item->p_input->ppsz_options[i];
+        char* psz_ret = NULL;
 
-    fprintf( p_file, "\t\t</track>\n" );
+        if ( psz_src[0] == ':' )
+            psz_src++;
 
-    return;
+        psz_ret = convert_xml_special_chars( psz_src );
+        if ( psz_ret == NULL )
+            continue;
+
+        fprintf( p_file, "\t\t\t\t<vlc:option>%s</vlc:option>\n", psz_ret );
+        free( psz_ret );
+    }
+    fputs( "\t\t\t</extension>\n", p_file );
+    fputs( "\t\t</track>\n", p_file );
 }
 
 /**
- * \param psz_name the location of the media ressource (e.g. local file,
- *        device, network stream, etc.)
- * \return a new char buffer which asserts that the location is valid UTF-8
- *         and a valid URI
- * \note the returned buffer must be freed, when it isn't used anymore
+ * \brief exports one item in extension to file and traverse if item is a node
+ * \param p_item playlist item to export
+ * \param p_file file to write xml-converted item to
+ * \param p_i_count counter for track identifiers
  */
-static char *assertUTF8URI( char *psz_name )
+static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
+                                 int *p_i_count )
 {
-    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;
-
-    if ( !psz_name || !*psz_name )
-        return NULL;
+    if( !p_item ) return;
 
-    /* check that string is valid UTF-8 */
-    /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
-    if( !( psz_s = EnsureUTF8( psz_name ) ) )
-        return NULL;
-
-    /* max. 3x for URI conversion (percent escaping) and
-       8 bytes for "file://" and NULL-termination */
-    psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
-    if ( !psz_ret )
-        return NULL;
-
-    /** \todo check for a valid scheme part preceding the colon */
-    if ( strchr( psz_s, ':' ) )
-    {
-        psz_d = psz_ret;
-        b_name_is_uri = VLC_TRUE;
-    }
-    /* assume "file" scheme if no scheme-part is included */
-    else
+    /* if we get a node here, we must traverse it */
+    if( p_item->i_children >= 0 )
     {
-        strcpy( psz_ret, "file://" );
-        psz_d = psz_ret + 7;
-    }
+        int i;
+        char *psz_temp = NULL;
+        if( p_item->p_input->psz_name )
+            psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
+        fprintf( p_file, "\t\t<vlc:node title=\"%s\">\n",
+                 psz_temp ? psz_temp : "" );
+        free( psz_temp );
 
-    while ( *psz_s )
-    {
-        /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
-        if ( *psz_s & B10000000 ||
-             *psz_s == '<' ||
-             *psz_s == '>' ||
-             *psz_s == '&' ||
-             *psz_s == ' ' ||
-             ( *psz_s == '%' && !b_name_is_uri ) )
+        for( i = 0; i < p_item->i_children; i++ )
         {
-            *psz_d++ = '%';
-            *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
-            *psz_d++ = hexchars[*psz_s & B00001111];
-        } else
-            *psz_d++ = *psz_s;
+            xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
+        }
 
-        psz_s++;
+        fprintf( p_file, "\t\t</vlc:node>\n" );
+        return;
     }
-    *psz_d = '\0';
 
-    return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );
+
+    /* print leaf and increase the counter */
+    fprintf( p_file, "\t\t\t<vlc:item tid=\"%i\" />\n", *p_i_count );
+    ( *p_i_count )++;
+
+    return;
 }