]> git.sesse.net Git - vlc/blob - src/playlist/loadsave.c
playlist_Export simplifications
[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 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_playlist.h>
29 #include <vlc_events.h>
30 #include "playlist_internal.h"
31 #include "config/configuration.h"
32 #include <vlc_charset.h>
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <errno.h>
38
39 int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
40                      playlist_item_t *p_export_root,const char *psz_type )
41 {
42     module_t *p_module;
43     playlist_export_t export;
44
45     if( p_export_root == NULL ) return VLC_EGENERIC;
46
47     msg_Dbg( p_playlist, "saving %s to file %s",
48                     p_export_root->p_input->psz_name, psz_filename );
49
50     /* Prepare the playlist_export_t structure */
51     export.psz_filename = psz_filename ? strdup( psz_filename ) : NULL;
52     export.p_file = utf8_fopen( psz_filename, "wt" );
53     if( export.p_file == NULL )
54     {
55         msg_Err( p_playlist , "could not create playlist file %s (%m)",
56                  psz_filename );
57         free( export.psz_filename );
58         return VLC_EGENERIC;
59     }
60
61     export.p_root = p_export_root;
62
63     playlist_Lock( p_playlist );
64     p_playlist->p_private = (void *)&export;
65
66     /* And call the module ! All work is done now */
67     p_module = module_need( p_playlist, "playlist export", psz_type, true);
68     if( !p_module )
69         msg_Warn( p_playlist, "exporting playlist failed" );
70     else
71         module_unneed( p_playlist , p_module );
72     p_playlist->p_private = NULL;
73     playlist_Unlock( p_playlist );
74
75     /* Clean up */
76     fclose( export.p_file );
77     free( export.psz_filename );
78
79     return p_module ? VLC_SUCCESS : VLC_ENOOBJ;
80 }
81
82 int playlist_Import( playlist_t *p_playlist, const char *psz_file )
83 {
84     input_item_t *p_input;
85     char *psz_uri;
86     const char *const psz_option = "meta-file";
87
88     if( asprintf( &psz_uri, "file/://%s", psz_file ) < 0 )
89         return VLC_EGENERIC;
90
91     p_input = input_item_NewExt( p_playlist, psz_uri, psz_file,
92                                  1, &psz_option, VLC_INPUT_OPTION_TRUSTED, -1 );
93     free( psz_uri );
94
95     playlist_AddInput( p_playlist, p_input, PLAYLIST_APPEND, PLAYLIST_END,
96                        true, false );
97     return input_Read( p_playlist, p_input, true );
98 }
99
100 /*****************************************************************************
101  * A subitem has been added to the Media Library (Event Callback)
102  *****************************************************************************/
103 static void input_item_subitem_added( const vlc_event_t * p_event,
104                                       void * user_data )
105 {
106     playlist_t *p_playlist = user_data;
107     input_item_t *p_item = p_event->u.input_item_subitem_added.p_new_child;
108
109     /* playlist_AddInput() can fail, but we have no way to report that ..
110      * Any way when it has failed, either the playlist is dying, either OOM */
111     playlist_AddInput( p_playlist, p_item, PLAYLIST_APPEND, PLAYLIST_END,
112             false, pl_Unlocked );
113 }
114
115 int playlist_MLLoad( playlist_t *p_playlist )
116 {
117     char *psz_datadir = config_GetUserDataDir();
118     char *psz_uri = NULL;
119     input_item_t *p_input;
120
121     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
122     if( !psz_datadir ) /* XXX: This should never happen */
123     {
124         msg_Err( p_playlist, "no data directory, cannot load media library") ;
125         return VLC_EGENERIC;
126     }
127
128     if( asprintf( &psz_uri, "%s" DIR_SEP "ml.xspf", psz_datadir ) == -1 )
129     {
130         psz_uri = NULL;
131         goto error;
132     }
133     struct stat p_stat;
134     /* checks if media library file is present */
135     if( utf8_stat( psz_uri , &p_stat ) )
136         goto error;
137     free( psz_uri );
138
139     /* FIXME: WTF? stat() should never be used right before open()! */
140     if( asprintf( &psz_uri, "file/xspf-open://%s" DIR_SEP "ml.xspf",
141                   psz_datadir ) == -1 )
142     {
143         psz_uri = NULL;
144         goto error;
145     }
146     free( psz_datadir );
147     psz_datadir = NULL;
148
149     const char *const psz_option = "meta-file";
150     /* that option has to be cleaned in input_item_subitem_added() */
151     /* vlc_gc_decref() in the same function */
152     p_input = input_item_NewExt( p_playlist, psz_uri, _("Media Library"),
153                                  1, &psz_option, VLC_INPUT_OPTION_TRUSTED, -1 );
154     if( p_input == NULL )
155         goto error;
156
157     PL_LOCK;
158     if( p_playlist->p_ml_onelevel->p_input )
159         vlc_gc_decref( p_playlist->p_ml_onelevel->p_input );
160     if( p_playlist->p_ml_category->p_input )
161         vlc_gc_decref( p_playlist->p_ml_category->p_input );
162
163     p_playlist->p_ml_onelevel->p_input =
164     p_playlist->p_ml_category->p_input = p_input;
165     /* We save the input at two different place, incref */
166     vlc_gc_incref( p_input );
167     vlc_gc_incref( p_input );
168
169     vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
170                         input_item_subitem_added, p_playlist );
171
172     pl_priv(p_playlist)->b_doing_ml = true;
173     PL_UNLOCK;
174
175     stats_TimerStart( p_playlist, "ML Load", STATS_TIMER_ML_LOAD );
176     input_Read( p_playlist, p_input, true );
177     stats_TimerStop( p_playlist,STATS_TIMER_ML_LOAD );
178
179     PL_LOCK;
180     pl_priv(p_playlist)->b_doing_ml = false;
181     PL_UNLOCK;
182
183     vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
184                         input_item_subitem_added, p_playlist );
185
186     vlc_gc_decref( p_input );
187     free( psz_uri );
188     return VLC_SUCCESS;
189
190 error:
191     free( psz_uri );
192     free( psz_datadir );
193     return VLC_ENOMEM;
194 }
195
196 int playlist_MLDump( playlist_t *p_playlist )
197 {
198     char *psz_datadir = config_GetUserDataDir();
199     if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
200     if( !psz_datadir ) /* XXX: This should never happen */
201     {
202         msg_Err( p_playlist, "no data directory, cannot save media library") ;
203         return VLC_EGENERIC;
204     }
205
206     char psz_dirname[ strlen( psz_datadir ) + sizeof( DIR_SEP "ml.xspf")];
207     strcpy( psz_dirname, psz_datadir );
208     free( psz_datadir );
209     if( config_CreateDir( (vlc_object_t *)p_playlist, psz_dirname ) )
210     {
211         return VLC_EGENERIC;
212     }
213
214     strcat( psz_dirname, DIR_SEP "ml.xspf" );
215
216     stats_TimerStart( p_playlist, "ML Dump", STATS_TIMER_ML_DUMP );
217     playlist_Export( p_playlist, psz_dirname, p_playlist->p_ml_category,
218                      "export-xspf" );
219     stats_TimerStop( p_playlist, STATS_TIMER_ML_DUMP );
220
221     return VLC_SUCCESS;
222 }