]> git.sesse.net Git - vlc/blob - src/playlist/loadsave.c
* 2nd review of /src/* \ libvlc.h (refs #438)
[vlc] / src / playlist / loadsave.c
1 /*****************************************************************************
2  * loadsave.c : Playlist loading / saving functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include <stdlib.h>                                      /* free(), strtol() */
24 #include <stdio.h>                                              /* sprintf() */
25 #include <string.h>                                            /* strerror() */
26 #include <errno.h>
27
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/sout.h>
31 #include <vlc/input.h>
32
33 #include "vlc_playlist.h"
34 #include "charset.h"
35
36 #define PLAYLIST_FILE_HEADER  "# vlc playlist file version 0.5"
37
38 /**
39  * Import a certain playlist file into the library
40  * This file will get inserted as a new category
41  *
42  * XXX: TODO
43  * \param p_playlist the playlist to which the new items will be added
44  * \param psz_filename the name of the playlistfile to import
45  * \return VLC_SUCCESS on success
46  */
47 int playlist_Import( playlist_t * p_playlist, const char *psz_filename )
48 {
49     playlist_item_t *p_item;
50     char *psz_uri;
51     int i_id;
52
53     msg_Info( p_playlist, "clearing playlist");
54     playlist_Clear( p_playlist );
55
56
57     psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
58     sprintf( psz_uri, "file/playlist://%s", psz_filename);
59
60     i_id = playlist_Add( p_playlist, psz_uri, psz_uri,
61                   PLAYLIST_INSERT  , PLAYLIST_END);
62
63     vlc_mutex_lock( &p_playlist->object_lock );
64     p_item = playlist_ItemGetById( p_playlist, i_id );
65     p_item->b_autodeletion = VLC_TRUE;
66     vlc_mutex_unlock( &p_playlist->object_lock );
67
68     playlist_Play(p_playlist);
69
70     return VLC_SUCCESS;
71 }
72
73 /**
74  * Load a certain playlist file into the playlist
75  * This file will replace the contents of the "current" view
76  *
77  * \param p_playlist the playlist to which the new items will be added
78  * \param psz_filename the name of the playlistfile to import
79  * \return VLC_SUCCESS on success
80  */
81 int playlist_Load( playlist_t * p_playlist, const char *psz_filename )
82 {
83     playlist_item_t *p_item;
84     char *psz_uri;
85     int i_id;
86
87     msg_Info( p_playlist, "clearing playlist");
88     playlist_Clear( p_playlist );
89
90
91     psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
92     sprintf( psz_uri, "file/playlist://%s", psz_filename);
93
94     i_id = playlist_Add( p_playlist, psz_uri, psz_uri,
95                   PLAYLIST_INSERT  , PLAYLIST_END);
96
97     vlc_mutex_lock( &p_playlist->object_lock );
98     p_item = playlist_ItemGetById( p_playlist, i_id );
99     p_item->b_autodeletion = VLC_TRUE;
100     vlc_mutex_unlock( &p_playlist->object_lock );
101
102     playlist_Play(p_playlist);
103
104     return VLC_SUCCESS;
105 }
106
107 /**
108  * Export a playlist to a certain type of playlistfile
109  *
110  * \param p_playlist the playlist to export
111  * \param psz_filename the location where the exported file will be saved
112  * \param psz_type the type of playlist file to create.
113  * \return VLC_SUCCESS on success
114  */
115 int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
116                      const char *psz_type)
117 {
118     module_t *p_module;
119     playlist_export_t *p_export;
120
121     msg_Info( p_playlist, "saving playlist to file %s", psz_filename );
122
123     /* Prepare the playlist_export_t structure */
124     p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
125     if( !p_export)
126     {
127         msg_Err( p_playlist, "out of memory");
128         return VLC_ENOMEM;
129     }
130     p_export->psz_filename = NULL;
131     if ( psz_filename )
132         p_export->psz_filename = strdup( psz_filename );
133     p_export->p_file = utf8_fopen( psz_filename, "wt" );
134     if( !p_export->p_file )
135     {
136         msg_Err( p_playlist , "could not create playlist file %s"
137                  " (%s)", psz_filename, strerror(errno) );
138         return VLC_EGENERIC;
139     }
140
141     p_playlist->p_private = (void *)p_export;
142     /* Lock the playlist */
143     vlc_mutex_lock( &p_playlist->object_lock );
144
145     /* And call the module ! All work is done now */
146     p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
147     if( !p_module )
148     {
149         msg_Warn( p_playlist, "exporting playlist failed" );
150         vlc_mutex_unlock( &p_playlist->object_lock );
151         return VLC_ENOOBJ;
152     }
153     module_Unneed( p_playlist , p_module );
154
155     /* Clean up */
156     fclose( p_export->p_file );
157     if ( p_export->psz_filename )
158         free( p_export->psz_filename );
159     free ( p_export );
160     p_playlist->p_private = NULL;
161     vlc_mutex_unlock( &p_playlist->object_lock );
162
163     return VLC_SUCCESS;
164 }