loadsave.c 5.47 KB
Newer Older
1 2 3
/*****************************************************************************
 * loadsave.c : Playlist loading / saving functions
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
zorglub's avatar
zorglub committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Samuel Hocevar <sam@zoy.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
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25
 *****************************************************************************/
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */
26
#include <errno.h>
27 28 29 30

#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/sout.h>
Laurent Aimar's avatar
Laurent Aimar committed
31
#include <vlc/input.h>
32 33

#include "vlc_playlist.h"
34
#include "charset.h"
35

zorglub's avatar
zorglub committed
36
#define PLAYLIST_FILE_HEADER  "# vlc playlist file version 0.5"
37

38
/**
zorglub's avatar
zorglub committed
39 40
 * Import a certain playlist file into the library
 * This file will get inserted as a new category
zorglub's avatar
zorglub committed
41
 *
zorglub's avatar
zorglub committed
42
 * XXX: TODO
43 44
 * \param p_playlist the playlist to which the new items will be added
 * \param psz_filename the name of the playlistfile to import
zorglub's avatar
zorglub committed
45
 * \return VLC_SUCCESS on success
46
 */
zorglub's avatar
zorglub committed
47
int playlist_Import( playlist_t * p_playlist, const char *psz_filename )
48
{
zorglub's avatar
zorglub committed
49 50 51
    playlist_item_t *p_item;
    char *psz_uri;
    int i_id;
52

zorglub's avatar
zorglub committed
53
    msg_Info( p_playlist, "clearing playlist");
zorglub's avatar
zorglub committed
54
    playlist_Clear( p_playlist );
55 56


zorglub's avatar
zorglub committed
57 58 59
    psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
    sprintf( psz_uri, "file/playlist://%s", psz_filename);

60
    i_id = playlist_PlaylistAdd( p_playlist, psz_uri, psz_uri,
zorglub's avatar
zorglub committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                  PLAYLIST_INSERT  , PLAYLIST_END);

    vlc_mutex_lock( &p_playlist->object_lock );
    p_item = playlist_ItemGetById( p_playlist, i_id );
    vlc_mutex_unlock( &p_playlist->object_lock );

    playlist_Play(p_playlist);

    return VLC_SUCCESS;
}

/**
 * Load a certain playlist file into the playlist
 * This file will replace the contents of the "current" view
 *
 * \param p_playlist the playlist to which the new items will be added
 * \param psz_filename the name of the playlistfile to import
 * \return VLC_SUCCESS on success
 */
int playlist_Load( playlist_t * p_playlist, const char *psz_filename )
{
    playlist_item_t *p_item;
    char *psz_uri;
    int i_id;

zorglub's avatar
zorglub committed
86
    msg_Info( p_playlist, "clearing playlist");
zorglub's avatar
zorglub committed
87 88 89
    playlist_Clear( p_playlist );


zorglub's avatar
zorglub committed
90 91
    psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
    sprintf( psz_uri, "file/playlist://%s", psz_filename);
92

93
    i_id = playlist_PlaylistAdd( p_playlist, psz_uri, psz_uri,
zorglub's avatar
zorglub committed
94
                  PLAYLIST_INSERT  , PLAYLIST_END);
95

zorglub's avatar
zorglub committed
96
    vlc_mutex_lock( &p_playlist->object_lock );
97
    p_item = playlist_ItemGetById( p_playlist, i_id );
zorglub's avatar
zorglub committed
98
    vlc_mutex_unlock( &p_playlist->object_lock );
99

zorglub's avatar
zorglub committed
100 101
    playlist_Play(p_playlist);

zorglub's avatar
zorglub committed
102
    return VLC_SUCCESS;
103 104
}

105
/**
106
 * Export a node of the playlist to a certain type of playlistfile
zorglub's avatar
zorglub committed
107
 *
108 109
 * \param p_playlist the playlist to export
 * \param psz_filename the location where the exported file will be saved
110
 * \param p_export_root the root node to export
111
 * \param psz_type the type of playlist file to create.
zorglub's avatar
zorglub committed
112
 * \return VLC_SUCCESS on success
113
 */
zorglub's avatar
zorglub committed
114
int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
115
                     playlist_item_t *p_export_root,const char *psz_type )
116
{
zorglub's avatar
zorglub committed
117 118
    module_t *p_module;
    playlist_export_t *p_export;
119

120 121 122 123
    if( p_export_root == NULL ) return VLC_EGENERIC;

    msg_Info( p_playlist, "saving %s to file %s",
                    p_export_root->p_input->psz_name, psz_filename );
124

zorglub's avatar
zorglub committed
125 126 127 128
    /* Prepare the playlist_export_t structure */
    p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
    if( !p_export)
    {
129
        msg_Err( p_playlist, "out of memory" );
zorglub's avatar
zorglub committed
130 131
        return VLC_ENOMEM;
    }
132 133 134
    p_export->psz_filename = NULL;
    if ( psz_filename )
        p_export->psz_filename = strdup( psz_filename );
135
    p_export->p_file = utf8_fopen( psz_filename, "wt" );
zorglub's avatar
zorglub committed
136
    if( !p_export->p_file )
137
    {
zorglub's avatar
zorglub committed
138
        msg_Err( p_playlist , "could not create playlist file %s"
gbazin's avatar
 
gbazin committed
139 140
                 " (%s)", psz_filename, strerror(errno) );
        return VLC_EGENERIC;
141 142
    }

143 144
    p_export->p_root = p_export_root;

zorglub's avatar
zorglub committed
145 146
    /* Lock the playlist */
    vlc_mutex_lock( &p_playlist->object_lock );
147
    p_playlist->p_private = (void *)p_export;
148

zorglub's avatar
zorglub committed
149
    /* And call the module ! All work is done now */
gbazin's avatar
 
gbazin committed
150
    p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
zorglub's avatar
zorglub committed
151
    if( !p_module )
152
    {
153
        msg_Warn( p_playlist, "exporting playlist failed" );
zorglub's avatar
zorglub committed
154 155
        vlc_mutex_unlock( &p_playlist->object_lock );
        return VLC_ENOOBJ;
156
    }
zorglub's avatar
zorglub committed
157
    module_Unneed( p_playlist , p_module );
158

159
    /* Clean up */
zorglub's avatar
zorglub committed
160
    fclose( p_export->p_file );
161 162 163 164
    if ( p_export->psz_filename )
        free( p_export->psz_filename );
    free ( p_export );
    p_playlist->p_private = NULL;
165 166
    vlc_mutex_unlock( &p_playlist->object_lock );

zorglub's avatar
zorglub committed
167
    return VLC_SUCCESS;
168
}