]> git.sesse.net Git - vlc/blob - src/playlist/loadsave.c
Streaming output works again. Closes #1047
[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 <vlc/vlc.h>
24 #include <vlc_playlist.h>
25 #include "playlist_internal.h"
26 #include "misc/configuration.h"
27 #include <vlc_charset.h>
28
29 #include <errno.h>
30
31 int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
32                      playlist_item_t *p_export_root,const char *psz_type )
33 {
34     module_t *p_module;
35     playlist_export_t *p_export;
36
37     if( p_export_root == NULL ) return VLC_EGENERIC;
38
39     msg_Info( p_playlist, "saving %s to file %s",
40                     p_export_root->p_input->psz_name, psz_filename );
41
42     /* Prepare the playlist_export_t structure */
43     p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
44     if( !p_export)
45     {
46         msg_Err( p_playlist, "out of memory" );
47         return VLC_ENOMEM;
48     }
49     p_export->psz_filename = NULL;
50     if ( psz_filename )
51         p_export->psz_filename = strdup( psz_filename );
52     p_export->p_file = utf8_fopen( psz_filename, "wt" );
53     if( !p_export->p_file )
54     {
55         msg_Err( p_playlist , "could not create playlist file %s"
56                  " (%s)", psz_filename, strerror(errno) );
57         return VLC_EGENERIC;
58     }
59
60     p_export->p_root = p_export_root;
61
62     /* Lock the playlist */
63     vlc_mutex_lock( &p_playlist->object_lock );
64     p_playlist->p_private = (void *)p_export;
65
66     /* And call the module ! All work is done now */
67     p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
68     if( !p_module )
69     {
70         msg_Warn( p_playlist, "exporting playlist failed" );
71         vlc_mutex_unlock( &p_playlist->object_lock );
72         return VLC_ENOOBJ;
73     }
74     module_Unneed( p_playlist , p_module );
75
76     /* Clean up */
77     fclose( p_export->p_file );
78     if ( p_export->psz_filename )
79         free( p_export->psz_filename );
80     free ( p_export );
81     p_playlist->p_private = NULL;
82     vlc_mutex_unlock( &p_playlist->object_lock );
83
84     return VLC_SUCCESS;
85 }
86
87 int playlist_MLLoad( playlist_t *p_playlist )
88 {
89     char *psz_uri, *psz_homedir =p_playlist->p_libvlc->psz_homedir;
90     input_item_t *p_input;
91
92     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
93     if( !psz_homedir )
94     {
95         msg_Err( p_playlist, "no home directory, cannot load media library") ;
96         return VLC_EGENERIC;
97     }
98     asprintf( &psz_uri, "file/xspf-open://%s" DIR_SEP CONFIG_DIR DIR_SEP
99                         "ml.xsp", psz_homedir );
100
101     p_input = input_ItemNewExt( p_playlist, psz_uri,
102                                 _("Media Library"), 0, NULL, -1 );
103     p_playlist->p_ml_category->p_input = p_input;
104     p_playlist->p_ml_onelevel->p_input = p_input;
105
106     p_playlist->b_doing_ml = VLC_TRUE;
107     stats_TimerStart( p_playlist, "ML Load", STATS_TIMER_ML_LOAD );
108     input_Read( p_playlist, p_input, VLC_TRUE );
109     stats_TimerStop( p_playlist,STATS_TIMER_ML_LOAD );
110     p_playlist->b_doing_ml = VLC_FALSE;
111
112     free( psz_uri );
113     return VLC_SUCCESS;
114 }
115
116 int playlist_MLDump( playlist_t *p_playlist )
117 {
118     char *psz_uri, *psz_homedir = p_playlist->p_libvlc->psz_homedir;
119     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
120     if( !psz_homedir )
121     {
122         msg_Err( p_playlist, "no home directory, cannot save media library") ;
123         return VLC_EGENERIC;
124     }
125
126     char psz_dirname[ strlen( psz_homedir ) + sizeof( DIR_SEP CONFIG_DIR ) ];
127     sprintf( psz_dirname, "%s" DIR_SEP CONFIG_DIR, psz_homedir );
128     if( config_CreateDir( (vlc_object_t *)p_playlist, psz_dirname ) )
129     {
130         return VLC_EGENERIC;
131     }
132
133     asprintf( &psz_uri, "%s" DIR_SEP "%s", psz_dirname, "ml.xsp" );
134     stats_TimerStart( p_playlist, "ML Dump", STATS_TIMER_ML_DUMP );
135     playlist_Export( p_playlist, psz_uri, p_playlist->p_ml_category,
136                      "export-xspf" );
137     stats_TimerStop( p_playlist, STATS_TIMER_ML_DUMP );
138
139     free( psz_uri );
140     return VLC_SUCCESS;
141 }