]> git.sesse.net Git - vlc/blob - src/playlist/loadsave.c
Print error message when loading the playlist fails
[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 "modules/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 #if 0
90     FIXME: this code breaks streaming output (or streaming output breaks this,
91     whichever you prefer).
92
93     const char *psz_homedir = p_playlist->p_libvlc->psz_homedir;
94     char *psz_uri = NULL;
95     input_item_t *p_input;
96
97     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
98     if( !psz_homedir )
99     {
100         msg_Err( p_playlist, "no home directory, cannot load media library") ;
101         return VLC_EGENERIC;
102     }
103
104     if( asprintf( &psz_uri, "file/xspf-open://%s" DIR_SEP CONFIG_DIR DIR_SEP
105                         "ml.xsp", psz_homedir ) == -1 )
106     {
107         psz_uri = NULL;
108         goto error;
109     }
110
111     p_input = input_ItemNewExt( p_playlist, psz_uri,
112                                 _("Media Library"), 0, NULL, -1 );
113     if( p_input == NULL )
114         goto error;
115
116     p_playlist->p_ml_onelevel->p_input =
117         p_playlist->p_ml_category->p_input = p_input;
118
119     p_playlist->b_doing_ml = VLC_TRUE;
120     stats_TimerStart( p_playlist, "ML Load", STATS_TIMER_ML_LOAD );
121     input_Read( p_playlist, p_input, VLC_TRUE );
122     stats_TimerStop( p_playlist,STATS_TIMER_ML_LOAD );
123     p_playlist->b_doing_ml = VLC_FALSE;
124
125     free( psz_uri );
126     return VLC_SUCCESS;
127
128 error:
129     free( psz_uri );
130     return VLC_ENOMEM;
131 #else
132     msg_Err( p_playlist, "Reloading playlist not implemented." );
133     return VLC_EGENERIC;
134 #endif
135 }
136
137 int playlist_MLDump( playlist_t *p_playlist )
138 {
139     char *psz_uri, *psz_homedir = p_playlist->p_libvlc->psz_homedir;
140     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
141     if( !psz_homedir )
142     {
143         msg_Err( p_playlist, "no home directory, cannot save media library") ;
144         return VLC_EGENERIC;
145     }
146
147     char psz_dirname[ strlen( psz_homedir ) + sizeof( DIR_SEP CONFIG_DIR ) ];
148     sprintf( psz_dirname, "%s" DIR_SEP CONFIG_DIR, psz_homedir );
149     if( config_CreateDir( (vlc_object_t *)p_playlist, psz_dirname ) )
150     {
151         return VLC_EGENERIC;
152     }
153
154     asprintf( &psz_uri, "%s" DIR_SEP "%s", psz_dirname, "ml.xsp" );
155     stats_TimerStart( p_playlist, "ML Dump", STATS_TIMER_ML_DUMP );
156     playlist_Export( p_playlist, psz_uri, p_playlist->p_ml_category,
157                      "export-xspf" );
158     stats_TimerStop( p_playlist, STATS_TIMER_ML_DUMP );
159
160     free( psz_uri );
161     return VLC_SUCCESS;
162 }