]> git.sesse.net Git - vlc/blobdiff - src/playlist/services_discovery.c
Qt: efficient iconView browsing demands a specialized playlist event
[vlc] / src / playlist / services_discovery.c
index e75b702946794b64e113a692cb90a5159027689a..355dffb7f1483b9d7d60c697bbc0e50147a6e537 100644 (file)
 #include "vlc_playlist.h"
 #include "vlc_events.h"
 #include <vlc_services_discovery.h>
+#include <vlc_probe.h>
 #include "playlist_internal.h"
 #include "../libvlc.h"
 
+typedef struct
+{
+    char *name;
+    char *longname;
+} vlc_sd_probe_t;
+
+int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
+                      const char *longname)
+{
+    vlc_sd_probe_t names = { strdup(name), strdup(longname) };
+
+    if (unlikely (names.name == NULL || names.longname == NULL
+               || vlc_probe_add (probe, &names, sizeof (names))))
+    {
+        free (names.name);
+        free (names.longname);
+        return VLC_ENOMEM;
+    }
+    return VLC_PROBE_CONTINUE;
+}
+
+#undef vlc_sd_GetNames
+
+/**
+ * Gets the list of available services discovery plugins.
+ */
+char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
+{
+    size_t count;
+    vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
+
+    if (count == 0)
+    {
+        free (tab);
+        return NULL;
+    }
+
+    char **names = malloc (sizeof(char *) * (count + 1));
+    char **longnames = malloc (sizeof(char *) * (count + 1));
+
+    if (unlikely (names == NULL || longnames == NULL))
+        abort();
+    for( size_t i = 0; i < count; i++ )
+    {
+        names[i] = tab[i].name;
+        longnames[i] = tab[i].longname;
+    }
+    free (tab);
+    names[count] = longnames[count] = NULL;
+    *pppsz_longnames = longnames;
+    return names;
+}
+
+
+struct vlc_sd_internal_t
+{
+    /* the playlist items for category and onelevel */
+    playlist_item_t      *p_cat;
+    playlist_item_t      *p_one;
+    services_discovery_t *p_sd; /**< Loaded service discovery modules */
+    char                 *psz_name;
+};
+
+static void services_discovery_Destructor ( vlc_object_t *p_obj );
 
 /*
  * Services discovery
  * That's how the playlist get's Service Discovery information
  */
 
-/***********************************************************************
- * GetServicesNames
- ***********************************************************************/
-char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
-                                               char ***pppsz_longnames )
-{
-    return module_GetModulesNamesForCapability( "services_discovery",
-                                                pppsz_longnames );
-}
-
 /***********************************************************************
  * Create
  ***********************************************************************/
@@ -72,6 +127,7 @@ services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
     vlc_event_manager_register_event_type( &p_sd->event_manager,
             vlc_ServicesDiscoveryEnded );
 
+    vlc_object_set_destructor( p_sd, services_discovery_Destructor );
     vlc_object_attach( p_sd, p_super );
 
     return p_sd;
@@ -114,6 +170,16 @@ void vlc_sd_Stop ( services_discovery_t * p_sd )
     p_sd->p_module = NULL;
 }
 
+/***********************************************************************
+ * Destructor
+ ***********************************************************************/
+static void services_discovery_Destructor ( vlc_object_t *p_obj )
+{
+    services_discovery_t * p_sd = (services_discovery_t *)p_obj;
+    assert(!p_sd->p_module); /* Forgot to call Stop */
+    vlc_event_manager_fini( &p_sd->event_manager );
+}
+
 /***********************************************************************
  * GetLocalizedName
  ***********************************************************************/
@@ -212,129 +278,105 @@ static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_d
 
     /* First make sure that if item is a node it will be deleted.
      * XXX: Why don't we have a function to ensure that in the playlist code ? */
-    vlc_object_lock( p_parent->p_playlist );
+    playlist_Lock( p_parent->p_playlist );
     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
-            p_input->i_id, p_parent, false );
+            p_input, p_parent, false );
 
     if( p_pl_item && p_pl_item->i_children > -1 )
-    {
         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
-        vlc_object_unlock( p_parent->p_playlist );
-        return;
-    }
-
-    /* Delete the non-node item normally */
-    playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
-                                      p_parent, pl_Locked );
+    else
+        /* Delete the non-node item normally */
+        playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input,
+                                          p_parent, pl_Locked );
 
-    vlc_object_unlock( p_parent->p_playlist );
+    playlist_Unlock( p_parent->p_playlist );
 }
 
-int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
+int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
 {
-    const char *psz_parser = psz_modules ?: "";
+    /* Perform the addition */
+    msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
+
+    services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
+    if( !p_sd )
+        return VLC_ENOMEM;
 
-    for (;;)
+    module_t *m = module_find_by_shortcut( psz_module );
+    if( !m )
     {
-        struct playlist_services_discovery_support_t * p_sds;
-        playlist_item_t * p_cat;
-        playlist_item_t * p_one;
+        msg_Err( p_playlist, "No such module: %s", psz_module );
+        vlc_sd_Destroy( p_sd );
+        return VLC_EGENERIC;
+    }
 
-        while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
-            psz_parser++;
+    /* Free in playlist_ServicesDiscoveryRemove */
+    vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
+    if( !p_sds )
+    {
+        vlc_sd_Destroy( p_sd );
+        module_release( m );
+        return VLC_ENOMEM;
+    }
 
-        if( *psz_parser == '\0' )
-            break;
+    playlist_item_t * p_cat;
+    playlist_item_t * p_one;
 
-        const char *psz_next = strchr( psz_parser, ':' );
-        if( psz_next == NULL )
-            psz_next = psz_parser + strlen( psz_parser );
+    PL_LOCK;
+    playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
+                              &p_cat, &p_one, false );
+    PL_UNLOCK;
+    module_release( m );
 
