]> git.sesse.net Git - vlc/blobdiff - src/input/meta.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / input / meta.c
index 77dfae3529447cadec492b4c0267eca5742462ea..bff1b7d2eb7bce2b55397e9814059ddc06cc389f 100644 (file)
 #include <vlc_common.h>
 #include <vlc_playlist.h>
 #include <vlc_url.h>
+#include <vlc_arrays.h>
+#include <vlc_modules.h>
 
 #include "input_internal.h"
 #include "../playlist/art.h"
 
+struct vlc_meta_t
+{
+    char * ppsz_meta[VLC_META_TYPE_COUNT];
+    
+    vlc_dictionary_t extra_tags;
+    
+    int i_status;
+};
+
 /* FIXME bad name convention */
-const char * input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
+const char * vlc_meta_TypeToLocalizedString( vlc_meta_type_t meta_type )
 {
     switch( meta_type )
     {
@@ -60,6 +71,128 @@ const char * input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
     }
 };
 
+
+/**
+ * vlc_meta contructor.
+ * vlc_meta_Delete() will free the returned pointer.
+ */ 
+vlc_meta_t *vlc_meta_New( void )
+{
+    vlc_meta_t *m = (vlc_meta_t*)malloc( sizeof(*m) );
+    if( !m )
+        return NULL;
+    memset( m->ppsz_meta, 0, sizeof(m->ppsz_meta) );
+    m->i_status = 0;
+    vlc_dictionary_init( &m->extra_tags, 0 );
+    return m;
+}
+
+/* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */
+static void vlc_meta_FreeExtraKey( void *p_data, void *p_obj )
+{
+    VLC_UNUSED( p_obj );
+    free( p_data );
+}
+
+void vlc_meta_Delete( vlc_meta_t *m )
+{
+    int i;
+    for( i = 0; i < VLC_META_TYPE_COUNT ; i++ )
+        free( m->ppsz_meta[i] );
+    vlc_dictionary_clear( &m->extra_tags, vlc_meta_FreeExtraKey, NULL );
+    free( m );
+}
+
+/**
+ * vlc_meta has two kinds of meta, the one in a table, and the one in a
+ * dictionary.
+ * FIXME - Why don't we merge those two?
+ */ 
+
+void vlc_meta_Set( vlc_meta_t *p_meta, vlc_meta_type_t meta_type, const char *psz_val )
+{
+    free( p_meta->ppsz_meta[meta_type] );
+    p_meta->ppsz_meta[meta_type] = psz_val ? strdup( psz_val ) : NULL;
+}
+
+const char *vlc_meta_Get( const vlc_meta_t *p_meta, vlc_meta_type_t meta_type )
+{
+    return p_meta->ppsz_meta[meta_type];
+}
+
+void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const char *psz_value )
+{
+    char *psz_oldvalue = (char *)vlc_dictionary_value_for_key( &m->extra_tags, psz_name );
+    if( psz_oldvalue != kVLCDictionaryNotFound )
+        vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name,
+                                            vlc_meta_FreeExtraKey, NULL );
+    vlc_dictionary_insert( &m->extra_tags, psz_name, strdup(psz_value) );
+}
+
+const char * vlc_meta_GetExtra( const vlc_meta_t *m, const char *psz_name )
+{
+    return (char *)vlc_dictionary_value_for_key(&m->extra_tags, psz_name);
+}
+
+unsigned vlc_meta_GetExtraCount( const vlc_meta_t *m )
+{
+    return vlc_dictionary_keys_count(&m->extra_tags);
+}
+
+char** vlc_meta_CopyExtraNames( const vlc_meta_t *m )
+{
+    return vlc_dictionary_all_keys(&m->extra_tags);
+}
+
+/**
+ * vlc_meta status (see vlc_meta_status_e)
+ */
+int vlc_meta_GetStatus( vlc_meta_t *m )
+{
+    return m->i_status;
+}
+
+void vlc_meta_SetStatus( vlc_meta_t *m, int status )
+{
+    m->i_status = status;
+}
+
+
+/**
+ * Merging meta
+ */
+void vlc_meta_Merge( vlc_meta_t *dst, const vlc_meta_t *src )
+{
+    char **ppsz_all_keys;
+    int i;
+    
+    if( !dst || !src )
+        return;
+    
+    for( i = 0; i < VLC_META_TYPE_COUNT; i++ )
+    {
+        if( src->ppsz_meta[i] )
+        {
+            free( dst->ppsz_meta[i] );
+            dst->ppsz_meta[i] = strdup( src->ppsz_meta[i] );
+        }
+    }
+    
+    /* XXX: If speed up are needed, it is possible */
+    ppsz_all_keys = vlc_dictionary_all_keys( &src->extra_tags );
+    for( i = 0; ppsz_all_keys && ppsz_all_keys[i]; i++ )
+    {
+        /* Always try to remove the previous value */
+        vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i], vlc_meta_FreeExtraKey, NULL );
+        
+        void *p_value = vlc_dictionary_value_for_key( &src->extra_tags, ppsz_all_keys[i] );
+        vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], strdup( (const char*)p_value ) );
+        free( ppsz_all_keys[i] );
+    }
+    free( ppsz_all_keys );
+}
+
+
 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
 {
     input_item_t *p_item = p_input->p->p_item;
@@ -73,13 +206,7 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
         return;
     }
 
-    playlist_t *p_playlist = pl_Hold( p_input );
-    if( !p_playlist )
-    {
-        free( psz_arturl );
-        return;
-    }
-
+    playlist_t *p_playlist = pl_Get( p_input );
 
     if( input_item_IsArtFetched( p_item ) )
     {
@@ -127,7 +254,6 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
     vlc_input_attachment_Delete( p_attachment );
 
 exit:
-    pl_Release( p_input );
     free( psz_arturl );
 }
 
@@ -146,29 +272,23 @@ int input_item_WriteMeta( vlc_object_t *obj, input_item_t *p_item )
     type = p_item->i_type;
     vlc_mutex_unlock( &p_item->lock );
     if( type != ITEM_TYPE_FILE )
-    {
-        char *psz_uri = input_item_GetURI( p_item );
+        goto error;
 
-#warning FIXME: function for URI->path conversion!
-        decode_URI( psz_uri );
-        if( !strncmp( psz_uri, "file://", 7 ) )
-        {
-            p_export->psz_file = strdup( psz_uri + 7 );
-            free( psz_uri );
-        }
-        else
-#warning This should not happen!
-            p_export->psz_file = psz_uri;
-    }
-    else
-    {
-        vlc_object_release( p_export );
-        return VLC_EGENERIC;
-    }
+    char *psz_uri = input_item_GetURI( p_item );
+    p_export->psz_file = make_path( psz_uri );
+    if( p_export->psz_file == NULL )
+        msg_Err( p_export, "cannot write meta to remote media %s", psz_uri );
+    free( psz_uri );
+    if( p_export->psz_file == NULL )
+        goto error;
 
     module_t *p_mod = module_need( p_export, "meta writer", NULL, false );
     if( p_mod )
         module_unneed( p_export, p_mod );
     vlc_object_release( p_export );
     return VLC_SUCCESS;
+
+error:
+    vlc_object_release( p_export );
+    return VLC_EGENERIC;
 }