xspf.c 9.79 KB
Newer Older
1
/******************************************************************************
yoann's avatar
yoann committed
2 3 4 5 6 7 8
 * xspf.c : XSPF playlist export functions
 ******************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
 *
 * Authors: Daniel Stränger <vlc at schmaller dot de>
 *          Yoann Peronneau <yoann@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *******************************************************************************/

/**
 * \file modules/misc/playlist/xspf.c
 * \brief XSPF playlist export functions
 */
#include <stdio.h>
#include <vlc/vlc.h>
#include <vlc/intf.h>
32

33 34
#include "vlc_meta.h"
#include "vlc_strings.h"
35
#include "charset.h"
36 37 38 39 40 41 42 43 44 45 46 47 48
#include "xspf.h"

/**
 * \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 )
{
    const playlist_t *p_playlist = (playlist_t *)p_this;
    const playlist_export_t *p_export =
        (playlist_export_t *)p_playlist->p_private;
49 50
    int               i, i_count;
    char             *psz_temp;
yoann's avatar
yoann committed
51
    playlist_item_t  *p_node = p_export->p_root;
52

yoann's avatar
yoann committed
53
    /* write XSPF XML header */
54 55
    fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
    fprintf( p_export->p_file,
yoann's avatar
yoann committed
56 57 58
             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n" );

    if( !p_node ) return VLC_SUCCESS;
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    /* 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 location of the playlist node */
    psz_temp = assertUTF8URI( p_export->psz_filename );
    if( psz_temp && *psz_temp )
    {
        fprintf( p_export->p_file, "\t<location>%s</location>\n", psz_temp );
        free( psz_temp );
    }

yoann's avatar
yoann committed
76
    /* export all items in a flat format */
77
    fprintf( p_export->p_file, "\t<trackList>\n" );
yoann's avatar
yoann committed
78 79 80 81 82 83 84 85 86 87 88 89
    i_count = 0;
    for( i = 0; i < p_node->i_children; i++ )
    {
        xspf_export_item( p_node->pp_children[i], p_export->p_file,
                          &i_count );
    }
    fprintf( p_export->p_file, "\t</trackList>\n" );

    /* export the tree structure in <extension> */
    fprintf( p_export->p_file, "\t<extension>\n" );
    i_count = 0;
    for( i = 0; i < p_node->i_children; i++ )
90
    {
yoann's avatar
yoann committed
91 92
        xspf_extension_item( p_node->pp_children[i], p_export->p_file,
                             &i_count );
93
    }
yoann's avatar
yoann committed
94
    fprintf( p_export->p_file, "\t</extension>\n" );
95 96 97 98 99 100 101 102 103 104 105

    /* close the header elements */
    fprintf( p_export->p_file, "</playlist>\n" );

    return VLC_SUCCESS;
}

/**
 * \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
yoann's avatar
yoann committed
106
 * \param p_i_count counter for track identifiers
107
 */
yoann's avatar
yoann committed
108 109
static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
                              int *p_i_count )
110 111 112 113
{
    char *psz;
    char *psz_temp;

yoann's avatar
yoann committed
114
    if( !p_item ) return;
115 116

    /* if we get a node here, we must traverse it */
yoann's avatar
yoann committed
117
    if( p_item->i_children > 0 )
118
    {
yoann's avatar
yoann committed
119 120
        int i;
        for( i = 0; i < p_item->i_children; i++ )
121
        {
yoann's avatar
yoann committed
122
            xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
123 124 125 126
        }
        return;
    }

yoann's avatar
yoann committed
127 128 129 130 131 132
    /* don't write empty nodes */
    if( p_item->i_children == 0 )
    {
        return;
    }

133 134 135
    /* leaves can be written directly */
    fprintf( p_file, "\t\t<track>\n" );

yoann's avatar
yoann committed
136
    /* print identifier and increase the counter */
yoann's avatar
yoann committed
137
    fprintf( p_file, "\t\t\t<identifier>%i</identifier>\n", *p_i_count );
yoann's avatar
yoann committed
138 139
    ( *p_i_count )++;

140
    /* -> the location */
yoann's avatar
yoann committed
141
    if( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
142
    {
143
        psz = assertUTF8URI( p_item->p_input->psz_uri );
144 145 146 147 148
        fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
        free( psz );
    }

    /* -> the name/title (only if different from uri)*/
yoann's avatar
yoann committed
149 150 151
    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 ) )
152
    {
153
        psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
yoann's avatar
yoann committed
154
        if( *psz_temp )
155 156 157 158
            fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
        free( psz_temp );
    }

yoann's avatar
yoann committed
159 160 161 162 163
    if( p_item->p_input->p_meta == NULL )
    {
        goto xspfexportitem_end;
    }

164
    /* -> the artist/creator */
yoann's avatar
yoann committed
165
    psz = p_item->p_input->p_meta->psz_artist ?
