]> git.sesse.net Git - vlc/blobdiff - src/playlist/loadsave.c
Include errno.h
[vlc] / src / playlist / loadsave.c
index e379b1fdbb2f4878361c5959457369a2a8428af4..b32c0ce0a22786b770f452b8583ea87a7b871846 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * loadsave.c : Playlist loading / saving functions
  *****************************************************************************
- * Copyright (C) 1999-2001 VideoLAN
- * $Id: loadsave.c,v 1.2 2004/01/05 12:59:43 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 <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 "playlist_internal.h"
+#include "charset.h"
+#include <errno.h>
+
+#if defined( WIN32 ) || defined( UNDER_CE )
+#   define DIR_SEP "\\"
+#else
+#   define DIR_SEP "/"
+#endif
 
-#define PLAYLIST_FILE_HEADER_0_5  "# vlc playlist file version 0.5"
-#define PLAYLIST_FILE_HEADER_0_6  "# vlc playlist file version 0.6"
-
-
-/*****************************************************************************
- * playlist_LoadFile: load a playlist file.
- ****************************************************************************/
-int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
+/**
+ * Import a playlist file at a given point of a given 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_Import( playlist_t * p_playlist, const char *psz_filename,
+                     playlist_item_t *p_root, vlc_bool_t b_only_there )
 {
-    FILE *file;
-    char line[1024];
-    int i_current_status;
-    int i_format;
-    int i;
-
-    msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );
-
-    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 );
+    char *psz_uri, *psz_opt;
+    input_item_t *p_input;
 
-    /* check the file is not empty */
-    if ( ! fgets( line, 1024, file ) )
+    asprintf( &psz_uri, "file/playlist://%s", psz_filename );
+    p_input = input_ItemNewExt( p_playlist, psz_uri, "playlist", 0, NULL, -1 );
+    if( b_only_there )
     {
-        msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
-        fclose( file );
-        return -1;
+        asprintf( &psz_opt, "parent-item=%i", p_root->i_id );
+        vlc_input_item_AddOption( p_input, psz_opt );
+        free( psz_opt );
     }
+    playlist_PlaylistAddInput( p_playlist, p_input, PLAYLIST_APPEND,
+                               PLAYLIST_END );
+    input_Read( p_playlist, p_input, VLC_TRUE );
+    free( psz_uri );
+    return VLC_SUCCESS;
+}
 
-    /* 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;
-    }
+/**
+ * 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 )
+{
+    module_t *p_module;
+    playlist_export_t *p_export;
 
-    /* stop playing */
-    i_current_status = p_playlist->i_status;
-    if ( p_playlist->i_status != PLAYLIST_STOPPED )
-    {
-        playlist_Stop ( p_playlist );
-    }
+    if( p_export_root == NULL ) return VLC_EGENERIC;
 
-    /* delete current content of the playlist */
-    for( i = p_playlist->i_size - 1; i >= 0; i-- )
-    {
-        playlist_Delete ( p_playlist , i );
-    }
+    msg_Info( p_playlist, "saving %s to file %s",
+                    p_export_root->p_input->psz_name, psz_filename );
 
-    /* simply add each line */
-    while( fgets( line, 1024, file ) )
+    /* Prepare the playlist_export_t structure */
+    p_export = (playlist_export_t *)malloc( sizeof(playlist_export_t) );
+    if( !p_export)
     {
-       /* 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");
-       }
+        msg_Err( p_playlist, "out of memory" );
+        return VLC_ENOMEM;
     }
-
-    /* start playing */
-    if ( i_current_status != PLAYLIST_STOPPED )
+    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 )
     {
-        playlist_Play ( p_playlist );
+        msg_Err( p_playlist , "could not create playlist file %s"
+                 " (%s)", psz_filename, strerror(errno) );
+        return VLC_EGENERIC;
     }
 
-    fclose( file );
-
-    return 0;
-}
-
-/*****************************************************************************
- * playlist_SaveFile: Save a playlist in a file.
- *****************************************************************************/
-int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
-{
-    FILE *file;
-    int i;
+    p_export->p_root = p_export_root;
 
+    /* Lock the playlist */
     vlc_mutex_lock( &p_playlist->object_lock );
+    p_playlist->p_private = (void *)p_export;
 
-    msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );
-
-    file = fopen( psz_filename, "wt" );
-    if( !file )
+    /* And call the module ! All work is done now */
+    p_module = module_Need( p_playlist, "playlist export", psz_type, VLC_TRUE);
+    if( !p_module )
     {
-        msg_Err( p_playlist , "could not create playlist file %s"
-                , psz_filename );
-        return -1;
+        msg_Warn( p_playlist, "exporting playlist failed" );
+        vlc_mutex_unlock( &p_playlist->object_lock );
+        return VLC_ENOOBJ;
     }
-    /* Save is done in 0_5 mode at the moment*/
+    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 VLC_SUCCESS;
+}
 
-    fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
+int playlist_MLLoad( playlist_t *p_playlist )
+{
+    char *psz_uri, *psz_homedir =p_playlist->p_vlc->psz_homedir;
+    input_item_t *p_input;
 
-    for ( i = 0 ; i < p_playlist->i_size ; i++ )
+    if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
+    if( !psz_homedir )
     {
-        fprintf( file , p_playlist->pp_items[i]->psz_uri );
-        fprintf( file , "\n" );
+        msg_Err( p_playlist, "no home directory, cannot load media library") ;
+        return VLC_EGENERIC;
     }
-#if 0
-    fprintf( file, PLAYLIST_FILE_HEADER_0_6 "\n" );
+    asprintf( &psz_uri, "file/xspf-open://%s" DIR_SEP CONFIG_DIR DIR_SEP
+                        "ml.xsp", psz_homedir );
+
+    p_input = input_ItemNewExt( p_playlist, psz_uri,
+                                _("Media Library"), 0, NULL, -1 );
+    p_playlist->p_ml_category->p_input = p_input;
+    p_playlist->p_ml_onelevel->p_input = p_input;
+
+    p_playlist->b_doing_ml = VLC_TRUE;
+    stats_TimerStart( p_playlist, "ML Load", STATS_TIMER_ML_LOAD );
+    input_Read( p_playlist, p_input, VLC_TRUE );
+    stats_TimerStop( p_playlist,STATS_TIMER_ML_LOAD );
+    p_playlist->b_doing_ml = VLC_FALSE;
+
+    free( psz_uri );
+    return VLC_SUCCESS;
+}
 
-    for ( i=0 ; i< p_playlist->i_size ; i++ )
+int playlist_MLDump( playlist_t *p_playlist )
+{
+    char *psz_uri, *psz_homedir =p_playlist->p_vlc->psz_homedir;
+    if( !config_GetInt( p_playlist, "media-library") ) return VLC_SUCCESS;
+    if( !psz_homedir )
     {
-        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_Err( p_playlist, "no home directory, cannot load media library") ;
+        return VLC_EGENERIC;
     }
-#endif
-    fclose( file );
-
-    vlc_mutex_unlock( &p_playlist->object_lock );
-
-    return 0;
+    asprintf( &psz_uri, "%s" DIR_SEP CONFIG_DIR DIR_SEP
+                        "ml.xsp",  psz_homedir );
+    stats_TimerStart( p_playlist, "ML Dump", STATS_TIMER_ML_DUMP );
+    playlist_Export( p_playlist, psz_uri, p_playlist->p_ml_category,
+                     "export-xspf" );
+    stats_TimerStop( p_playlist, STATS_TIMER_ML_DUMP );
+
+    free( psz_uri );
+    return VLC_SUCCESS;
 }