mlt_repository.[hc]: add mlt_repository_languages helper function for localizing...
[melted] / src / framework / mlt_repository.c
index 3ab95be..eab2777 100644 (file)
@@ -2,6 +2,7 @@
  * repository.c -- provides a map between service and shared objects
  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
  * Author: Charles Yates <charles.yates@pandora.be>
+ * Contributor: Dan Dennedy <dan@dennedy.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
 
 #include "mlt_repository.h"
 #include "mlt_properties.h"
+#include "mlt_tokeniser.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -35,6 +37,9 @@ struct mlt_repository_s
        mlt_properties transitions;
 };
 
+/** Construct a new repository
+*/
+
 mlt_repository mlt_repository_init( const char *directory )
 {
        // Safety check
@@ -70,7 +75,7 @@ mlt_repository mlt_repository_init( const char *directory )
                if ( object != NULL )
                {
                        // Get the registration function
-                       int ( *symbol_ptr )( mlt_repository ) = dlsym( object, "mlt_register" );
+                       mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
                        
                        // Call the registration function
                        if ( symbol_ptr != NULL )
@@ -85,60 +90,92 @@ mlt_repository mlt_repository_init( const char *directory )
                                dlclose( object );
                        }
                }
+               else if ( strstr( object_name, "libmlt" ) )
+               {
+                       fprintf( stderr, "%s, %s: failed to dlopen %s\n", __FILE__, __FUNCTION__, object_name );
+               }
        }
        
        return this;
 }
 
-void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, void *symbol )
+static mlt_properties new_service( void *symbol )
+{
+       mlt_properties properties = mlt_properties_new();
+       mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
+       return properties;
+}
+
+/** Register a service with the repository
+    Typically, this is invoked by a module within its mlt_register().
+*/
+
+void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
 {
        // Add the entry point to the corresponding service list
        switch ( service_type )
        {
                case consumer_type:
-                       mlt_properties_set_data( this->consumers, service, symbol, 0, NULL, NULL );
+                       mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
                        break;
                case filter_type:
-                       mlt_properties_set_data( this->filters, service, symbol, 0, NULL, NULL );
+                       mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
                        break;
                case producer_type:
-                       mlt_properties_set_data( this->producers, service, symbol, 0, NULL, NULL );
+                       mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
                        break;
                case transition_type:
-                       mlt_properties_set_data( this->transitions, service, symbol, 0, NULL, NULL );
+                       mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
                        break;
                default:
                        break;
        }
 }
 
-void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
+static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
 {
-       void *( *symbol_ptr )( mlt_profile, mlt_service_type, const char *, void * ) = NULL;
+       mlt_properties service_properties = NULL;
 
        // Get the entry point from the corresponding service list
        switch ( type )
        {
                case consumer_type:
-                       symbol_ptr = mlt_properties_get_data( this->consumers, service, NULL );
+                       service_properties = mlt_properties_get_data( this->consumers, service, NULL );
                        break;
                case filter_type:
-                       symbol_ptr = mlt_properties_get_data( this->filters, service, NULL );
+                       service_properties = mlt_properties_get_data( this->filters, service, NULL );
                        break;
                case producer_type:
-                       symbol_ptr = mlt_properties_get_data( this->producers, service, NULL );
+                       service_properties = mlt_properties_get_data( this->producers, service, NULL );
                        break;
                case transition_type:
-                       symbol_ptr = mlt_properties_get_data( this->transitions, service, NULL );
+                       service_properties = mlt_properties_get_data( this->transitions, service, NULL );
                        break;
                default:
                        break;
        }
+       return service_properties;
+}
+
+/** Construct a new instance of a service
+*/
+
+void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
+{
+       mlt_properties properties = get_service_properties( this, type, service );
+       if ( properties != NULL )
+       {
+               mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
        
-       // Construct the service
-       return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
+               // Construct the service
+               return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
+       }
+       return NULL;
 }
 
+/** Destroy a repository
+*/
+
 void mlt_repository_close( mlt_repository this )
 {
        mlt_properties_close( this->consumers );
@@ -148,3 +185,144 @@ void mlt_repository_close( mlt_repository this )
        mlt_properties_close( &this->parent );
        free( this );
 }
+
+/** Get the list of registered consumers
+*/
+
+mlt_properties mlt_repository_consumers( mlt_repository self )
+{
+       return self->consumers;
+}
+
+/** Get the list of registered filters
+*/
+
+mlt_properties mlt_repository_filters( mlt_repository self )
+{
+       return self->filters;
+}
+
+/** Get the list of registered producers
+*/
+
+mlt_properties mlt_repository_producers( mlt_repository self )
+{
+       return self->producers;
+}
+
+/** Get the list of registered transitions
+*/
+
+mlt_properties mlt_repository_transitions( mlt_repository self )
+{
+       return self->transitions;
+}
+
+/** Register the metadata for a service
+    IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
+*/
+void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
+{
+       mlt_properties service_properties = get_service_properties( self, type, service );
+       mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL );
+       mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL );
+}
+
+/** Get the metadata about a service
+    Returns NULL if service or its metadata are unavailable.
+*/
+
+mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
+{
+       mlt_properties metadata = NULL;
+       mlt_properties properties = get_service_properties( self, type, service );
+       
+       // If this is a valid service
+       if ( properties )
+       {
+               // Lookup cached metadata
+               metadata = mlt_properties_get_data( properties, "metadata", NULL );
+               if ( ! metadata )
+               {
+                       // Not cached, so get the registered metadata callback function
+                       mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
+
+                       // If a metadata callback function is registered
+                       if ( callback )
+                       {
+                               // Fetch the callback data arg
+                               void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
+                       
+                               // Fetch the metadata through the callback
+                               metadata = callback( type, service, data );
+                               
+                               // Cache the metadata
+                               if ( metadata )
+                                       // Include dellocation and serialisation
+                                       mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml );
+                       }
+               }
+       }
+       return metadata;
+}
+
+static char *getenv_locale()
+{
+       char *s = getenv( "LANGUAGE" );
+       if ( s && s[0] )
+               return s;
+       s = getenv( "LC_ALL" );
+       if ( s && s[0] )
+               return s;
+       s = getenv( "LC_MESSAGES" );
+       if ( s && s[0] )
+               return s;
+       s = getenv( "LANG" );
+       if ( s && s[0] )
+               return s;
+       return NULL;
+}
+
+/** Return a list of user-preferred language codes taken from environment variables.
+*/ 
+
+mlt_properties mlt_repository_languages( mlt_repository self )
+{
+       mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL );
+       if ( languages )
+               return languages;
+               
+       const char *locale = getenv_locale();
+       languages = mlt_properties_new();
+       if ( locale )
+       {
+               mlt_tokeniser tokeniser = mlt_tokeniser_init();
+               int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" );
+               if ( count )
+               {
+                       int i;
+                       for ( i = 0; i < count; i++ )
+                       {
+                               char *locale = mlt_tokeniser_get_string( tokeniser, i );
+                               if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 )
+                                       locale = "en";
+                               else if ( strlen( locale ) > 2 )
+                                       locale[2] = 0;
+                               char string[21];
+                               snprintf( string, sizeof(string), "%d", i );
+                               mlt_properties_set( languages, string, locale );
+                       }
+               }
+               else
+               {
+                       mlt_properties_set( languages, "0", "en" );
+               }
+               mlt_tokeniser_close( tokeniser );
+       }
+       else
+       {
+               mlt_properties_set( languages, "0", "en" );
+       }
+       mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL );
+       return languages;
+}