/*****************************************************************************
* loadsave.c : Playlist loading / saving functions
*****************************************************************************
- * Copyright (C) 1999-2004 VideoLAN
- * $Id: loadsave.c,v 1.3 2004/01/06 08:50:20 zorglub Exp $
+ * Copyright (C) 1999-2004 the VideoLAN team
+ * $Id$
*
* Authors: Samuel Hocevar <sam@zoy.org>
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <stdlib.h> /* free(), strtol() */
#include <stdio.h> /* sprintf() */
#include <string.h> /* strerror() */
+#include <errno.h>
#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/sout.h>
-
-#include "stream_control.h"
-#include "input_ext-intf.h"
+#include <vlc/input.h>
#include "vlc_playlist.h"
+#include "charset.h"
-#define PLAYLIST_FILE_HEADER_0_5 "# vlc playlist file version 0.5"
-#define PLAYLIST_FILE_HEADER_0_6 "# vlc playlist file version 0.6"
-
+#define PLAYLIST_FILE_HEADER "# vlc playlist file version 0.5"
-/*****************************************************************************
- * playlist_LoadFile: load a playlist file.
- ****************************************************************************/
-int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
+/**
+ * Import a certain playlist file into the library
+ * This file will get inserted as a new category
+ *
+ * XXX: TODO
+ * \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_Import( playlist_t * p_playlist, const char *psz_filename )
{
- FILE *file;
- char line[1024];
- int i_current_status;
- int i_format;
- int i;
+ playlist_item_t *p_item;
+ char *psz_uri;
+ int i_id;
- msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
+ msg_Info( p_playlist, "clearing playlist");
+ playlist_Clear( p_playlist );
- file = fopen( psz_filename, "rt" );
- if( !file )
- {
- msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
- return -1;
- }
- fseek( file, 0L, SEEK_SET );
- /* check the file is not empty */
- if ( ! fgets( line, 1024, file ) )
- {
- msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
- fclose( file );
- return -1;
- }
+ psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
+ sprintf( psz_uri, "file/playlist://%s", psz_filename);
- /* get rid of line feed */
- if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
- {
- line[strlen(line)-1] = (char)0;
- if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
- }
- /* check the file format is valid */
- if ( !strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
- {
- i_format = 5;
- }
- else if( !strcmp ( line , PLAYLIST_FILE_HEADER_0_6 ) )
- {
- i_format = 6;
- }
- else
- {
- msg_Err( p_playlist, "playlist file %s format is unsupported"
- , psz_filename );
- fclose( file );
- return -1;
- }
+ i_id = playlist_PlaylistAdd( p_playlist, psz_uri, psz_uri,
+ PLAYLIST_INSERT , PLAYLIST_END);
- /* stop playing */
- i_current_status = p_playlist->i_status;
- if ( p_playlist->i_status != PLAYLIST_STOPPED )
- {
- playlist_Stop ( p_playlist );
- }
+ vlc_mutex_lock( &p_playlist->object_lock );
+ p_item = playlist_ItemGetById( p_playlist, i_id );
+ vlc_mutex_unlock( &p_playlist->object_lock );
- /* delete current content of the playlist */
- for( i = p_playlist->i_size - 1; i >= 0; i-- )
- {
- playlist_Delete ( p_playlist , i );
- }
+ playlist_Play(p_playlist);
- /* simply add each line */
- while( fgets( line, 1024, file ) )
- {
- /* ignore comments or empty lines */
- if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
- || (line[0] == (char)0) )
- continue;
-
- /* get rid of line feed */
- if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
- {
- line[strlen(line)-1] = (char)0;
- if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
- }
- if( i_format == 5 )
- {
- playlist_Add ( p_playlist , (char *)&line , (char *)&line,
- PLAYLIST_APPEND , PLAYLIST_END );
- }
- else
- {
- msg_Warn( p_playlist, "Not supported yet");
- }
- }
+ return VLC_SUCCESS;
+}
- /* start playing */
- if ( i_current_status != PLAYLIST_STOPPED )
- {
- playlist_Play ( p_playlist );
- }
+/**
+ * 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;
- fclose( file );
+ msg_Info( p_playlist, "clearing playlist");
+ playlist_Clear( p_playlist );
+
+
+ psz_uri = (char *)malloc(sizeof(char)*strlen(psz_filename) + 17 );
+ sprintf( psz_uri, "file/playlist://%s", psz_filename);
+
+ i_id = playlist_PlaylistAdd( p_playlist, psz_uri, psz_uri,
+ 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 );
- return 0;
+ playlist_Play(p_playlist);
+
+ return VLC_SUCCESS;
}
-/*****************************************************************************
- * playlist_SaveFile: Save a playlist in a file.
- *****************************************************************************/
-int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
+/**
+ * Export a node of the playlist to a certain type of playlistfile
+ *
+ * \param p_playlist the playlist to export
+ * \param psz_filename the location where the exported file will be saved
+ * \param p_export_root the root node to export
+ * \param psz_type the type of playlist file to create.
+ * \return VLC_SUCCESS on success
+ */
+int playlist_Export( playlist_t * p_playlist, const char *psz_filename ,
+ playlist_item_t *p_export_root,const char *psz_type )
{
- FILE *file;
- int i;
+ module_t *p_module;
+ playlist_export_t *p_export;
- vlc_mutex_lock( &p_playlist->object_lock );
+ if( p_export_root == NULL ) return VLC_EGENERIC;
- msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
+ msg_Info( p_playlist, "saving %s to file %s",
+ p_export_root->p_input->psz_name, psz_filename );
- file = fopen( psz_filename, "wt" );
- if( !file )
+ /* Prepare the playlist_export_t structure */
+ p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
+ if( !p_export)
+ {
+ msg_Err( p_playlist, "out of memory" );
+ return VLC_ENOMEM;
+ }
+ p_export->psz_filename = NULL;
+ if ( psz_filename )
+ p_export->psz_filename = strdup( psz_filename );
+ p_export->p_file = utf8_fopen( psz_filename, "wt" );
+ if( !p_export->p_file )
{
msg_Err( p_playlist , "could not create playlist file %s"
- , psz_filename );
- return -1;
+ " (%s)", psz_filename, strerror(errno) );
+ return VLC_EGENERIC;
}
- /* Save is done in 0_5 mode at the moment*/
- fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
+ p_export->p_root = p_export_root;
- for ( i = 0 ; i < p_playlist->i_size ; i++ )
- {
- fprintf( file , p_playlist->pp_items[i]->psz_uri );
- fprintf( file , "\n" );
- }
-#if 0
- fprintf( file, PLAYLIST_FILE_HEADER_0_6 "\n" );
+ /* Lock the playlist */
+ vlc_mutex_lock( &p_playlist->object_lock );
+ p_playlist->p_private = (void *)p_export;
- for ( i=0 ; i< p_playlist->i_size ; i++ )
+ /* And call the module ! All work is done now */
+ p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
+ if( !p_module )
{
- fprintf( file, p_playlist->pp_items[i]->psz_uri );
- fprintf( file, "||" );
- fprintf( file, p_playlist->pp_items[i]->psz_name );
- fprintf( file, "||" );
- fprintf( file, "%i",p_playlist->pp_items[i]->b_enabled = VLC_TRUE ?
- 1:0 );
- fprintf( file, "||" );
- fprintf( file, "%i", p_playlist->pp_items[i]->i_group );
- fprintf( file, "||" );
- fprintf( file, p_playlist->pp_items[i]->psz_author );
- fprintf( file , "\n" );
+ msg_Warn( p_playlist, "exporting playlist failed" );
+ vlc_mutex_unlock( &p_playlist->object_lock );
+ return VLC_ENOOBJ;
}
-#endif
- fclose( file );
-
+ module_Unneed( p_playlist , p_module );
+
+ /* Clean up */
+ fclose( p_export->p_file );
+ if ( p_export->psz_filename )
+ free( p_export->psz_filename );
+ free ( p_export );
+ p_playlist->p_private = NULL;
vlc_mutex_unlock( &p_playlist->object_lock );
- return 0;
+ return VLC_SUCCESS;
}