playlist: Properly clean the playlist using the object destructor.
[vlc.git] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
27
28 #include <vlc/vlc.h>
29 #include "vlc_playlist.h"
30 #include "vlc_events.h"
31 #include "playlist_internal.h"
32
33 static void RunSD( services_discovery_t *p_sd );
34
35 /*
36  * Services discovery
37  * Basically you just listen to Service discovery event through the
38  * sd's event manager.
39  * That's how the playlist get's Service Discovery information
40  */
41
42 /***********************************************************************
43  * GetServicesNames
44  ***********************************************************************/
45 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
46                                                char ***pppsz_longnames )
47 {
48     return module_GetModulesNamesForCapability( p_super, "services_discovery",
49                                                 pppsz_longnames );
50 }
51
52 /***********************************************************************
53  * Create
54  ***********************************************************************/
55 services_discovery_t *
56 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
57 {
58     services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD );
59     if( !p_sd )
60         return NULL;
61
62     p_sd->pf_run = NULL;
63     p_sd->psz_localized_name = NULL;
64
65     vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
66     vlc_event_manager_register_event_type( &p_sd->event_manager,
67             vlc_ServicesDiscoveryItemAdded );
68     vlc_event_manager_register_event_type( &p_sd->event_manager,
69             vlc_ServicesDiscoveryItemRemoved );
70     vlc_event_manager_register_event_type( &p_sd->event_manager,
71             vlc_ServicesDiscoveryStarted );
72     vlc_event_manager_register_event_type( &p_sd->event_manager,
73             vlc_ServicesDiscoveryEnded );
74
75     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, VLC_TRUE );
76
77     if( p_sd->p_module == NULL )
78     {
79         msg_Err( p_super, "no suitable services discovery module" );
80         vlc_object_release( p_sd );
81         return NULL;
82     }
83     p_sd->psz_module = strdup( psz_module_name );
84     p_sd->b_die = VLC_FALSE; /* FIXME */
85
86     vlc_object_attach( p_sd, p_super );
87     return p_sd;
88 }
89
90 /***********************************************************************
91  * Destroy
92  ***********************************************************************/
93 void services_discovery_Destroy ( services_discovery_t * p_sd )
94 {
95     vlc_event_manager_fini( &p_sd->event_manager );
96
97     free( p_sd->psz_module );
98     free( p_sd->psz_localized_name );
99
100     vlc_object_detach( p_sd );
101     vlc_object_release( p_sd );
102 }
103
104 /***********************************************************************
105  * Start
106  ***********************************************************************/
107 int services_discovery_Start ( services_discovery_t * p_sd )
108 {
109     if ((p_sd->pf_run != NULL)
110         && vlc_thread_create( p_sd, "services_discovery", RunSD,
111                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
112     {
113         msg_Err( p_sd, "cannot create services discovery thread" );
114         vlc_object_release( p_sd );
115         return VLC_EGENERIC;
116     }
117     return VLC_SUCCESS;
118 }
119
120 /***********************************************************************
121  * Stop
122  ***********************************************************************/
123 void services_discovery_Stop ( services_discovery_t * p_sd )
124 {
125     vlc_object_kill( p_sd );
126     if( p_sd->pf_run ) vlc_thread_join( p_sd );
127
128     module_Unneed( p_sd, p_sd->p_module );
129 }
130
131 /***********************************************************************
132  * GetLocalizedName
133  ***********************************************************************/
134 char *
135 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
136 {
137     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
138 }
139
140 /***********************************************************************
141  * SetLocalizedName
142  ***********************************************************************/
143 void
144 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
145 {
146     free( p_sd->psz_localized_name );
147     p_sd->psz_localized_name = strdup(psz);
148 }
149
150 /***********************************************************************
151  * EventManager
152  ***********************************************************************/
153 vlc_event_manager_t *
154 services_discovery_EventManager ( services_discovery_t * p_sd )
155 {
156     return &p_sd->event_manager;
157 }
158
159 /***********************************************************************
160  * AddItem
161  ***********************************************************************/
162 void
163 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
164                              const char * psz_category )
165 {
166     vlc_event_t event;
167     event.type = vlc_ServicesDiscoveryItemAdded;
168     event.u.services_discovery_item_added.p_new_item = p_item;
169     event.u.services_discovery_item_added.psz_category = psz_category;
170
171     vlc_event_send( &p_sd->event_manager, &event );
172 }
173
174 /***********************************************************************
175  * RemoveItem
176  ***********************************************************************/
177 void
178 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
179 {
180     vlc_event_t event;
181     event.type = vlc_ServicesDiscoveryItemRemoved;
182     event.u.services_discovery_item_removed.p_item = p_item;
183
184     vlc_event_send( &p_sd->event_manager, &event );
185 }
186
187 /***********************************************************************
188  * RunSD (Private)
189  ***********************************************************************/
190 static void RunSD( services_discovery_t *p_sd )
191 {
192     vlc_event_t event;
193
194     event.type = vlc_ServicesDiscoveryStarted;
195     vlc_event_send( &p_sd->event_manager, &event );
196
197     p_sd->pf_run( p_sd );
198
199     event.type = vlc_ServicesDiscoveryEnded;
200     vlc_event_send( &p_sd->event_manager, &event );
201     return;
202 }
203
204 /*
205  * Playlist - Services discovery bridge
206  */
207
208  /* A new item has been added to a certain sd */
209 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
210 {
211     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
212     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
213     playlist_item_t *p_new_item, * p_parent = user_data;
214
215     msg_Dbg( p_parent->p_playlist, "Adding %s in %s",
216                 p_input->psz_name ? p_input->psz_name : "(null)",
217                 psz_cat ? psz_cat : "(null)" );
218
219     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
220     if( !EMPTY_STR(psz_cat) &&
221         p_parent->p_parent == p_parent->p_playlist->p_root_category )
222     {
223         /* */
224         playlist_item_t * p_cat;
225         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
226         if( !p_cat )
227         {
228             p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
229                                          p_parent, 0, NULL );
230             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
231         }
232         p_parent = p_cat;
233     }
234
235     p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
236                                         PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
237     if( p_new_item )
238     {
239         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
240         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
241     }
242 }
243
244  /* A new item has been removed from a certain sd */
245 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
246 {
247     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
248     playlist_item_t * p_parent = user_data;
249     playlist_item_t * p_pl_item;
250
251     /* First make sure that if item is a node it will be deleted.
252      * XXX: Why don't we have a function to ensure that in the playlist code ? */
253     vlc_object_lock( p_parent->p_playlist );
254     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
255             p_input->i_id, p_parent, VLC_FALSE );
256
257     if( p_pl_item && p_pl_item->i_children > -1 )
258     {
259         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, VLC_TRUE, VLC_FALSE );
260         vlc_object_unlock( p_parent->p_playlist );
261         return;
262     }
263     vlc_object_unlock( p_parent->p_playlist );
264
265     /* Delete the non-node item normally */
266     playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
267                                   p_parent, VLC_FALSE );
268 }
269
270 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
271 {
272     const char *psz_parser = psz_modules ?: "";
273     int retval = VLC_SUCCESS;
274
275     for (;;)
276     {
277         struct playlist_services_discovery_support_t * p_sds;
278         playlist_item_t * p_cat;
279         playlist_item_t * p_one;
280
281         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
282             psz_parser++;
283
284         if( *psz_parser == '\0' )
285             break;
286
287         const char *psz_next = strchr( psz_parser, ':' );
288         if( psz_next == NULL )
289             psz_next = psz_parser + strlen( psz_parser );
290
291         char psz_plugin[psz_next - psz_parser + 1];
292         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
293         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
294         psz_parser = psz_next;
295
296         /* Perform the addition */
297         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
298         services_discovery_t *p_sd;
299
300         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
301         if( !p_sd )
302             continue;
303
304         char * psz = services_discovery_GetLocalizedName( p_sd );
305         assert( psz );
306         playlist_NodesPairCreate( p_playlist, psz,
307                 &p_cat, &p_one, VLC_FALSE );
308         free( psz );
309
310         vlc_event_attach( services_discovery_EventManager( p_sd ),
311                           vlc_ServicesDiscoveryItemAdded,
312                           playlist_sd_item_added,
313                           p_one );
314
315         vlc_event_attach( services_discovery_EventManager( p_sd ),
316                           vlc_ServicesDiscoveryItemAdded,
317                           playlist_sd_item_added,
318                           p_cat );
319
320         vlc_event_attach( services_discovery_EventManager( p_sd ),
321                           vlc_ServicesDiscoveryItemRemoved,
322                           playlist_sd_item_removed,
323                           p_one );
324
325         vlc_event_attach( services_discovery_EventManager( p_sd ),
326                           vlc_ServicesDiscoveryItemRemoved,
327                           playlist_sd_item_removed,
328                           p_cat );
329
330         services_discovery_Start( p_sd );
331
332         /* Free in playlist_ServicesDiscoveryRemove */
333         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
334         if( !p_sds )
335         {
336             msg_Err( p_playlist, "No more memory" );
337             return VLC_ENOMEM;
338         }
339         p_sds->p_sd = p_sd;
340         p_sds->p_one = p_one;
341         p_sds->p_cat = p_cat;
342
343         PL_LOCK;
344         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
345         PL_UNLOCK;
346     }
347
348     return retval;
349 }
350
351 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
352                                        const char *psz_module )
353 {
354     struct playlist_services_discovery_support_t * p_sds = NULL;
355     int i;
356
357     PL_LOCK;
358     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
359     {
360         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
361         {
362             p_sds = p_playlist->pp_sds[i];
363             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
364             break;
365         }
366     }
367     PL_UNLOCK;
368
369     if( !p_sds || !p_sds->p_sd )
370     {
371         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
372         return VLC_EGENERIC;
373     }
374
375     services_discovery_Stop( p_sds->p_sd );
376
377     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
378                         vlc_ServicesDiscoveryItemAdded,
379                         playlist_sd_item_added,
380                         p_sds->p_one );
381
382     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
383                         vlc_ServicesDiscoveryItemAdded,
384                         playlist_sd_item_added,
385                         p_sds->p_cat );
386
387     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
388                         vlc_ServicesDiscoveryItemRemoved,
389                         playlist_sd_item_removed,
390                         p_sds->p_one );
391
392     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
393                         vlc_ServicesDiscoveryItemRemoved,
394                         playlist_sd_item_removed,
395                         p_sds->p_cat );
396
397     /* Remove the sd playlist node if it exists */
398     PL_LOCK;
399     if( p_sds->p_cat != p_playlist->p_root_category &&
400         p_sds->p_one != p_playlist->p_root_onelevel )
401     {
402         playlist_NodeDelete( p_playlist, p_sds->p_cat, VLC_TRUE, VLC_FALSE );
403         playlist_NodeDelete( p_playlist, p_sds->p_one, VLC_TRUE, VLC_FALSE );
404     }
405     PL_UNLOCK;
406
407     services_discovery_Destroy( p_sds->p_sd );
408
409     return VLC_SUCCESS;
410 }
411
412 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
413                                               const char *psz_module )
414 {
415     int i;
416     PL_LOCK;
417
418     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
419     {
420         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
421         {
422             PL_UNLOCK;
423             return VLC_TRUE;
424         }
425     }
426     PL_UNLOCK;
427     return VLC_FALSE;
428 }
429