-        char psz_plugin[psz_next - psz_parser + 1];
-        memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
-        psz_plugin[sizeof (psz_plugin) - 1] = '\0';
-        psz_parser = psz_next;
+    vlc_event_attach( services_discovery_EventManager( p_sd ),
+                      vlc_ServicesDiscoveryItemAdded,
+                      playlist_sd_item_added, p_one );
+        
+    vlc_event_attach( services_discovery_EventManager( p_sd ),
+                      vlc_ServicesDiscoveryItemAdded,
+                      playlist_sd_item_added, p_cat );
 
-        /* Perform the addition */
-        msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
-        services_discovery_t *p_sd;
+    vlc_event_attach( services_discovery_EventManager( p_sd ),
+                      vlc_ServicesDiscoveryItemRemoved,
+                      playlist_sd_item_removed, p_one );
 
-        p_sd = vlc_sd_Create( (vlc_object_t*)p_playlist );
-        if( !p_sd )
-            return VLC_ENOMEM;
+    vlc_event_attach( services_discovery_EventManager( p_sd ),
+                      vlc_ServicesDiscoveryItemRemoved,
+                      playlist_sd_item_removed, p_cat );
 
-        /* Free in playlist_ServicesDiscoveryRemove */
-        p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
-        if( !p_sds )
-        {
-            vlc_sd_Destroy( p_sd );
-            return VLC_ENOMEM;
-        }
+    if( !vlc_sd_Start( p_sd, psz_module ) )
+    {
+        vlc_sd_Destroy( p_sd );
+        free( p_sds );
+        return VLC_EGENERIC;
+    }
 
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemAdded,
-                          playlist_sd_item_added,
-                          p_one );
-        
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemAdded,
-                          playlist_sd_item_added,
-                          p_cat );
-
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemRemoved,
-                          playlist_sd_item_removed,
-                          p_one );
-
-        vlc_event_attach( services_discovery_EventManager( p_sd ),
-                          vlc_ServicesDiscoveryItemRemoved,
-                          playlist_sd_item_removed,
-                          p_cat );
-
-        if( !vlc_sd_Start( p_sd, psz_plugin ) )
-        {
-            vlc_sd_Destroy( p_sd );
-            free( p_sds );
-            return VLC_EGENERIC;
-        }        
-
-        char *psz = services_discovery_GetLocalizedName( p_sd );
-        assert( psz );
-        PL_LOCK;
-        playlist_NodesPairCreate( p_playlist, psz,
-                &p_cat, &p_one, false );
-        PL_UNLOCK;
-        free( psz );
-
-
-        /* We want tree-view for service directory */
-        p_one->p_input->b_prefers_tree = true;
-        p_sds->p_sd = p_sd;
-        p_sds->p_one = p_one;
-        p_sds->p_cat = p_cat;
-
-        PL_LOCK;
-        TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
-        PL_UNLOCK;
+    /* We want tree-view for service directory */
+    p_one->p_input->b_prefers_tree = true;
+    p_sds->p_sd = p_sd;
+    p_sds->p_one = p_one;
+    p_sds->p_cat = p_cat;
+    p_sds->psz_name = strdup( psz_module );
 
-    }
+    PL_LOCK;
+    TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
+    PL_UNLOCK;
 
     return VLC_SUCCESS;
 }
 
 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
-                                      const char *psz_module )
+                                      const char *psz_name )
 {
-    struct playlist_services_discovery_support_t * p_sds = NULL;
-    int i;
+    playlist_private_t *priv = pl_priv( p_playlist );
+    vlc_sd_internal_t * p_sds = NULL;
 
     PL_LOCK;
-    for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
+    for( int i = 0; i < priv->i_sds; i++ )
     {
-        if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
+        if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
         {
-            p_sds = pl_priv(p_playlist)->pp_sds[i];
-            REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
+            p_sds = priv->pp_sds[i];
+            REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
             break;
         }
     }
@@ -342,7 +384,7 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
 
     if( !p_sds )
     {
-        msg_Warn( p_playlist, "module %s is not loaded", psz_module );
+        msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
         return VLC_EGENERIC;
     }
 
@@ -382,32 +424,38 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
     PL_UNLOCK;
 
     vlc_sd_Destroy( p_sd );
+    free( p_sds->psz_name );
     free( p_sds );
 
     return VLC_SUCCESS;
 }
 
 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
-                                              const char *psz_module )
+                                         const char *psz_name )
 {
-    int i;
+    playlist_private_t *priv = pl_priv( p_playlist );
+    bool found = false;
     PL_LOCK;
 
-    for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
+    for( int i = 0; i < priv->i_sds; i++ )
     {
-        if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
+        vlc_sd_internal_t *sd = priv->pp_sds[i];
+
+        if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
         {
-            PL_UNLOCK;
-            return true;
+            found = true;
+            break;
         }
     }
     PL_UNLOCK;
-    return false;
+    return found;
 }
 
 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
 {
-    while( pl_priv(p_playlist)->i_sds > 0 )
+    playlist_private_t *priv = pl_priv( p_playlist );
+
+    while( priv->i_sds > 0 )
         playlist_ServicesDiscoveryRemove( p_playlist,
-                                          module_get_object( pl_priv(p_playlist)->pp_sds[0]->p_sd->p_module ) );
+                                          priv->pp_sds[0]->psz_name );
 }