166 167
                        strdup( p_item->p_input->p_meta->psz_artist ):
                        strdup( "" );
yoann's avatar
yoann committed
168
    if( psz && !*psz )
169
    {
yoann's avatar
yoann committed
170
        free( psz );
171 172
        psz = NULL;
    }
yoann's avatar
yoann committed
173 174
    if( !psz )
    {
zorglub's avatar
zorglub committed
175 176
        psz = p_item->p_input->p_meta->psz_artist ?
                        strdup( p_item->p_input->p_meta->psz_artist ):
177
                        strdup( "" );
yoann's avatar
yoann committed
178
    }
179
    psz_temp = convert_xml_special_chars( psz );
yoann's avatar
yoann committed
180 181 182
    if( psz ) free( psz );
    if( *psz_temp )
    {
183
        fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
yoann's avatar
yoann committed
184
    }
185 186 187
    free( psz_temp );

    /* -> the album */
188 189 190
    psz = p_item->p_input->p_meta->psz_album ?
                        strdup( p_item->p_input->p_meta->psz_album ):
                        strdup( "" );
191
    psz_temp = convert_xml_special_chars( psz );
yoann's avatar
yoann committed
192 193 194
    if( psz ) free( psz );
    if( *psz_temp )
    {
195
        fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
yoann's avatar
yoann committed
196
    }
197 198 199
    free( psz_temp );

    /* -> the track number */
200 201 202
    psz = p_item->p_input->p_meta->psz_tracknum ?
                        strdup( p_item->p_input->p_meta->psz_tracknum ):
                        strdup( "" );
yoann's avatar
yoann committed
203
    if( psz )
204
    {
yoann's avatar
yoann committed
205 206
        if( *psz )
        {
207
            fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
yoann's avatar
yoann committed
208
        }
209 210 211
        free( psz );
    }

yoann's avatar
yoann committed
212
xspfexportitem_end:
213
    /* -> the duration */
yoann's avatar
yoann committed
214
    if( p_item->p_input->i_duration > 0 )
215 216
    {
        fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
217
                 (long)(p_item->p_input->i_duration / 1000) );
218 219 220 221 222 223 224
    }

    fprintf( p_file, "\t\t</track>\n" );

    return;
}

yoann's avatar
yoann committed
225 226 227 228 229 230 231 232 233 234 235 236
/**
 * \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 void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
                                 int *p_i_count )
{
    if( !p_item ) return;

    /* if we get a node here, we must traverse it */
yoann's avatar
yoann committed
237
    if( p_item->i_children >= 0 )
yoann's avatar
yoann committed
238 239
    {
        int i;
yoann's avatar
yoann committed
240 241 242 243 244
        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 : "" );
        free( psz_temp );
yoann's avatar
yoann committed
245 246 247 248 249 250 251 252 253 254 255 256

        for( i = 0; i < p_item->i_children; i++ )
        {
            xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
        }

        fprintf( p_file, "\t\t</node>\n" );
        return;
    }


    /* print leaf and increase the counter */
yoann's avatar
yoann committed
257
    fprintf( p_file, "\t\t\t<item href=\"%i\" />\n", *p_i_count );
yoann's avatar
yoann committed
258 259 260 261 262
    ( *p_i_count )++;

    return;
}

263 264 265 266 267 268 269 270 271 272 273 274 275
/**
 * \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
 */
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;

yoann's avatar
yoann committed
276
    if( !psz_name || !*psz_name )
277 278 279 280 281 282 283 284 285 286
        return NULL;

    /* 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 );
yoann's avatar
yoann committed
287
    if( !psz_ret )
288 289 290
        return NULL;

    /** \todo check for a valid scheme part preceding the colon */
yoann's avatar
yoann committed
291
    if( strchr( psz_s, ':' ) )
292 293 294 295 296 297 298 299 300 301 302
    {
        psz_d = psz_ret;
        b_name_is_uri = VLC_TRUE;
    }
    /* assume "file" scheme if no scheme-part is included */
    else
    {
        strcpy( psz_ret, "file://" );
        psz_d = psz_ret + 7;
    }

yoann's avatar
yoann committed
303
    while( *psz_s )
304 305
    {
        /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
yoann's avatar
yoann committed
306 307 308 309 310 311
        if( *psz_s & B10000000 ||
            *psz_s == '<' ||
            *psz_s == '>' ||
            *psz_s == '&' ||
            *psz_s == ' ' ||
            ( *psz_s == '%' && !b_name_is_uri ) )
312 313 314 315
        {
            *psz_d++ = '%';
            *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
            *psz_d++ = hexchars[*psz_s & B00001111];
yoann's avatar
yoann committed
316 317 318
        }
        else
        {
319
            *psz_d++ = *psz_s;
yoann's avatar
yoann committed
320
        }
321 322 323 324 325 326 327

        psz_s++;
    }
    *psz_d = '\0';

    return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );
}